Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | Tag: | Revision:
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.commands.operations.IUndoContext;
14
import org.eclipse.core.commands.operations.UndoContext;
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.jface.action.IMenuManager;
17
import org.eclipse.jface.dialogs.MessageDialog;
18
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
19
import org.eclipse.jface.preference.IPreferenceStore;
20
import org.eclipse.jface.text.IDocument;
21
import org.eclipse.jface.text.ITextSelection;
22
import org.eclipse.jface.text.TextSelection;
23
import org.eclipse.jface.text.source.Annotation;
24
import org.eclipse.jface.text.source.AnnotationModel;
25
import org.eclipse.jface.text.source.ISourceViewer;
26
import org.eclipse.jface.text.source.IVerticalRuler;
27
import org.eclipse.jface.util.IPropertyChangeListener;
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.ui.IEditorInput;
38
import org.eclipse.ui.IEditorSite;
39
import org.eclipse.ui.PartInitException;
40
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
41

    
42
import eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineDocumentProvider;
43
import eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor;
44
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
45
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
46
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
47
import eu.etaxonomy.taxeditor.bulkeditor.input.TaxonEditorInput;
48
import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
49
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
50
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
51
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
52
import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
53
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
54
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
55
import eu.etaxonomy.taxeditor.store.CdmStore;
56

    
57
/**
58
 * @author p.ciardelli
59
 * @created 07.07.2009
60
 * @version 1.0
61
 */
