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
.IDirtyMarkableSelectionProvider
;
46 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
47 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
48 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
49 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
60 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
, IPartContentHasMedia
,
61 IDirtyMarkableSelectionProvider
{
63 /** Constant <code>ID="bulkeditor.editor"</code> */
64 public static final String ID
= "bulkeditor.editor";
66 private boolean isInitialFocus
= true;
68 private BulkEditorSearch searchBar
= null;
70 private IPropertyChangeListener markerPreferenceListener
;
72 private boolean isDirty
;
75 * <p>Constructor for BulkEditor.</p>
78 super(CdmStore
.createConversation());
82 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
86 protected void initializeEditor() {
87 super.initializeEditor();
90 * see AbstractTextEditor javadoc for explanation of context menu ids
92 setEditorContextMenuId("#BulkEditorContext");
94 // setEntityCreatorService(new BulkEditorEntityCreatorService());
96 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
98 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
102 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
106 protected ISourceViewer
createSourceViewer(Composite parent
,
107 IVerticalRuler ruler
, int styles
) {
108 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
109 if (getEditorInput().isMergingEnabled()) {
110 addToggleMergeCandidateListener(ruler
.getControl());
116 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
120 public void init(IEditorSite site
, IEditorInput input
)
121 throws PartInitException
{
123 if (!(input
instanceof AbstractBulkEditorInput
)) {
124 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
127 super.init(site
, input
);
131 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
135 public void createPartControl(Composite parent
) {
137 parent
.setLayout(new GridLayout());
139 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
140 layoutComposite
.setLayout(new GridLayout());
142 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
143 layoutComposite
.setLayoutData(gridData
);
145 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
148 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
149 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
151 super.createPartControl(parent
);
153 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
154 for (Control control
: parent
.getChildren()) {
155 if (control
instanceof Composite
&&
156 !(control
.equals(layoutComposite
))) {
157 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
163 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
167 public void dispose() {
168 if (markerPreferenceListener
!= null ) {
169 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
175 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
179 public boolean isEditable() {
184 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
188 public void setFocus() {
189 searchBar
.setFocus();
191 // TODO find a better place to put this - this dialog should be shown after initial contents of
192 // Editor are displayed
193 if (isInitialFocus
) {
194 displayWarningDialog();
195 isInitialFocus
= false;
202 private void displayWarningDialog() {
203 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
204 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
205 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
206 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
207 "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.";
208 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
209 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
210 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
211 if (dialog
.getReturnCode() == Window
.OK
) {
212 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
220 private void addToggleMergeCandidateListener(Control control
) {
221 control
.addMouseListener(new MouseAdapter() {
223 public void mouseDoubleClick(MouseEvent e
) {
224 StyledText textWidget
= getSourceViewer().getTextWidget();
225 int line
= textWidget
.getLineIndex(e
.y
);
226 toggleMergeCandidateAnnotation(line
);
232 * <p>toggleMergeCandidateAnnotation</p>
236 public void toggleMergeCandidateAnnotation(int line
) {
238 IDocument document
= getSourceViewer().getDocument();
239 LineAnnotationModel model
=
240 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
243 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
245 if (annotation
!= null) {
246 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
247 model
.changeAnnotationType(
248 annotation
, LineAnnotation
.TYPE_GENERIC
);
250 model
.changeAnnotationType(
251 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
258 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#isDirty()
261 public boolean isDirty() {
266 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
);