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