1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.bulkeditor;
|
12
|
|
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;
|
39
|
|
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.model.IDerivedUnitFacadePart;
|
46
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
47
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
48
|
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
|
49
|
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
|
50
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
51
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
52
|
|
53
|
/**
|
54
|
* @author p.ciardelli
|
55
|
* @created 07.07.2009
|
56
|
* @version 1.0
|
57
|
*/
|
58
|
public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDetails,
|
59
|
IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData, IPartContentHasMedia {
|
60
|
|
61
|
/** Constant <code>ID="bulkeditor.editor"</code> */
|
62
|
public static final String ID = "bulkeditor.editor";
|
63
|
|
64
|
private boolean isInitialFocus = true;
|
65
|
|
66
|
private BulkEditorSearch searchBar = null;
|
67
|
|
68
|
private IPropertyChangeListener markerPreferenceListener;
|
69
|
|
70
|
private boolean isDirty;
|
71
|
|
72
|
public BulkEditor() {
|
73
|
super(CdmStore.createConversation());
|
74
|
}
|
75
|
|
76
|
/** {@inheritDoc} */
|
77
|
@Override
|
78
|
protected void initializeEditor() {
|
79
|
super.initializeEditor();
|
80
|
|
81
|
/**
|
82
|
* see AbstractTextEditor javadoc for explanation of context menu ids
|
83
|
*/
|
84
|
setEditorContextMenuId("#BulkEditorContext");
|
85
|
|
86
|
// setEntityCreatorService(new BulkEditorEntityCreatorService());
|
87
|
|
88
|
setLineDisplayStrategy(new BulkEditorLineDisplay(this));
|
89
|
|
90
|
setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy));
|
91
|
}
|
92
|
|
93
|
/** {@inheritDoc} */
|
94
|
@Override
|
95
|
protected ISourceViewer createSourceViewer(Composite parent,
|
96
|
IVerticalRuler ruler, int styles) {
|
97
|
ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles);
|
98
|
if (getEditorInput().isMergingEnabled()) {
|
99
|
addToggleMergeCandidateListener(ruler.getControl());
|
100
|
}
|
101
|
return viewer;
|
102
|
}
|
103
|
|
104
|
/** {@inheritDoc} */
|
105
|
@Override
|
106
|
public void init(IEditorSite site, IEditorInput input)
|
107
|
throws PartInitException {
|
108
|
|
109
|
if (!(input instanceof AbstractBulkEditorInput)) {
|
110
|
throw new PartInitException("Invalid Input: Must be BulkEditorInput");
|
111
|
}
|
112
|
|
113
|
super.init(site, input);
|
114
|
}
|
115
|
|
116
|
/** {@inheritDoc} */
|
117
|
@Override
|
118
|
public void createPartControl(Composite parent) {
|
119
|
|
120
|
parent.setLayout(new GridLayout());
|
121
|
|
122
|
Composite layoutComposite = new Composite(parent, SWT.NONE);
|
123
|
layoutComposite.setLayout(new GridLayout());
|
124
|
|
125
|
GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
|
126
|
layoutComposite.setLayoutData(gridData);
|
127
|
|
128
|
// layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
|
129
|
|
130
|
|
131
|
searchBar = new BulkEditorSearch(this, layoutComposite, SWT.NONE);
|
132
|
// layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
|
133
|
|
134
|
super.createPartControl(parent);
|
135
|
|
136
|
// Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
|
137
|
for (Control control : parent.getChildren()) {
|
138
|
if (control instanceof Composite &&
|
139
|
!(control.equals(layoutComposite))) {
|
140
|
control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
/** {@inheritDoc} */
|
146
|
@Override
|
147
|
public void dispose() {
|
148
|
if (markerPreferenceListener != null ) {
|
149
|
PreferencesUtil.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener);
|
150
|
}
|
151
|
super.dispose();
|
152
|
}
|
153
|
|
154
|
|
155
|
/** {@inheritDoc} */
|
156
|
@Override
|
157
|
public boolean isEditable() {
|
158
|
return false;
|
159
|
}
|
160
|
|
161
|
/** {@inheritDoc} */
|
162
|
@Override
|
163
|
public void setFocus() {
|
164
|
conversation.bind();
|
165
|
searchBar.setFocus();
|
166
|
|
167
|
// TODO find a better place to put this - this dialog should be shown after initial contents of
|
168
|
// Editor are displayed
|
169
|
if (isInitialFocus) {
|
170
|
displayWarningDialog();
|
171
|
isInitialFocus = false;
|
172
|
}
|
173
|
super.setFocus();
|
174
|
}
|
175
|
|
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());
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
|
191
|
private void addToggleMergeCandidateListener(Control control) {
|
192
|
control.addMouseListener(new MouseAdapter() {
|
193
|
@Override
|
194
|
public void mouseDoubleClick(MouseEvent e) {
|
195
|
StyledText textWidget = getSourceViewer().getTextWidget();
|
196
|
int line = textWidget.getLineIndex(e.y);
|
197
|
toggleMergeCandidateAnnotation(line);
|
198
|
}
|
199
|
});
|
200
|
}
|
201
|
|
202
|
public void toggleMergeCandidateAnnotation(int line) {
|
203
|
|
204
|
IDocument document = getSourceViewer().getDocument();
|
205
|
LineAnnotationModel model =
|
206
|
(LineAnnotationModel) getSourceViewer().getAnnotationModel();
|
207
|
|
208
|
if(model != null){
|
209
|
Annotation annotation = model.getAnnotationAtLine(line, document);
|
210
|
|
211
|
if (annotation != null) {
|
212
|
if (annotation.getType().equals(IBulkEditorConstants.TYPE_MERGE_CANDIDATE)) {
|
213
|
model.changeAnnotationType(
|
214
|
annotation, LineAnnotation.TYPE_GENERIC);
|
215
|
} else {
|
216
|
model.changeAnnotationType(
|
217
|
annotation, IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public boolean isDirty() {
|
225
|
if(isDirty){
|
226
|
return isDirty;
|
227
|
}
|
228
|
else{
|
229
|
return super.isDirty();
|
230
|
}
|
231
|
}
|
232
|
|
233
|
@Override
|
234
|
public void forceDirty(){
|
235
|
isDirty = true;
|
236
|
firePropertyChange(PROP_DIRTY);
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public void doSave(IProgressMonitor progressMonitor) {
|
241
|
isDirty = false;
|
242
|
|
243
|
super.doSave(progressMonitor);
|
244
|
|
245
|
selectFirstItem();
|
246
|
|
247
|
getSourceViewer().getTextWidget().setFocus();
|
248
|
|
249
|
}
|
250
|
|
251
|
/** {@inheritDoc} */
|
252
|
@Override
|
253
|
public void changed(Object object) {
|
254
|
// this.dirty = dirty;
|
255
|
AnnotatedLineDocumentProvider p = (AnnotatedLineDocumentProvider) getDocumentProvider();
|
256
|
p.changed(object);
|
257
|
// firePropertyChange(PROP_DIRTY);
|
258
|
}
|
259
|
|
260
|
/** {@inheritDoc} */
|
261
|
public void performSearch(BulkEditorQuery query) {
|
262
|
if (query != null) {
|
263
|
|
264
|
// TODO check if dirty, prompt save
|
265
|
if (isDirty()) {
|
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?");
|
268
|
if (proceed) {
|
269
|
doSave(null);
|
270
|
} else {
|
271
|
return;
|
272
|
}
|
273
|
}
|
274
|
conversation.commit();
|
275
|
conversation.clear();
|
276
|
|
277
|
getEditorInput().performSearch(query);
|
278
|
|
279
|
refresh();
|
280
|
|
281
|
selectFirstItem();
|
282
|
|
283
|
getSourceViewer().getTextWidget().setFocus();
|
284
|
}
|
285
|
}
|
286
|
|
287
|
private void selectFirstItem() {
|
288
|
ITextSelection selection = new TextSelection(0, 0);
|
289
|
getSelectionProvider().setSelection(selection);
|
290
|
}
|
291
|
|
292
|
public void refresh() {
|
293
|
if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
|
294
|
((AnnotationModel) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
|
295
|
}
|
296
|
|
297
|
setInput(getEditorInput());
|
298
|
}
|
299
|
|
300
|
/** {@inheritDoc} */
|
301
|
@Override
|
302
|
public AbstractBulkEditorInput getEditorInput() {
|
303
|
return (AbstractBulkEditorInput) super.getEditorInput();
|
304
|
}
|
305
|
|
306
|
@Override
|
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);
|
312
|
}
|
313
|
}
|