Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.bulkeditor;
11

    
12
import org.eclipse.core.commands.operations.IUndoContext;
13
import org.eclipse.core.commands.operations.UndoContext;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.jface.action.GroupMarker;
16
import org.eclipse.jface.action.IMenuManager;
17
import org.eclipse.jface.action.MenuManager;
18
import org.eclipse.jface.dialogs.MessageDialog;
19
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
20
import org.eclipse.jface.preference.IPreferenceStore;
21
import org.eclipse.jface.text.IDocument;
22
import org.eclipse.jface.text.ITextSelection;
23
import org.eclipse.jface.text.TextSelection;
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.util.IPropertyChangeListener;
29
import org.eclipse.jface.window.Window;
30
import org.eclipse.swt.SWT;
31
import org.eclipse.swt.custom.StyledText;
32
import org.eclipse.swt.events.MouseAdapter;
33
import org.eclipse.swt.events.MouseEvent;
34
import org.eclipse.swt.layout.GridData;
35
import org.eclipse.swt.layout.GridLayout;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Control;
38
import org.eclipse.swt.widgets.Menu;
39
import org.eclipse.ui.IEditorInput;
40
import org.eclipse.ui.IEditorSite;
41
import org.eclipse.ui.IWorkbenchActionConstants;
42
import org.eclipse.ui.PartInitException;
43
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
44

    
45
import eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineDocumentProvider;
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.AbstractBulkEditorInput;
50
import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
51
import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
52
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
53
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
54
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
55
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
56
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
57
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
58
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
59
import eu.etaxonomy.taxeditor.store.CdmStore;
60

    
61
/**
62
 * @author p.ciardelli
63
 * @created 07.07.2009
64
 * @version 1.0
65
 */
66
public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDetails,
67
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData, IPartContentHasMedia {
68

    
69
	/** Constant <code>ID="bulkeditor.editor"</code> */
70
	public static final String ID = "bulkeditor.editor";
71

    
72
	private boolean isInitialFocus = true;
73

    
74
	private BulkEditorSearch searchBar = null;
75

    
76
	private IPropertyChangeListener markerPreferenceListener;
77

    
78
	private boolean isDirty;
79

    
80
    private IUndoContext undoContext;
81

    
82
	public BulkEditor() {
83
		super(CdmStore.createConversation());
84
        undoContext = new UndoContext();
85
	}
86

    
87
	/** {@inheritDoc} */
88
	@Override
89
	protected void initializeEditor() {
90
		super.initializeEditor();
91

    
92
//		setEntityCreatorService(new BulkEditorEntityCreatorService());
93

    
94
		setLineDisplayStrategy(new BulkEditorLineDisplay(this));
95

    
96
		setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy));
97

    
98
	}
99

    
100
	/** {@inheritDoc} */
101
	@Override
102
    protected ISourceViewer createSourceViewer(Composite parent,
103
			IVerticalRuler ruler, int styles) {
104
		ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles);
105
		if (getEditorInput().isMergingEnabled()) {
106
			addToggleMergeCandidateListener(ruler.getControl());
107
		}
108
		return viewer;
109
	}
110

    
111
	/** {@inheritDoc} */
112
	@Override
113
	public void init(IEditorSite site, IEditorInput input)
114
			throws PartInitException {
115

    
116
		if (!(input instanceof AbstractBulkEditorInput)) {
117
			throw new PartInitException("Invalid Input: Must be BulkEditorInput");
118
		}
119
		else{
120
		    AbstractBulkEditorInput<?> bulkEditorInput = (AbstractBulkEditorInput<?>)input;
121
		    if(bulkEditorInput.getEntityUuid()!=null){
122
		        bulkEditorInput.performSearch(new BulkEditorQuery(bulkEditorInput.getEntityUuid().toString(), null));
123
		    }
124
		}
125
		super.init(site, input);
126
	}
127

    
128
	/** {@inheritDoc} */
129
	@Override
130
	public void createPartControl(Composite parent) {
131

    
132
		parent.setLayout(new GridLayout());
133

    
134
		Composite layoutComposite = new Composite(parent, SWT.NONE);
135
		layoutComposite.setLayout(new GridLayout());
136

    
137
		GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
138
		layoutComposite.setLayoutData(gridData);
139

    
140
//		layoutComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
141

    
142

    
143
		searchBar = new BulkEditorSearch(this, layoutComposite, SWT.NONE);
144
//		layoutComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
145

    
146
		super.createPartControl(parent);
147

    
148
		// Set viewer composite to fill grid. Unfortunately it is private and we have to do a little hack here.
149
		for (Control control : parent.getChildren()) {
150
			if (control instanceof Composite &&
151
						!(control.equals(layoutComposite))) {
152
				control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
153
			}
154
		}
155

    
156
        //create context menu
157
        MenuManager menuManager = new MenuManager();
158
        menuManager.setRemoveAllWhenShown(true);
159
        menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
160
        getSite().registerContextMenu(menuManager, getSelectionProvider());
161
        Menu menu = menuManager.createContextMenu(this.getSourceViewer().getTextWidget());
162
        this.getSourceViewer().getTextWidget().setMenu(menu);
163

    
164
	}
