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
.PreferencesUtil
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 public class BulkEditor
extends AnnotatedLineEditor
implements IPartContentHasDetails
,
60 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
, IPartContentHasMedia
{
62 /** Constant <code>ID="bulkeditor.editor"</code> */
63 public static final String ID
= "bulkeditor.editor";
65 private boolean isInitialFocus
= true;
67 private BulkEditorSearch searchBar
= null;
69 private IPropertyChangeListener markerPreferenceListener
;
71 private boolean isDirty
;
74 super(CdmStore
.createConversation());
79 protected void initializeEditor() {
80 super.initializeEditor();
83 * see AbstractTextEditor javadoc for explanation of context menu ids
85 setEditorContextMenuId("#BulkEditorContext");
87 // setEntityCreatorService(new BulkEditorEntityCreatorService());
89 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
91 setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy
));
96 protected ISourceViewer
createSourceViewer(Composite parent
,
97 IVerticalRuler ruler
, int styles
) {
98 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
99 if (getEditorInput().isMergingEnabled()) {
100 addToggleMergeCandidateListener(ruler
.getControl());
107 public void init(IEditorSite site
, IEditorInput input
)
108 throws PartInitException
{
110 if (!(input
instanceof AbstractBulkEditorInput
)) {
111 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
114 super.init(site
, input
);
119 public void createPartControl(Composite parent
) {
121 parent
.setLayout(new GridLayout());
123 Composite layoutComposite
= new Composite(parent
, SWT
.NONE
);
124 layoutComposite
.setLayout(new GridLayout());
126 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
127 layoutComposite
.setLayoutData(gridData
);
129 // layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
132 searchBar
= new BulkEditorSearch(this, layoutComposite
, SWT
.NONE
);
133 // layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
135 super.createPartControl(parent
);
137 // Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
138 for (Control control
: parent
.getChildren()) {
139 if (control
instanceof Composite
&&
140 !(control
.equals(layoutComposite
))) {
141 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
148 public void dispose() {
149 if (markerPreferenceListener
!= null ) {
150 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener
);
158 public boolean isEditable() {
164 public void setFocus() {
166 searchBar
.setFocus();
168 // TODO find a better place to put this - this dialog should be shown after initial contents of
169 // Editor are displayed
170 if (isInitialFocus
) {
171 displayWarningDialog();
172 isInitialFocus
= false;
176 private void displayWarningDialog() {
177 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
178 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
179 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
180 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
181 "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.";
182 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
183 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
184 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
185 if (dialog
.getReturnCode() == Window
.OK
) {
186 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
191 private void addToggleMergeCandidateListener(Control control
) {
192 control
.addMouseListener(new MouseAdapter() {
194 public void mouseDoubleClick(MouseEvent e
) {
195 StyledText textWidget
= getSourceViewer().getTextWidget();
196 int line
= textWidget
.getLineIndex(e
.y
);
197 toggleMergeCandidateAnnotation(line
);
202 public void toggleMergeCandidateAnnotation(int line
) {
204 IDocument document
= getSourceViewer().getDocument();
205 LineAnnotationModel model
=
206 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
209 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
211 if (annotation
!= null) {
212 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
213 model
.changeAnnotationType(
214 annotation
, LineAnnotation
.TYPE_GENERIC
);
216 model
.changeAnnotationType(
217 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
224 public boolean isDirty() {
229 return super.isDirty();
234 public void forceDirty(){
236 firePropertyChange(PROP_DIRTY
);
240 public void doSave(IProgressMonitor progressMonitor
) {
243 super.doSave(progressMonitor
);
247 getSourceViewer().getTextWidget().setFocus();
253 public void changed(Object object
) {
254 // this.dirty = dirty;
255 AnnotatedLineDocumentProvider p
= (AnnotatedLineDocumentProvider
) getDocumentProvider();
257 // firePropertyChange(PROP_DIRTY);
261 public void performSearch(BulkEditorQuery query
) {
264 // TODO check if dirty, prompt save
266 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
267 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
274 conversation
.commit();
275 conversation
.clear();
277 getEditorInput().performSearch(query
);
283 getSourceViewer().getTextWidget().setFocus();
287 private void selectFirstItem() {
288 ITextSelection selection
= new TextSelection(0, 0);
289 getSelectionProvider().setSelection(selection
);
292 public void refresh() {
293 if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
294 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
297 setInput(getEditorInput());
302 public AbstractBulkEditorInput
getEditorInput() {
303 return (AbstractBulkEditorInput
) super.getEditorInput();
307 protected void editorContextMenuAboutToShow(IMenuManager menu
) {
308 super.editorContextMenuAboutToShow(menu
);
309 menu
.remove(ITextEditorActionConstants
.SHIFT_RIGHT
);
310 menu
.remove(ITextEditorActionConstants
.SHIFT_LEFT
);
311 menu
.remove(ITextEditorActionConstants
.CONTEXT_PREFERENCES
);
315 public boolean canAttachMedia() {
316 return getEditorInput() instanceof TaxonEditorInput?
true:false;