3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.bulkeditor
;
13 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
14 import org
.eclipse
.jface
.action
.IMenuManager
;
15 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
17 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
18 import org
.eclipse
.jface
.text
.IDocument
;
19 import org
.eclipse
.jface
.text
.ITextSelection
;
20 import org
.eclipse
.jface
.text
.TextSelection
;
21 import org
.eclipse
.jface
.text
.source
.Annotation
;
22 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
23 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
24 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
25 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
26 import org
.eclipse
.jface
.window
.Window
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.custom
.StyledText
;
29 import org
.eclipse
.swt
.events
.MouseAdapter
;
30 import org
.eclipse
.swt
.events
.MouseEvent
;
31 import org
.eclipse
.swt
.layout
.GridData
;
32 import org
.eclipse
.swt
.layout
.GridLayout
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Control
;
35 import org
.eclipse
.ui
.IEditorInput
;
36 import org
.eclipse
.ui
.IEditorSite
;
37 import org
.eclipse
.ui
.PartInitException
;
38 import org
.eclipse
.ui
.texteditor
.ITextEditorActionConstants
;
40 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineDocumentProvider
;
41 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineEditor
;
42 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
43 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
44 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
45 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
46 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
47 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
,
62 /** Constant <code>ID="bulkeditor.editor"</code> */
63 public static final String ID
= "bulkeditor.editor";
65 private boolean isInitialFocus
= true;
67 private BulkEditorSearch searchBar
= null;
69 private IPropertyChangeListener markerPreferenceListener
;
71 private boolean isDirty
;
74 * <p>Constructor for BulkEditor.</p>
77 super(CdmStore
.createConversation());
81 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
85 protected void initializeEditor() {
86 super.initializeEditor();
89 * see AbstractTextEditor javadoc for explanation of context menu ids
91 setEditorContextMenuId("#BulkEditorContext");
93 // setEntityCreatorService(new BulkEditorEntityCreatorService());
95 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
97 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
101 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
105 protected ISourceViewer
createSourceViewer(Composite parent
,
106 IVerticalRuler ruler
, int styles
) {
107 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
108 if (getEditorInput().isMergingEnabled()) {
109 addToggleMergeCandidateListener(ruler
.getControl());
115 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
119 public void init(IEditorSite site
, IEditorInput input
)
120 throws PartInitException
{
122 if (!(input
instanceof AbstractBulkEditorInput
)) {
123 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
126 super.init(site
, input
);
130 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
134 public void createPartControl(Composite parent
) {
136 parent
.setLayout(new GridLayout());
138 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
139 layoutComposite
.setLayout(new GridLayout());
141 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
142 layoutComposite
.setLayoutData(gridData
);
144 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
147 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
148 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
150 super.createPartControl(parent
);
152 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
153 for (Control control
: parent
.getChildren()) {
154 if (control
instanceof Composite
&&
155 !(control
.equals(layoutComposite
))) {
156 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
162 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
166 public void dispose() {
167 if (markerPreferenceListener
!= null ) {
168 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
174 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
178 public boolean isEditable() {
183 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
187 public void setFocus() {
188 searchBar
.setFocus();
190 // TODO find a better place to put this - this dialog should be shown after initial contents of
191 // Editor are displayed
192 if (isInitialFocus
) {
193 displayWarningDialog();
194 isInitialFocus
= false;
201 private void displayWarningDialog() {
202 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
203 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
204 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
205 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
206 "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.";
207 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
208 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
209 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
210 if (dialog
.getReturnCode() == Window
.OK
) {
211 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
219 private void addToggleMergeCandidateListener(Control control
) {
220 control
.addMouseListener(new MouseAdapter() {
222 public void mouseDoubleClick(MouseEvent e
) {
223 StyledText textWidget
= getSourceViewer().getTextWidget();
224 int line
= textWidget
.getLineIndex(e
.y
);
225 toggleMergeCandidateAnnotation(line
);
231 * <p>toggleMergeCandidateAnnotation</p>
235 public void toggleMergeCandidateAnnotation(int line
) {
237 IDocument document
= getSourceViewer().getDocument();
238 LineAnnotationModel model
=
239 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
242 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
244 if (annotation
!= null) {
245 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
246 model
.changeAnnotationType(
247 annotation
, LineAnnotation
.TYPE_GENERIC
);
249 model
.changeAnnotationType(
250 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
257 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#isDirty()
260 public boolean isDirty() {
265 return super.isDirty();
270 public void forceDirty(){
272 firePropertyChange(PROP_DIRTY
);
276 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
279 public void doSave(IProgressMonitor progressMonitor
) {
281 super.doSave(progressMonitor
);
286 public void changed(Object object
) {
287 // this.dirty = dirty;
288 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
290 // firePropertyChange(PROP_DIRTY);
294 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
297 public void performSearch(BulkEditorQuery query
) {
300 // TODO check if dirty, prompt save
302 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
303 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
311 getEditorInput().performSearch(query
);
317 getSourceViewer().getTextWidget().setFocus();
321 private void selectFirstItem() {
322 ITextSelection selection
= new TextSelection(0, 0);
323 getSelectionProvider().setSelection(selection
);
329 public void refresh() {
330 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
331 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
334 setInput(getEditorInput());
339 public AbstractBulkEditorInput
getEditorInput() {
340 return (AbstractBulkEditorInput
) super.getEditorInput();
344 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
345 super.editorContextMenuAboutToShow(menu
);
346 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
347 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
348 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);