[jbosstools-commits] JBoss Tools SVN: r35804 - in trunk/forge/plugins: org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Wed Oct 19 10:02:42 EDT 2011


Author: koen.aers at jboss.com
Date: 2011-10-19 10:02:42 -0400 (Wed, 19 Oct 2011)
New Revision: 35804

Added:
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/FieldPostProcessor.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessor.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessorHelper.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/NewProjectPostProcessor.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PersistencePostProcessor.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PickUpPostProcessor.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PrettyFacesPostProcessor.java
Removed:
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/CommandRecorder.java
Modified:
   trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/AllCommandsInterceptor.java
   trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/EventHandler.java
   trunk/forge/plugins/org.jboss.tools.forge.runtime/META-INF/MANIFEST.MF
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandProcessor.java
   trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeTextViewer.java
Log:
JBIDE-9945: Refactor the Command Recorder So That Post Processing Becomes Cleaner

Modified: trunk/forge/plugins/org.jboss.tools.forge.runtime/META-INF/MANIFEST.MF
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.runtime/META-INF/MANIFEST.MF	2011-10-19 13:31:52 UTC (rev 35803)
+++ trunk/forge/plugins/org.jboss.tools.forge.runtime/META-INF/MANIFEST.MF	2011-10-19 14:02:42 UTC (rev 35804)
@@ -9,6 +9,8 @@
  modules/org/jboss/forge/shell-api/main/forge-shell-api.jar
 Export-Package: javax.enterprise.event,
  javax.inject,
+ org.jboss.forge.project,
+ org.jboss.forge.resources,
  org.jboss.forge.shell,
  org.jboss.forge.shell.command,
  org.jboss.forge.shell.events,

Modified: trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/AllCommandsInterceptor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/AllCommandsInterceptor.java	2011-10-19 13:31:52 UTC (rev 35803)
+++ trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/AllCommandsInterceptor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -2,6 +2,7 @@
 
 import javax.inject.Inject;
 
+import org.jboss.forge.project.Project;
 import org.jboss.forge.shell.Shell;
 import org.jboss.forge.shell.spi.CommandInterceptor;
 
@@ -14,7 +15,12 @@
 	
 	@Override
 	public String intercept(String line) {
-		sendEscaped("Intercepted Command: " + line);
+		Project project = shell.getCurrentProject();
+		String str = "no project";
+		if (project != null) {
+			str = project.getProjectRoot().getName();
+		}
+		sendEscaped("Intercepted Command: " + line + " Current Project Directory: " + str);
 		return line;
 	}
 

Modified: trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/EventHandler.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/EventHandler.java	2011-10-19 13:31:52 UTC (rev 35803)
+++ trunk/forge/plugins/org.jboss.tools.forge.runtime.ext/src/org/jboss/tools/forge/runtime/ext/EventHandler.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -3,6 +3,7 @@
 import javax.enterprise.event.Observes;
 import javax.inject.Inject;
 
+import org.jboss.forge.project.Project;
 import org.jboss.forge.shell.Shell;
 import org.jboss.forge.shell.events.CommandExecuted;
 
