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