Features lists now sorted alphabetically. Added microreference, cache strings to...
[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.AbstractTaxonEditor;
32 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
33 import eu.etaxonomy.taxeditor.editor.TemporaryTaxon;
34 import eu.etaxonomy.taxeditor.editor.description.TaxonDescriptionEditor;
35 import eu.etaxonomy.taxeditor.editor.images.TaxonImagesEditor;
36 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
37 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
38 import eu.etaxonomy.taxeditor.model.CdmUtil;
39 import eu.etaxonomy.taxeditor.model.NameEditorInput;
40 import eu.etaxonomy.taxeditor.navigation.RecentNamesView;
41 import eu.etaxonomy.taxeditor.navigation.TaxonomicTreeViewer;
42
43 /**
44 * @author n.hoffmann
45 * @created 20.01.2009
46 * @version 1.0
47 */
48 public class EditorController {
49 private static final Logger logger = Logger.getLogger(EditorController.class);
50
51 private static Map<Taxon, TaxonNameEditor> taxonNameEditors;
52 private static Map<Taxon, TaxonDescriptionEditor> taxonDescriptionEditors;
53
54 private static boolean isSaving = false;
55
56 private static HashMap<Taxon, TaxonImagesEditor> taxonImagesEditors;
57
58 private static IWorkbenchPage getActivePage(){
59 return GlobalController.getActivePage();
60 }
61
62 /**
63 * @param input
64 * @return
65 * @throws PartInitException
66 */
67 private static IEditorPart getEditorByInput(IEditorInput input)
68 throws PartInitException {
69 for (IEditorReference reference : getActivePage().getEditorReferences()) {
70 if (reference.getEditorInput().equals(input)) {
71 IEditorPart editor = reference.getEditor(false);
72 return editor;
73 }
74 }
75 return null;
76 }
77
78 public static boolean closeAll() {
79 for (IEditorPart editor : getOpenEditors()) {
80 if (!getActivePage().closeEditor(editor, true)) {
81 return false;
82 }
83 }
84 return true;
85 }
86
87 public static IEditorPart getEditorByTaxon(Taxon taxon)
88 throws PartInitException {
89 IEditorInput input = new NameEditorInput(taxon);
90 return getEditorByInput(input);
91 }
92
93 /**
94 * @param input
95 * @param editorId
96 * @return
97 * @throws PartInitException
98 */
99 private static IEditorPart open(IEditorInput input, String editorId)
100 throws PartInitException {
101 return getActivePage().openEditor(input, editorId);
102 }
103
104 /**
105 * Open a taxon editor for the given taxon. If the taxon has been saved before, it is
106 * considered initialized, and opening its editor will give it an entry in the list
107 * of recent names and open its node in the tree.
108 *
109 * @param taxon
110 * @param isInitialized
111 * @return
112 */
113 public static IEditorPart open(Taxon taxon, boolean isInitialized){
114
115 if (isInitialized) {
116
117 TaxonomicTreeViewer treeViewer = TreeController.getTreeViewer();
118 if (treeViewer != null) {
119 treeViewer.revealTaxon(taxon);
120 }
121
122 RecentNamesView.addRecentName(taxon);
123 }
124
125 IEditorInput input = new NameEditorInput(taxon);
126
127 try {
128 return open(input, MultiPageTaxonEditor.ID);
129 } catch (PartInitException e) {
130 logger.error("Error opening editor.", e);
131 }
132 return null;
133 }
134
135 public static IEditorPart open(Taxon taxon){
136
137 // TemporaryTaxon taxonCopy = TemporaryTaxon.newInstance(taxon);
138 Taxon taxonCopy = getEditorTaxon(taxon);
139
140 return open(getEditorTaxon(taxon), true);
141 }
142
143 /**
144 * @param taxon
145 * @return
146 */
147 private static Taxon getEditorTaxon(Taxon taxon) {
148 return (Taxon) CdmUtil.getTaxonService().getTaxonByUuid(taxon.getUuid());
149 }
150
151 /**
152 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
153 * the list of recent names nor in the taxonomic tree when opened.
154 *
155 * @param taxon
156 * @return
157 */
158 public static IEditorPart openUnitializedChildTaxon(Taxon parentTaxon) {
159
160 TaxonNameBase name = PreferencesController.getInstanceOfPreferredNameClass();
161 Taxon taxon = Taxon.NewInstance(name, parentTaxon.getSec());
162 parentTaxon.addTaxonomicChild(taxon, null, null);
163
164 return open(taxon, false);
165 }
166
167 /**
168 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
169 * the list of recent names nor in the taxonomic tree when opened.
170 *
171 * @param taxon
172 * @return
173 */
174 public static IEditorPart openUnitializedRootTaxon() {
175
176 TaxonNameBase name = PreferencesController.getInstanceOfPreferredNameClass();
177 Taxon taxon = Taxon.NewInstance(name,
178 CdmSessionDataRepository.getDefault().getDefaultSec());
179
180 return open(taxon, false);
181 }
182
183 public static IEditorPart openTaxonEditor(TaxonBase taxonBase) {
184
185 Taxon taxon = null;
186 if (taxonBase instanceof Synonym) {
187 // TODO: in case of pro parte synonym or any other where we might have multiple
188 // accepted taxa we have to provide a mechanism that can deal with that
189 // TODO set focus to the synonym
190 taxon = (Taxon) ((Synonym) taxonBase).getAcceptedTaxa().toArray()[0];
191 } else {
192 taxon = (Taxon) taxonBase;
193 }
194
195 // Open a taxon editor
196 return EditorController.open(taxon);
197 }
198
199 /**
200 * Redraws an open editor if it exists for the given taxon
201 *
202 * @param taxon
203 * @return
204 */
205 public static boolean redraw(Taxon taxon){
206 // return redraw(EditorController.getNameEditor(taxon)) &&
207 // redraw(EditorController.getDescriptionEditor(taxon));
208
209 boolean returnCode = redraw(EditorController.getNameEditor(taxon)) &&
210 redraw(EditorController.getDescriptionEditor(taxon)) &&
211 redraw(EditorController.getImageEditor(taxon));
212 getActiveEditor().setFocus();
213 return returnCode;
214 }
215
216 private static boolean redraw(AbstractTaxonEditor editor) {
217 if(editor == null || editor.redraw()){
218
219 // if (getActiveEditor().setFocus())
220 // getActivePage()
221
222 // Mark editor as changed and unsaved
223 editor.setDirty();
224 return true;
225 }
226 return false;
227 }
228
229 /**
230 * @param taxon
231 * @param save
232 */
233 public static void close(Taxon taxon, boolean save) {
234 IEditorPart editor;
235 try {
236 editor = getEditorByTaxon(taxon);
237 close(editor, save);
238 } catch (PartInitException e) {
239 logger.error("Error closing taxon editor", e);
240 }
241
242 }
243
244 /**
245 * @param editor
246 * @param save
247 * @throws PartInitException
248 */
249 private static void close(IEditorPart editor, boolean save)
250 throws PartInitException {
251 if (editor != null) {
252 getActivePage().closeEditor(editor, save);
253 }
254 }
255
256 public static boolean save(Taxon taxon, boolean confirm) {
257 IEditorPart editor = null;
258 try {
259 editor = getEditorByTaxon(taxon);
260 } catch (PartInitException e) {
261 logger.error("Error saving taxon.", e);
262 }
263 if (editor != null) {
264 return GlobalController.getActivePage().saveEditor(editor, confirm);
265 }
266 return false;
267 }
268
269 public static void saveAll(){
270 setSaving(true);
271
272 // Get all open windows
273 for (IEditorPart taxonEditor : getOpenEditors()) {
274
275 // Save the dirty ones
276 if (taxonEditor.isDirty()) {
277
278
279 IEditorInput input = taxonEditor.getEditorInput();
280 if (input.getAdapter(Taxon.class) != null) {
281 Taxon taxon = (Taxon) input.getAdapter(Taxon.class);
282 CdmSessionDataRepository.getDefault().saveTaxon(taxon);
283 if (taxonEditor instanceof MultiPageTaxonEditor) {
284 ((MultiPageTaxonEditor) taxonEditor).setDirtyExtern(false);
285 }
286 }
287 }
288 }
289
290 // Commit the transaction
291 CdmTransactionController.commitTransaction();
292
293 // Force library objects to be associated with new transaction
294 CdmSessionDataRepository.getDefault().clearNonTaxonData();
295
296 // Start a new transaction
297 CdmTransactionController.startTransaction();
298
299 // Put all open taxa in the new transaction
300 CdmTransactionController.addSessionTaxaToTransaction();
301
302 setSaving(false);
303 // TODO: delete undoHistory
304 }
305
306 public static void setSaving(boolean isSaving) {
307 EditorController.isSaving = isSaving;
308 }
309
310 public static boolean isSaving() {
311 return isSaving;
312 }
313
314 public static void addNameEditor(
315 Taxon taxon, TaxonNameEditor taxonNameEditor) {
316 if (taxonNameEditors == null) {
317 taxonNameEditors = new HashMap<Taxon, TaxonNameEditor>();
318 }
319 taxonNameEditors.put(taxon, taxonNameEditor);
320 }
321
322 public static TaxonNameEditor getNameEditor(Taxon taxon) {
323 if (taxonNameEditors == null) {
324 return null;
325 }
326 return taxonNameEditors.get(taxon);
327 }
328
329 public static void addDescriptionEditor(
330 Taxon taxon, TaxonDescriptionEditor taxonDescriptionEditor) {
331 if (taxonDescriptionEditors == null) {
332 taxonDescriptionEditors = new HashMap<Taxon, TaxonDescriptionEditor>();
333 }
334 taxonDescriptionEditors.put(taxon, taxonDescriptionEditor);
335 }
336
337 public static TaxonDescriptionEditor getDescriptionEditor(Taxon taxon) {
338 if (taxonDescriptionEditors == null) {
339 return null;
340 }
341 return taxonDescriptionEditors.get(taxon);
342 }
343
344 /**
345 * Returns a set of all currently open
346 * <code>MultiPageTaxonEditor</code>s.
347 *
348 * @return
349 */
350 public static Set<IEditorPart> getOpenEditors() {
351
352 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
353
354 for (IEditorReference reference : getActivePage().getEditorReferences()) {
355 IEditorPart editor = reference.getEditor(false);
356 if (editor instanceof MultiPageTaxonEditor) {
357 taxonEditors.add(editor);
358 }
359 }
360 return taxonEditors;
361 }
362
363 /**
364 * Returns the currently active taxon editor
365 *
366 * @return the taxon editor that has focus
367 */
368 public static MultiPageTaxonEditor getActiveEditor(){
369 if(getActivePage().getActiveEditor() instanceof MultiPageTaxonEditor){
370 return (MultiPageTaxonEditor) getActivePage().getActiveEditor();
371 }
372 return null;
373 }
374
375 public static IUndoContext getUndoContext(Taxon taxon) {
376 // TODO make this taxon name editor specific
377 // return getTaxonNameEditor(taxon).getUndoContext();
378 return getUndoContext();
379 }
380
381 public static IUndoContext getUndoContext() {
382 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
383 }
384
385 /**
386 * @param taxon
387 * @return
388 */
389 public static TaxonImagesEditor getImageEditor(Taxon taxon) {
390 if (taxonImagesEditors == null) {
391 return null;
392 }
393 return taxonImagesEditors.get(taxon);
394 }
395
396 /**
397 * @param taxon
398 * @param taxonImagesEditor
399 */
400 public static void addImagesEditor(Taxon taxon,
401 TaxonImagesEditor taxonImagesEditor) {
402 if (taxonImagesEditors == null) {
403 taxonImagesEditors = new HashMap<Taxon, TaxonImagesEditor>();
404 }
405 taxonImagesEditors.put(taxon, taxonImagesEditor);
406 }
407
408
409
410
411 }