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
.PreferencesUtil
;
47 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 * <p>BulkEditor class.</p>
56 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
, IDirtyMarkableSelectionProvider
{
58 /** Constant <code>ID="bulkeditor.editor"</code> */
59 public static final String ID
= "bulkeditor.editor";
61 private boolean isInitialFocus
= true;
63 private BulkEditorSearch searchBar
= null;
65 private IPropertyChangeListener markerPreferenceListener
;
68 * <p>Constructor for BulkEditor.</p>
71 super(CdmStore
.createConversation());
75 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
79 protected void initializeEditor() {
80 super.initializeEditor();
83 * see AbstractTextEditor javadoc for explanation of context menu ids
85 setEditorContextMenuId("#BulkEditorContext");
87 // setEntityCreatorService(new BulkEditorEntityCreatorService());
89 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
91 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
95 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
99 protected ISourceViewer
createSourceViewer(Composite parent
,
100 IVerticalRuler ruler
, int styles
) {
101 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
102 if (getEditorInput().isMergingEnabled()) {
103 addToggleMergeCandidateListener(ruler
.getControl());
109 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
113 public void init(IEditorSite site
, IEditorInput input
)
114 throws PartInitException
{
116 if (!(input
instanceof AbstractBulkEditorInput
)) {
117 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
120 super.init(site
, input
);
124 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
128 public void createPartControl(Composite parent
) {
130 parent
.setLayout(new GridLayout());
132 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
133 layoutComposite
.setLayout(new GridLayout());
135 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
136 layoutComposite
.setLayoutData(gridData
);
138 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
141 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
142 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
144 super.createPartControl(parent
);
146 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
147 for (Control control
: parent
.getChildren()) {
148 if (control
instanceof Composite
&&
149 !(control
.equals(layoutComposite
))) {
150 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
156 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
160 public void dispose() {
161 if (markerPreferenceListener
!= null ) {
162 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
168 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
172 public boolean isEditable() {
177 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
181 public void setFocus() {
182 searchBar
.setFocus();
184 // TODO find a better place to put this - this dialog should be shown after initial contents of
185 // Editor are displayed
186 if (isInitialFocus
) {
187 displayWarningDialog();
188 isInitialFocus
= false;
195 private void displayWarningDialog() {
196 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
197 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
198 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
199 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
200 "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.";
201 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
202 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
203 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
204 if (dialog
.getReturnCode() == Window
.OK
) {
205 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
213 private void addToggleMergeCandidateListener(Control control
) {
214 control
.addMouseListener(new MouseAdapter() {
216 public void mouseDoubleClick(MouseEvent e
) {
217 StyledText textWidget
= getSourceViewer().getTextWidget();
218 int line
= textWidget
.getLineIndex(e
.y
);
219 toggleMergeCandidateAnnotation(line
);
225 * <p>toggleMergeCandidateAnnotation</p>
229 public void toggleMergeCandidateAnnotation(int line
) {
231 IDocument document
= getSourceViewer().getDocument();
232 LineAnnotationModel model
=
233 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
236 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
238 if (annotation
!= null) {
239 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
240 model
.changeAnnotationType(
241 annotation
, LineAnnotation
.TYPE_GENERIC
);
243 model
.changeAnnotationType(
244 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
252 public void changed(Object object
) {
253 // this.dirty = dirty;
254 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
256 // firePropertyChange(PROP_DIRTY);
260 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
263 public void performSearch(BulkEditorQuery query
) {
266 // TODO check if dirty, prompt save
268 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
269 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
277 getEditorInput().performSearch(query
);
283 getSourceViewer().getTextWidget().setFocus();
287 private void selectFirstItem() {
288 ITextSelection selection
= new TextSelection(0, 0);
289 getSelectionProvider().setSelection(selection
);
295 public void refresh() {
296 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
297 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
300 setInput(getEditorInput());
305 public AbstractBulkEditorInput
getEditorInput() {
306 return (AbstractBulkEditorInput
) super.getEditorInput();
310 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
311 super.editorContextMenuAboutToShow(menu
);
312 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
313 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
314 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);