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
.bulkeditor
.input
.TaxonEditorInput
;
46 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
47 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
48 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
49 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
50 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
51 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
52 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
60 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
,
61 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
, IPartContentHasMedia
{
63 /** Constant <code>ID="bulkeditor.editor"</code> */
64 public static final String ID
= "bulkeditor.editor";
66 private boolean isInitialFocus
= true;
68 private BulkEditorSearch searchBar
= null;
70 private IPropertyChangeListener markerPreferenceListener
;
72 private boolean isDirty
;
75 super(CdmStore
.createConversation());
80 protected void initializeEditor() {
81 super.initializeEditor();
84 * see AbstractTextEditor javadoc for explanation of context menu ids
86 setEditorContextMenuId("#BulkEditorContext");
88 // setEntityCreatorService(new BulkEditorEntityCreatorService());
90 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
92 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
97 protected ISourceViewer
createSourceViewer(Composite parent
,
98 IVerticalRuler ruler
, int styles
) {
99 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
100 if (getEditorInput().isMergingEnabled()) {
101 addToggleMergeCandidateListener(ruler
.getControl());
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 AbstractBulkEditorInput
<?
> bulkEditorInput
= (AbstractBulkEditorInput
<?
>)input
;
116 if(bulkEditorInput
.getEntityUuid()!=null){
117 bulkEditorInput
.performSearch(new BulkEditorQuery(bulkEditorInput
.getEntityUuid().toString(), null));
120 super.init(site
, input
);
125 public void createPartControl(Composite parent
) {
127 parent
.setLayout(new GridLayout());
129 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
130 layoutComposite
.setLayout(new GridLayout());
132 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
133 layoutComposite
.setLayoutData(gridData
);
135 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
138 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
139 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
141 super.createPartControl(parent
);
143 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
144 for (Control control
: parent
.getChildren()) {
145 if (control
instanceof Composite
&&
146 !(control
.equals(layoutComposite
))) {
147 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
154 public void dispose() {
155 if (markerPreferenceListener
!= null ) {
156 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
164 public boolean isEditable() {
170 public void setFocus() {
173 // TODO find a better place to put this - this dialog should be shown after initial contents of
174 // Editor are displayed
175 if (isInitialFocus
) {
176 displayWarningDialog();
177 isInitialFocus
= false;
180 searchBar
.setFocus();
183 private void displayWarningDialog() {
184 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
185 if (!prefs
.getBoolean(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
)) {
186 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
187 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
188 "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.";
189 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
190 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
191 false, null, IPreferenceKeys
.HIDE_BULKEDITOR_INFO
);
192 if (dialog
.getReturnCode() == Window
.OK
) {
193 prefs
.setValue(IPreferenceKeys
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
198 private void addToggleMergeCandidateListener(Control control
) {
199 control
.addMouseListener(new MouseAdapter() {
201 public void mouseDoubleClick(MouseEvent e
) {
202 StyledText textWidget
= getSourceViewer().getTextWidget();
203 int line
= textWidget
.getLineIndex(e
.y
);
204 toggleMergeCandidateAnnotation(line
);
209 public void toggleMergeCandidateAnnotation(int line
) {
211 IDocument document
= getSourceViewer().getDocument();
212 LineAnnotationModel model
=
213 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
216 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
218 if (annotation
!= null) {
219 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
220 model
.changeAnnotationType(
221 annotation
, LineAnnotation
.TYPE_GENERIC
);
223 model
.changeAnnotationType(
224 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
231 public boolean isDirty() {
236 return super.isDirty();
241 public void forceDirty(){
243 firePropertyChange(PROP_DIRTY
);
247 public void doSave(IProgressMonitor progressMonitor
) {
250 super.doSave(progressMonitor
);
254 getSourceViewer().getTextWidget().setFocus();
260 public void changed(Object object
) {
261 // this.dirty = dirty;
262 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
264 // firePropertyChange(PROP_DIRTY);
268 public void performSearch(BulkEditorQuery query
) {
271 // TODO check if dirty, prompt save
273 String
[] labels
= {"Save (and Search)", "Don't save (and Search)","Cancel"};
274 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);
275 int result
= dialog
.open();
276 //MessageDialog.openQuestion(getEditorSite().getShell(),
277 //"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
280 } else if (result
== 2){
284 conversation
.clear();
285 conversation
.commit(true);
287 getEditorInput().dispose();
288 getEditorInput().bind();
289 getEditorInput().performSearch(query
);
295 getSourceViewer().getTextWidget().setFocus();
299 private void selectFirstItem() {
300 ITextSelection selection
= new TextSelection(0, 0);
301 getSelectionProvider().setSelection(selection
);
304 public void refresh() {
305 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
306 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
309 setInput(getEditorInput());
314 public AbstractBulkEditorInput
getEditorInput() {
315 return (AbstractBulkEditorInput
) super.getEditorInput();
319 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
320 super.editorContextMenuAboutToShow(menu
);
321 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
322 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
323 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);
327 public boolean canAttachMedia() {
328 return getEditorInput() instanceof TaxonEditorInput?
true:false;