Massive refactoring of the methodology in former class UiUtils
[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.Taxon;
28 import eu.etaxonomy.taxeditor.datasource.CdmTransactionController;
29 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
30 import eu.etaxonomy.taxeditor.editor.name.CdmParserController;
31 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
32 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
33 import eu.etaxonomy.taxeditor.model.NameEditorInput;
34 import eu.etaxonomy.taxeditor.navigation.RecentNamesView;
35 import eu.etaxonomy.taxeditor.navigation.TaxonomicTreeViewer;
36
37 /**
38 * @author n.hoffmann
39 * @created 20.01.2009
40 * @version 1.0
41 */
42 public class EditorController {
43 private static final Logger logger = Logger.getLogger(EditorController.class);
44
45
46 private static Map<Taxon, TaxonNameEditor> taxonNameEditors;
47
48
49 private static boolean isSaving = false;
50
51 private static IWorkbenchPage getActivePage(){
52 return GlobalController.getActivePage();
53 }
54
55 /**
56 * @param input
57 * @return
58 * @throws PartInitException
59 */
60 private static IEditorPart getEditorByInput(IEditorInput input)
61 throws PartInitException {
62 for (IEditorReference reference : getActivePage().getEditorReferences()) {
63 if (reference.getEditorInput().equals(input)) {
64 IEditorPart editor = reference.getEditor(false);
65 return editor;
66 }
67 }
68 return null;
69 }
70
71 public static boolean closeAll() {
72 for (IEditorPart editor : getOpenEditors()) {
73 if (!getActivePage().closeEditor(editor, true)) {
74 return false;
75 }
76 }
77 return true;
78 }
79
80 public static IEditorPart getEditorByTaxon(Taxon taxon)
81 throws PartInitException {
82 IEditorInput input = new NameEditorInput(taxon);
83 return getEditorByInput(input);
84 }
85
86 /**
87 * @param input
88 * @param editorId
89 * @return
90 * @throws PartInitException
91 */
92 private static IEditorPart open(IEditorInput input, String editorId)
93 throws PartInitException {
94 return getActivePage().openEditor(input, editorId);
95 }
96
97 /**
98 * Open an emtpy taxon editor
99 *
100 * @return
101 * @throws PartInitException
102 */
103 public static IEditorPart open()
104 throws PartInitException{
105 return open(null);
106 }
107
108 /**
109 * Open a taxon editor for the given taxon
110 *
111 * @param taxon
112 * @return
113 * @throws PartInitException
114 */
115 public static IEditorPart open(Taxon taxon){
116 if(taxon == null){
117 TaxonNameBase name = CdmParserController.parseFullReference("", null, null);
118 name.setFullTitleCache("", false);
119 name.setTitleCache("", false);
120 taxon = Taxon.NewInstance(name,
121 CdmSessionDataRepository.getDefault().getDefaultSec());
122 }else{
123 // If this taxon is not visible in the tree, open node
124 TaxonomicTreeViewer treeViewer = TreeController.getTreeViewer();
125 if (treeViewer != null) {
126 treeViewer.revealTaxon(taxon);
127 }
128
129 // Add to recent names list
130 RecentNamesView.addRecentName(taxon);
131 }
132
133 IEditorInput input = new NameEditorInput(taxon);
134
135 try {
136 return open(input, MultiPageTaxonEditor.ID);
137 } catch (PartInitException e) {
138 logger.error("Error opening editor.", e);
139 }
140 return null;
141 }
142
143 /**
144 * Redraws an open editor if it exists for the given taxon
145 *
146 * @param taxon
147 * @return
148 */
149 public static boolean redraw(Taxon taxon){
150
151 TaxonNameEditor editor = EditorController.getEditor(taxon);
152
153 if(editor == null || editor.redraw()){
154
155 // Mark editor as changed and unsaved
156 editor.setDirty();
157
158 return true;
159 }
160 return false;
161 }
162
163 /**
164 * @param taxon
165 * @param save
166 */
167 public static void close(Taxon taxon, boolean save) {
168 IEditorPart editor;
169 try {
170 editor = getEditorByTaxon(taxon);
171 close(editor, save);
172 } catch (PartInitException e) {
173 logger.error("Error closing taxon editor", e);
174 }
175
176 }
177
178 /**
179 * @param editor
180 * @param save
181 * @throws PartInitException
182 */
183 private static void close(IEditorPart editor, boolean save)
184 throws PartInitException {
185 if (editor != null) {
186 getActivePage().closeEditor(editor, save);
187 }
188 }
189
190 public static void save(Taxon taxon, boolean confirm) {
191 IEditorPart editor = null;
192 try {
193 editor = getEditorByTaxon(taxon);
194 } catch (PartInitException e) {
195 // TODO Auto-generated catch block
196 e.printStackTrace();
197 }
198 if (editor != null) {
199 GlobalController.getActivePage().saveEditor(editor, confirm);
200 }
201 }
202
203 public static void saveAll(){
204 setSaving(true);
205
206 // Get all open windows
207 for (IEditorPart taxonEditor : getOpenEditors()) {
208
209 // Save the dirty ones
210 if (taxonEditor.isDirty()) {
211
212
213 IEditorInput input = taxonEditor.getEditorInput();
214 if (input.getAdapter(Taxon.class) != null) {
215 Taxon taxon = (Taxon) input.getAdapter(Taxon.class);
216 CdmSessionDataRepository.getDefault().saveTaxon(taxon);
217 if (taxonEditor instanceof MultiPageTaxonEditor) {
218 ((MultiPageTaxonEditor) taxonEditor).setDirtyExtern(false);
219 }
220 }
221 }
222 }
223
224 // Commit the transaction
225 CdmTransactionController.commitTransaction();
226
227 // Force library objects to be associated with new transaction
228 CdmSessionDataRepository.getDefault().clearNonTaxonData();
229
230 // Start a new transaction
231 CdmTransactionController.startTransaction();
232
233 // Put all open taxa in the new transaction
234 CdmTransactionController.addSessionTaxaToTransaction();
235
236 setSaving(false);
237 // TODO: delete undoHistory
238 }
239
240 public static void setSaving(boolean isSaving) {
241 EditorController.isSaving = isSaving;
242 }
243
244 public static boolean isSaving() {
245 return isSaving;
246 }
247
248 public static void addEditor(
249 Taxon taxon, TaxonNameEditor taxonNameEditor) {
250 if (taxonNameEditors == null) {
251 taxonNameEditors = new HashMap<Taxon, TaxonNameEditor>();
252 }
253 taxonNameEditors.put(taxon, taxonNameEditor);
254 }
255
256 public static TaxonNameEditor getEditor(Taxon taxon) {
257 if (taxonNameEditors == null) {
258 return null;
259 }
260 return taxonNameEditors.get(taxon);
261 }
262
263 /**
264 * Returns a set of all currently open
265 * <code>MultiPageTaxonEditor</code>s.
266 *
267 * @return
268 */
269 public static Set<IEditorPart> getOpenEditors() {
270
271 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
272
273 for (IEditorReference reference : getActivePage().getEditorReferences()) {
274 IEditorPart editor = reference.getEditor(false);
275 if (editor instanceof MultiPageTaxonEditor) {
276 taxonEditors.add(editor);
277 }
278 }
279 return taxonEditors;
280 }
281
282 public static IUndoContext getUndoContext(Taxon taxon) {
283 // TODO make this taxon name editor specific
284 // return getTaxonNameEditor(taxon).getUndoContext();
285 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
286 }
287
288
289
290
291 }