Merged refactoring from development branch.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
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.internal.TaxeditorEditorPlugin;
30 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditor;
31 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
32 import eu.etaxonomy.taxeditor.model.AbstractUtility;
33 import eu.etaxonomy.taxeditor.view.AbstractCdmDataViewer;
34 import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
35 import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
36
37 /**
38 * Utility for the editor package
39 *
40 * @author n.hoffmann
41 * @created 20.01.2009
42 * @version 1.0
43 */
44 public class EditorUtil extends AbstractUtility{
45
46 private static boolean isSaving = false;
47
48 /**
49 * Opens a new editor window with the given input
50 *
51 * @param input
52 * @param editorId
53 * @return
54 * @return
55 * @throws PartInitException
56 */
57 private static IEditorPart open(final IEditorInput input, final String editorId) throws PartInitException{
58 return getActivePage().openEditor(input, editorId);
59 }
60
61 /**
62 * Opens a new editor window with the given TaxonEditorInput
63 *
64 * @param input a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
65 * @throws org.eclipse.ui.PartInitException if any.
66 */
67 public static void open(TaxonEditorInput input) throws PartInitException{
68 open(input, MultiPageTaxonEditor.ID);
69 }
70
71 public static void open(PolytomousKeyEditorInput input) throws PartInitException{
72 open(input, PolytomousKeyEditor.ID);
73 }
74
75 /**
76 * Taxon Editors may be opened by supplying a taxon node uuid.
77 * Session gets initialised here and is passed to the editor
78 *
79 * @param taxonNodeUuid a {@link java.util.UUID} object.
80 * @throws java.lang.Exception if any.
81 */
82 public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
83 TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
84 open(input);
85 }
86
87 /**
88 * <p>openTaxonBase</p>
89 *
90 * @param taxonBaseUuid a {@link java.util.UUID} object.
91 * @throws org.eclipse.ui.PartInitException if any.
92 */
93 public static void openTaxonBase(UUID taxonBaseUuid) throws PartInitException {
94 TaxonEditorInput input = TaxonEditorInput.NewInstanceFromTaxonBase(taxonBaseUuid);
95 open(input);
96 }
97
98
99 /**
100 * <p>findEditorByTaxonNodeUuid</p>
101 *
102 * @param taxonNodeUuid a {@link java.util.UUID} object.
103 * @return a {@link org.eclipse.ui.IEditorPart} object.
104 * @throws java.lang.Exception if any.
105 */
106 public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid) throws Exception{
107 IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
108 return getActivePage().findEditor(input);
109 }
110
111 /**
112 * An uninitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
113 * the list of recent names nor in the taxonomic tree when opened.
114 *
115 * @throws org.eclipse.ui.PartInitException if any.
116 * @param parentNodeUuid a {@link java.util.UUID} object.
117 */
118 public static void openEmpty(UUID parentNodeUuid) throws PartInitException{
119 TaxonEditorInput input = TaxonEditorInput.NewEmptyInstance(parentNodeUuid);
120 open(input, MultiPageTaxonEditor.ID);
121
122 getActiveMultiPageTaxonEditor().changed(null);
123
124
125 }
126
127 /**
128 * <p>setSaving</p>
129 *
130 * @param isSaving a boolean.
131 */
132 public static void setSaving(boolean isSaving) {
133 EditorUtil.isSaving = isSaving;
134 }
135
136 /**
137 * <p>isSaving</p>
138 *
139 * @return a boolean.
140 */
141 public static boolean isSaving() {
142 return isSaving;
143 }
144
145 /**
146 * Returns a set of all currently open
147 * <code>MultiPageTaxonEditor</code>s.
148 *
149 * @return a {@link java.util.Set} object.
150 */
151 public static Set<IEditorPart> getOpenEditors() {
152 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
153
154 if(getActivePage() != null){
155 for (IEditorReference reference : getActivePage().getEditorReferences()) {
156 IEditorPart editor = reference.getEditor(false);
157 if (editor instanceof MultiPageTaxonEditor) {
158 taxonEditors.add(editor);
159 }
160 }
161 }
162
163 return taxonEditors;
164 }
165
166 /**
167 * Returns the currently active taxon editor
168 *
169 * @return the taxon editor that has focus
170 */
171 public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor(){
172 IEditorPart editorPart = getActiveEditor();
173 if(editorPart != null && editorPart instanceof MultiPageTaxonEditor){
174 MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
175 editor.getConversationHolder().bind();
176 return editor;
177 }
178 return null;
179 }
180
181 /**
182 * <p>getActiveEditorPage</p>
183 *
184 * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
185 * @return a {@link org.eclipse.ui.IEditorPart} object.
186 */
187 public static IEditorPart getActiveEditorPage(Page page){
188 MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
189
190 return editor != null ? editor.getPage(page) : null;
191 }
192
193 /**
194 * Returns the selection of the currently active taxon editor
195 *
196 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
197 */
198 public static ISelection getCurrentSelection(){
199 if(getActiveMultiPageTaxonEditor() == null){
200 return null;
201 }else{
202 return getActiveMultiPageTaxonEditor().getSite().getSelectionProvider().getSelection();
203 }
204 }
205
206 /**
207 * <p>getUndoContext</p>
208 *
209 * @param editor a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor} object.
210 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
211 */
212 public static IUndoContext getUndoContext(MultiPageTaxonEditor editor){
213 return editor.getUndoContext();
214 }
215
216 /**
217 * <p>getUndoContext</p>
218 *
219 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
220 */
221 public static IUndoContext getUndoContext() {
222 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
223 }
224
225
226 /**
227 * <p>forceUserSave</p>
228 *
229 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
230 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
231 * @return a boolean.
232 */
233 public static boolean forceUserSave(IEditorPart editor, Shell shell) {
234 if (editor.isDirty()) {
235
236 boolean doSave = MessageDialog.openConfirm(shell, "Confirm save", "The current editor must be saved before this action can be executed.");
237
238 if (!doSave) {
239 return false;
240 }
241
242 editor.doSave(EditorUtil.getMonitor());
243 }
244 return true;
245 }
246
247 /**
248 * <p>getSelection</p>
249 *
250 * @param event a {@link org.eclipse.core.commands.ExecutionEvent} object.
251 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
252 */
253 public static IStructuredSelection getSelection(ExecutionEvent event){
254 IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
255
256 return (IStructuredSelection) activeEditor.getSite().getSelectionProvider().getSelection();
257 }
258
259 /**
260 * <p>getPluginId</p>
261 *
262 * @return a {@link java.lang.String} object.
263 */
264 protected static String getPluginId(){
265 return TaxeditorEditorPlugin.PLUGIN_ID;
266 }
267
268 public static void openPolytomousKey(UUID polytomousKeyUuid) throws Exception{
269 PolytomousKeyEditorInput input = PolytomousKeyEditorInput.NewInstance(polytomousKeyUuid);
270 open(input);
271 }
272 }