Version 1.01
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / SaveAllAction.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.actions;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IWorkbenchWindow;
18 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
19
20 import eu.etaxonomy.cdm.model.taxon.Taxon;
21 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
22 import eu.etaxonomy.taxeditor.UiUtil;
23 import eu.etaxonomy.taxeditor.actions.cdm.SaveTaxonAction;
24 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
25 import eu.etaxonomy.taxeditor.model.CdmUtil;
26
27 /**
28 * Calls <code>MultiPageTaxonEditor.doSave()</code> method for all
29 * open <code>MultiPageTaxonEditor</code>s. Commits universal transaction,
30 * then opens a new one.
31 *
32 * @see IWorkbenchWindowActionDelegate
33 *
34 * @author p.ciardelli
35 * @created 07.10.2008
36 * @version 1.0
37 */
38 public class SaveAllAction implements IWorkbenchWindowActionDelegate {
39 private static final Logger logger = Logger.getLogger(SaveAllAction.class);
40
41 private IWorkbenchWindow window;
42 /**
43 * The constructor.
44 */
45 public SaveAllAction() {
46 }
47
48 /**
49 * The action has been activated. The argument of the
50 * method represents the 'real' action sitting
51 * in the workbench UI.
52 * @see IWorkbenchWindowActionDelegate#run
53 */
54 public void run(IAction action) {
55
56 UiUtil.setIsSaving(true);
57
58 // Get all open windows
59 for (IEditorPart taxonEditor : UiUtil.getOpenTaxonEditors()) {
60
61 // Save the dirty ones
62 if (taxonEditor.isDirty()) {
63 IEditorInput input = taxonEditor.getEditorInput();
64 if (input.getAdapter(Taxon.class) != null) {
65 Taxon taxon = (Taxon) input.getAdapter(Taxon.class);
66 new SaveTaxonAction(taxon).run();
67 if (taxonEditor instanceof MultiPageTaxonEditor) {
68 ((MultiPageTaxonEditor) taxonEditor).setDirtyExtern(false);
69 }
70 }
71 }
72 }
73
74 // Commit the transaction
75 TaxEditorPlugin.getDefault().commitTransaction();
76
77 // Force library objects to be associated with new transaction
78 CdmUtil.clearLibraryObjects();
79
80 // Start a new transaction
81 TaxEditorPlugin.getDefault().startTransaction();
82
83 // Put all open taxa in the new transaction
84 for (Object taxon : TaxEditorPlugin.getDefault().getObservableSessionTaxa()) {
85 CdmUtil.getTaxonService().saveTaxon((Taxon) taxon);
86 }
87
88 UiUtil.setIsSaving(false);
89 }
90
91 /**
92 * Selection in the workbench has been changed. We
93 * can change the state of the 'real' action here
94 * if we want, but this can only happen after
95 * the delegate has been created.
96 * @see IWorkbenchWindowActionDelegate#selectionChanged
97 */
98 public void selectionChanged(IAction action, ISelection selection) {
99 }
100
101 /**
102 * We can use this method to dispose of any system
103 * resources we previously allocated.
104 * @see IWorkbenchWindowActionDelegate#dispose
105 */
106 public void dispose() {
107 }
108
109 /**
110 * We will cache window object in order to
111 * be able to provide parent shell for the message dialog.
112 * @see IWorkbenchWindowActionDelegate#init
113 */
114 public void init(IWorkbenchWindow window) {
115 this.window = window;
116 }
117 }