2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
;
12 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
13 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
14 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
15 import org
.eclipse
.jface
.action
.IMenuManager
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
18 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
19 import org
.eclipse
.jface
.text
.IDocument
;
20 import org
.eclipse
.jface
.text
.ITextSelection
;
21 import org
.eclipse
.jface
.text
.TextSelection
;
22 import org
.eclipse
.jface
.text
.source
.Annotation
;
23 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
24 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
25 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
26 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
27 import org
.eclipse
.jface
.window
.Window
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.custom
.StyledText
;
30 import org
.eclipse
.swt
.events
.MouseAdapter
;
31 import org
.eclipse
.swt
.events
.MouseEvent
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Control
;
36 import org
.eclipse
.ui
.IEditorInput
;
37 import org
.eclipse
.ui
.IEditorSite
;
38 import org
.eclipse
.ui
.PartInitException
;
39 import org
.eclipse
.ui
.texteditor
.ITextEditorActionConstants
;
41 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineDocumentProvider
;
42 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineEditor
;
43 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
44 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
45 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
46 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.OccurrenceEditorInput
;
47 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.TaxonEditorInput
;
48 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
49 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
50 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
51 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
52 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
53 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
54 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
55 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
;
76 private IUndoContext undoContext
;
79 super(CdmStore
.createConversation());
80 undoContext
= new UndoContext();
85 protected void initializeEditor() {
86 super.initializeEditor();
89 * see AbstractTextEditor javadoc for explanation of context menu ids
91 setEditorContextMenuId("#BulkEditorContext");
93 // setEntityCreatorService(new BulkEditorEntityCreatorService());
95 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
97 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
102 protected ISourceViewer
createSourceViewer(Composite parent
,
103 IVerticalRuler ruler
, int styles
) {
104 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
105 if (getEditorInput().isMergingEnabled()) {
106 addToggleMergeCandidateListener(ruler
.getControl());
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 AbstractBulkEditorInput
<?
> bulkEditorInput
= (AbstractBulkEditorInput
<?
>)input
;
121 if(bulkEditorInput
.getEntityUuid()!=null){
122 bulkEditorInput
.performSearch(new BulkEditorQuery(bulkEditorInput
.getEntityUuid().toString(), null));
125 super.init(site
, input
);
130 public void createPartControl(Composite parent
) {
132 parent
.setLayout(new GridLayout());
134 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
135 layoutComposite
.setLayout(new GridLayout());
137 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
138 layoutComposite
.setLayoutData(gridData
);
140 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
143 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
144 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
146 super.createPartControl(parent
);
148 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
149 for (Control control
: parent
.getChildren()) {
150 if (control
instanceof Composite
&&
151 !(control
.equals(layoutComposite
))) {
152 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
159 public void dispose() {
160 if (markerPreferenceListener
!= null ) {
161 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
169 public boolean isEditable() {
175 public void setFocus() {
178 // TODO find a better place to put this - this dialog should be shown after initial contents of
179 // Editor are displayed
180 if (isInitialFocus
) {
181 displayWarningDialog();
182 isInitialFocus
= false;
185 searchBar
.setFocus();
189 * @return the searchBar
191 public BulkEditorSearch
getSearchBar() {
195 private void displayWarningDialog() {
196 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
197 if (!prefs
.getBoolean(IPreferenceKeys
.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, IPreferenceKeys
.HIDE_BULKEDITOR_INFO
);
204 if (dialog
.getReturnCode() == Window
.OK
) {
205 prefs
.setValue(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
210 private void addToggleMergeCandidateListener(Control control
) {
211 control
.addMouseListener(new MouseAdapter() {
213 public void mouseDoubleClick(MouseEvent e
) {
214 StyledText textWidget
= getSourceViewer().getTextWidget();
215 int line
= textWidget
.getLineIndex(e
.y
);
216 toggleMergeCandidateAnnotation(line
);
221 public void toggleMergeCandidateAnnotation(int line
) {
223 IDocument document
= getSourceViewer().getDocument();
224 LineAnnotationModel model
=
225 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
228 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
230 if (annotation
!= null) {
231 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
232 model
.changeAnnotationType(
233 annotation
, LineAnnotation
.TYPE_GENERIC
);
235 model
.changeAnnotationType(
236 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
243 public boolean isDirty() {
248 return super.isDirty();
253 public void forceDirty(){
255 firePropertyChange(PROP_DIRTY
);
260 public void doSave(IProgressMonitor progressMonitor
) {
263 super.doSave(progressMonitor
);
267 getSourceViewer().getTextWidget().setFocus();
273 public void changed(Object object
) {
274 // this.dirty = dirty;
275 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
277 // firePropertyChange(PROP_DIRTY);
281 public void performSearch(BulkEditorQuery query
) {
284 // TODO check if dirty, prompt save
286 String
[] labels
= {"Save (and Search)", "Don't save (and Search)","Cancel"};
287 MessageDialog dialog
=new MessageDialog(getEditorSite().getShell(), "Save changes", null, "You have made changes that must be saved before this query can be executed. Would you like to proceed?", MessageDialog
.QUESTION
,labels
, 0);
288 int result
= dialog
.open();
289 //MessageDialog.openQuestion(getEditorSite().getShell(),
290 //"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
293 } else if (result
== 2){
297 //conversation.clear();
298 //conversation.commit(true);
300 getEditorInput().dispose();
301 getEditorInput().bind();
302 getEditorInput().performSearch(query
);
308 getSourceViewer().getTextWidget().setFocus();
312 private void selectFirstItem() {
313 ITextSelection selection
= new TextSelection(0, 0);
314 getSelectionProvider().setSelection(selection
);
317 public void refresh() {
318 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
319 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
322 setInput(getEditorInput());
327 public AbstractBulkEditorInput
getEditorInput() {
328 return (AbstractBulkEditorInput
) super.getEditorInput();
332 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
333 super.editorContextMenuAboutToShow(menu
);
334 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
335 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
336 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);
340 public boolean canAttachMedia() {
341 return (getEditorInput() instanceof TaxonEditorInput
|| getEditorInput() instanceof OccurrenceEditorInput
) ?
true:false;
344 public IUndoContext
getUndoContext() {