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