Project

General

Profile

Download (6.41 KB) Statistics
| Branch: | Tag: | Revision:
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.cdm.database.PermissionDeniedException;
16
import eu.etaxonomy.taxeditor.model.MessagingUtils;
17
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
18
import eu.etaxonomy.taxeditor.store.CdmAuthenticationException;
19

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

    
27
	private CdmStatusHandler cdmStatusHandler;
28

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

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

    
41
		return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
42
	}
43

    
44
	@Override
45
    public void initialize(IWorkbenchConfigurer configurer) {
46
		super.initialize(configurer);
47

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

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

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

    
73
	    private Throwable previousT;
74

    
75
		@Override
76
		public void handle(StatusAdapter statusAdapter, int style){
77

    
78
		    if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
79

    
80
		    	Throwable t = statusAdapter.getStatus().getException();
81
		    	// NOTE : the global status handling mechanism in the case of
82
		    	//        runtime exceptions is called twice, once by the application
83
		    	//        throwing the exception and then by the rcp logging mechanism
84
		    	//        The check below is to make sure that the same exception is
85
		    	//        not shown twice in succession.
86
		    	if(t != null && previousT == t) {
87
	                return;
88
	            }
89
		    	previousT = t;
90
		    	if (t != null && t.getCause() instanceof PermissionDeniedException){
91
		    	    MessagingUtils.informationDialog("Permission denied", MessagingUtils.PERMISSION_DENIED);
92
	            }
93
		    	else if (t != null &&
94
		    	        ( t instanceof NoHttpResponseException
95
		    	          || t.getCause() instanceof CdmAuthenticationException
96
		    	          || (t.getMessage() != null && t.getMessage().contains("status code = 403")))){
97
                    MessagingUtils.informationDialog("Access denied", MessagingUtils.ACCESS_DENIED);
98
                }else
99

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

    
111
		    	    MessagingUtils.errorDialog("Error",
112
		    	            null,
113
		    	            MessagingUtils.UNEXPECTED_ERROR_MESSAGE,
114
		    	            statusAdapter.getStatus().getPlugin(),
115
		    	            t,
116
		    	            true);
117

    
118
		    	} else if (t != null && ("Widget is disposed".equals(t.getMessage()))){
119
                    MessagingUtils.warn(this.getClass(), t);
120
                    if (PreferencesUtil.isShowUpWidgetIsDisposedMessages()){
121
                        MessagingUtils.errorDialog("Widget is disposed",
122
                                null,
123
                                MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
124
                                statusAdapter.getStatus().getPlugin(),
125
                                t,
126
                                true);
127
                    }
128
                }else if (t != null && t.getStackTrace().toString().contains("java.io.OptionalDataException")){
129
                    MessagingUtils.warn(this.getClass(), MessagingUtils.RESTART_EDITOR_MESSAGE);
130
                }
131
		    }
132
		}
133

    
134
		private boolean handleKnownRuntimeException(Throwable t, String pluginId) {
135
		    if(t instanceof RemoteConnectFailureException ||
136
		            t.getCause() instanceof RemoteConnectFailureException) {
137
		        MessagingUtils.errorDialog("Connection Failure",
138
		                null,
139
		                MessagingUtils.CONNECTION_FAILURE_MESSAGE + System.getProperty("line.separator"),
140
		                pluginId,
141
		                t,
142
		                true,
143
		                false);
144
		        return true;
145
		    }
146
		    if(t instanceof RemoteAccessException ||
147
		            t.getCause() instanceof RemoteAccessException ) {
148
		        MessagingUtils.errorDialog("Remote Access Error",
149
		                null,
150
		                MessagingUtils.REMOTE_ACCESS_FAILURE_MESSAGE + System.getProperty("line.separator"),
151
		                pluginId,
152
		                t,
153
		                false,
154
		                true);
155
		        return true;
156
		    }
157
		    if (t instanceof CdmAuthenticationException){
158
		        MessagingUtils.info("You are logged in now but you are not permitted to use the TaxEditor with the selected data source");
159
		    }
160
		    return false;
161
		}
162
	}
163
}
(3-3/7)