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