Author: DartPeng
Date: 2010-01-05 04:20:25 -0500 (Tue, 05 Jan 2010)
New Revision: 19644
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/plugin.xml
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksResourceChangeListener.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksToolsStartup.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksXMLEditor.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/editor/AbstractSmooksFormEditor.java
Log:
JBIDE-5561
Fixed it.
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.ui/plugin.xml
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.ui/plugin.xml 2010-01-05 07:33:17 UTC (rev
19643)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.ui/plugin.xml 2010-01-05 09:20:25 UTC (rev
19644)
@@ -44,12 +44,6 @@
</extension>
<extension
- point="org.eclipse.ui.startup">
- <startup
-
class="org.jboss.tools.smooks.configuration.editors.SmooksToolsStartup">
- </startup>
- </extension>
- <extension
point="org.eclipse.ui.decorators">
<decorator
adaptable="false"
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksResourceChangeListener.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksResourceChangeListener.java 2010-01-05
07:33:17 UTC (rev 19643)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksResourceChangeListener.java 2010-01-05
09:20:25 UTC (rev 19644)
@@ -39,6 +39,12 @@
public class SmooksResourceChangeListener implements IResourceChangeListener {
public static final String SMOOKS_CONTENTTYPE_ID =
"org.jboss.tools.smooks.ui.smooks.contentType";
+ private IEditorPart currentEditPart = null;
+
+ public SmooksResourceChangeListener(IEditorPart editorPart) {
+ this.currentEditPart = editorPart;
+ }
+
/*
* (non-Javadoc)
*
@@ -59,12 +65,14 @@
}
-
class ChangePartNameVisitor implements IResourceDeltaVisitor {
-// int count = 0;
+ // int count = 0;
public boolean visit(IResourceDelta delta) {
+ if (delta == null
+ || !delta.getResource().equals(((IFileEditorInput)
currentEditPart.getEditorInput()).getFile()))
+ return true;
IResource res = delta.getResource();
if (res instanceof IFile) {
IFile file = (IFile) res;
@@ -98,7 +106,7 @@
IPath newPath = res.getFullPath();
String newfileName = newPath.lastSegment();
- if (newfileName.equals(fileName)) {
+ if (fileName.equals(newfileName)) {
return true;
}
final String newPartName = newfileName;
@@ -113,12 +121,15 @@
for (int i = 0; i < editorReferences.length; i++) {
IEditorReference iEditorReference = editorReferences[i];
IEditorPart editorPart = iEditorReference.getEditor(false);
+ if (editorPart != currentEditPart)
+ continue;
+
IEditorInput editorInput = editorPart.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IFile relatedFile = ((IFileEditorInput) editorInput).getFile();
if (relatedFile != null && relatedFile.getFullPath().equals(fOldPath))
{
if (editorPart instanceof AbstractSmooksFormEditor) {
- ((AbstractSmooksFormEditor) editorPart).setPartName(newPartName);
+// ((AbstractSmooksFormEditor) editorPart).setPartName(newPartName);
break;
}
}
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksToolsStartup.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksToolsStartup.java 2010-01-05
07:33:17 UTC (rev 19643)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksToolsStartup.java 2010-01-05
09:20:25 UTC (rev 19644)
@@ -10,7 +10,6 @@
******************************************************************************/
package org.jboss.tools.smooks.configuration.editors;
-import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.ui.IStartup;
/**
@@ -23,7 +22,7 @@
* @see org.eclipse.ui.IStartup#earlyStartup()
*/
public void earlyStartup() {
- ResourcesPlugin.getWorkspace().addResourceChangeListener(new
SmooksResourceChangeListener());
+// ResourcesPlugin.getWorkspace().addResourceChangeListener(new
SmooksResourceChangeListener());
}
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksXMLEditor.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksXMLEditor.java 2010-01-05
07:33:17 UTC (rev 19643)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SmooksXMLEditor.java 2010-01-05
09:20:25 UTC (rev 19644)
@@ -10,6 +10,8 @@
******************************************************************************/
package org.jboss.tools.smooks.configuration.editors;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.ui.IEditorInput;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
/**
@@ -18,4 +20,13 @@
*/
public class SmooksXMLEditor extends StructuredTextEditor {
+ /* (non-Javadoc)
+ * @see
org.eclipse.wst.sse.ui.StructuredTextEditor#doSetInput(org.eclipse.ui.IEditorInput)
+ */
+ @Override
+ public void doSetInput(IEditorInput input) throws CoreException {
+ // TODO Auto-generated method stub
+ super.doSetInput(input);
+ }
+
}
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/editor/AbstractSmooksFormEditor.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/editor/AbstractSmooksFormEditor.java 2010-01-05
07:33:17 UTC (rev 19643)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/editor/AbstractSmooksFormEditor.java 2010-01-05
09:20:25 UTC (rev 19644)
@@ -17,9 +17,13 @@
import java.util.Timer;
import java.util.TimerTask;
-import org.apache.xml.serialize.OutputFormat;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IResourceChangeEvent;
+import org.eclipse.core.resources.IResourceChangeListener;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.IResourceDeltaVisitor;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
@@ -47,7 +51,9 @@
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
+import org.eclipse.jface.text.TextViewer;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
@@ -55,6 +61,7 @@
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.ide.FileStoreEditorInput;
+import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.jboss.tools.smooks.configuration.RuntimeDependency;
@@ -103,6 +110,10 @@
public class AbstractSmooksFormEditor extends FormEditor implements
IEditingDomainProvider,
ISmooksModelValidateListener, ISmooksModelProvider {
+ private IResourceChangeListener resourceChangeListener = null;
+
+ private IDocumentListener xmlDocumentTraker = null;
+
private Exception initSmooksModelException = null;
protected String platformVersion = SmooksConstants.VERSION_1_2;
@@ -139,6 +150,8 @@
public AbstractSmooksFormEditor() {
super();
+ resourceChangeListener = new SmooksResourceTraker();
+ xmlDocumentTraker = new SmooksXMLEditorDocumentListener();
initEditingDomain();
}
@@ -239,7 +252,7 @@
EObject rootModel = this.getSmooksModel();
fillComments(doc, rootModel);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
-
+
XMLUtils.outDOMNode(doc, stream);
return stream;
} catch (Throwable t) {
@@ -462,49 +475,36 @@
protected StructuredTextEditor createTextEditor() {
SmooksXMLEditor xmlEditor = new SmooksXMLEditor() {
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.jboss.tools.smooks.configuration.editors.SmooksXMLEditor#
+ * doSetInput(org.eclipse.ui.IEditorInput)
+ */
+ @Override
+ public void doSetInput(IEditorInput input) throws CoreException {
+ TextViewer viewer = getTextViewer();
+ if (viewer != null) {
+ IDocument document = viewer.getDocument();
+ if (document != null) {
+ document.removeDocumentListener(xmlDocumentTraker);
+ }
+ }
+ super.doSetInput(input);
+ viewer = getTextViewer();
+ if (viewer != null) {
+ IDocument document = viewer.getDocument();
+ if (document != null) {
+ document.addDocumentListener(xmlDocumentTraker);
+ }
+ }
+ }
+
public void createPartControl(Composite parent) {
super.createPartControl(parent);
- getTextViewer().getDocument().addDocumentListener(new IDocumentListener() {
+ getTextViewer().getDocument().addDocumentListener(xmlDocumentTraker);
- protected Timer timer = new Timer();
- protected TimerTask timerTask;
-
- public void documentAboutToBeChanged(DocumentEvent documentEvent) {
- // Ingore
- }
-
- public void documentChanged(final DocumentEvent documentEvent) {
- try {
- // This is need for the Properties view.
- //
- // setSelection(StructuredSelection.EMPTY);
-
- if (timerTask != null) {
- timerTask.cancel();
- }
-
- if (handleEMFModelChange) {
- handleEMFModelChange = false;
- } else {
- timerTask = new TimerTask() {
- @Override
- public void run() {
- getSite().getShell().getDisplay().asyncExec(new Runnable() {
- public void run() {
- handleDocumentChange();
- }
- });
- }
- };
-
- timer.schedule(timerTask, 1000);
- }
- } catch (Exception exception) {
- SmooksConfigurationActivator.getDefault().log(exception);
- }
- }
- });
-
}
};
@@ -647,42 +647,36 @@
validator.startValidate(smooksModel.eResource().getContents(), editingDomain);
}
} catch (IOException e) {
- SmooksConfigurationActivator.getDefault().log(e);
+ SmooksConfigurationActivator.log(e);
} finally {
monitor.done();
}
}
-
- public void setPartName(String partName){
- super.setPartName(partName);
- }
- @Override
- public void init(IEditorSite site, IEditorInput input) throws PartInitException {
- super.init(site, input);
+ public void setInput(IEditorInput input) {
+ if (getEditorInput() != null) {
+ IFile file = ((IFileEditorInput) getEditorInput()).getFile();
+ file.getWorkspace().removeResourceChangeListener(resourceChangeListener);
+ }
+
+ super.setInput(input);
+
+ if (getEditorInput() != null) {
+ IFile file = ((IFileEditorInput) getEditorInput()).getFile();
+ file.getWorkspace().addResourceChangeListener(resourceChangeListener);
+ setPartName(file.getName());
+ }
+
String filePath = null;
- String partName = "smooks editor"; //$NON-NLS-1$
+
IFile file = null;
- RuntimeMetadata runtimeMetadata = new RuntimeMetadata();
- if (input instanceof FileStoreEditorInput) {
- try {
- filePath = ((FileStoreEditorInput) input).getURI().toURL().getFile();
- runtimeMetadata.setSmooksConfig(new File(filePath));
- } catch (MalformedURLException e) {
- throw new
PartInitException(Messages.AbstractSmooksFormEditor_Exception_Transform_URL, e);
- }
- }
if (input instanceof IFileEditorInput) {
file = ((IFileEditorInput) input).getFile();
- runtimeMetadata.setSmooksConfig(file);
filePath = file.getFullPath().toPortableString();
- partName = file.getName();
}
- if (filePath == null)
- throw new
PartInitException(Messages.AbstractSmooksFormEditor_Exception_Cannot_Get_Input_File);
-
+ editingDomain.getResourceSet().getResources().clear();
// create EMF resource
Resource smooksResource = null;
if (file != null) {
@@ -692,33 +686,63 @@
smooksResource = new
SmooksResourceFactoryImpl().createResource(URI.createFileURI(filePath));
}
editingDomain.getResourceSet().getResources().add(smooksResource);
- setPartName(partName);
- // create Smooks validator
- validator = new SmooksModelValidator();
- addValidateListener(this);
-
Exception ex = checkSmooksConfigContents(null);
if (ex == null) {
- try {
- smooksResource.load(Collections.emptyMap());
- smooksModel = smooksResource.getContents().get(0);
- } catch (IOException e) {
- initSmooksModelException = e;
- // throw new PartInitException(e.getMessage());
+ if (smooksModel == null) {
+ try {
+ smooksResource.load(Collections.emptyMap());
+ smooksModel = smooksResource.getContents().get(0);
+ } catch (IOException e) {
+ initSmooksModelException = e;
+ }
+ } else {
+ smooksResource.getContents().add(smooksModel);
}
+ }
+ }
- if (smooksModel != null) {
- setDiagnosticList(validator.validate(smooksModel.eResource().getContents(),
editingDomain));
- // if success to open editor , check if there isn't ext element
- // in
- // smooks config file
- // create new one for it
- String version = SmooksUIUtils.judgeSmooksPlatformVersion(smooksModel);
- this.setPlatformVersion(version);
- judgeInputReader();
+ @Override
+ public void init(IEditorSite site, IEditorInput input) throws PartInitException {
+
+ // create Smooks validator
+ if (validator == null) {
+ validator = new SmooksModelValidator();
+ addValidateListener(this);
+ }
+
+ RuntimeMetadata runtimeMetadata = new RuntimeMetadata();
+ String filePath = null;
+ IFile file = null;
+ if (input instanceof FileStoreEditorInput) {
+ try {
+ filePath = ((FileStoreEditorInput) input).getURI().toURL().getFile();
+ runtimeMetadata.setSmooksConfig(new File(filePath));
+ } catch (MalformedURLException e) {
+ throw new
PartInitException(Messages.AbstractSmooksFormEditor_Exception_Transform_URL, e);
}
}
+ if (input instanceof IFileEditorInput) {
+ file = ((IFileEditorInput) input).getFile();
+ runtimeMetadata.setSmooksConfig(file);
+ filePath = file.getFullPath().toPortableString();
+ }
+
+ if (filePath == null)
+ throw new
PartInitException(Messages.AbstractSmooksFormEditor_Exception_Cannot_Get_Input_File);
+
+ super.init(site, input);
+
+ if (smooksModel != null) {
+ setDiagnosticList(validator.validate(smooksModel.eResource().getContents(),
editingDomain));
+ // if success to open editor , check if there isn't ext element
+ // in
+ // smooks config file
+ // create new one for it
+ String version = SmooksUIUtils.judgeSmooksPlatformVersion(smooksModel);
+ this.setPlatformVersion(version);
+ judgeInputReader();
+ }
}
private void assertConfigSupported(RuntimeMetadata runtimeMetadata) throws
PartInitException {
@@ -727,8 +751,7 @@
for (RuntimeDependency dependency : dependencies) {
if (!dependency.isSupportedByEditor()) {
java.net.URI changeToNS = dependency.getChangeToNS();
- String errorMsg = Messages.AbstractSmooksFormEditor_Error_Unsupported
- + dependency.getNamespaceURI()
+ String errorMsg = Messages.AbstractSmooksFormEditor_Error_Unsupported +
dependency.getNamespaceURI()
+ Messages.AbstractSmooksFormEditor_Error_Unsupported2;
if (changeToNS != null) {
@@ -910,4 +933,106 @@
// firePropertyChange(PROP_DIRTY);
// }
}
+
+ public class SmooksXMLEditorDocumentListener implements IDocumentListener {
+ protected Timer timer = new Timer();
+ protected TimerTask timerTask;
+
+ public void documentAboutToBeChanged(DocumentEvent documentEvent) {
+ // Ingore
+ }
+
+ public void documentChanged(final DocumentEvent documentEvent) {
+ try {
+ // This is need for the Properties view.
+ //
+ // setSelection(StructuredSelection.EMPTY);
+
+ if (timerTask != null) {
+ timerTask.cancel();
+ }
+
+ if (handleEMFModelChange) {
+ handleEMFModelChange = false;
+ } else {
+ timerTask = new TimerTask() {
+ @Override
+ public void run() {
+ getSite().getShell().getDisplay().asyncExec(new Runnable() {
+ public void run() {
+ handleDocumentChange();
+ }
+ });
+ }
+ };
+
+ timer.schedule(timerTask, 1000);
+ }
+ } catch (Exception exception) {
+ SmooksConfigurationActivator.log(exception);
+ }
+ }
+ }
+
+ public class SmooksResourceTraker implements IResourceChangeListener,
IResourceDeltaVisitor {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.core.resources.IResourceChangeListener#resourceChanged
+ * (org.eclipse.core.resources.IResourceChangeEvent)
+ */
+ public void resourceChanged(IResourceChangeEvent event) {
+ IResourceDelta delta = event.getDelta();
+ try {
+ if (delta != null)
+ delta.accept(this);
+ } catch (CoreException exception) {
+ }
+ }
+
+ public boolean visit(IResourceDelta delta) throws CoreException {
+ if (delta == null || !delta.getResource().equals(((IFileEditorInput)
getEditorInput()).getFile()))
+ return true;
+
+ if (delta.getKind() == IResourceDelta.REMOVED) {
+ Display display = getSite().getShell().getDisplay();
+ if ((IResourceDelta.MOVED_TO & delta.getFlags()) == 0) { // if
+ // the
+ // file
+ // was
+ // deleted
+ // NOTE: The case where an open, unsaved file is deleted is
+ // being handled by the
+ // PartListener added to the Workbench in the initialize()
+ // method.
+ display.asyncExec(new Runnable() {
+ public void run() {
+ // if (!isDirty())
+ closeEditor(false);
+ }
+ });
+ } else { // else if it was moved or renamed
+ final IFile newFile =
ResourcesPlugin.getWorkspace().getRoot().getFile(delta.getMovedToPath());
+ display.asyncExec(new Runnable() {
+ public void run() {
+ // try {
+ // ((SmooksXMLEditor) textEditor).doSetInput(new
+ // FileEditorInput(newFile));
+ // } catch (CoreException e) {
+ // e.printStackTrace();
+ // }
+ setInput(new FileEditorInput(newFile));
+ }
+ });
+ }
+ }
+ return false;
+ }
+ }
+
+ private void closeEditor(boolean forceSave) {
+ this.close(forceSave);
+ }
}