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