d7ca5a0d06d1916e81d7a0eaa801f7ea9874e362
[taxeditor.git] / eu.etaxonomy.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.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
32
33 /**
34 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
35 *
36 * @author n.hoffmann
37 * @created Sep 30, 2010
38 * @version 1.0
39 */
40 public class ContextManager implements IWorkbenchListener{
41
42 private ListenerList contextListeners = new ListenerList();
43
44 private IMemento memento;
45
46 /**
47 * <p>Constructor for ContextManager.</p>
48 */
49 protected ContextManager() {
50 PlatformUI.getWorkbench().addWorkbenchListener(this);
51 }
52
53 /**
54 * <p>addContextListener</p>
55 *
56 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
57 */
58 public void addContextListener(IContextListener listener){
59 contextListeners.add(listener);
60 }
61
62 /**
63 * <p>removeContextListener</p>
64 *
65 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
66 */
67 public void removeContextListener(IContextListener listener) {
68 contextListeners.remove(listener);
69 }
70
71 /**
72 * <p>notifyContextStart</p>
73 */
74 public void notifyContextStart() {
75 MessagingUtils.info("Notifying context listeners, that the context has started.");
76 ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
77
78 try {
79 dialog.run(false, false, new IRunnableWithProgress() {
80 /* (non-Javadoc)
81 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
82 */
83 @Override
84 public void run(IProgressMonitor monitor)
85 throws InvocationTargetException, InterruptedException {
86 monitor.beginTask("Starting context", contextListeners.size());
87
88
89 readMemento();
90
91 for(final Object listener : contextListeners.getListeners()){
92 ((IContextListener) listener).contextStart(memento, monitor);
93 monitor.worked(1);
94 }
95 monitor.done();
96 }
97 });
98 } catch (InvocationTargetException e) {
99 MessagingUtils.error(getClass(), e);
100 } catch (InterruptedException e) {
101 MessagingUtils.error(getClass(), e);
102 }
103 }
104
105
106 /**
107 *
108 */
109 public void notifyContextRefresh() {
110 MessagingUtils.info("Notifying context listeners, that the context needs to be refreshed.");
111 ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
112
113 try {
114 dialog.run(false, false, new IRunnableWithProgress() {
115 /* (non-Javadoc)
116 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
117 */
118 @Override
119 public void run(IProgressMonitor monitor)
120 throws InvocationTargetException, InterruptedException {
121 monitor.beginTask("Refreshing context", contextListeners.size());
122
123 for(final Object listener : contextListeners.getListeners()){
124 ((IContextListener) listener).contextRefresh(monitor);
125 monitor.worked(1);
126 }
127 monitor.done();
128 }
129 });
130 } catch (InvocationTargetException e) {
131 MessagingUtils.error(getClass(), e);
132 } catch (InterruptedException e) {
133 MessagingUtils.error(getClass(), e);
134 }
135 }
136
137 /**
138 * <p>notifyContextAboutToStop</p>
139 *
140 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
141 */
142 public void notifyContextAboutToStop(final IProgressMonitor monitor){
143
144 IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
145
146 subMonitor.beginTask("Stoping context", contextListeners.size());
147 // we are creating the memento here; even if the context is not stopped
148 createMemento();
149
150 for(final Object listener : contextListeners.getListeners()){
151 ((IContextListener) listener).contextAboutToStop(memento, subMonitor);
152 subMonitor.worked(1);
153 }
154
155 subMonitor.done();
156 }
157
158 /**
159 * <p>notifyContextStop</p>
160 *
161 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
162 */
163 public void notifyContextStop(IProgressMonitor monitor) {
164
165 IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
166
167 subMonitor.beginTask("Stoping context", contextListeners.size());
168 MessagingUtils.info("Notifying context listeners, that the context has stopped.");
169
170 for(Object listener : contextListeners.getListeners()){
171 ((IContextListener) listener).contextStop(memento, subMonitor);
172 subMonitor.worked(1);
173 }
174
175 saveMemento();
176 subMonitor.done();
177 }
178
179 /* (non-Javadoc)
180 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
181 */
182 /** {@inheritDoc} */
183 @Override
184 public boolean preShutdown(IWorkbench workbench, boolean forced) {
185
186 createMemento();
187
188 IProgressMonitor monitor = null;
189
190 for(Object listener : contextListeners.getListeners()){
191 ((IContextListener) listener).workbenchShutdown(memento, monitor);
192 }
193
194 saveMemento();
195
196 // return true in any case, otherwise the application will not stop
197 return true;
198 }
199
200 /* (non-Javadoc)
201 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
202 */
203 /** {@inheritDoc} */
204 @Override
205 public void postShutdown(IWorkbench workbench) {
206
207
208 }
209
210
211 private void readMemento(){
212 try {
213 memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase());
214 } catch (FileNotFoundException e) {
215 // no memento -> no previous state
216 MessagingUtils.info("No state file for datasource");
217 }
218 }
219
220 private void createMemento(){
221
222 if (CdmStore.getActiveCdmSource() != null) {
223
224 try {
225 String name = CdmStore.getActiveCdmSource().getName();
226 name = name.trim();
227 name = name.replace(" ", "_");
228 memento = XMLMemento.createWriteRoot(name);
229
230 MessagingUtils.info("DataSource found. Memento created.");
231 } catch (Exception e) {
232 // The memento could not be created, but a not closable editor is avoided for this case.
233 MessagingUtils.error(this.getClass(), "The memento could not be created", e);
234 }
235 } else {
236 MessagingUtils.info("Not storing state data, because no DataSource present.");
237 }
238
239 }
240
241 private boolean saveMemento(){
242 return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null;
243 }
244
245 /**
246 * <p>getStateFileForCurrentDatabase</p>
247 *
248 * @return a {@link java.io.File} object.
249 */
250 protected File getStateFileForCurrentDatabase() {
251 if(CdmStore.getActiveCdmSource() == null){
252 return null;
253 }
254
255 IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
256 if (path == null) {
257 return null;
258 }
259 path = path.append("editor_state_" + CdmStore.getActiveCdmSource().getName() + ".xml");
260 return path.toFile();
261 }
262
263 }