merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditor.java
index 77869dfcdd4a451a305f15dc395cbeaf98e379a9..73bf55b4539c60bd16f84dcfbdb85d191291f4a3 100644 (file)
@@ -1,20 +1,23 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
 
 package eu.etaxonomy.taxeditor.bulkeditor;
 
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.TextSelection;
 import org.eclipse.jface.text.source.Annotation;
 import org.eclipse.jface.text.source.AnnotationModel;
 import org.eclipse.jface.text.source.ISourceViewer;
@@ -41,27 +44,33 @@ import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
 import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
+import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
- * <p>BulkEditor class.</p>
+ * <p>
+ * BulkEditor class.
+ * </p>
  *
  * @author p.ciardelli
  * @created 07.07.2009
  * @version 1.0
  */
-public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDetails, IDirtyMarkableSelectionProvider{
+public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDetails, IPartContentHasMedia,
+        IDirtyMarkableSelectionProvider {
 
        /** Constant <code>ID="bulkeditor.editor"</code> */
        public static final String ID = "bulkeditor.editor";
 
        private boolean isInitialFocus = true;
-       
+
        private BulkEditorSearch searchBar = null;
-       
+
        private IPropertyChangeListener markerPreferenceListener;
 
+       private boolean isDirty;
+
        /**
         * <p>Constructor for BulkEditor.</p>
         */
@@ -76,32 +85,33 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
        @Override
        protected void initializeEditor() {
                super.initializeEditor();
-                               
+
                /**
                 * see AbstractTextEditor javadoc for explanation of context menu ids
                 */
                setEditorContextMenuId("#BulkEditorContext");
 
 //             setEntityCreatorService(new BulkEditorEntityCreatorService());
-               
+
                setLineDisplayStrategy(new BulkEditorLineDisplay(this));
-               
+
                setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy));
        }
-       
+
        /* (non-Javadoc)
         * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
         */
        /** {@inheritDoc} */