@@ -14,7 +15,12 @@
 	private Shell shell;
 	
 	public void handleCommandExecuted(@Observes CommandExecuted event) {
-		sendEscaped("Executed Command: " + event.getCommand().getName());
+		Project project = shell.getCurrentProject();
+		String str = "no project";
+		if (project != null) {
+			str = project.getProjectRoot().getName();
+		}
+		sendEscaped("Executed Command: " + event.getCommand().getName() + " Current Project Directory: " + str);
 	}
 	
 	private void sendEscaped(String str) {

Deleted: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/CommandRecorder.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/CommandRecorder.java	2011-10-19 13:31:52 UTC (rev 35803)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/CommandRecorder.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -1,266 +0,0 @@
-package org.jboss.tools.forge.ui.console;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IField;
-import org.eclipse.jdt.core.IJavaElement;
-import org.eclipse.jdt.core.ISourceRange;
-import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.core.JavaCore;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jface.text.DocumentEvent;
-import org.eclipse.jface.text.IDocumentListener;
-import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.ide.IDE;
-import org.eclipse.ui.part.ISetSelectionTarget;
-import org.eclipse.ui.texteditor.ITextEditor;
-import org.jboss.tools.forge.importer.ProjectConfigurationUpdater;
-import org.jboss.tools.forge.importer.ProjectImporter;
-import org.jboss.tools.forge.ui.ForgeUIPlugin;
-
-public class CommandRecorder implements IDocumentListener {
-	
-	private StringBuffer buffer = new StringBuffer();
-	private String beforePrompt = null;
-	private String currentPrompt = null;
-	private String currentCommand = null;
-
-	@Override
-	public void documentAboutToBeChanged(DocumentEvent event) {
-	}
-
-	@Override
-	public void documentChanged(DocumentEvent event) {
-		if (event.getLength() == 1 && "".equals(event.getText())) {
-			buffer.setLength(buffer.length() - 1);
-		} else {
-			buffer.append(event.getText());
-			String newPrompt = getNewPrompt();
-			if (newPrompt != null) {
-				currentPrompt = newPrompt;
-				beforePrompt = buffer.substring(0, buffer.length() - newPrompt.length());
-				buffer.setLength(0);
-				if (currentCommand != null) {
-					postProcessCurrentCommand();
-					currentCommand = null;
-				}
-			} else {
-				if (currentPrompt != null) {
-					String newCommand = getNewCommand();
-					if (newCommand != null) {
-						currentCommand = newCommand;
-					}
-				}
-			}
-		}
-	}
-	
-	private String getNewPrompt() {
-		int lastLineBreak = buffer.lastIndexOf("\n");
-		if (lastLineBreak == -1) return null;
-		String lastLine = buffer.substring(lastLineBreak + 1);
-		if (lastLine.length() == 0) return null;
-		if (lastLine.charAt(0) != '[') return null;
-		int rightBracketIndex = lastLine.indexOf(']');
-		if (rightBracketIndex == -1) return null;
-		return lastLine.endsWith("$ ") ? lastLine : null;
-	}
-	
-	private String getNewCommand() {
-		String candidateCommand = buffer.toString();
-		if ("pwd".equals(candidateCommand)) {
-			return "pwd";
-		} else if ("new-project".equals(candidateCommand)) {
-			return "new-project";
-		} else if ("persistence".equals(candidateCommand)) {
-			return "persistence";
-		} else if ("entity".equals(candidateCommand)) {
-			return "entity";
-		} else if ("field".equals(candidateCommand)) {
-			return "field";
-		} else if ("prettyfaces".equals(candidateCommand)) {
-			return "prettyfaces";
-		} else if ("build".equals(candidateCommand)) {
-			return "build";
-		} else {
-			return null;
-		}
-	}
-	
-	private void postProcessCurrentCommand() {
-		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-		IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
-		String projectName = currentPrompt.substring(1, currentPrompt.indexOf(']'));
-		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
-		if (project != null) {
-			try {
-				project.refreshLocal(IResource.DEPTH_INFINITE, null);
-			} catch (CoreException e) {
-				ForgeUIPlugin.log(e);
-			}
-		}
-		if ("pwd".equals(currentCommand)) {
-			// do nothing
-		} else if ("new-project".equals(currentCommand)) {
-			int index = beforePrompt.lastIndexOf("***SUCCESS*** Created project [");
-			if (index == -1) return;
-			if (index + 31 > beforePrompt.length()) return;
-			String str = beforePrompt.substring(index + 31);
-			index = str.lastIndexOf("] in new working directory [");
-			if (index == -1) return;
-			if (index + 28 > str.length()) return;
-			str = str.substring(index + 28);
-			index = str.indexOf("]");
-			if (index == -1) return;
-			String projectPath = str.substring(0, index);
-			index = projectPath.lastIndexOf('/');
-			String projectDirName = projectPath.substring(index + 1);
-			String projectBaseDirPath = projectPath.substring(0, index);
-			ProjectImporter importer = new ProjectImporter(projectBaseDirPath, projectDirName);
-			importer.importProject();
-		} else if ("persistence".equals(currentCommand)) {
-			int index = beforePrompt.lastIndexOf("***SUCCESS*** Installed [forge.spec.jpa] successfully.\nWrote ");
-			if (index == -1) return;
-			try {
-				IFile file = project.getFile("/src/main/resources/META-INF/persistence.xml");
-				if (file == null) return;
-				Object objectToSelect = file;
-				IDE.openEditor(workbenchPage, file);
-				IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
-				if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
-					((ISetSelectionTarget)projectExplorer).selectReveal(new StructuredSelection(objectToSelect));
-				} 
-				IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer"); 
-				if (packageExplorer == null && projectExplorer == null) {
-					packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
-				} 
-				if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
-					((ISetSelectionTarget)packageExplorer).selectReveal(new StructuredSelection(objectToSelect));
-				}
-			} catch (PartInitException e) {
-				ForgeUIPlugin.log(e);
-			}
-		} else if ("entity".equals(currentCommand)) {
-			int index = beforePrompt.lastIndexOf("Picked up type <JavaResource>: ");
-			if (index == -1) return;
-			if (index + 31 > beforePrompt.length() -1) return;
-			String entityName = beforePrompt.substring(index + 31, beforePrompt.length() - 1).replace('.', '/');
-			try {
-				IFile file = project.getFile("/src/main/java/" + entityName + ".java");
-				if (file == null) return;
-				Object objectToSelect = file;
-				IDE.openEditor(workbenchPage, file);
-				IJavaElement javaElement = JavaCore.create(file);
-				if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
-					try {
-						objectToSelect = ((ICompilationUnit)javaElement).getTypes()[0];
-					} catch (JavaModelException e) {
-						ForgeUIPlugin.log(e);
-					}
-				}
-				IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
-				if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
-					((ISetSelectionTarget)projectExplorer).selectReveal(new StructuredSelection(objectToSelect));
-				} 
-				IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer"); 
-				if (packageExplorer == null && projectExplorer == null) {
-					packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
-				} 
-				if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
-					((ISetSelectionTarget)packageExplorer).selectReveal(new StructuredSelection(objectToSelect));
-				}
-			} catch (PartInitException e) {
-				ForgeUIPlugin.log(e);
-			}
-		} else if ("field".equals(currentCommand)) {
-			try {
-				int index = beforePrompt.lastIndexOf("Added field to ");
-				if (index == -1) return;
-				if (index + 15 > beforePrompt.length()) return;
-				String str = beforePrompt.substring(index + 15);
-				index = str.indexOf(':');
-				if (index == -1) return;
-				String entityName = str.substring(0, index);
-				str = str.substring(index);
-				index = str.lastIndexOf(';');
-				if (index == -1) return;
-				str = str.substring(0, index);
-				index = str.lastIndexOf(' ');
-				if (index == -1) return;
-				String fieldName = str.substring(index + 1);
-				IFile file = project.getFile("/src/main/java/" + entityName.replace('.', '/') + ".java");
-				if (file == null) return;
-				IEditorPart editorPart = IDE.openEditor(workbenchPage, file);
-				IJavaElement javaElement = JavaCore.create(file);
-				if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
-					try {
-						IType type = ((ICompilationUnit)javaElement).getTypes()[0];
-						IField field = type.getField(fieldName);
-						if (field != null) {
-							ISourceRange sourceRange = field.getSourceRange();
-							if (sourceRange != null && editorPart != null && editorPart instanceof ITextEditor) {
-								((ITextEditor)editorPart).selectAndReveal(sourceRange.getOffset(), sourceRange.getLength());
-							}
-						}
-					} catch (JavaModelException e) {
-						ForgeUIPlugin.log(e);
-					}
-				}
-			} catch (PartInitException e) {
-				ForgeUIPlugin.log(e);
-			}
-		} else if ("prettyfaces".equals(currentCommand)) {
-			int index = beforePrompt.lastIndexOf("***SUCCESS*** Installed [com.ocpsoft.prettyfaces] successfully.");
-			if (index == -1) return;
-			String str = beforePrompt.substring(0, index - 1);
-			index = str.lastIndexOf("Wrote ");
-			if (index == -1) return;
-			if (index + 6 > str.length()) return;
-			str = str.substring(index + 6);
-			String projectLocation = project.getLocation().toString();
-			index = str.lastIndexOf(projectLocation);
-			if (index != 0) return;
-			str = str.substring(projectLocation.length());
-			IFile file = project.getFile(str);
-			if (file == null) return;
-			Object objectToSelect = file;
-			new ProjectConfigurationUpdater(project).updateProject();
-			try {
-				IDE.openEditor(workbenchPage, file);
-				IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
-				if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
-					((ISetSelectionTarget)projectExplorer).selectReveal(new StructuredSelection(objectToSelect));
-				} 
-				IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer"); 
-				if (packageExplorer == null && projectExplorer == null) {
-					packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
-				} 
-				if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
-					((ISetSelectionTarget)packageExplorer).selectReveal(new StructuredSelection(objectToSelect));
-				}
-			} catch (PartInitException e) {
-				ForgeUIPlugin.log(e);
-			}
-		} else if ("build".equals(currentCommand)) {
-			
-		} else {
-			
-		}
-		try {
-			workbenchPage.showView("org.jboss.tools.forge.console").setFocus();
-		} catch (PartInitException e) {
-			ForgeUIPlugin.log(e);
-		}
-	}
-
-}

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/FieldPostProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/FieldPostProcessor.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/FieldPostProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,64 @@
+package org.jboss.tools.forge.ui.console;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IField;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.ISourceRange;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.jboss.tools.forge.ui.ForgeUIPlugin;
+
+public class FieldPostProcessor implements ForgeCommandPostProcessor {
+
+	@Override
+	public void postProcessCommand(String command, String output) {
+		try {
+			int index = output.lastIndexOf("Added field to ");
+			if (index == -1) return;
+			if (index + 15 > output.length()) return;
+			String str = output.substring(index + 15);
+			index = str.indexOf(':');
+			if (index == -1) return;
+			String entityName = str.substring(0, index);
+			str = str.substring(index);
+			index = str.lastIndexOf(';');
+			if (index == -1) return;
+			str = str.substring(0, index);
+			index = str.lastIndexOf(' ');
+			if (index == -1) return;
+			String fieldName = str.substring(index + 1);
+			IProject project = ForgeCommandPostProcessorHelper.getProject(command);
+			if (project == null) return;
+			IFile file = project.getFile("/src/main/java/" + entityName.replace('.', '/') + ".java");
+			if (file == null) return;
+			IJavaElement javaElement = JavaCore.create(file);
+			if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
+				try {
+					IType type = ((ICompilationUnit)javaElement).getTypes()[0];
+					IField field = type.getField(fieldName);
+					if (field != null) {
+						ISourceRange sourceRange = field.getSourceRange();
+						IWorkbenchPage workbenchPage = ForgeCommandPostProcessorHelper.getActiveWorkbenchPage();
+						IEditorPart editorPart = IDE.openEditor(workbenchPage, file);
+						if (sourceRange != null && editorPart != null && editorPart instanceof ITextEditor) {
+							((ITextEditor)editorPart).selectAndReveal(sourceRange.getOffset(), sourceRange.getLength());
+						}
+					}
+				} catch (JavaModelException e) {
+					ForgeUIPlugin.log(e);
+				}
+			}
+		} catch (PartInitException e) {
+			ForgeUIPlugin.log(e);
+		}
+	}
+
+}

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessor.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,7 @@
+package org.jboss.tools.forge.ui.console;
+
+public interface ForgeCommandPostProcessor {
+	
+	void postProcessCommand(String command, String str);
+
+}

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessorHelper.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessorHelper.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandPostProcessorHelper.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,41 @@
+package org.jboss.tools.forge.ui.console;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+
+public class ForgeCommandPostProcessorHelper {
+
+	public static String getProjectName(String command) {
+		String result = null;
+		int i = command.indexOf("Current Project Directory: ");
+		if (i != -1 && command.length() >= i + 27) {
+			result = command.substring(i + 27).trim();
+			if ("".equals(result)) {
+				result = null;
+			}
+		}
+		return result;
+	}
+	
+	public static IProject getProject(String command) {
+		IProject result = null;
+		String projectName = getProjectName(command);
+		if (projectName != null) {
+			result = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+		}
+		return result;
+	}
+	
+	public static IWorkbenchPage getActiveWorkbenchPage() {
+		IWorkbenchPage result = null;
+		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+		if (workbenchWindow != null) {
+			result = workbenchWindow.getActivePage();
+		}
+		return result;		
+	}
+	
+}

