Project

General

Profile

Download (8.97 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.editor;
11

    
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.eclipse.core.commands.ExecutionEvent;
17
import org.eclipse.core.commands.operations.IOperationHistory;
18
import org.eclipse.core.commands.operations.IUndoContext;
19
import org.eclipse.jface.dialogs.MessageDialog;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.IStructuredSelection;
22
import org.eclipse.swt.widgets.Shell;
23
import org.eclipse.ui.IEditorInput;
24
import org.eclipse.ui.IEditorPart;
25
import org.eclipse.ui.IEditorReference;
26
import org.eclipse.ui.PartInitException;
27
import org.eclipse.ui.handlers.HandlerUtil;
28

    
29
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
30
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
31
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
32
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
33
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
34
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
35
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
37

    
38
/**
39
 * Utility for the editor package
40
 *
41
 * @author n.hoffmann
42
 * @created 20.01.2009
43
 * @version 1.0
44
 */
45
public class EditorUtil extends AbstractUtility {
46

    
47
	private static boolean isSaving = false;
48

    
49
	/**
50
	 * Opens a new editor window with the given input
51
	 *
52
	 * @param input
53
	 * @param editorId
54
	 * @return
55
	 * @return
56
	 * @throws PartInitException
57
	 */
58
	private static IEditorPart open(final IEditorInput input,
59
			final String editorId) throws PartInitException {
60
		return getActivePage().openEditor(input, editorId);
61
	}
62

    
63
	/**
64
	 * Opens a new editor window with the given TaxonEditorInput
65
	 *
66
	 * @param input
67
	 *            a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
68
	 *            object.
69
	 * @throws org.eclipse.ui.PartInitException
70
	 *             if any.
71
	 */
72
	public static void open(TaxonEditorInput input) throws PartInitException {
73
		open(input, MultiPageTaxonEditor.ID);
74
	}
75

    
76
	public static void open(PolytomousKeyEditorInput input)
77
			throws PartInitException {
78
		open(input, KeyEditor.ID);
79
	}
80

    
81
	public static void open(CdmAuthorityEditorInput input)
82
			throws PartInitException {
83
		open(input, CdmAuthorityEditor.ID);
84
	}
85

    
86
	/**
87
	 * Opens a new DerivateView for the given input
88
	 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
89
	 * @throws PartInitException
90
	 */
91
	public static void open(DerivateViewEditorInput input)
92
	        throws PartInitException {
93
	    open(input, DerivateView.ID);
94
	}
95

    
96
	/**
97
	 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
98
	 * initialised here and is passed to the editor
99
	 *
100
	 * @param taxonNodeUuid
101
	 *            a {@link java.util.UUID} object.
102
	 * @throws java.lang.Exception
103
	 *             if any.
104
	 */
105
	public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
106
		TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
107
		open(input);
108
	}
109

    
110
	/**
111
	 * <p>
112
	 * openTaxonBase
113
	 * </p>
114
	 *
115
	 * @param taxonBaseUuid
116
	 *            a {@link java.util.UUID} object.
117
	 * @throws org.eclipse.ui.PartInitException
118
	 *             if any.
119
	 */
120
	public static void openTaxonBase(UUID taxonBaseUuid)
121
			throws PartInitException {
122
		TaxonEditorInput input = TaxonEditorInput
123
				.NewInstanceFromTaxonBase(taxonBaseUuid);
124
		open(input);
125
	}
126

    
127
	/**
128
	 * <p>
129
	 * findEditorByTaxonNodeUuid
130
	 * </p>
131
	 *
132
	 * @param taxonNodeUuid
133
	 *            a {@link java.util.UUID} object.
134
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
135
	 * @throws java.lang.Exception
136
	 *             if any.
137
	 */
138
	public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
139
			throws Exception {
140
		IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
141
		return getActivePage().findEditor(input);
142
	}
143

    
144
	/**
145
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
146
	 * should appear in neither the list of recent names nor in the taxonomic
147
	 * tree when opened.
148
	 *
149
	 * @throws org.eclipse.ui.PartInitException
150
	 *             if any.
151
	 * @param parentNodeUuid
152
	 *            a {@link java.util.UUID} object.
153
	 */
154
	public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
155
		TaxonEditorInput input = TaxonEditorInput
156
				.NewEmptyInstance(parentNodeUuid);
157
		open(input, MultiPageTaxonEditor.ID);
158

    
159
		getActiveMultiPageTaxonEditor().changed(null);
160

    
161
	}
