7221836b086a56693cec7c34890fe4fa557777b7
[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.operations.IOperationHistory;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IEditorReference;
23 import org.eclipse.ui.IPageLayout;
24 import org.eclipse.ui.IViewPart;
25 import org.eclipse.ui.PartInitException;
26
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.taxeditor.model.AbstractUtility;
29
30 /**
31 * @author n.hoffmann
32 * @created 20.01.2009
33 * @version 1.0
34 */
35 public class EditorUtil extends AbstractUtility{
36
37 private static boolean isSaving = false;
38
39 private static IViewPart view;
40
41 public static boolean closeAll() {
42 for (IEditorPart editor : getOpenEditors()) {
43 if (!getActivePage().closeEditor(editor, true)) {
44 return false;
45 }
46 }
47 return true;
48 }
49
50 /**
51 * Close the given editor.
52 *
53 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
54 * @return <tt>true</tt> on success
55 */
56 public static boolean close(MultiPageTaxonEditor editor) {
57 return getActivePage().closeEditor(editor, true);
58 }
59
60 /**
61 * Opens a new editor window with the given input
62 *
63 * @param input
64 * @param editorId
65 * @return
66 * @throws PartInitException
67 */
68 private static IEditorPart open(IEditorInput input, String editorId)
69 throws PartInitException {
70 return getActivePage().openEditor(input, editorId);
71 }
72
73
74 /**
75 * Taxon Editors may be opened by supplying a taxon uuid only.
76 * Session gets initialised here and is passed to the editor
77 *
78 * @param taxonNodeUuid
79 * @return
80 * @throws PartInitException
81 */
82 public static IEditorPart open(UUID taxonNodeUuid) throws PartInitException {
83 IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
84 return open(input, MultiPageTaxonEditor.ID);
85 }
86
87
88 public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid){
89 IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
90 return getActivePage().findEditor(input);
91 }
92
93 /**
94 * An uninitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
95 * the list of recent names nor in the taxonomic tree when opened.
96 *
97 * @param parentTaxon The UUID of the parental taxon or <code>null</code> if this is
98 * supposed to be a root taxon.
99 * @return The EditorPart.
100 * @throws PartInitException
101 */
102 public static IEditorPart openEmpty(UUID parentNodeUuid) throws PartInitException{
103 TaxonEditorInput input = TaxonEditorInput.NewEmptyInstance(parentNodeUuid);
104 IEditorPart editorPart = open(input, MultiPageTaxonEditor.ID);
105
106 if(editorPart instanceof MultiPageTaxonEditor){
107 ((MultiPageTaxonEditor) editorPart).setDirty();
108 }
109
110 return editorPart;
111 }
112
113 public static void setSaving(boolean isSaving) {
114 EditorUtil.isSaving = isSaving;
115 }
116
117 public static boolean isSaving() {
118 return isSaving;
119 }
120
121 /**
122 * Returns a set of all currently open
123 * <code>MultiPageTaxonEditor</code>s.
124 *
125 * @return
126 */
127 public static Set<IEditorPart> getOpenEditors() {
128 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
129
130 if(getActivePage() != null){
131 for (IEditorReference reference : getActivePage().getEditorReferences()) {
132 IEditorPart editor = reference.getEditor(false);
133 if (editor instanceof MultiPageTaxonEditor) {
134 taxonEditors.add(editor);
135 }
136 }
137 }
138
139 return taxonEditors;
140 }
141
142 /**
143 * Returns the currently active taxon editor
144 *
145 * @return the taxon editor that has focus
146 */
147 public static MultiPageTaxonEditor getActiveEditor(){
148 IEditorPart editorPart = getActivePage().getActiveEditor();
149 if(editorPart instanceof MultiPageTaxonEditor){
150 MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
151 editor.getConversationHolder().bind();
152 return editor;
153 }
154 return null;
155 }
156
157 public static IEditorPart getActiveEditorPage(Page page){
158 MultiPageTaxonEditor editor = getActiveEditor();
159
160 return editor.getPage(page);
161 }
162
163 public static IUndoContext getUndoContext(MultiPageTaxonEditor editor){
164 return editor.getUndoContext();
165 }
166
167 public static IUndoContext getUndoContext() {
168 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
169 }
170
171 public static void showPropertySheet() {
172 if (getActivePage() == null) {
173 return;
174 }
175 try {
176 view = getActivePage().showView(IPageLayout.ID_PROP_SHEET);
177 } catch (PartInitException e) {
178 // TODO Auto-generated catch block
179 e.printStackTrace();
180 }
181 }
182
183 public static void checkHidePropertySheet() {
184 if (getOpenEditors().size() == 0) {
185 if (view != null && getActivePage() != null) {
186 getActivePage().hideView(view);
187 }
188 }
189 }
190
191 /**
192 * @param editor
193 * @param shell
194 * @return
195 */
196 public static boolean forceUserSave(IEditorPart editor, Shell shell) {
197 if (editor.isDirty()) {
198
199 boolean doSave = MessageDialog.openConfirm(shell, "Confirm save", "The current editor must be saved before this action can be executed.");
200
201 if (!doSave) {
202 return false;
203 }
204
205 editor.doSave(null);
206 }
207 return true;
208 }
209 }