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
.jface
.action
.IMenuManager
;
14 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
15 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
16 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
17 import org
.eclipse
.jface
.text
.IDocument
;
18 import org
.eclipse
.jface
.text
.ITextSelection
;
19 import org
.eclipse
.jface
.text
.TextSelection
;
20 import org
.eclipse
.jface
.text
.source
.Annotation
;
21 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
22 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
23 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
24 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
25 import org
.eclipse
.jface
.window
.Window
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.custom
.StyledText
;
28 import org
.eclipse
.swt
.events
.MouseAdapter
;
29 import org
.eclipse
.swt
.events
.MouseEvent
;
30 import org
.eclipse
.swt
.layout
.GridData
;
31 import org
.eclipse
.swt
.layout
.GridLayout
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.ui
.IEditorInput
;
35 import org
.eclipse
.ui
.IEditorSite
;
36 import org
.eclipse
.ui
.PartInitException
;
37 import org
.eclipse
.ui
.texteditor
.ITextEditorActionConstants
;
39 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineDocumentProvider
;
40 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineEditor
;
41 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
42 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
43 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
44 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
45 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
46 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
47 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
51 * <p>BulkEditor class.</p>
57 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
, IDirtyMarkableSelectionProvider
{
59 /** Constant <code>ID="bulkeditor.editor"</code> */
60 public static final String ID
= "bulkeditor.editor";
62 private boolean isInitialFocus
= true;
64 private BulkEditorSearch searchBar
= null;
66 private IPropertyChangeListener markerPreferenceListener
;
69 * <p>Constructor for BulkEditor.</p>
72 super(CdmStore
.createConversation());
76 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
80 protected void initializeEditor() {
81 super.initializeEditor();
84 * see AbstractTextEditor javadoc for explanation of context menu ids
86 setEditorContextMenuId("#BulkEditorContext");
88 // setEntityCreatorService(new BulkEditorEntityCreatorService());
90 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
92 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
96 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
100 protected ISourceViewer
createSourceViewer(Composite parent
,
101 IVerticalRuler ruler
, int styles
) {
102 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
103 if (getEditorInput().isMergingEnabled()) {
104 addToggleMergeCandidateListener(ruler
.getControl());
110 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
114 public void init(IEditorSite site
, IEditorInput input
)
115 throws PartInitException
{
117 if (!(input
instanceof AbstractBulkEditorInput
)) {
118 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
121 super.init(site
, input
);
125 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
129 public void createPartControl(Composite parent
) {
131 parent
.setLayout(new GridLayout());
133 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
134 layoutComposite
.setLayout(new GridLayout());
136 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
137 layoutComposite
.setLayoutData(gridData
);
139 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
142 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
143 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
145 super.createPartControl(parent
);
147 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
148 for (Control control
: parent
.getChildren()) {
149 if (control
instanceof Composite
&&
150 !(control
.equals(layoutComposite
))) {
151 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
157 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
161 public void dispose() {
162 if (markerPreferenceListener
!= null ) {
163 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
169 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
173 public boolean isEditable() {
178 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
182 public void setFocus() {
183 searchBar
.setFocus();
185 // TODO find a better place to put this - this dialog should be shown after initial contents of
186 // Editor are displayed
187 if (isInitialFocus
) {
188 displayWarningDialog();
189 isInitialFocus
= false;
196 private void displayWarningDialog() {
197 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
198 if (!prefs
.getBoolean(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
)) {
199 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
200 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
201 "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.";
202 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
203 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
204 false, null, IPreferenceKeys
.HIDE_BULKEDITOR_INFO
);
205 if (dialog
.getReturnCode() == Window
.OK
) {
206 prefs
.setValue(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
214 private void addToggleMergeCandidateListener(Control control
) {
215 control
.addMouseListener(new MouseAdapter() {
217 public void mouseDoubleClick(MouseEvent e
) {
218 StyledText textWidget
= getSourceViewer().getTextWidget();
219 int line
= textWidget
.getLineIndex(e
.y
);
220 toggleMergeCandidateAnnotation(line
);
226 * <p>toggleMergeCandidateAnnotation</p>
230 public void toggleMergeCandidateAnnotation(int line
) {
232 IDocument document
= getSourceViewer().getDocument();
233 LineAnnotationModel model
=
234 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
237 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
239 if (annotation
!= null) {
240 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
241 model
.changeAnnotationType(
242 annotation
, LineAnnotation
.TYPE_GENERIC
);
244 model
.changeAnnotationType(
245 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
253 public void changed(Object object
) {
254 // this.dirty = dirty;
255 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
257 // firePropertyChange(PROP_DIRTY);
261 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
264 public void performSearch(BulkEditorQuery query
) {
267 // TODO check if dirty, prompt save
269 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
270 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
278 getEditorInput().performSearch(query
);
284 getSourceViewer().getTextWidget().setFocus();
288 private void selectFirstItem() {
289 ITextSelection selection
= new TextSelection(0, 0);
290 getSelectionProvider().setSelection(selection
);
296 public void refresh() {
297 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
298 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
301 setInput(getEditorInput());
306 public AbstractBulkEditorInput
getEditorInput() {
307 return (AbstractBulkEditorInput
) super.getEditorInput();
311 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
312 super.editorContextMenuAboutToShow(menu
);
313 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
314 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
315 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);