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 java
.beans
.PropertyChangeEvent
;
14 import java
.beans
.PropertyChangeListener
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.jface
.action
.IMenuManager
;
18 import org
.eclipse
.jface
.action
.IStatusLineManager
;
19 import org
.eclipse
.jface
.action
.IToolBarManager
;
20 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
22 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
23 import org
.eclipse
.jface
.text
.IDocument
;
24 import org
.eclipse
.jface
.text
.source
.Annotation
;
25 import org
.eclipse
.jface
.text
.source
.AnnotationModel
;
26 import org
.eclipse
.jface
.text
.source
.ISourceViewer
;
27 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
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
.swt
.widgets
.Tree
;
38 import org
.eclipse
.ui
.IEditorInput
;
39 import org
.eclipse
.ui
.IEditorSite
;
40 import org
.eclipse
.ui
.PartInitException
;
41 import org
.eclipse
.ui
.views
.properties
.IPropertySheetEntry
;
42 import org
.eclipse
.ui
.views
.properties
.IPropertySheetPage
;
43 import org
.eclipse
.ui
.views
.properties
.PropertySheetPage
;
44 import org
.eclipse
.ui
.views
.properties
.PropertySheetSorter
;
46 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.AnnotatedLineEditor
;
47 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
48 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
49 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.BulkEditorInput
;
50 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
51 import eu
.etaxonomy
.taxeditor
.propertysheet
.EditorPropertySheetEntry
;
52 import eu
.etaxonomy
.taxeditor
.propertysheet
.PropertySheetUtil
;
59 public class BulkEditor
extends AnnotatedLineEditor
implements PropertyChangeListener
{
60 private static final Logger logger
= Logger
.getLogger(BulkEditor
.class);
62 public static final String ID
= "bulkeditor.editor";
64 private boolean isInitialFocus
= true;
73 * see AbstractTextEditor javadoc for explanation of context menu ids
75 setEditorContextMenuId("#BulkEditorContext");
77 setEntityCreatorService(new BulkEditorEntityCreatorService());
79 setPersistenceService(new BulkEditorPersistenceService());
81 setLineDisplayStrategy(new BulkEditorLineDisplay(this));
85 * @see eu.etaxonomy.taxeditor.bulkeditor.ListEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
87 protected ISourceViewer
createSourceViewer(Composite parent
,
88 IVerticalRuler ruler
, int styles
) {
89 ISourceViewer viewer
= super.createSourceViewer(parent
, ruler
, styles
);
90 addToggleMergeCandidateListener(ruler
.getControl());
95 * @see eu.etaxonomy.taxeditor.bulkeditor.AnnotatedLineEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
98 public void init(IEditorSite site
, IEditorInput input
)
99 throws PartInitException
{
101 if (!(input
instanceof BulkEditorInput
)) {
102 throw new PartInitException("Invalid Input: Must be BulkEditorInput");
105 super.init(site
, input
);
107 ((BulkEditorInput
) input
).addPropertyChangeListener(this);
111 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
114 public void createPartControl(Composite parent
) {
116 GridLayout gridLayout
= new GridLayout();
117 parent
.setLayout(gridLayout
);
119 BulkEditorSearchComposite searchBar
= new BulkEditorSearchComposite(this, parent
, SWT
.NONE
);
120 searchBar
.getParent().setBackground(parent
.getDisplay().getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
));
122 super.createPartControl(parent
);
124 // Set viewer composite to fill grid
125 for (Control control
: parent
.getChildren()) {
126 if (control
instanceof Composite
&& !(control
instanceof BulkEditorSearchComposite
)) {
127 control
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
133 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isEditable()
136 public boolean isEditable() {
141 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor#setFocus()
144 public void setFocus() {
147 // TODO find a better place to put this - this dialog should be shown after initial contents of
148 // Editor are displayed
149 if (isInitialFocus
) {
150 displayWarningDialog();
151 isInitialFocus
= false;
158 private void displayWarningDialog() {
159 IPreferenceStore prefs
= PreferencesUtil
.getPreferenceStore();
160 if (!prefs
.getBoolean(PreferencesUtil
.HIDE_BULKEDITOR_INFO
)) {
161 String msg
= "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
162 "Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
163 "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.";
164 MessageDialogWithToggle dialog
= MessageDialogWithToggle
.openOkCancelConfirm
165 (getSite().getShell(), "Bulk Editor", msg
, "Do not show this message again",
166 false, null, PreferencesUtil
.HIDE_BULKEDITOR_INFO
);
167 if (dialog
.getReturnCode() == Window
.OK
) {
168 prefs
.setValue(PreferencesUtil
.HIDE_BULKEDITOR_INFO
, dialog
.getToggleState());
176 private void addToggleMergeCandidateListener(Control control
) {
177 control
.addMouseListener(new MouseAdapter() {
179 public void mouseDoubleClick(MouseEvent e
) {
180 StyledText textWidget
= getSourceViewer().getTextWidget();
181 int line
= textWidget
.getLineIndex(e
.y
);
182 toggleMergeCandidateAnnotation(line
);
190 public void toggleMergeCandidateAnnotation(int line
) {
191 // TODO merge this with duplicate code in SetMergeCandidateHandler
192 IDocument document
= getSourceViewer().getDocument();
193 LineAnnotationModel model
=
194 (LineAnnotationModel
) getSourceViewer().getAnnotationModel();
196 Annotation annotation
= model
.getAnnotationAtLine(line
, document
);
198 if (annotation
!= null) {
199 if (annotation
.getType().equals(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
)) {
200 model
.changeAnnotationType(
201 annotation
, LineAnnotation
.TYPE_GENERIC
);
203 model
.changeAnnotationType(
204 annotation
, IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
210 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
212 public void propertyChange(PropertyChangeEvent evt
) {
213 if (evt
.getPropertyName().equals(BulkEditorInput
.QUERY_CHANGED
)) {
215 // TODO check if dirty, prompt save
217 boolean proceed
= MessageDialog
.openQuestion(getEditorSite().getShell(),
218 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
225 ((AnnotationModel
) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
226 setInput(getEditorInput());
230 @SuppressWarnings("unchecked")
231 public Object
getAdapter(Class type
) {
232 if (type
== IPropertySheetPage
.class) {
233 PropertySheetPage page
= new EditorPropertySheetPage();
236 return super.getAdapter(type
);
239 class EditorPropertySheetPage
extends PropertySheetPage
{
241 EditorPropertySheetPage() {
244 // Override sorter to simply display names as first-in-first-out
245 setSorter(new PropertySheetSorter() {
246 public int compare(IPropertySheetEntry entryA
, IPropertySheetEntry entryB
) {
249 public int compareCategories(String categoryA
, String categoryB
) {
252 public void sort(IPropertySheetEntry
[] entries
) {
258 public void makeContributions(IMenuManager menuManager
,
259 IToolBarManager toolBarManager
, IStatusLineManager statusLineManager
) {
260 super.makeContributions(menuManager
, toolBarManager
, statusLineManager
);
262 // Remove "Show categories", "Show advanced properties", "Restore default value"
263 toolBarManager
.removeAll();
264 menuManager
.removeAll();