62
public class BulkEditor extends AnnotatedLineEditor implements IPartContentHasDetails,
63
        IDirtyMarkable, IDerivedUnitFacadePart, IPartContentHasFactualData, IPartContentHasMedia {
64

    
65
	/** Constant <code>ID="bulkeditor.editor"</code> */
66
	public static final String ID = "bulkeditor.editor";
67

    
68
	private boolean isInitialFocus = true;
69

    
70
	private BulkEditorSearch searchBar = null;
71

    
72
	private IPropertyChangeListener markerPreferenceListener;
73

    
74
	private boolean isDirty;
75

    
76
    private IUndoContext undoContext;
77

    
78
	public BulkEditor() {
79
		super(CdmStore.createConversation());
80
        undoContext = new UndoContext();
81
	}
82

    
83
	/** {@inheritDoc} */
84
	@Override
85
	protected void initializeEditor() {
86
		super.initializeEditor();
87

    
88
		/**
89
		 * see AbstractTextEditor javadoc for explanation of context menu ids
90
		 */
91
		setEditorContextMenuId("#BulkEditorContext");
92

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

    
95
		setLineDisplayStrategy(new BulkEditorLineDisplay(this));
96

    
97
		setSourceViewerConfiguration(new BulkEditorViewerConfiguration(lineDisplayStrategy));
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

    
157
	/** {@inheritDoc} */
158
	@Override
159
	public void dispose() {
160
		if (markerPreferenceListener != null ) {
161
			PreferencesUtil.getPreferenceStore().removePropertyChangeListener(markerPreferenceListener);
162
		}
163
		super.dispose();
164
	}
165

    
166

    
167
	/** {@inheritDoc} */
168
	@Override
169
	public boolean isEditable() {
170
		return false;
171
	}
172

    
173
	/** {@inheritDoc} */
174
	@Override
175
	public void setFocus() {
176
	    conversation.bind();
177

    
178
		// TODO find a better place to put this - this dialog should be shown after initial contents of
179
		//	Editor are displayed
180
		if (isInitialFocus) {
181
			displayWarningDialog();
182
			isInitialFocus = false;
183
		}
184
		super.setFocus();
185
		searchBar.setFocus();
186
	}
187

    
188
	private void displayWarningDialog() {
189
		IPreferenceStore prefs = PreferencesUtil.getPreferenceStore();
190
		if (!prefs.getBoolean(IPreferenceKeys.HIDE_BULKEDITOR_INFO)) {
191
			String msg = "The Bulk Editor allows you to edit objects used to reference other objects, such as names, references, and authors.\n\n" +
192
							"Any changes you make to an object in the Bulk Editor will be displayed wherever the object is used.\n\n" +
193
							"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.";
194
			MessageDialogWithToggle dialog = MessageDialogWithToggle.openOkCancelConfirm
195
												(getSite().getShell(), "Bulk Editor", msg, "Do not show this message again",
196
														false, null, IPreferenceKeys.HIDE_BULKEDITOR_INFO);
197
			if (dialog.getReturnCode() == Window.OK) {
198
				prefs.setValue(IPreferenceKeys.HIDE_BULKEDITOR_INFO, dialog.getToggleState());
199
			}
200
		}
201
	}
202

    
203
	private void addToggleMergeCandidateListener(Control control) {
204
		control.addMouseListener(new MouseAdapter() {
205
			@Override
206
			public void mouseDoubleClick(MouseEvent e) {
207
				StyledText textWidget = getSourceViewer().getTextWidget();
208
				int line = textWidget.getLineIndex(e.y);
209
				toggleMergeCandidateAnnotation(line);
210
			}
211
		});
212
	}
213

    
214
	public void toggleMergeCandidateAnnotation(int line) {
215

    
216
		IDocument document = getSourceViewer().getDocument();
217
		LineAnnotationModel model =
218
				(LineAnnotationModel) getSourceViewer().getAnnotationModel();
219

    
220
		if(model != null){
221
			Annotation annotation = model.getAnnotationAtLine(line, document);
222

    
223
			if (annotation != null) {
224
				if (annotation.getType().equals(IBulkEditorConstants.TYPE_MERGE_CANDIDATE)) {
225
					model.changeAnnotationType(
226
							annotation, LineAnnotation.TYPE_GENERIC);
227
				} else {
228
					model.changeAnnotationType(
229
							annotation, IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
230
				}
231
			}
232
		}
233
	}
234

    
235
	@Override
236
	public boolean isDirty() {
237
	    if(isDirty){
238
	        return isDirty;
239
	    }
240
	    else{
241
	        return super.isDirty();
242
	    }
243
	}
244

    
245
	@Override
246
    public void forceDirty(){
247
	    isDirty = true;
248
	    firePropertyChange(PROP_DIRTY);
249
	}
250

    
251
	@Override
252
	public void doSave(IProgressMonitor progressMonitor) {
253
	    isDirty = false;
254

    
255
	    super.doSave(progressMonitor);
256

    
257
	    selectFirstItem();
258

    
259
		getSourceViewer().getTextWidget().setFocus();
260

    
261
	}
262

    
263
	/** {@inheritDoc} */
264
	@Override
265
    public void changed(Object object) {
266
//		this.dirty = dirty;
267
		AnnotatedLineDocumentProvider p = (AnnotatedLineDocumentProvider) getDocumentProvider();
268
		p.changed(object);
269
//		firePropertyChange(PROP_DIRTY);
270
	}
271

    
272
	/** {@inheritDoc} */
273
	public void performSearch(BulkEditorQuery query) {
274
		if (query != null) {
275

    
276
			// TODO check if dirty, prompt save
277
			if (isDirty()) {
278
				String[] labels = {"Save (and Search)", "Don't save (and Search)","Cancel"};
279
				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);
280
				int result = dialog.open();
281
						//MessageDialog.openQuestion(getEditorSite().getShell(),
282
						//"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
283
				if (result == 0) {
284
					doSave(null);
285
				} else if (result == 2){
286
					return;
287
				}
288
			}
289
			conversation.clear();
290
			conversation.commit(true);
291

    
292
			getEditorInput().dispose();
293
			getEditorInput().bind();
294
			getEditorInput().performSearch(query);
295

    
296
			refresh();
297

    
298
			selectFirstItem();
299

    
300
			getSourceViewer().getTextWidget().setFocus();
301
		}
302
	}
303

    
304
	private void selectFirstItem() {
305
		ITextSelection selection = new TextSelection(0, 0);
306
		getSelectionProvider().setSelection(selection);
307
	}
308

    
309
	public void refresh() {
310
		if(getDocumentProvider().getAnnotationModel(getEditorInput()) != null){
311
			((AnnotationModel) getDocumentProvider().getAnnotationModel(getEditorInput())).removeAllAnnotations();
312
		}
313

    
314
		setInput(getEditorInput());
315
	}
316

    
317
	/** {@inheritDoc} */
318
	@Override
319
	public AbstractBulkEditorInput getEditorInput() {
320
		return (AbstractBulkEditorInput) super.getEditorInput();
321
	}
322

    
323
	@Override
324
    protected void editorContextMenuAboutToShow(IMenuManager menu) {
325
		super.editorContextMenuAboutToShow(menu);
326
		menu.remove(ITextEditorActionConstants.SHIFT_RIGHT);
327
		menu.remove(ITextEditorActionConstants.SHIFT_LEFT);
328
		menu.remove(ITextEditorActionConstants.CONTEXT_PREFERENCES);
329
	}
330

    
331
	@Override
332
	public boolean canAttachMedia() {
333
	    return getEditorInput() instanceof TaxonEditorInput?true:false;
334
	}
335

    
336
    public IUndoContext getUndoContext() {
337
        return undoContext;
338
    }
339
}
(1-1/10)