fixes #673, #2169, #2186
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / ContextManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.store;
12
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.lang.reflect.InvocationTargetException;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.ListenerList;
20 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchListener;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.XMLMemento;
27
28 import eu.etaxonomy.taxeditor.model.IContextListener;
29 import eu.etaxonomy.taxeditor.model.MementoHelper;
30 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
31
32 /**
33 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
34 *
35 * @author n.hoffmann
36 * @created Sep 30, 2010
37 * @version 1.0
38 */
39 public class ContextManager implements IWorkbenchListener{
40
41 private ListenerList contextListeners = new ListenerList();
42
43 private IMemento memento;
44
45 /**
46 * <p>Constructor for ContextManager.</p>
47 */
48 protected ContextManager() {
49 PlatformUI.getWorkbench().addWorkbenchListener(this);
50 }
51
52 /**
53 * <p>addContextListener</p>
54 *
55 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
56 */
57 public void addContextListener(IContextListener listener){
58 contextListeners.add(listener);
59 }
60
61 /**
62 * <p>removeContextListener</p>
63 *
64 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
65 */
66 public void removeContextListener(IContextListener listener) {
67 contextListeners.remove(listener);
68 }
69
70 /**
71 * <p>notifyContextStart</p>
72 */
73 public void notifyContextStart() {
74 StoreUtil.info("Notifying context listeners, that the context has started.");
75 ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
76
77 try {
78 dialog.run(false, false, new IRunnableWithProgress() {
79 /* (non-Javadoc)
80 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
81 */
82 @Override
83 public void run(IProgressMonitor monitor)
84 throws InvocationTargetException, InterruptedException {
85 monitor.beginTask("Starting context", contextListeners.size());
86
87
88 readMemento();
89
90 for(final Object listener : contextListeners.getListeners()){
91 ((IContextListener) listener).contextStart(memento, monitor);
92 monitor.worked(1);
93 }
94 monitor.done();
95 }
96 });
97 } catch (InvocationTargetException e) {
98 StoreUtil.error(getClass(), e);
99 } catch (InterruptedException e) {
100 StoreUtil.error(getClass(), e);
101 }
102 }
103
104 /**
105 * <p>notifyContextAboutToStop</p>
106 *
107 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
108 */
109 public void notifyContextAboutToStop(final IProgressMonitor monitor){
110
111 IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
112
113 subMonitor.beginTask("Stoping context", contextListeners.size());
114 // we are creating the memento here; even if the context is not stopped
115 createMemento();
116
117 for(final Object listener : contextListeners.getListeners()){
118 ((IContextListener) listener).contextAboutToStop(memento, subMonitor);
119 subMonitor.worked(1);
120 }
121
122 subMonitor.done();
123 }
124
125 /**
126 * <p>notifyContextStop</p>
127 *
128 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
129 */
130 public void notifyContextStop(IProgressMonitor monitor) {
131
132 IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
133
134 subMonitor.beginTask("Stoping context", contextListeners.size());
135 StoreUtil.info("Notifying context listeners, that the context has stopped.");
136
137 for(Object listener : contextListeners.getListeners()){
138 ((IContextListener) listener).contextStop(memento, subMonitor);
139 subMonitor.worked(1);
140 }
141
142 saveMemento();
143 subMonitor.done();
144 }
145
146 /* (non-Javadoc)
147 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
148 */
149 /** {@inheritDoc} */
150 @Override
151 public boolean preShutdown(IWorkbench workbench, boolean forced) {
152
153 createMemento();
154
155 IProgressMonitor monitor = null;
156
157 for(Object listener : contextListeners.getListeners()){
158 ((IContextListener) listener).workbenchShutdown(memento, monitor);
159 }
160
161 saveMemento();
162
163 // return true in any case, otherwise the application will not stop
164 return true;
165 }
166
167 /* (non-Javadoc)
168 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
169 */
170 /** {@inheritDoc} */
171 @Override
172 public void postShutdown(IWorkbench workbench) {
173
174
175 }
176
177
178 private void readMemento(){
179 try {
180 memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase());
181 } catch (FileNotFoundException e) {
182 // no memento -> no previous state
183 StoreUtil.info("No state file for datasource");
184 }
185 }
186
187 private void createMemento(){
188
189 if (CdmStore.getDataSource() != null) {
190
191 try {
192 String name = CdmStore.getDataSource().getName();
193 name = name.trim();
194 name = name.replace(" ", "_");
195 memento = XMLMemento.createWriteRoot(name);
196
197 StoreUtil.info("DataSource found. Memento created.");
198 } catch (Exception e) {
199 // The memento could not be created, but a not closable editor is avoided for this case.
200 StoreUtil.error(this.getClass(), "The memento could not be created", e);
201 }
202 } else {
203 StoreUtil.info("Not storing state data, because no DataSource present.");
204 }
205
206 }
207
208 private boolean saveMemento(){
209 return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null;
210 }
211
212 /**
213 * <p>getStateFileForCurrentDatabase</p>
214 *
215 * @return a {@link java.io.File} object.
216 */
217 protected File getStateFileForCurrentDatabase() {
218 if(CdmStore.getDataSource() == null){
219 return null;
220 }
221
222 IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
223 if (path == null) {
224 return null;
225 }
226 path = path.append("editor_state_" + CdmStore.getDataSource().getName() + ".xml");
227 return path.toFile();
228 }
229 }