-       protected ISourceViewer createSourceViewer(Composite parent,
+       @Override
+    protected ISourceViewer createSourceViewer(Composite parent,
                        IVerticalRuler ruler, int styles) {
                ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles);
-               if (((AbstractBulkEditorInput) getEditorInput()).isMergingEnabled()) {
+               if (getEditorInput().isMergingEnabled()) {
                        addToggleMergeCandidateListener(ruler.getControl());
                }
                return viewer;
        }
-       
+
        /* (non-Javadoc)
         * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
         */
@@ -109,46 +119,46 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
        @Override
        public void init(IEditorSite site, IEditorInput input)
                        throws PartInitException {
-               
+
                if (!(input instanceof AbstractBulkEditorInput)) {
                        throw new PartInitException("Invalid Input: Must be BulkEditorInput");
                }
-               
+
                super.init(site, input);
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
         */
        /** {@inheritDoc} */
        @Override
        public void createPartControl(Composite parent) {
-               
+
                parent.setLayout(new GridLayout());
-               
+
                Composite layoutComposite = new Composite(parent, SWT.NONE);
                layoutComposite.setLayout(new GridLayout());
 
                GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
                layoutComposite.setLayoutData(gridData);
-               
+
 //             layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
-               
-               
+
+
                searchBar = new BulkEditorSearch(this, layoutComposite, SWT.NONE);
 //             layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
-                               
+
                super.createPartControl(parent);
-               
+
                // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
                for (Control control : parent.getChildren()) {
-                       if (control instanceof Composite && 
+                       if (control instanceof Composite &&
                                                !(control.equals(layoutComposite))) {
                                control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                        }
                }
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.ui.editors.text.TextEditor#dispose()
         */
@@ -178,16 +188,16 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
        public void setFocus() {
                searchBar.setFocus();
 
-               // TODO find a better place to put this - this dialog should be shown after initial contents of 
+               // TODO find a better place to put this - this dialog should be shown after initial contents of
                //      Editor are displayed
                if (isInitialFocus) {
                        displayWarningDialog();
                        isInitialFocus = false;
                }
        }
-       
+
        /**
-        * 
+        *
         */
        private void displayWarningDialog() {
                IPreferenceStore prefs = PreferencesUtil.getPreferenceStore();
@@ -196,14 +206,14 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
                                                        "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
                                                        "For instance, a reference may be displayed with both a name and a descriptive element. If the reference name is changed here, the display of both the name and the descriptive element will be affected.";
                        MessageDialogWithToggle dialog = MessageDialogWithToggle.openOkCancelConfirm
-                                                                                               (getSite().getShell(), "Bulk Editor", msg, "Do not show this message again", 
+                                                                                               (getSite().getShell(), "Bulk Editor", msg, "Do not show this message again",
                                                                                                                false, null, PreferencesUtil.HIDE_BULKEDITOR_INFO);
-                       if (dialog.getReturnCode() == Window.OK) { 
+                       if (dialog.getReturnCode() == Window.OK) {
                                prefs.setValue(PreferencesUtil.HIDE_BULKEDITOR_INFO, dialog.getToggleState());
                        }
                }
        }
-       
+
        /**
         * @param control
         */
@@ -224,14 +234,14 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
         * @param line a int.
         */
        public void toggleMergeCandidateAnnotation(int line) {
-               
+
                IDocument document = getSourceViewer().getDocument();
-               LineAnnotationModel model = 
+               LineAnnotationModel model =
                                (LineAnnotationModel) getSourceViewer().getAnnotationModel();
-               
+
                if(model != null){
                        Annotation annotation = model.getAnnotationAtLine(line, document);
-                       
+
                        if (annotation != null) {
                                if (annotation.getType().equals(IBulkEditorConstants.TYPE_MERGE_CANDIDATE)) {
                                        model.changeAnnotationType(
@@ -243,25 +253,53 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
                        }
                }
        }
-       
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#isDirty()
+        */
+       @Override
+       public boolean isDirty() {
+           if(isDirty){
+               return isDirty;
+           }
+           else{
+               return super.isDirty();
+           }
+       }
+
+       public void forceDirty(){
+           isDirty = true;
+           firePropertyChange(PROP_DIRTY);
+       }
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
+        */
+       @Override
+       public void doSave(IProgressMonitor progressMonitor) {
+           isDirty = false;
+           super.doSave(progressMonitor);
+       }
+
        /** {@inheritDoc} */
-       public void changed(Object object) {
+       @Override
+    public void changed(Object object) {
 //             this.dirty = dirty;
                AnnotatedLineDocumentProvider p = (AnnotatedLineDocumentProvider) getDocumentProvider();
                p.changed(object);
 //             firePropertyChange(PROP_DIRTY);
        }
-       
+
        /* (non-Javadoc)
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
         */
        /** {@inheritDoc} */
        public void performSearch(BulkEditorQuery query) {
                if (query != null) {
-                       
+
                        // TODO check if dirty, prompt save
                        if (isDirty()) {
-                               boolean proceed = MessageDialog.openQuestion(getEditorSite().getShell(), 
+                               boolean proceed = MessageDialog.openQuestion(getEditorSite().getShell(),
                                                "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
                                if (proceed) {
                                        doSave(null);
@@ -269,31 +307,41 @@ public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDe
                                        return;
                                }
                        }
-                                               
+
                        getEditorInput().performSearch(query);
-                       
+
                        refresh();
+
+                       selectFirstItem();
+
+                       getSourceViewer().getTextWidget().setFocus();
                }
        }
-       
+
+       private void selectFirstItem() {
+               ITextSelection selection = new TextSelection(0, 0);
+               getSelectionProvider().setSelection(selection);
+       }
+
        /**
-        * 
+        *
         */
        public void refresh() {
                if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
                        ((AnnotationModel) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
                }
-               
+
                setInput(getEditorInput());
        }
-       
+
        /** {@inheritDoc} */
        @Override
        public AbstractBulkEditorInput getEditorInput() {
                return (AbstractBulkEditorInput) super.getEditorInput();
        }
-       
-       protected void editorContextMenuAboutToShow(IMenuManager menu) {
+
+       @Override
+    protected void editorContextMenuAboutToShow(IMenuManager menu) {
                super.editorContextMenuAboutToShow(menu);
                menu.remove(ITextEditorActionConstants.SHIFT_RIGHT);
                menu.remove(ITextEditorActionConstants.SHIFT_LEFT);