165

    
166
	/** {@inheritDoc} */
167
	@Override
168
	public void dispose() {
169
		if (markerPreferenceListener != null ) {
170
			PreferencesUtil.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener);
171
		}
172
		super.dispose();
173
	}
174

    
175

    
176
	/** {@inheritDoc} */
177
	@Override
178
	public boolean isEditable() {
179
		return false;
180
	}
181

    
182
	/** {@inheritDoc} */
183
	@Override
184
	public void setFocus() {
185
	    conversation.bind();
186

    
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;
192
		}
193
		super.setFocus();
194
		searchBar.setFocus();
195
	}
196

    
197
	/**
198
	 * @return the searchBar
199
	 */
200
	public BulkEditorSearch getSearchBar() {
201
		return searchBar;
202
	}
203

    
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());
215
			}
216
		}
217
	}
218

    
219
	private void addToggleMergeCandidateListener(Control control) {
220
		control.addMouseListener(new MouseAdapter() {
221
			@Override
222
			public void mouseDoubleClick(MouseEvent e) {
223
				StyledText textWidget = getSourceViewer().getTextWidget();
224
				int line = textWidget.getLineIndex(e.y);
225
				toggleMergeCandidateAnnotation(line);
226
			}
227
		});
228
	}
229

    
230
	public void toggleMergeCandidateAnnotation(int line) {
231

    
232
		IDocument document = getSourceViewer().getDocument();
233
		LineAnnotationModel model =
234
				(LineAnnotationModel) getSourceViewer().getAnnotationModel();
235

    
236
		if(model != null){
237
			Annotation annotation = model.getAnnotationAtLine(line, document);
238

    
239
			if (annotation != null) {
240
				if (annotation.getType().equals(IBulkEditorConstants.TYPE_MERGE_CANDIDATE)) {
241
					model.changeAnnotationType(
242
							annotation, LineAnnotation.TYPE_GENERIC);
243
				} else {
244
					model.changeAnnotationType(
245
							annotation, IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
246
				}
247
			}
248
		}
249
	}
250

    
251
	@Override
252
	public boolean isDirty() {
253
	    if(isDirty){
254
	        return isDirty;
255
	    }
256
	    else{
257
	        return super.isDirty();
258
	    }
259
	}
260

    
261
	@Override
262
    public void forceDirty(){
263
	    isDirty = true;
264
	    firePropertyChange(PROP_DIRTY);
265
	}
266

    
267

    
268
   	@Override
269
	public void doSave(IProgressMonitor progressMonitor) {
270
	    isDirty = false;
271

    
272
	    super.doSave(progressMonitor);
273

    
274
	    this.searchBar.updateEditorInput();
275
	    selectFirstItem();
276

    
277
		getSourceViewer().getTextWidget().setFocus();
278

    
279
	}
280

    
281
	/** {@inheritDoc} */
282
	@Override
283
    public void changed(Object object) {
284
//		this.dirty = dirty;
285
		AnnotatedLineDocumentProvider p = (AnnotatedLineDocumentProvider) getDocumentProvider();
286
		p.changed(object);
287
//		firePropertyChange(PROP_DIRTY);
288
	}
289

    
290
	/** {@inheritDoc} */
291
	public void performSearch(BulkEditorQuery query) {
292
		if (query != null) {
293

    
294
			// TODO check if dirty, prompt save
295
			if (isDirty()) {
296
				String[] labels = {"Save (and Search)", "Don't save (and Search)","Cancel"};
297
				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);
298
				int result = dialog.open();
299
						//MessageDialog.openQuestion(getEditorSite().getShell(),
300
						//"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
301
				if (result == 0) {
302
					doSave(null);
303
				} else if (result == 2){
304
					return;
305
				}
306
			}
307
			//conversation.clear();
308
			//conversation.commit(true);
309

    
310
			getEditorInput().dispose();
311
			getEditorInput().bind();
312
			getEditorInput().performSearch(query);
313

    
314
			refresh();
315

    
316
			selectFirstItem();
317

    
318
			getSourceViewer().getTextWidget().setFocus();
319
		}
320
	}
321

    
322
	private void selectFirstItem() {
323
		ITextSelection selection = new TextSelection(0, 0);
324
		getSelectionProvider().setSelection(selection);
325
	}
326

    
327
	public void refresh() {
328
		if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
329
			((AnnotationModel) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
330
		}
331

    
332
		setInput(getEditorInput());
333
	}
334

    
335
	/** {@inheritDoc} */
336
	@Override
337
	public AbstractBulkEditorInput getEditorInput() {
338
		return (AbstractBulkEditorInput) super.getEditorInput();
339
	}
340

    
341
	@Override
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);
347
	}
348

    
349
	@Override
350
	public boolean canAttachMedia() {
351
	    return (getEditorInput() instanceof TaxonEditorInput || getEditorInput() instanceof OccurrenceEditorInput) ?true:false;
352
	}
353

    
354
    public IUndoContext getUndoContext() {
355
        return undoContext;
356
    }
357

    
358

    
359
}
(1-1/10)