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
.commands
.operations
.IUndoContext
;
14 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.jface
.action
.IMenuManager
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
18 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
19 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
20 import org
.eclipse
.jface
.text
.IDocument
;
21 import org
.eclipse
.jface
.text
.ITextSelection
;
22 import org
.eclipse
.jface
.text
.TextSelection
;
23 import org
.eclipse
.jface
.text
.source
.Annotation
;
24 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
25 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
26 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
27 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
28 import org
.eclipse
.jface
.window
.Window
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.custom
.StyledText
;
31 import org
.eclipse
.swt
.events
.MouseAdapter
;
32 import org
.eclipse
.swt
.events
.MouseEvent
;
33 import org
.eclipse
.swt
.layout
.GridData
;
34 import org
.eclipse
.swt
.layout
.GridLayout
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.ui
.IEditorInput
;
38 import org
.eclipse
.ui
.IEditorSite
;
39 import org
.eclipse
.ui
.PartInitException
;
40 import org
.eclipse
.ui
.texteditor
.ITextEditorActionConstants
;
42 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineDocumentProvider
;
43 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineEditor
;
44 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
45 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
46 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
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
;
78 private BulkEditorQuery lastQuery
;
81 * @return the lastQuery
83 public BulkEditorQuery
getLastQuery() {
88 super(CdmStore
.createConversation());
89 undoContext
= new UndoContext();
94 protected void initializeEditor() {
95 super.initializeEditor();
98 * see AbstractTextEditor javadoc for explanation of context menu ids
100 setEditorContextMenuId("#BulkEditorContext");
102 // setEntityCreatorService(new BulkEditorEntityCreatorService());
104 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
106 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
111 protected ISourceViewer
createSourceViewer(Composite parent
,
112 IVerticalRuler ruler
, int styles
) {
113 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
114 if (getEditorInput().isMergingEnabled()) {
115 addToggleMergeCandidateListener(ruler
.getControl());
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 AbstractBulkEditorInput
<?
> bulkEditorInput
= (AbstractBulkEditorInput
<?
>)input
;
130 if(bulkEditorInput
.getEntityUuid()!=null){
131 bulkEditorInput
.performSearch(new BulkEditorQuery(bulkEditorInput
.getEntityUuid().toString(), null));
134 super.init(site
, input
);
139 public void createPartControl(Composite parent
) {
141 parent
.setLayout(new GridLayout());
143 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
144 layoutComposite
.setLayout(new GridLayout());
146 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
147 layoutComposite
.setLayoutData(gridData
);
149 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
152 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
153 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
155 super.createPartControl(parent
);
157 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
158 for (Control control
: parent
.getChildren()) {
159 if (control
instanceof Composite
&&
160 !(control
.equals(layoutComposite
))) {
161 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
168 public void dispose() {
169 if (markerPreferenceListener
!= null ) {
170 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
178 public boolean isEditable() {
184 public void setFocus() {
187 // TODO find a better place to put this - this dialog should be shown after initial contents of
188 // Editor are displayed
189 if (isInitialFocus
) {
190 displayWarningDialog();
191 isInitialFocus
= false;
194 searchBar
.setFocus();
198 * @return the searchBar
200 public BulkEditorSearch
getSearchBar() {
204 private void displayWarningDialog() {
205 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
206 if (!prefs
.getBoolean(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
)) {
207 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
208 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
209 "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.";
210 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
211 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
212 false, null, IPreferenceKeys
.HIDE_BULKEDITOR_INFO
);
213 if (dialog
.getReturnCode() == Window
.OK
) {
214 prefs
.setValue(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
219 private void addToggleMergeCandidateListener(Control control
) {
220 control
.addMouseListener(new MouseAdapter() {
222 public void mouseDoubleClick(MouseEvent e
) {
223 StyledText textWidget
= getSourceViewer().getTextWidget();
224 int line
= textWidget
.getLineIndex(e
.y
);
225 toggleMergeCandidateAnnotation(line
);
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
);
252 public boolean isDirty() {
257 return super.isDirty();
262 public void forceDirty(){
264 firePropertyChange(PROP_DIRTY
);
269 public void doSave(IProgressMonitor progressMonitor
) {
272 super.doSave(progressMonitor
);
276 getSourceViewer().getTextWidget().setFocus();
282 public void changed(Object object
) {
283 // this.dirty = dirty;
284 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
286 // firePropertyChange(PROP_DIRTY);
290 public void performSearch(BulkEditorQuery query
) {
293 // TODO check if dirty, prompt save
295 String
[] labels
= {"Save (and Search)", "Don't save (and Search)","Cancel"};
296 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);
297 int result
= dialog
.open();
298 //MessageDialog.openQuestion(getEditorSite().getShell(),
299 //"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
302 } else if (result
== 2){
306 //conversation.clear();
307 //conversation.commit(true);
309 getEditorInput().dispose();
310 getEditorInput().bind();
311 getEditorInput().performSearch(query
);
312 this.lastQuery
= query
;
313 this.lastQuery
.getSearchConfigurator().setCheckResult(false);
318 getSourceViewer().getTextWidget().setFocus();
322 private void selectFirstItem() {
323 ITextSelection selection
= new TextSelection(0, 0);
324 getSelectionProvider().setSelection(selection
);
327 public void refresh() {
328 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
329 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
332 setInput(getEditorInput());
337 public AbstractBulkEditorInput
getEditorInput() {
338 return (AbstractBulkEditorInput
) super.getEditorInput();
342 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
343 super.editorContextMenuAboutToShow(menu
);
344 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
345 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
346 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);
350 public boolean canAttachMedia() {
351 return getEditorInput() instanceof TaxonEditorInput?
true:false;
354 public IUndoContext
getUndoContext() {
358 public void setDirty(boolean isDirty
) {
359 this.isDirty
= isDirty
;