Project

General

Profile

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

    
3

    
4
import org.eclipse.core.runtime.IStatus;
5
import org.eclipse.ui.application.IWorkbenchConfigurer;
6
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
7
import org.eclipse.ui.application.WorkbenchAdvisor;
8
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
9
import org.eclipse.ui.statushandlers.AbstractStatusHandler;
10
import org.eclipse.ui.statushandlers.StatusAdapter;
11
import org.springframework.remoting.RemoteAccessException;
12
import org.springframework.remoting.RemoteConnectFailureException;
13

    
14
import eu.etaxonomy.taxeditor.model.MessagingUtils;
15
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
16

    
17

    
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
	/*
30
	 * (non-Javadoc)
31
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
32
	 */
33
	/** {@inheritDoc} */
34
	@Override
35
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
36
			IWorkbenchWindowConfigurer configurer) {
37
		return new ApplicationWorkbenchWindowAdvisor(configurer);
38
	}
39

    
40

    
41
	/*
42
	 * (non-Javadoc)
43
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
44
	 */
45
	/**
46
	 * <p>getInitialWindowPerspectiveId</p>
47
	 *
48
	 * @return a {@link java.lang.String} object.
49
	 */
50
	@Override
51
    public String getInitialWindowPerspectiveId() {
52
//	    if (PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_CHECKLIST_PERSPECTIVE)){
53
//	        return "eu.etaxonomy.taxeditor.perspective.checklistperspective";
54
//	    }
55

    
56
		return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
57
	}
58

    
59
	/*
60
	 * (non-Javadoc)
61
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
62
	 */
63
	/** {@inheritDoc} */
64
	@Override
65
    public void initialize(IWorkbenchConfigurer configurer) {
66
		super.initialize(configurer);
67

    
68
		// Remembers the user's view layout, window size, window location etc.
69
		//  for the next time application is started
70
		configurer.setSaveAndRestore(true);
71
	}
72

    
73

    
74
	/* (non-Javadoc)
75
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#getWorkbenchErrorHandler()
76
	 */
77
	@Override
78
	public synchronized AbstractStatusHandler getWorkbenchErrorHandler() {
79
	    if (cdmStatusHandler == null) {
80
	        cdmStatusHandler = new CdmStatusHandler();
81
	    }
82
	    return cdmStatusHandler;
83
	}
84

    
85

    
86
	/**
87
	 * Custom status handler for handling scenarios which are
88
	 * not handled by the editor (e.g. runtime exceptions).
89
	 *
90
	 * The default {@link org.eclipse.ui.statushandlers.WorkbenchErrorHandler}
91
	 * is not used or extended because we need a handler for specific scenarios
92
	 * which displays a custom built error dialog.
93
	 *
94
	 * @author cmathew
95
	 *
96
	 */
97
	class CdmStatusHandler extends AbstractStatusHandler {
98

    
99
	    private Throwable previousT;
100
		/* (non-Javadoc)
101
		 * @see org.eclipse.ui.statushandlers.AbstractStatusHandler#handle(org.eclipse.ui.statushandlers.StatusAdapter, int)
102
		 */
103
		@Override
104
		public void handle(StatusAdapter statusAdapter, int style)
105
		{
106

    
107
		    if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
108

    
109
		    	IStatus status = statusAdapter.getStatus();
110
		    	Throwable t = statusAdapter.getStatus().getException();
111
		    	// NOTE : the global status handling mechanism in the case of
112
		    	//        runtime exceptions is called twice, once by the application
113
		    	//        throwing the exception and then by the rcp logging mechanism
114
		    	//        The check below is to make sure that the same exception is
115
		    	//        not shown twice in succession.
116
		    	if(t != null && previousT == t) {
117
	                return;
118
	            }
119
		    	previousT = t;
120

    
121

    
122

    
123
		    	// NOTE : Currently we only allow RuntimeExceptions since
124
		    	//        allowing all kinds of exceptions would also include
125
		    	//        those in generated status objects coming from from logging triggers
126
		    	//        leading to a recursive infinite loop of :
127
		    	//        initial exception thrown -> status handling -> dialog opening + logging of status ->
128
		    	//        status handling -> dialog opening + logging of status ... and so on
129
		    	if(t != null &&
130
		    	        t instanceof RuntimeException &&
131
		    	        ! "Widget is disposed".equals(t.getMessage()) &&
132
		    	        ! handleKnownRuntimeException(t,statusAdapter.getStatus().getPlugin())) {
133

    
134
		    	    MessagingUtils.errorDialog("Unexpected error",
135
		    	            null,
136
		    	            MessagingUtils.UNEXPECTED_ERROR_MESSAGE,
137
		    	            statusAdapter.getStatus().getPlugin(),
138
		    	            t,
139
		    	            true);
140

    
141
		    	} else if (t != null && ("Widget is disposed".equals(t.getMessage()))){
142
                    MessagingUtils.warn(this.getClass(), t);
143
                    if (PreferencesUtil.isShowUpWidgetIsDisposedMessages()){
144
                        MessagingUtils.errorDialog("Widget is disposed",
145
                                null,
146
                                MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
147
                                statusAdapter.getStatus().getPlugin(),
148
                                t,
149
                                true);
150

    
151
                    }
152
                }
153
		    }
154
		}
155

    
156
		private boolean handleKnownRuntimeException(Throwable t, String pluginId) {
157
		    if(t instanceof RemoteConnectFailureException ||
158
		            t.getCause() instanceof RemoteConnectFailureException) {
159
		        MessagingUtils.errorDialog("Connection Failure",
160
		                null,
161
		                MessagingUtils.CONNECTION_FAILURE_MESSAGE + System.getProperty("line.separator"),
162
		                pluginId,
163
		                t,
164
		                true,
165
		                false);
166
		        return true;
167
		    }
168
		    if(t instanceof RemoteAccessException ||
169
		            t.getCause() instanceof RemoteAccessException) {
170
		        MessagingUtils.errorDialog("Remote Access Error",
171
		                null,
172
		                MessagingUtils.REMOTE_ACCESS_FAILURE_MESSAGE + System.getProperty("line.separator"),
173
		                pluginId,
174
		                t,
175
		                false,
176
		                true);
177
		        return true;
178
		    }
179
		    return false;
180
		}
181
	}
182

    
183

    
184

    
185
}
(3-3/7)