162

    
163
	/**
164
	 * <p>
165
	 * setSaving
166
	 * </p>
167
	 *
168
	 * @param isSaving
169
	 *            a boolean.
170
	 */
171
	public static void setSaving(boolean isSaving) {
172
		EditorUtil.isSaving = isSaving;
173
	}
174

    
175
	/**
176
	 * <p>
177
	 * isSaving
178
	 * </p>
179
	 *
180
	 * @return a boolean.
181
	 */
182
	public static boolean isSaving() {
183
		return isSaving;
184
	}
185

    
186
	/**
187
	 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
188
	 *
189
	 * @return a {@link java.util.Set} object.
190
	 */
191
	public static Set<IEditorPart> getOpenEditors() {
192
		Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
193

    
194
		if (getActivePage() != null) {
195
			for (IEditorReference reference : getActivePage()
196
					.getEditorReferences()) {
197
				IEditorPart editor = reference.getEditor(false);
198
				if (editor instanceof MultiPageTaxonEditor) {
199
					taxonEditors.add(editor);
200
				}
201
			}
202
		}
203

    
204
		return taxonEditors;
205
	}
206

    
207
	/**
208
	 * Returns the currently active taxon editor
209
	 *
210
	 * @return the taxon editor that has focus
211
	 */
212
	public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
213
		IEditorPart editorPart = getActiveEditor();
214
		if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
215
			MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
216
			editor.getConversationHolder().bind();
217
			return editor;
218
		}
219
		return null;
220
	}
221

    
222
	/**
223
	 * <p>
224
	 * getActiveEditorPage
225
	 * </p>
226
	 *
227
	 * @param page
228
	 *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
229
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
230
	 */
231
	public static IEditorPart getActiveEditorPage(Page page) {
232
		MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
233

    
234
		return editor != null ? editor.getPage(page) : null;
235
	}
236

    
237
	/**
238
	 * Returns the selection of the currently active taxon editor
239
	 *
240
	 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
241
	 */
242
	public static ISelection getCurrentSelection() {
243
		if (getActiveMultiPageTaxonEditor() == null) {
244
			return null;
245
		} else {
246
			return getActiveMultiPageTaxonEditor().getSite()
247
					.getSelectionProvider().getSelection();
248
		}
249
	}
250

    
251
	/**
252
	 * <p>
253
	 * getUndoContext
254
	 * </p>
255
	 *
256
	 * @param editor
257
	 *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
258
	 *            object.
259
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
260
	 *         object.
261
	 */
262
	public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
263
		return editor.getUndoContext();
264
	}
265

    
266
	/**
267
	 * <p>
268
	 * getUndoContext
269
	 * </p>
270
	 *
271
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
272
	 *         object.
273
	 */
274
	public static IUndoContext getUndoContext() {
275
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
276
	}
277

    
278
	/**
279
	 * <p>
280
	 * forceUserSave
281
	 * </p>
282
	 *
283
	 * @param editor
284
	 *            a {@link org.eclipse.ui.IEditorPart} object.
285
	 * @param shell
286
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
287
	 * @return a boolean.
288
	 */
289
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
290
		if (editor.isDirty()) {
291

    
292
			boolean doSave = MessageDialog
293
					.openConfirm(shell, "Confirm save",
294
							"Warning - this operation will save the editor. This will also save all other unsaved changes " +
295
							"in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
296

    
297
			if (!doSave) {
298
				return false;
299
			}
300

    
301
			editor.doSave(EditorUtil.getMonitor());
302
		}
303
		return true;
304
	}
305

    
306
	/**
307
	 * <p>
308
	 * getSelection
309
	 * </p>
310
	 *
311
	 * @param event
312
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
313
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
314
	 */
315
	public static IStructuredSelection getSelection(ExecutionEvent event) {
316
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
317

    
318
		return (IStructuredSelection) activeEditor.getSite()
319
				.getSelectionProvider().getSelection();
320
	}
321

    
322
	/**
323
	 * <p>
324
	 * getPluginId
325
	 * </p>
326
	 *
327
	 * @return a {@link java.lang.String} object.
328
	 */
329
	public static String getPluginId() {
330
		return TaxeditorEditorPlugin.PLUGIN_ID;
331
	}
332

    
333
	public static void openPolytomousKey(UUID polytomousKeyUuid)
334
			throws Exception {
335
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
336
				.NewInstance(polytomousKeyUuid);
337
		open(input);
338
	}
339

    
340
	public static void openCdmAuthorities(UUID groupUuid)
341
			throws Exception {
342
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
343
		open(input);
344
	}
345
}
(4-4/17)