Project

General

Profile

Download (6.11 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.taxeditor.model.MessagingUtils;
16
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
17
import eu.etaxonomy.taxeditor.store.CdmAuthenticationException;
18

    
19
/**
20
 * <p>ApplicationWorkbenchAdvisor class.</p>
21
 *
22
 * @author n.hoffmann
23
 * @version $Id: $
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
	/**
36
	 * <p>getInitialWindowPerspectiveId</p>
37
	 *
38
	 * @return a {@link java.lang.String} object.
39
	 */
40
	@Override
41
    public String getInitialWindowPerspectiveId() {
42
//	    if (PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_CHECKLIST_PERSPECTIVE)){
43
//	        return "eu.etaxonomy.taxeditor.perspective.checklistperspective";
44
//	    }
45

    
46
		return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
47
	}
48

    
49
	@Override
50
    public void initialize(IWorkbenchConfigurer configurer) {
51
		super.initialize(configurer);
52

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

    
58
	@Override
59
	public synchronized AbstractStatusHandler getWorkbenchErrorHandler() {
60
	    if (cdmStatusHandler == null) {
61
	        cdmStatusHandler = new CdmStatusHandler();
62
	    }
63
	    return cdmStatusHandler;
64
	}
65

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

    
78
	    private Throwable previousT;
79

    
80
		@Override
81
		public void handle(StatusAdapter statusAdapter, int style)
82
		{
83

    
84
		    if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
85

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

    
98
                if (t != null && ( t instanceof NoHttpResponseException || (t.getCause() != null && t.getCause() instanceof CdmAuthenticationException) || (t.getMessage() != null && t.getMessage().contains("status code = 403")))){
99
                    MessagingUtils.informationDialog("Access denied", MessagingUtils.ACCESS_DENIED);
100

    
101
                }else
102

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

    
114
		    	    MessagingUtils.errorDialog("Error",
115
		    	            null,
116
		    	            MessagingUtils.UNEXPECTED_ERROR_MESSAGE,
117
		    	            statusAdapter.getStatus().getPlugin(),
118
		    	            t,
119
		    	            true);
120

    
121
		    	} else if (t != null && ("Widget is disposed".equals(t.getMessage()))){
122
                    MessagingUtils.warn(this.getClass(), t);
123
                    if (PreferencesUtil.isShowUpWidgetIsDisposedMessages()){
124
                        MessagingUtils.errorDialog("Widget is disposed",
125
                                null,
126
                                MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
127
                                statusAdapter.getStatus().getPlugin(),
128
                                t,
129
                                true);
130

    
131
                    }
132
                }
133
		    }
134
		}
135

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