started to refactor action delegation
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / controller / EditorController.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.controller;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.commands.operations.IOperationHistory;
19 import org.eclipse.core.commands.operations.IUndoContext;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IEditorReference;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.PartInitException;
25
26 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.taxeditor.datasource.CdmTransactionController;
31 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
32 import eu.etaxonomy.taxeditor.editor.name.CdmParserController;
33 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
34 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
35 import eu.etaxonomy.taxeditor.model.NameEditorInput;
36 import eu.etaxonomy.taxeditor.navigation.RecentNamesView;
37 import eu.etaxonomy.taxeditor.navigation.TaxonomicTreeViewer;
38
39 /**
40 * @author n.hoffmann
41 * @created 20.01.2009
42 * @version 1.0
43 */
44 public class EditorController {
45 private static final Logger logger = Logger.getLogger(EditorController.class);
46
47
48 private static Map<Taxon, TaxonNameEditor> taxonNameEditors;
49
50
51 private static boolean isSaving = false;
52
53 private static IWorkbenchPage getActivePage(){
54 return GlobalController.getActivePage();
55 }
56
57 /**
58 * @param input
59 * @return
60 * @throws PartInitException
61 */
62 private static IEditorPart getEditorByInput(IEditorInput input)
63 throws PartInitException {
64 for (IEditorReference reference : getActivePage().getEditorReferences()) {
65 if (reference.getEditorInput().equals(input)) {
66 IEditorPart editor = reference.getEditor(false);
67 return editor;
68 }
69 }
70 return null;
71 }
72
73 public static boolean closeAll() {
74 for (IEditorPart editor : getOpenEditors()) {
75 if (!getActivePage().closeEditor(editor, true)) {
76 return false;
77 }
78 }
79 return true;
80 }
81
82 public static IEditorPart getEditorByTaxon(Taxon taxon)
83 throws PartInitException {
84 IEditorInput input = new NameEditorInput(taxon);
85 return getEditorByInput(input);
86 }
87
88 /**
89 * @param input
90 * @param editorId
91 * @return
92 * @throws PartInitException
93 */
94 private static IEditorPart open(IEditorInput input, String editorId)
95 throws PartInitException {
96 return getActivePage().openEditor(input, editorId);
97 }
98
99 /**
100 * Open an empty taxon editor
101 *
102 * @return
103 * @throws PartInitException
104 */
105 public static IEditorPart open()
106 throws PartInitException{
107 return open(null);
108 }
109
110 /**
111 * Open a taxon editor for the given taxon. If the taxon has been saved before, it is
112 * considered initialized, and opening its editor will give it an entry in the list
113 * of recent names and open its node in the tree.
114 *
115 * @param taxon
116 * @param isInitialized
117 * @return
118 */
119 public static IEditorPart open(Taxon taxon, boolean isInitialized){
120 if(taxon == null){
121 TaxonNameBase name = CdmParserController.parseFullReference("", null, null);
122 name.setFullTitleCache("", false);
123 name.setTitleCache("", false);
124 taxon = Taxon.NewInstance(name,
125 CdmSessionDataRepository.getDefault().getDefaultSec());
126 }
127
128 if (isInitialized) {
129
130 TaxonomicTreeViewer treeViewer = TreeController.getTreeViewer();
131 if (treeViewer != null) {
132 treeViewer.revealTaxon(taxon);
133 }
134
135 RecentNamesView.addRecentName(taxon);
136 }
137
138 IEditorInput input = new NameEditorInput(taxon);
139
140 try {
141 return open(input, MultiPageTaxonEditor.ID);
142 } catch (PartInitException e) {
143 logger.error("Error opening editor.", e);
144 }
145 return null;
146 }
147
148 public static IEditorPart open(Taxon taxon){
149 return open(taxon, true);
150 }
151
152 /**
153 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
154 * the list of recent names nor in the taxonomic tree when opened.
155 *
156 * @param taxon
157 * @return
158 */
159 public static IEditorPart openUnitializedTaxon(Taxon taxon) {
160 return open(taxon, false);
161 }
162
163 public static IEditorPart openTaxonEditor(TaxonBase taxonBase) {
164
165 Taxon taxon = null;
166 if (taxonBase instanceof Synonym) {
167 // TODO: in case of pro parte synonym or any other where we might have multiple
168 // accepted taxa we have to provide a mechanism that can deal with that
169 // TODO set focus to the synonym
170 taxon = (Taxon) ((Synonym) taxonBase).getAcceptedTaxa().toArray()[0];
171 } else {
172 taxon = (Taxon) taxonBase;
173 }
174
175 // Open a taxon editor
176 return EditorController.open(taxon);
177 }
178
179 /**
180 * Redraws an open editor if it exists for the given taxon
181 *
182 * @param taxon
183 * @return
184 */
185 public static boolean redraw(Taxon taxon){
186
187 TaxonNameEditor editor = EditorController.getEditor(taxon);
188
189 if(editor == null || editor.redraw()){
190
191 // Mark editor as changed and unsaved
192 editor.setDirty();
193
194 return true;
195 }
196 return false;
197 }
198
199 /**
200 * @param taxon
201 * @param save
202 */
203 public static void close(Taxon taxon, boolean save) {
204 IEditorPart editor;
205 try {
206 editor = getEditorByTaxon(taxon);
207 close(editor, save);
208 } catch (PartInitException e) {
209 logger.error("Error closing taxon editor", e);
210 }
211
212 }
213
214 /**
215 * @param editor
216 * @param save
217 * @throws PartInitException
218 */
219 private static void close(IEditorPart editor, boolean save)
220 throws PartInitException {
221 if (editor != null) {
222 getActivePage().closeEditor(editor, save);
223 }
224 }
225
226 public static boolean save(Taxon taxon, boolean confirm) {
227 IEditorPart editor = null;
228 try {
229 editor = getEditorByTaxon(taxon);
230 } catch (PartInitException e) {
231 logger.error("Error saving taxon.", e);
232 }
233 if (editor != null) {
234 return GlobalController.getActivePage().saveEditor(editor, confirm);
235 }
236 return false;
237 }
238
239 public static void saveAll(){
240 setSaving(true);
241
242 // Get all open windows
243 for (IEditorPart taxonEditor : getOpenEditors()) {
244
245 // Save the dirty ones
246 if (taxonEditor.isDirty()) {
247
248
249 IEditorInput input = taxonEditor.getEditorInput();
250 if (input.getAdapter(Taxon.class) != null) {
251 Taxon taxon = (Taxon) input.getAdapter(Taxon.class);
252 CdmSessionDataRepository.getDefault().saveTaxon(taxon);
253 if (taxonEditor instanceof MultiPageTaxonEditor) {
254 ((MultiPageTaxonEditor) taxonEditor).setDirtyExtern(false);
255 }
256 }
257 }
258 }
259
260 // Commit the transaction
261 CdmTransactionController.commitTransaction();
262
263 // Force library objects to be associated with new transaction
264 CdmSessionDataRepository.getDefault().clearNonTaxonData();
265
266 // Start a new transaction
267 CdmTransactionController.startTransaction();
268
269 // Put all open taxa in the new transaction
270 CdmTransactionController.addSessionTaxaToTransaction();
271
272 setSaving(false);
273 // TODO: delete undoHistory
274 }
275
276 public static void setSaving(boolean isSaving) {
277 EditorController.isSaving = isSaving;
278 }
279
280 public static boolean isSaving() {
281 return isSaving;
282 }
283
284 public static void addEditor(
285 Taxon taxon, TaxonNameEditor taxonNameEditor) {
286 if (taxonNameEditors == null) {
287 taxonNameEditors = new HashMap<Taxon, TaxonNameEditor>();
288 }
289 taxonNameEditors.put(taxon, taxonNameEditor);
290 }
291
292 public static TaxonNameEditor getEditor(Taxon taxon) {
293 if (taxonNameEditors == null) {
294 return null;
295 }
296 return taxonNameEditors.get(taxon);
297 }
298
299 /**
300 * Returns a set of all currently open
301 * <code>MultiPageTaxonEditor</code>s.
302 *
303 * @return
304 */
305 public static Set<IEditorPart> getOpenEditors() {
306
307 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
308
309 for (IEditorReference reference : getActivePage().getEditorReferences()) {
310 IEditorPart editor = reference.getEditor(false);
311 if (editor instanceof MultiPageTaxonEditor) {
312 taxonEditors.add(editor);
313 }
314 }
315 return taxonEditors;
316 }
317
318 /**
319 * Returns the currently active taxon editor
320 *
321 * @return the taxon editor that has focus
322 */
323 public static MultiPageTaxonEditor getActiveEditor(){
324 if(getActivePage().getActiveEditor() instanceof MultiPageTaxonEditor){
325 return (MultiPageTaxonEditor) getActivePage().getActiveEditor();
326 }
327 return null;
328 }
329
330 public static IUndoContext getUndoContext(Taxon taxon) {
331 // TODO make this taxon name editor specific
332 // return getTaxonNameEditor(taxon).getUndoContext();
333 return getUndoContext();
334 }
335
336 public static IUndoContext getUndoContext() {
337 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
338 }
339
340
341
342
343 }