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
.dialogs
.MessageDialog
;
14 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
15 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
16 import org
.eclipse
.jface
.text
.IDocument
;
17 import org
.eclipse
.jface
.text
.source
.Annotation
;
18 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
19 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
20 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
21 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
22 import org
.eclipse
.jface
.window
.Window
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.custom
.StyledText
;
25 import org
.eclipse
.swt
.events
.MouseAdapter
;
26 import org
.eclipse
.swt
.events
.MouseEvent
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.ui
.IEditorInput
;
32 import org
.eclipse
.ui
.IEditorSite
;
33 import org
.eclipse
.ui
.PartInitException
;
35 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineDocumentProvider
;
36 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineEditor
;
37 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
38 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
39 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
40 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
41 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
43 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 * <p>BulkEditor class.</p>
52 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
, IDirtyMarkableSelectionProvider
{
54 /** Constant <code>ID="bulkeditor.editor"</code> */
55 public static final String ID
= "bulkeditor.editor";
57 private boolean isInitialFocus
= true;
59 private BulkEditorSearch searchBar
= null;
61 private IPropertyChangeListener markerPreferenceListener
;
64 * <p>Constructor for BulkEditor.</p>
67 super(CdmStore
.createConversation());
71 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
75 protected void initializeEditor() {
76 super.initializeEditor();
79 * see AbstractTextEditor javadoc for explanation of context menu ids
81 setEditorContextMenuId("#BulkEditorContext");
83 // setEntityCreatorService(new BulkEditorEntityCreatorService());
85 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
87 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
91 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
94 protected ISourceViewer
createSourceViewer(Composite parent
,
95 IVerticalRuler ruler
, int styles
) {
96 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
97 if (((AbstractBulkEditorInput
) getEditorInput()).isMergingEnabled()) {
98 addToggleMergeCandidateListener(ruler
.getControl());
104 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
108 public void init(IEditorSite site
, IEditorInput input
)
109 throws PartInitException
{
111 if (!(input
instanceof AbstractBulkEditorInput
)) {
112 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
115 super.init(site
, input
);
119 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
123 public void createPartControl(Composite parent
) {
125 parent
.setLayout(new GridLayout());
127 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
128 layoutComposite
.setLayout(new GridLayout());
130 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
131 layoutComposite
.setLayoutData(gridData
);
133 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
136 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
137 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
139 super.createPartControl(parent
);
141 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
142 for (Control control
: parent
.getChildren()) {
143 if (control
instanceof Composite
&&
144 !(control
.equals(layoutComposite
))) {
145 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
151 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
155 public void dispose() {
156 if (markerPreferenceListener
!= null ) {
157 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
163 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
167 public boolean isEditable() {
172 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
176 public void setFocus() {
177 searchBar
.setFocus();
179 // TODO find a better place to put this - this dialog should be shown after initial contents of
180 // Editor are displayed
181 if (isInitialFocus
) {
182 displayWarningDialog();
183 isInitialFocus
= false;
190 private void displayWarningDialog() {
191 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
192 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
193 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
194 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
195 "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.";
196 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
197 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
198 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
199 if (dialog
.getReturnCode() == Window
.OK
) {
200 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
208 private void addToggleMergeCandidateListener(Control control
) {
209 control
.addMouseListener(new MouseAdapter() {
211 public void mouseDoubleClick(MouseEvent e
) {
212 StyledText textWidget
= getSourceViewer().getTextWidget();
213 int line
= textWidget
.getLineIndex(e
.y
);
214 toggleMergeCandidateAnnotation(line
);
220 * <p>toggleMergeCandidateAnnotation</p>
224 public void toggleMergeCandidateAnnotation(int line
) {
226 IDocument document
= getSourceViewer().getDocument();
227 LineAnnotationModel model
=
228 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
231 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
233 if (annotation
!= null) {
234 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
235 model
.changeAnnotationType(
236 annotation
, LineAnnotation
.TYPE_GENERIC
);
238 model
.changeAnnotationType(
239 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
246 public void changed(Object object
) {
247 // this.dirty = dirty;
248 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
250 // firePropertyChange(PROP_DIRTY);
254 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
257 public void performSearch(BulkEditorQuery query
) {
260 // TODO check if dirty, prompt save
262 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
263 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
271 getEditorInput().performSearch(query
);
280 public void refresh() {
281 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
282 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
285 setInput(getEditorInput());
290 public AbstractBulkEditorInput
getEditorInput() {
291 return (AbstractBulkEditorInput
) super.getEditorInput();