Project

General

Profile

Download (7.02 KB) Statistics
| Branch: | Tag: | Revision:
1 e8409423 n.hoffmann
package eu.etaxonomy.taxeditor;
2
3 41e2f693 Cherian Mathew
4 4f18c04e Katja Luther
import org.apache.http.NoHttpResponseException;
5 41e2f693 Cherian Mathew
import org.eclipse.core.runtime.IStatus;
6 e8409423 n.hoffmann
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 41e2f693 Cherian Mathew
import org.eclipse.ui.statushandlers.AbstractStatusHandler;
11
import org.eclipse.ui.statushandlers.StatusAdapter;
12 65e61086 Cherian Mathew
import org.springframework.remoting.RemoteAccessException;
13
import org.springframework.remoting.RemoteConnectFailureException;
14 41e2f693 Cherian Mathew
15
import eu.etaxonomy.taxeditor.model.MessagingUtils;
16 8c1b17fc Katja Luther
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
17 4f18c04e Katja Luther
import eu.etaxonomy.taxeditor.store.CdmAuthenticationException;
18 41e2f693 Cherian Mathew
19 e8409423 n.hoffmann
20 ba38418d n.hoffmann
21 3be6ef3e n.hoffmann
/**
22
 * <p>ApplicationWorkbenchAdvisor class.</p>
23
 *
24
 * @author n.hoffmann
25
 * @version $Id: $
26
 */
27 e8409423 n.hoffmann
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
28
29 41e2f693 Cherian Mathew
	private CdmStatusHandler cdmStatusHandler;
30 65e61086 Cherian Mathew
31 e380ba33 n.hoffmann
	/*
32
	 * (non-Javadoc)
33
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
34
	 */
35 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
36 49c6e8c3 Cherian Mathew
	@Override
37
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
38 e8409423 n.hoffmann
			IWorkbenchWindowConfigurer configurer) {
39
		return new ApplicationWorkbenchWindowAdvisor(configurer);
40
	}
41
42 49c6e8c3 Cherian Mathew
43 e380ba33 n.hoffmann
	/*
44
	 * (non-Javadoc)
45
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
46
	 */
47 3be6ef3e n.hoffmann
	/**
48
	 * <p>getInitialWindowPerspectiveId</p>
49
	 *
50
	 * @return a {@link java.lang.String} object.
51
	 */
52 49c6e8c3 Cherian Mathew
	@Override
53
    public String getInitialWindowPerspectiveId() {
54 69e82edd Katja Luther
//	    if (PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_CHECKLIST_PERSPECTIVE)){
55
//	        return "eu.etaxonomy.taxeditor.perspective.checklistperspective";
56
//	    }
57 cc4e78d1 Katja Luther
58 ba38418d n.hoffmann
		return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
59 e8409423 n.hoffmann
	}
60 49c6e8c3 Cherian Mathew
61 e380ba33 n.hoffmann
	/*
62
	 * (non-Javadoc)
63
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
64
	 */
65 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
66 49c6e8c3 Cherian Mathew
	@Override
67
    public void initialize(IWorkbenchConfigurer configurer) {
68 e8409423 n.hoffmann
		super.initialize(configurer);
69 49c6e8c3 Cherian Mathew
70 e8409423 n.hoffmann
		// Remembers the user's view layout, window size, window location etc.
71
		//  for the next time application is started
72 d0379f92 p.ciardelli
		configurer.setSaveAndRestore(true);
73 f9dc4154 n.hoffmann
	}
74 49c6e8c3 Cherian Mathew
75
76 41e2f693 Cherian Mathew
	/* (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 49c6e8c3 Cherian Mathew
87 41e2f693 Cherian Mathew
88
	/**
89 49c6e8c3 Cherian Mathew
	 * Custom status handler for handling scenarios which are
90 35a95f17 Cherian Mathew
	 * not handled by the editor (e.g. runtime exceptions).
91 49c6e8c3 Cherian Mathew
	 *
92 35a95f17 Cherian Mathew
	 * 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 49c6e8c3 Cherian Mathew
	 *
96 41e2f693 Cherian Mathew
	 * @author cmathew
97
	 *
98
	 */
99
	class CdmStatusHandler extends AbstractStatusHandler {
100 49c6e8c3 Cherian Mathew
101 65e61086 Cherian Mathew
	    private Throwable previousT;
102 41e2f693 Cherian Mathew
		/* (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 65e61086 Cherian Mathew
109 41e2f693 Cherian Mathew
		    if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
110 49c6e8c3 Cherian Mathew
111
		    	IStatus status = statusAdapter.getStatus();
112
		    	Throwable t = statusAdapter.getStatus().getException();
113 65e61086 Cherian Mathew
		    	// 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 8c1b17fc Katja Luther
123 4f18c04e Katja Luther
                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 8c1b17fc Katja Luther
131 4f18c04e Katja Luther
                }else
132 8c1b17fc Katja Luther
133 41e2f693 Cherian Mathew
		    	// NOTE : Currently we only allow RuntimeExceptions since
134 49c6e8c3 Cherian Mathew
		    	//        allowing all kinds of exceptions would also include
135
		    	//        those in generated status objects coming from from logging triggers
136 41e2f693 Cherian Mathew
		    	//        leading to a recursive infinite loop of :
137 49c6e8c3 Cherian Mathew
		    	//        initial exception thrown -> status handling -> dialog opening + logging of status ->
138 41e2f693 Cherian Mathew
		    	//        status handling -> dialog opening + logging of status ... and so on
139 65e61086 Cherian Mathew
		    	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 8c1b17fc Katja Luther
		    	} else if (t != null && ("Widget is disposed".equals(t.getMessage()))){
152 9d0bce28 Katja Luther
                    MessagingUtils.warn(this.getClass(), t);
153 d3d01e43 Katja Luther
                    if (PreferencesUtil.isShowUpWidgetIsDisposedMessages()){
154 9d0bce28 Katja Luther
                        MessagingUtils.errorDialog("Widget is disposed",
155
                                null,
156
                                MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
157
                                statusAdapter.getStatus().getPlugin(),
158
                                t,
159
                                true);
160
161 8c1b17fc Katja Luther
                    }
162 0704865a Katja Luther
                }
163 65e61086 Cherian Mathew
		    }
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 4f18c04e Katja Luther
		            t.getCause() instanceof RemoteAccessException ) {
180 65e61086 Cherian Mathew
		        MessagingUtils.errorDialog("Remote Access Error",
181
		                null,
182
		                MessagingUtils.REMOTE_ACCESS_FAILURE_MESSAGE + System.getProperty("line.separator"),
183
		                pluginId,
184
		                t,
185 48621fdd Katja Luther
		                false,
186
		                true);
187 65e61086 Cherian Mathew
		        return true;
188 41e2f693 Cherian Mathew
		    }
189 4f18c04e Katja Luther
		    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 65e61086 Cherian Mathew
		    return false;
193 41e2f693 Cherian Mathew
		}
194
	}
195 d0379f92 p.ciardelli
196 49c6e8c3 Cherian Mathew
197
198 e8409423 n.hoffmann
}