Modified: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandProcessor.java	2011-10-19 13:31:52 UTC (rev 35803)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeCommandProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -1,5 +1,92 @@
 package org.jboss.tools.forge.ui.console;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.jboss.tools.forge.ui.ForgeUIPlugin;
+
+
 public class ForgeCommandProcessor {
-
+	
+	static Map<String, ForgeCommandPostProcessor> POST_PROCESSORS = null;
+	
+	public static Map<String, ForgeCommandPostProcessor> getPostProcessors() {
+		if (POST_PROCESSORS == null) {
+			POST_PROCESSORS = new HashMap<String, ForgeCommandPostProcessor>();
+			POST_PROCESSORS.put("new-project", new NewProjectPostProcessor());
+			POST_PROCESSORS.put("persistence", new PersistencePostProcessor());
+			POST_PROCESSORS.put("pick-up", new PickUpPostProcessor());
+			POST_PROCESSORS.put("field", new FieldPostProcessor());
+			POST_PROCESSORS.put("prettyfaces", new PrettyFacesPostProcessor());
+		}
+		return POST_PROCESSORS;
+	}
+	
+	private String currentCommand;
+	private StringBuffer buffer = new StringBuffer();
+	
+	public void startCommand(String command) {
+		buffer.setLength(0);
+		currentCommand = command;
+	}
+	
+	public void stopCurrentCommand() {
+		if (currentCommand != null) {
+			postProcessCommand(currentCommand, buffer.toString());
+		}
+		currentCommand = null;
+	}
+	
+	public void log(String str) {
+		if (currentCommand != null) {
+			buffer.append(str);
+		}
+	}
+	
+	private void postProcessCommand(final String command, final String output) {
+		String mainCommand = command;
+		int i = mainCommand.indexOf(' ');
+		if (i != -1) {
+			mainCommand = command.substring(0, i);
+		}
+		final ForgeCommandPostProcessor postProcessor = getPostProcessors().get(mainCommand);
+		if (postProcessor != null) {
+			Display.getDefault().asyncExec(new Runnable() {
+				@Override
+				public void run() {
+					refreshWorkspace();
+					postProcessor.postProcessCommand(command, output);
+					showForgeConsole();
+				}				
+			});
+		}
+	}
+		
+	private void refreshWorkspace() {
+		try {
+			ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
+		} catch (CoreException e) {
+			ForgeUIPlugin.log(e);
+		}
+	}
+	
+	private void showForgeConsole() {
+		try {
+			IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+			IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
+			workbenchPage.showView("org.jboss.tools.forge.console").setFocus();
+		} catch (PartInitException e) {
+			ForgeUIPlugin.log(e);
+		}
+		
+	}
+	
 }

