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
.IDerivedUnitFacadePart
;
46 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
47 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
48 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
49 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
50 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
51 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
62 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
,
63 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
, IPartContentHasMedia
{
65 /** Constant <code>ID="bulkeditor.editor"</code> */
66 public static final String ID
= "bulkeditor.editor";
68 private boolean isInitialFocus
= true;
70 private BulkEditorSearch searchBar
= null;
72 private IPropertyChangeListener markerPreferenceListener
;
74 private boolean isDirty
;
77 * <p>Constructor for BulkEditor.</p>
80 super(CdmStore
.createConversation());
84 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
88 protected void initializeEditor() {
89 super.initializeEditor();
92 * see AbstractTextEditor javadoc for explanation of context menu ids
94 setEditorContextMenuId("#BulkEditorContext");
96 // setEntityCreatorService(new BulkEditorEntityCreatorService());
98 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
100 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
104 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
108 protected ISourceViewer
createSourceViewer(Composite parent
,
109 IVerticalRuler ruler
, int styles
) {
110 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
111 if (getEditorInput().isMergingEnabled()) {
112 addToggleMergeCandidateListener(ruler
.getControl());
118 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
122 public void init(IEditorSite site
, IEditorInput input
)
123 throws PartInitException
{
125 if (!(input
instanceof AbstractBulkEditorInput
)) {
126 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
129 super.init(site
, input
);
133 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
137 public void createPartControl(Composite parent
) {
139 parent
.setLayout(new GridLayout());
141 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
142 layoutComposite
.setLayout(new GridLayout());
144 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
145 layoutComposite
.setLayoutData(gridData
);
147 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
150 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
151 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
153 super.createPartControl(parent
);
155 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
156 for (Control control
: parent
.getChildren()) {
157 if (control
instanceof Composite
&&
158 !(control
.equals(layoutComposite
))) {
159 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
165 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
169 public void dispose() {
170 if (markerPreferenceListener
!= null ) {
171 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
178 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
182 public boolean isEditable() {
187 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
191 public void setFocus() {
193 searchBar
.setFocus();
195 // TODO find a better place to put this - this dialog should be shown after initial contents of
196 // Editor are displayed
197 if (isInitialFocus
) {
198 displayWarningDialog();
199 isInitialFocus
= false;
207 private void displayWarningDialog() {
208 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
209 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
210 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
211 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
212 "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.";
213 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
214 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
215 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
216 if (dialog
.getReturnCode() == Window
.OK
) {
217 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
225 private void addToggleMergeCandidateListener(Control control
) {
226 control
.addMouseListener(new MouseAdapter() {
228 public void mouseDoubleClick(MouseEvent e
) {
229 StyledText textWidget
= getSourceViewer().getTextWidget();
230 int line
= textWidget
.getLineIndex(e
.y
);
231 toggleMergeCandidateAnnotation(line
);
237 * <p>toggleMergeCandidateAnnotation</p>
241 public void toggleMergeCandidateAnnotation(int line
) {
243 IDocument document
= getSourceViewer().getDocument();
244 LineAnnotationModel model
=
245 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
248 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
250 if (annotation
!= null) {
251 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
252 model
.changeAnnotationType(
253 annotation
, LineAnnotation
.TYPE_GENERIC
);
255 model
.changeAnnotationType(
256 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
263 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#isDirty()
266 public boolean isDirty() {
271 return super.isDirty();
276 public void forceDirty(){
278 firePropertyChange(PROP_DIRTY
);
282 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
285 public void doSave(IProgressMonitor progressMonitor
) {
288 super.doSave(progressMonitor
);
292 getSourceViewer().getTextWidget().setFocus();
298 public void changed(Object object
) {
299 // this.dirty = dirty;
300 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
302 // firePropertyChange(PROP_DIRTY);
306 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
309 public void performSearch(BulkEditorQuery query
) {
312 // TODO check if dirty, prompt save
314 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
315 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
322 conversation
.commit();
323 conversation
.clear();
325 getEditorInput().performSearch(query
);
331 getSourceViewer().getTextWidget().setFocus();
335 private void selectFirstItem() {
336 ITextSelection selection
= new TextSelection(0, 0);
337 getSelectionProvider().setSelection(selection
);
343 public void refresh() {
344 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
345 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
348 setInput(getEditorInput());
353 public AbstractBulkEditorInput
getEditorInput() {
354 return (AbstractBulkEditorInput
) super.getEditorInput();
358 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
359 super.editorContextMenuAboutToShow(menu
);
360 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
361 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
362 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);