Author: joern
Date: Mon Oct 4 14:28:28 2010
New Revision: 1004252
URL: http://svn.apache.org/viewvc?rev=1004252&view=rev
Log:
UIMA-1887 Added a button to the editor error page to chose a type system, add a
resource dialog to chose the type system and updated the editor to be able to
replace the error page with the new input
Added:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/WorkspaceResourceDialog.java
(with props)
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java
URL:
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java?rev=1004252&r1=1004251&r2=1004252&view=diff
==============================================================================
---
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java
(original)
+++
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java
Mon Oct 4 14:28:28 2010
@@ -55,6 +55,7 @@ import org.eclipse.core.resources.IResou
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
@@ -88,13 +89,20 @@ import org.eclipse.swt.events.KeyListene
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.layout.RowLayout;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
@@ -106,10 +114,13 @@ import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.ui.part.PageBookView;
+import org.eclipse.ui.texteditor.IDocumentProviderExtension;
import org.eclipse.ui.texteditor.IStatusField;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
+import org.eclipse.ui.texteditor.InfoForm;
import org.eclipse.ui.texteditor.StatusTextEditor;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
@@ -521,7 +532,7 @@ public final class AnnotationEditor exte
*/
private int mCursorPosition;
- private ICasDocument mDocument;
+// private ICasDocument mDocument;
boolean mIsSomethingHighlighted = false;
@@ -662,56 +673,8 @@ public final class AnnotationEditor exte
getSourceViewer().setEditable(false);
getSite().setSelectionProvider(mFeatureStructureSelectionProvider);
-
- if (getDocument() != null) {
- mShowAnnotationsMenu = new ShowAnnotationsMenu(
-
getDocumentProvider().getEditorAnnotationStatus(getEditorInput()),
- getDocument().getCAS().getTypeSystem(), shownAnnotationTypes);
- mShowAnnotationsMenu.addListener(new IShowAnnotationsListener() {
-
- public void selectionChanged(Collection<Type> selection) {
-
- // if changes selection is either larger, or smaller
- // if larger an annotation type was added
- // if smaller one was removed
- if (shownAnnotationTypes.size() < selection.size()) {
- List<Type> clonedCollection = new ArrayList<Type>(selection);
- clonedCollection.removeAll(shownAnnotationTypes);
-
- Type addedAnnotationType = clonedCollection.get(0);
- showAnnotationType(addedAnnotationType, true);
- getDocumentProvider().addShownType(getEditorInput(),
addedAnnotationType);
- }
- else if (selection.size() < shownAnnotationTypes.size()) {
- List<Type> clonedCollection = new
ArrayList<Type>(shownAnnotationTypes);
- clonedCollection.removeAll(selection);
-
- Type removedAnnotationType = clonedCollection.get(0);
- showAnnotationType(removedAnnotationType, false);
- getDocumentProvider().removeShownType(getEditorInput(),
removedAnnotationType);
- }
-
- // Repaint after annotations are changed
- mPainter.paint(IPainter.CONFIGURATION);
-
- EditorAnnotationStatus status =
-
getDocumentProvider().getEditorAnnotationStatus(getEditorInput());
-
- getDocumentProvider().setEditorAnnotationStatus(getEditorInput(),
- new EditorAnnotationStatus(status.getMode(), selection));
-
- if (mEditorListener != null) {
- for (IAnnotationEditorModifyListener listener : mEditorListener)
- listener.showAnnotationsChanged(selection);
- }
- }
- });
-
- EditorAnnotationStatus status = getDocumentProvider()
- .getEditorAnnotationStatus(getEditorInput());
-
- setAnnotationMode(getDocument().getType(status.getMode()));
- }
+
+ initiallySynchronizeUI();
}
// TODO: still not called always, e.g. on mouse selection
@@ -731,22 +694,29 @@ public final class AnnotationEditor exte
@Override
protected void doSetInput(IEditorInput input) throws CoreException {
+
super.doSetInput(input);
+
+ if (getDocument() != null) {
- mDocument = (ICasDocument) getDocumentProvider().getDocument(input);
-
- if (mDocument != null) {
-
+ // Register listener to listens for deletion events,
+ // if the file opened in this editor is deleted, the editor should be
closed!
closeEditorListener = new CloseEditorListener(this);
ResourcesPlugin.getWorkspace().addResourceChangeListener(closeEditorListener,
IResourceChangeEvent.POST_CHANGE);
+ // Synchronize all annotation from the document with
+ // the editor
syncAnnotations();
+ // Register listener to synchronize annotations between the
+ // editor and the document in case the annotations
+ // change e.g. updated in a view
mAnnotationSynchronizer = new DocumentListener();
-
getDocument().addChangeListener(mAnnotationSynchronizer);
+ // Register listener to synchronize annotation styles
+ // between multiple open annotation editors
mAnnotationStyleListener = new IAnnotationStyleListener() {
public void annotationStylesChanged(Collection<AnnotationStyle>
styles) {
@@ -757,20 +727,104 @@ public final class AnnotationEditor exte
getDocumentProvider().addAnnotationStyleListener(getEditorInput(),
mAnnotationStyleListener);
+ // Synchronize shown types with the editor
Collection<String> shownTypes =
getDocumentProvider().getShownTypes(input);
for (String shownType : shownTypes) {
+ Type type = getDocument().getType(shownType);
+
// Types can be deleted from the type system but still be marked
// as shown in the .dotCorpus file, in that case the type
// name cannot be mapped to a type and should be ignored.
- Type type = getDocument().getType(shownType);
+
if (type != null)
shownAnnotationTypes.add(type);
}
+
+ if (getSourceViewer() != null) {
+
+ // This branch is usually only executed when the
+ // input was updated because it could not be opened the
+ // first time trough an error e.g. no type system available
+ //
+ // Compared to the usual code branch createPartControl was
+ // already called without a document, which means
+ // that the state between the UI and the document
+ // must be synchronized now
+
+ initiallySynchronizeUI();
+
+ IWorkbenchPage page = getSite().getWorkbenchWindow().getActivePage();
+
+ for (IWorkbenchPart view : page.getViews()) {
+ if (view instanceof PageBookView) {
+ ((PageBookView) view).partBroughtToTop(getEditorSite().getPart());
+ }
+ }
+ }
+
}
}
+
+ /**
+ * Initialized the UI from the freshly set document.
+ *
+ * Note: Does nothing if getDoucment() return null.
+ */
+ private void initiallySynchronizeUI() {
+ if (getDocument() != null) {
+
+ mShowAnnotationsMenu = new ShowAnnotationsMenu(
+ null,
+ getDocument().getCAS().getTypeSystem(), shownAnnotationTypes);
+ mShowAnnotationsMenu.addListener(new IShowAnnotationsListener() {
+
+ public void selectionChanged(Collection<Type> selection) {
+
+ // if changes selection is either larger, or smaller
+ // if larger an annotation type was added
+ // if smaller one was removed
+ if (shownAnnotationTypes.size() < selection.size()) {
+ List<Type> clonedCollection = new ArrayList<Type>(selection);
+ clonedCollection.removeAll(shownAnnotationTypes);
+
+ Type addedAnnotationType = clonedCollection.get(0);
+ showAnnotationType(addedAnnotationType, true);
+ getDocumentProvider().addShownType(getEditorInput(),
addedAnnotationType);
+ }
+ else if (selection.size() < shownAnnotationTypes.size()) {
+ List<Type> clonedCollection = new
ArrayList<Type>(shownAnnotationTypes);
+ clonedCollection.removeAll(selection);
+
+ Type removedAnnotationType = clonedCollection.get(0);
+ showAnnotationType(removedAnnotationType, false);
+ getDocumentProvider().removeShownType(getEditorInput(),
removedAnnotationType);
+ }
+
+ // Repaint after annotations are changed
+ mPainter.paint(IPainter.CONFIGURATION);
+
+ EditorAnnotationStatus status =
+
getDocumentProvider().getEditorAnnotationStatus(getEditorInput());
+ getDocumentProvider().setEditorAnnotationStatus(getEditorInput(),
+ new EditorAnnotationStatus(status.getMode(), selection));
+
+ if (mEditorListener != null) {
+ for (IAnnotationEditorModifyListener listener : mEditorListener)
+ listener.showAnnotationsChanged(selection);
+ }
+ }
+ });
+
+ EditorAnnotationStatus status =
+ getDocumentProvider().getEditorAnnotationStatus(getEditorInput());
+
+ setAnnotationMode(getDocument().getType(status.getMode()));
+ }
+ }
+
@Override
protected void editorContextMenuAboutToShow(IMenuManager menu) {
super.editorContextMenuAboutToShow(menu);
@@ -824,7 +878,7 @@ public final class AnnotationEditor exte
* @return current <code>AnnotationDocument</code>
*/
public ICasDocument getDocument() {
- return mDocument;
+ return (ICasDocument) getDocumentProvider().getDocument(getEditorInput());
}
/**
@@ -860,7 +914,8 @@ public final class AnnotationEditor exte
}
public Collection<Type> getShownAnnotationTypes() {
- return mShowAnnotationsMenu.getSelectedTypes();
+ return Collections.unmodifiableCollection(shownAnnotationTypes);
+// return mShowAnnotationsMenu.getSelectedTypes();
}
public void setShownAnnotationType(Type type, boolean isShown) {
@@ -926,7 +981,7 @@ public final class AnnotationEditor exte
// Add all annotation to the model
// copy annotations into annotation model
- final Iterator<AnnotationFS> mAnnotations =
mDocument.getCAS().getAnnotationIndex()
+ final Iterator<AnnotationFS> mAnnotations =
getDocument().getCAS().getAnnotationIndex()
.iterator();
while (mAnnotations.hasNext()) {
@@ -1211,4 +1266,57 @@ public final class AnnotationEditor exte
return dirtyParts.toArray(new AnnotationEditor[dirtyParts.size()]);
}
+
+ @Override
+ protected Control createStatusControl(Composite parent, IStatus status) {
+
+ // Type System is missing in non Cas Editor Project case
+ if (status.getCode() == 12 && getEditorInput() instanceof FileEditorInput)
{
+
+ // Note:
+ // If the editor is not active and the user clicks on the button
+ // the editor gets activated and an exception is logged
+ // on the second click the button is selected
+ // How to fix the exception ?!
+ // Only tested on OS X Snow Leopard
+
+ Composite provideTypeSystemForm = new Composite(parent, SWT.NONE);
+ provideTypeSystemForm.setLayout(new GridLayout(1, false));
+ Label infoLabel = new Label(provideTypeSystemForm, SWT.NONE);
+ infoLabel.setText(status.getMessage());
+ Button retryButton = new Button(provideTypeSystemForm, SWT.NONE);
+ retryButton.setText("Choose Type System ...");
+ retryButton.addSelectionListener(new SelectionListener() {
+ public void widgetSelected(SelectionEvent e) {
+
+ // Open a dialog to let the user choose a type system
+ WorkspaceResourceDialog resourceDialog = new
WorkspaceResourceDialog();
+ IResource resource =
resourceDialog.getWorkspaceResourceElement(Display.getCurrent().getActiveShell(),
+ ResourcesPlugin.getWorkspace().getRoot(),
+ "Select a Type System", "Please select a Type System:");
+
+ if (resource != null) {
+ DefaultCasDocumentProvider provider = (DefaultCasDocumentProvider)
getDocumentProvider();
+
+ FileEditorInput editorInput = (FileEditorInput) getEditorInput();
+
provider.setTypeSystem(editorInput.getFile().getFullPath().toPortableString(),
+ resource.getFullPath().toPortableString());
+
+ // Now set the input again to open the editor with the
+ // specified type system
+ setInput(getEditorInput());
+ }
+ }
+
+ public void widgetDefaultSelected(SelectionEvent e) {
+ throw new IllegalStateException("Never be called!");
+ }
+ });
+
+ return provideTypeSystemForm;
+ }
+ else {
+ return super.createStatusControl(parent, status);
+ }
+ }
}
Modified:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
URL:
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java?rev=1004252&r1=1004251&r2=1004252&view=diff
==============================================================================
---
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
(original)
+++
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/DefaultCasDocumentProvider.java
Mon Oct 4 14:28:28 2010
@@ -37,6 +37,7 @@ import org.apache.uima.caseditor.core.mo
import org.apache.uima.caseditor.core.model.dotcorpus.DotCorpus;
import org.apache.uima.caseditor.core.model.dotcorpus.DotCorpusSerializer;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
@@ -67,6 +68,15 @@ public class DefaultCasDocumentProvider
*/
private Map<String, DotCorpus> styles = new HashMap<String, DotCorpus>();
+ private String getStyleFileForTypeSystem(String typeSystemFile) {
+ int lastSlashIndex = typeSystemFile.lastIndexOf("/");
+
+ String styleId = typeSystemFile.substring(0, lastSlashIndex + 1);
+ styleId = styleId + ".style-" + typeSystemFile.substring(lastSlashIndex +
1);
+
+ return styleId;
+ }
+
@Override
protected IDocument createDocument(Object element) throws CoreException {
if (element instanceof FileEditorInput) {
@@ -113,12 +123,26 @@ public class DefaultCasDocumentProvider
else {
// Try to find a type system for the CAS file
-
- // TODO: Discuss logic to resolve type system on dev list
- // For now it will just be assumed that the type system is placed
- // in a file called TypeSystem.xml at the root of the project
-
- IFile typeSystemFile = casFile.getProject().getFile("TypeSystem.xml");
+
+ // First check if a type system is already known or was
+ // set by the editor for this specific CAS
+ String typeSystemFileString =
documentToTypeSystemMap.get(casFile.getFullPath().toPortableString());
+
+ // If non was found, use the default name!
+ if (typeSystemFileString == null)
+ typeSystemFileString = "TypeSystem.xml";
+
+ // TODO: Change to only use full path
+ IFile typeSystemFile = null;
+
+ IResource typeSystemResource =
ResourcesPlugin.getWorkspace().getRoot().
+ findMember(new Path(typeSystemFileString));
+
+ if (typeSystemResource instanceof IFile)
+ typeSystemFile = (IFile) typeSystemResource;
+
+ if (typeSystemFile == null)
+ typeSystemFile = casFile.getProject().getFile(typeSystemFileString);
if (typeSystemFile.exists()) {
@@ -129,7 +153,8 @@ public class DefaultCasDocumentProvider
// colors could change completely when the a type is
// added or removed to the type system
- IFile styleFile =
casFile.getProject().getFile(".style-TypeSystem.xml");
+ IFile styleFile =
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(
+
getStyleFileForTypeSystem(typeSystemFile.getFullPath().toPortableString())));
DotCorpus dotCorpus =
styles.get(styleFile.getFullPath().toPortableString());
@@ -200,10 +225,14 @@ public class DefaultCasDocumentProvider
try {
casIn.close();
} catch (IOException e) {
- // TODO: how to handle this error ?
- // Throw an exception, or continue to display file and
- // leave stream open ?
- e.printStackTrace();
+ // Unable to close file after loading it
+ //
+ // In the current implementation the user
+ // does not notice the error and can just
+ // edit the file, tough saving it might fail
+ // if the io error persists
+
+ CasEditorPlugin.log(e);
}
}
@@ -215,14 +244,13 @@ public class DefaultCasDocumentProvider
return document;
}
else {
- IStatus status = new Status(IStatus.ERROR, "org.apache.uima.dev",
IStatus.OK,
+ IStatus status = new Status(IStatus.ERROR, "org.apache.uima.dev", 12,
"Cannot find type system!\nPlease place a valid type system
in this path:\n" +
typeSystemFile.getLocation().toOSString(), null);
elementErrorStatus.put(element, status);
}
}
-
}
return null;
@@ -234,10 +262,10 @@ public class DefaultCasDocumentProvider
super.disposeElementInfo(element, info);
// Remove the mapping of document to type system
- if (element instanceof FileEditorInput) {
- FileEditorInput editorInput = (FileEditorInput) element;
-
documentToTypeSystemMap.remove(editorInput.getFile().getFullPath().toPortableString());
- }
+// if (element instanceof FileEditorInput) {
+// FileEditorInput editorInput = (FileEditorInput) element;
+//
documentToTypeSystemMap.remove(editorInput.getFile().getFullPath().toPortableString());
+// }
}
@Override
@@ -323,12 +351,7 @@ public class DefaultCasDocumentProvider
}
private void saveStyles(Object element) {
- String tsId = getTypesystemId(element);
-
- int lastSlashIndex = tsId.lastIndexOf("/");
-
- String styleId = tsId.substring(0, lastSlashIndex + 1);
- styleId = styleId + ".style-" + tsId.substring(lastSlashIndex + 1);
+ String styleId = getStyleFileForTypeSystem(getTypesystemId(element));
DotCorpus style = styles.get(styleId);
@@ -478,4 +501,8 @@ public class DefaultCasDocumentProvider
sharedEditorStatus.put(getTypesystemId(element), editorAnnotationStatus);
}
}
+
+ void setTypeSystem(String document, String typeSystem) {
+ documentToTypeSystemMap.put(document, typeSystem);
+ }
}
Added:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/WorkspaceResourceDialog.java
URL:
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/WorkspaceResourceDialog.java?rev=1004252&view=auto
==============================================================================
---
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/WorkspaceResourceDialog.java
(added)
+++
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/WorkspaceResourceDialog.java
Mon Oct 4 14:28:28 2010
@@ -0,0 +1,71 @@
+package org.apache.uima.caseditor.editor;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
+import org.eclipse.ui.model.WorkbenchContentProvider;
+import org.eclipse.ui.model.WorkbenchLabelProvider;
+import org.eclipse.ui.views.navigator.ResourceSorter;
+
+/**
+ *
+ *
+ */
+public class WorkspaceResourceDialog
+{
+ public static IResource getWorkspaceResourceElement (Shell shell,
IResource root,
+ String dialogTitle, String dialogMessage)
+ {
+ IResource resource = null;
+
+ ElementTreeSelectionDialog dialog = new
ElementTreeSelectionDialog(shell,
+ new WorkbenchLabelProvider(), new WorkbenchContentProvider());
+ dialog.setTitle(dialogTitle);
+ dialog.setMessage(dialogMessage);
+ dialog.setInput(root);
+ dialog.setSorter(new ResourceSorter(ResourceSorter.NAME));
+ int buttonId = dialog.open();
+ if (buttonId == IDialogConstants.OK_ID) {
+ resource = (IResource) dialog.getFirstResult();
+ if (!resource.isAccessible()) {
+ return null;
+ }
+ if (resource instanceof IContainer) {
+ }
+ String arg = resource.getFullPath().toString();
+ // String fileLoc =
VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc",
arg); //$NON-NLS-1$
+ // Trace.trace("resource.getFullPath().toString():" + arg);
+ // Trace.trace(fileLoc);
+ // selectedElement = resource.getLocation().toOSString();
+ }
+ return resource;
+ }
+
+
+ public static IResource getWorkspaceResourceElement (Shell shell)
+ {
+ IResource resource = null;
+ ElementTreeSelectionDialog dialog = new
ElementTreeSelectionDialog(shell,
+ new WorkbenchLabelProvider(), new WorkbenchContentProvider());
+ dialog.setTitle("Select Cpe descriptor");
+ dialog.setMessage("Select Cpe Xml descriptor file");
+ dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
+ dialog.setSorter(new ResourceSorter(ResourceSorter.NAME));
+ int buttonId = dialog.open();
+ if (buttonId == IDialogConstants.OK_ID) {
+ resource = (IResource) dialog.getFirstResult();
+ if (!resource.isAccessible()) {
+ return null;
+ }
+ String arg = resource.getFullPath().toString();
+ // String fileLoc =
VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc",
arg); //$NON-NLS-1$
+ // Trace.trace(fileLoc);
+ // selectedElement = resource.getLocation().toOSString();
+ }
+ return resource;
+ }
+
+}
\ No newline at end of file
Propchange:
uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/WorkspaceResourceDialog.java
------------------------------------------------------------------------------
svn:mime-type = text/plain