6e8c7373af37edde1f9719bdb2eb546712d88607
[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.UiUtil;
22 import eu.etaxonomy.taxeditor.actions.cdm.SaveTaxonAction;
23 import eu.etaxonomy.taxeditor.datasource.CdmTransactionController;
24 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
25 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
26 import eu.etaxonomy.taxeditor.model.CdmUtil;
27
28 /**
29 * Calls <code>MultiPageTaxonEditor.doSave()</code> method for all
30 * open <code>MultiPageTaxonEditor</code>s. Commits universal transaction,
31 * then opens a new one.
32 *
33 * @see IWorkbenchWindowActionDelegate
34 *
35 * @author p.ciardelli
36 * @created 07.10.2008
37 * @version 1.0
38 */
39 public class SaveAllAction implements IWorkbenchWindowActionDelegate {
40 private static final Logger logger = Logger.getLogger(SaveAllAction.class);
41
42 private IWorkbenchWindow window;
43 /**
44 * The constructor.
45 */
46 public SaveAllAction() {
47 }
48
49 /**
50 * The action has been activated. The argument of the
51 * method represents the 'real' action sitting
52 * in the workbench UI.
53 * @see IWorkbenchWindowActionDelegate#run
54 */
55 public void run(IAction action) {
56
57 UiUtil.setIsSaving(true);
58
59 // Get all open windows
60 for (IEditorPart taxonEditor : UiUtil.getOpenTaxonEditors()) {
61
62 // Save the dirty ones
63 if (taxonEditor.isDirty()) {
64 IEditorInput input = taxonEditor.getEditorInput();
65 if (input.getAdapter(Taxon.class) != null) {
66 Taxon taxon = (Taxon) input.getAdapter(Taxon.class);
67 new SaveTaxonAction(taxon).run();
68 if (taxonEditor instanceof MultiPageTaxonEditor) {
69 ((MultiPageTaxonEditor) taxonEditor).setDirtyExtern(false);
70 }
71 }
72 }
73 }
74
75 // Commit the transaction
76 CdmTransactionController.commitTransaction();
77
78 // Force library objects to be associated with new transaction
79 CdmSessionDataRepository.getDefault().clearNonTaxonData();
80
81 // Start a new transaction
82 CdmTransactionController.startTransaction();
83
84 // Put all open taxa in the new transaction
85 CdmTransactionController.addSessionTaxaToTransaction();
86
87 UiUtil.setIsSaving(false);
88 // TODO: delete undoHistory
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 }