Modified: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeTextViewer.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeTextViewer.java	2011-10-19 13:31:52 UTC (rev 35803)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/ForgeTextViewer.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -64,6 +64,7 @@
         }
     }
 	
+    private ForgeCommandProcessor commandProcessor;
     private RuntimeStopListener stopListener;
     private ForgeOutputListener outputListener;
     private ForgeRuntime runtime;
@@ -80,7 +81,7 @@
     private void initialize() {
         initDocument();
         initViewer();
-        initCommandRecorder();
+        initForgeCommandProcessor();
         initOutputListener();
         initStopListener();
     }
@@ -141,14 +142,15 @@
     	return Display.getDefault().getSystemColor(colorCode);
     }
     
-    private void initCommandRecorder() {
-    	getDocument().addDocumentListener(new CommandRecorder());
+    private void initForgeCommandProcessor() {
+    	commandProcessor = new ForgeCommandProcessor();
     }
     
     private void initOutputListener() {
     	ForgeOutputListener target = new ForgeOutputListener() {			
 			@Override
 			public void outputAvailable(final String output) {
+				commandProcessor.log(output);
 				Display.getDefault().asyncExec(new Runnable() {
 					@Override
 					public void run() {
@@ -176,12 +178,13 @@
 		outputListener = new ForgeHiddenOutputFilter(ansiCommandFilter) {
 			@Override
 			public void handleFilteredString(String str) {
+//				System.out.println("handle filtered string: " + str);
 				if (str.startsWith("Intercepted Command: ")) {
-					
+					commandProcessor.startCommand(str.substring(21));
 				} else if (str.startsWith("Executed Command: ")) {
-					
-				} else {
-					System.out.println("unhandled hidden output: " + str);
+					commandProcessor.stopCurrentCommand();
+//				} else {
+//					System.out.println("unhandled hidden output: " + str);
 				}
 			}
 		};

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/NewProjectPostProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/NewProjectPostProcessor.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/NewProjectPostProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,27 @@
+package org.jboss.tools.forge.ui.console;
+
+import org.jboss.tools.forge.importer.ProjectImporter;
+
+public class NewProjectPostProcessor implements ForgeCommandPostProcessor {
+
+	@Override
+	public void postProcessCommand(String command, String output) {
+		int index = output.lastIndexOf("***SUCCESS*** Created project [");
+		if (index == -1) return;
+		if (index + 31 > output.length()) return;
+		output = output.substring(index + 31);
+		index = output.lastIndexOf("] in new working directory [");
+		if (index == -1) return;
+		if (index + 28 > output.length()) return;
+		output = output.substring(index + 28);
+		index = output.indexOf("]");
+		if (index == -1) return;
+		String projectPath = output.substring(0, index);
+		index = projectPath.lastIndexOf('/');
+		String projectDirName = projectPath.substring(index + 1);
+		String projectBaseDirPath = projectPath.substring(0, index);
+		ProjectImporter importer = new ProjectImporter(projectBaseDirPath, projectDirName);
+		importer.importProject();
+	}
+
+}

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PersistencePostProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PersistencePostProcessor.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PersistencePostProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,50 @@
+package org.jboss.tools.forge.ui.console;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.part.ISetSelectionTarget;
+import org.jboss.tools.forge.ui.ForgeUIPlugin;
+
+public class PersistencePostProcessor implements ForgeCommandPostProcessor {
+
+	@Override
+	public void postProcessCommand(String command, String output) {
+		if (command.indexOf("setup") != -1) {
+			postProcessPersistenceSetup(command, output);
+		}
+	}
+	
+	private void postProcessPersistenceSetup(final String command, final String output) {
+			IProject project = ForgeCommandPostProcessorHelper.getProject(command);
+			if (project == null) return;
+			int index = output.lastIndexOf("***SUCCESS***");
+			if (index == -1) return;
+			try {
+				IFile file = project.getFile("/src/main/resources/META-INF/persistence.xml");
+				if (file == null) return;
+				Object objectToSelect = file;
+				IWorkbenchPage workbenchPage = ForgeCommandPostProcessorHelper.getActiveWorkbenchPage();
+				IDE.openEditor(workbenchPage, file);
+				IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
+				if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
+					((ISetSelectionTarget)projectExplorer).selectReveal(new StructuredSelection(objectToSelect));
+				} 
+				IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer"); 
+				if (packageExplorer == null && projectExplorer == null) {
+					packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
+				} 
+				if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
+					((ISetSelectionTarget)packageExplorer).selectReveal(new StructuredSelection(objectToSelect));
+				}
+			} catch (PartInitException e) {
+				ForgeUIPlugin.log(e);
+			}
+	}
+	
+	
+}

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PickUpPostProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PickUpPostProcessor.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PickUpPostProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,59 @@
+package org.jboss.tools.forge.ui.console;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.part.ISetSelectionTarget;
+import org.jboss.tools.forge.ui.ForgeUIPlugin;
+
+public class PickUpPostProcessor implements ForgeCommandPostProcessor {
+
+	@Override
+	public void postProcessCommand(final String command, final String output) {
+		IProject project = ForgeCommandPostProcessorHelper.getProject(command);
+		if (project == null) return;
+		int index = output.lastIndexOf("Picked up type <JavaResource>: ");
+		if (index == -1) return;
+		if (index + 31 > output.length() -1) return;
+		String entityName = output.substring(index + 31, output.length() - 1).replace('.', '/');
+		try {
+			IFile file = project.getFile("/src/main/java/" + entityName + ".java");
+			if (file == null) return;
+			Object objectToSelect = file;
+			IWorkbenchPage workbenchPage = ForgeCommandPostProcessorHelper.getActiveWorkbenchPage();
+			if (workbenchPage == null) return;
+			IDE.openEditor(workbenchPage, file);
+			IJavaElement javaElement = JavaCore.create(file);
+			if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
+				try {
+					objectToSelect = ((ICompilationUnit)javaElement).getTypes()[0];
+				} catch (JavaModelException e) {
+					ForgeUIPlugin.log(e);
+				}
+			}
+			IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
+			if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
+				((ISetSelectionTarget)projectExplorer).selectReveal(new StructuredSelection(objectToSelect));
+			} 
+			IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer"); 
+			if (packageExplorer == null && projectExplorer == null) {
+				packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
+			} 
+			if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
+				((ISetSelectionTarget)packageExplorer).selectReveal(new StructuredSelection(objectToSelect));
+			}
+		} catch (PartInitException e) {
+			ForgeUIPlugin.log(e);
+		}
+	}
+
+	
+}

Added: trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PrettyFacesPostProcessor.java
===================================================================
--- trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PrettyFacesPostProcessor.java	                        (rev 0)
+++ trunk/forge/plugins/org.jboss.tools.forge.ui/src/org/jboss/tools/forge/ui/console/PrettyFacesPostProcessor.java	2011-10-19 14:02:42 UTC (rev 35804)
@@ -0,0 +1,53 @@
+package org.jboss.tools.forge.ui.console;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.part.ISetSelectionTarget;
+import org.jboss.tools.forge.importer.ProjectConfigurationUpdater;
+import org.jboss.tools.forge.ui.ForgeUIPlugin;
+
+public class PrettyFacesPostProcessor implements ForgeCommandPostProcessor {
+
+	@Override
+	public void postProcessCommand(String command, String output) {
+		IProject project = ForgeCommandPostProcessorHelper.getProject(command);
+		int index = output.lastIndexOf("***SUCCESS*** Installed [com.ocpsoft.prettyfaces] successfully.");
+		if (index == -1) return;
+		String str = output.substring(0, index - 1);
+		index = str.lastIndexOf("Wrote ");
+		if (index == -1) return;
+		if (index + 6 > str.length()) return;
+		str = str.substring(index + 6);
+		String projectLocation = project.getLocation().toString();
+		index = str.lastIndexOf(projectLocation);
+		if (index != 0) return;
+		str = str.substring(projectLocation.length());
+		IFile file = project.getFile(str);
+		if (file == null) return;
+		Object objectToSelect = file;
+		new ProjectConfigurationUpdater(project).updateProject();
+		try {
+			IWorkbenchPage workbenchPage = ForgeCommandPostProcessorHelper.getActiveWorkbenchPage();
+			IDE.openEditor(workbenchPage, file);
+			IViewPart projectExplorer = workbenchPage.findView("org.eclipse.ui.navigator.ProjectExplorer");
+			if (projectExplorer != null && projectExplorer instanceof ISetSelectionTarget) {
+				((ISetSelectionTarget)projectExplorer).selectReveal(new StructuredSelection(objectToSelect));
+			} 
+			IViewPart packageExplorer = workbenchPage.findView("org.eclipse.jdt.ui.PackageExplorer"); 
+			if (packageExplorer == null && projectExplorer == null) {
+				packageExplorer = workbenchPage.showView("org.eclipse.jdt.ui.PackageExplorer");
+			} 
+			if (packageExplorer != null && packageExplorer instanceof ISetSelectionTarget) {
+				((ISetSelectionTarget)packageExplorer).selectReveal(new StructuredSelection(objectToSelect));
+			}
+		} catch (PartInitException e) {
+			ForgeUIPlugin.log(e);
+		}
+	}
+
+}



More information about the jbosstools-commits mailing list