Merge branch 'develop' of ssh://dev.e-taxonomy.eu/var/git/taxeditor into
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / ApplicationWorkbenchAdvisor.java
1 package eu.etaxonomy.taxeditor;
2
3
4 import org.eclipse.core.runtime.IStatus;
5 import org.eclipse.ui.application.IWorkbenchConfigurer;
6 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
7 import org.eclipse.ui.application.WorkbenchAdvisor;
8 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
9 import org.eclipse.ui.statushandlers.AbstractStatusHandler;
10 import org.eclipse.ui.statushandlers.StatusAdapter;
11 import org.springframework.remoting.RemoteAccessException;
12 import org.springframework.remoting.RemoteConnectFailureException;
13
14 import eu.etaxonomy.taxeditor.model.MessagingUtils;
15
16
17
18 /**
19 * <p>ApplicationWorkbenchAdvisor class.</p>
20 *
21 * @author n.hoffmann
22 * @version $Id: $
23 */
24 public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
25
26 private CdmStatusHandler cdmStatusHandler;
27
28 /*
29 * (non-Javadoc)
30 * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
31 */
32 /** {@inheritDoc} */
33 @Override
34 public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
35 IWorkbenchWindowConfigurer configurer) {
36 return new ApplicationWorkbenchWindowAdvisor(configurer);
37 }
38
39
40 /*
41 * (non-Javadoc)
42 * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
43 */
44 /**
45 * <p>getInitialWindowPerspectiveId</p>
46 *
47 * @return a {@link java.lang.String} object.
48 */
49 @Override
50 public String getInitialWindowPerspectiveId() {
51 return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
52 }
53
54 /*
55 * (non-Javadoc)
56 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
57 */
58 /** {@inheritDoc} */
59 @Override
60 public void initialize(IWorkbenchConfigurer configurer) {
61 super.initialize(configurer);
62
63 // Remembers the user's view layout, window size, window location etc.
64 // for the next time application is started
65 configurer.setSaveAndRestore(true);
66 }
67
68
69 /* (non-Javadoc)
70 * @see org.eclipse.ui.application.WorkbenchAdvisor#getWorkbenchErrorHandler()
71 */
72 @Override
73 public synchronized AbstractStatusHandler getWorkbenchErrorHandler() {
74 if (cdmStatusHandler == null) {
75 cdmStatusHandler = new CdmStatusHandler();
76 }
77 return cdmStatusHandler;
78 }
79
80
81 /**
82 * Custom status handler for handling scenarios which are
83 * not handled by the editor (e.g. runtime exceptions).
84 *
85 * The default {@link org.eclipse.ui.statushandlers.WorkbenchErrorHandler}
86 * is not used or extended because we need a handler for specific scenarios
87 * which displays a custom built error dialog.
88 *
89 * @author cmathew
90 *
91 */
92 class CdmStatusHandler extends AbstractStatusHandler {
93
94 private Throwable previousT;
95 /* (non-Javadoc)
96 * @see org.eclipse.ui.statushandlers.AbstractStatusHandler#handle(org.eclipse.ui.statushandlers.StatusAdapter, int)
97 */
98 @Override
99 public void handle(StatusAdapter statusAdapter, int style)
100 {
101
102 if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
103
104 IStatus status = statusAdapter.getStatus();
105 Throwable t = statusAdapter.getStatus().getException();
106 // NOTE : the global status handling mechanism in the case of
107 // runtime exceptions is called twice, once by the application
108 // throwing the exception and then by the rcp logging mechanism
109 // The check below is to make sure that the same exception is
110 // not shown twice in succession.
111 if(t != null && previousT == t) {
112 return;
113 }
114 previousT = t;
115 // NOTE : Currently we only allow RuntimeExceptions since
116 // allowing all kinds of exceptions would also include
117 // those in generated status objects coming from from logging triggers
118 // leading to a recursive infinite loop of :
119 // initial exception thrown -> status handling -> dialog opening + logging of status ->
120 // status handling -> dialog opening + logging of status ... and so on
121 if(t != null &&
122 t instanceof RuntimeException &&
123 ! "Widget is disposed".equals(t.getMessage()) &&
124 ! handleKnownRuntimeException(t,statusAdapter.getStatus().getPlugin())) {
125
126 MessagingUtils.errorDialog("Unexpected error",
127 null,
128 MessagingUtils.UNEXPECTED_ERROR_MESSAGE,
129 statusAdapter.getStatus().getPlugin(),
130 t,
131 true);
132
133 }
134 }
135 }
136
137 private boolean handleKnownRuntimeException(Throwable t, String pluginId) {
138 if(t instanceof RemoteConnectFailureException ||
139 t.getCause() instanceof RemoteConnectFailureException) {
140 MessagingUtils.errorDialog("Connection Failure",
141 null,
142 MessagingUtils.CONNECTION_FAILURE_MESSAGE + System.getProperty("line.separator"),
143 pluginId,
144 t,
145 true,
146 false);
147 return true;
148 }
149 if(t instanceof RemoteAccessException ||
150 t.getCause() instanceof RemoteAccessException) {
151 MessagingUtils.errorDialog("Remote Access Error",
152 null,
153 MessagingUtils.REMOTE_ACCESS_FAILURE_MESSAGE + System.getProperty("line.separator"),
154 pluginId,
155 t,
156 true,
157 false);
158 return true;
159 }
160 return false;
161 }
162 }
163
164
165
166 }