Project

General

Profile

Download (7.06 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
                        MessagingUtils.informationDialog("Access denied", MessagingUtils.ACCESS_DENIED);
101
                    }else if (includesCause(t, OptionalDataException.class)){
102
                        MessagingUtils.informationDialog("Error (OptionalDataException)",
103
                                MessagingUtils.RESTART_EDITOR_MESSAGE
104
                                );
105
                    }else
106

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

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

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

    
139
		/**
140
		 * analyzes whether the
141
         */
142
        private <T extends Exception> boolean includesCause(Throwable t, Class<? extends Throwable> clazz) {
143
            boolean result = false;
144

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

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