Project

General

Profile

Download (7.16 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor;
2

    
3

    
4
import java.io.OptionalDataException;
5

    
6
import org.apache.http.NoHttpResponseException;
7
import org.eclipse.core.runtime.IStatus;
8
import org.eclipse.ui.application.IWorkbenchConfigurer;
9
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
10
import org.eclipse.ui.application.WorkbenchAdvisor;
11
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
12
import org.eclipse.ui.statushandlers.AbstractStatusHandler;
13
import org.eclipse.ui.statushandlers.StatusAdapter;
14
import org.springframework.remoting.RemoteAccessException;
15
import org.springframework.remoting.RemoteConnectFailureException;
16

    
17
import eu.etaxonomy.cdm.database.PermissionDeniedException;
18
import eu.etaxonomy.taxeditor.model.MessagingUtils;
19
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
20
import eu.etaxonomy.taxeditor.store.CdmAuthenticationException;
21

    
22
/**
23
 * <p>ApplicationWorkbenchAdvisor class.</p>
24
 *
25
 * @author n.hoffmann
26
 */
27
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
28

    
29
	private CdmStatusHandler cdmStatusHandler;
30

    
31
	@Override
32
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
33
			IWorkbenchWindowConfigurer configurer) {
34
		return new ApplicationWorkbenchWindowAdvisor(configurer);
35
	}
36

    
37
	@Override
38
    public String getInitialWindowPerspectiveId() {
39
//	    if (PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_CHECKLIST_PERSPECTIVE)){
40
//	        return "eu.etaxonomy.taxeditor.perspective.checklistperspective";
41
//	    }
42

    
43
		return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
44
	}
45

    
46
	@Override
47
    public void initialize(IWorkbenchConfigurer configurer) {
48
		super.initialize(configurer);
49

    
50
		// Remembers the user's view layout, window size, window location etc.
51
		//  for the next time application is started
52
		configurer.setSaveAndRestore(true);
53
	}
54

    
55
	@Override
56
	public synchronized AbstractStatusHandler getWorkbenchErrorHandler() {
57
	    if (cdmStatusHandler == null) {
58
	        cdmStatusHandler = new CdmStatusHandler();
59
	    }
60
	    return cdmStatusHandler;
61
	}
62

    
63
	/**
64
	 * Custom status handler for handling scenarios which are
65
	 * not handled by the editor (e.g. runtime exceptions).
66
	 *
67
	 * The default {@link org.eclipse.ui.statushandlers.WorkbenchErrorHandler}
68
	 * is not used or extended because we need a handler for specific scenarios
69
	 * which displays a custom built error dialog.
70
	 *
71
	 * @author cmathew
72
	 */
73
	class CdmStatusHandler extends AbstractStatusHandler {
74

    
75
	    private Throwable previousT;
76

    
77
		@Override
78
		public void handle(StatusAdapter statusAdapter, int style){
79

    
80
		    if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
81

    
82
		    	Throwable t = statusAdapter.getStatus().getException();
83
		    	if (t != null){
84
    		    	// NOTE : the global status handling mechanism in the case of
85
    		    	//        runtime exceptions is called twice, once by the application
86
    		    	//        throwing the exception and then by the rcp logging mechanism.
87
    		    	//        The check below is to make sure that the same exception is
88
    		    	//        not shown twice in succession.
89
    		    	if(previousT == t) {
90
    	                return;
91
    	            }
92
    		    	previousT = t;
93

    
94
    		    	if (t.getCause() instanceof PermissionDeniedException){
95
    		    	    MessagingUtils.informationDialog("Permission denied", MessagingUtils.PERMISSION_DENIED);
96
    	            }
97
    		    	else if (t instanceof NoHttpResponseException
98
    		    	          || t.getCause() instanceof CdmAuthenticationException
99
    		    	          || (t.getMessage() != null && t.getMessage().contains("status code = 403"))){
100
    		    		t.printStackTrace();
101
                        MessagingUtils.informationDialog("Access denied", MessagingUtils.ACCESS_DENIED);
102
                    }else if (includesCause(t, OptionalDataException.class)){
103
                        MessagingUtils.informationDialog("Error (OptionalDataException)",
104
                                MessagingUtils.RESTART_EDITOR_MESSAGE
105
                                );
106
                    }else
107

    
108
    		    	// NOTE : Currently we only allow RuntimeExceptions since
109
    		    	//        allowing all kinds of exceptions would also include
110
    		    	//        those in generated status objects coming from from logging triggers
111
    		    	//        leading to a recursive infinite loop of :
112
    		    	//        initial exception thrown -> status handling -> dialog opening + logging of status ->
113
    		    	//        status handling -> dialog opening + logging of status ... and so on
114
    		    	if(t instanceof RuntimeException &&
115
    		    	        ! "Widget is disposed".equals(t.getMessage()) &&
116
    		    	        ! handleKnownRuntimeException(t,statusAdapter.getStatus().getPlugin())) {
117

    
118
    		    	    MessagingUtils.errorDialog("Error",
119
    		    	            null,
120
    		    	            MessagingUtils.UNEXPECTED_ERROR_MESSAGE,
121
    		    	            statusAdapter.getStatus().getPlugin(),
122
    		    	            t,
123
    		    	            true);
124

    
125
    		    	} else if (("Widget is disposed".equals(t.getMessage()))){
126
                        MessagingUtils.warn(this.getClass(), t);
127
                        if (PreferencesUtil.isShowUpWidgetIsDisposedMessages()){
128
                            MessagingUtils.errorDialog("Widget is disposed",
129
                                    null,
130
                                    MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
131
                                    statusAdapter.getStatus().getPlugin(),
132
                                    t,
133
                                    true);
134
                        }
135
                    }
136
		    	}
137
		    }
138
		}
139

    
140
		/**
141
		 * Analyzes whether the throwable includes an exception of type clazz
142
		 * in the stacktrace.
143
         */
144
        private <T extends Exception> boolean includesCause(Throwable t, Class<? extends Throwable> clazz) {
145
            boolean result = false;
146

    
147
            if (clazz.isAssignableFrom(t.getClass()) ){
148
                return true;
149
            }else if (t.getCause() != null && t.getCause() != t){
150
                return includesCause(t.getCause(), clazz);
151
            }
152
            return result;
153
        }
154

    
155
        private boolean handleKnownRuntimeException(Throwable t, String pluginId) {
156
		    if(t instanceof RemoteConnectFailureException ||
157
		            t.getCause() instanceof RemoteConnectFailureException) {
158
		        MessagingUtils.errorDialog("Connection Failure",
159
		                null,
160
		                MessagingUtils.CONNECTION_FAILURE_MESSAGE + System.getProperty("line.separator"),
161
		                pluginId,
162
		                t,
163
		                true,
164
		                false);
165
		        return true;
166
		    }
167
		    if(t instanceof RemoteAccessException ||
168
		            t.getCause() instanceof RemoteAccessException ) {
169
		        MessagingUtils.errorDialog("Remote Access Error",
170
		                null,
171
		                MessagingUtils.REMOTE_ACCESS_FAILURE_MESSAGE + System.getProperty("line.separator"),
172
		                pluginId,
173
		                t,
174
		                false,
175
		                true);
176
		        return true;
177
		    }
178
		    if (t instanceof CdmAuthenticationException){
179
		        MessagingUtils.info("You are logged in now but you are not permitted to use the TaxEditor with the selected data source");
180
		    }
181
		    return false;
182
		}
183
	}
184
}
(3-3/7)