cb759ae747aee5b97b7471cbc05cc2a9a151d05d
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / ApplicationWorkbenchAdvisor.java
1 package eu.etaxonomy.taxeditor;
2
3
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.rmi.activation.Activator;
7
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.jface.dialogs.ErrorDialog;
11 import org.eclipse.swt.widgets.Shell;
12 import org.eclipse.ui.application.IWorkbenchConfigurer;
13 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
14 import org.eclipse.ui.application.WorkbenchAdvisor;
15 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
16 import org.eclipse.ui.statushandlers.AbstractStatusHandler;
17 import org.eclipse.ui.statushandlers.StatusAdapter;
18 import org.eclipse.ui.statushandlers.StatusManager;
19
20 import eu.etaxonomy.taxeditor.model.MessagingUtils;
21
22
23
24 /**
25 * <p>ApplicationWorkbenchAdvisor class.</p>
26 *
27 * @author n.hoffmann
28 * @version $Id: $
29 */
30 public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
31
32 private CdmStatusHandler cdmStatusHandler;
33 /*
34 * (non-Javadoc)
35 * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
36 */
37 /** {@inheritDoc} */
38 public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
39 IWorkbenchWindowConfigurer configurer) {
40 return new ApplicationWorkbenchWindowAdvisor(configurer);
41 }
42
43
44 /*
45 * (non-Javadoc)
46 * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
47 */
48 /**
49 * <p>getInitialWindowPerspectiveId</p>
50 *
51 * @return a {@link java.lang.String} object.
52 */
53 public String getInitialWindowPerspectiveId() {
54 return "eu.etaxonomy.taxeditor.application.perspective.taxonomic";
55 }
56
57 /*
58 * (non-Javadoc)
59 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
60 */
61 /** {@inheritDoc} */
62 public void initialize(IWorkbenchConfigurer configurer) {
63 super.initialize(configurer);
64
65 // Remembers the user's view layout, window size, window location etc.
66 // for the next time application is started
67 configurer.setSaveAndRestore(true);
68 }
69
70 /* (non-Javadoc)
71 * @see org.eclipse.ui.application.WorkbenchAdvisor#preStartup()
72 */
73 /** {@inheritDoc} */
74 @Override
75 public void preStartup() {
76 // TODO Auto-generated method stub
77 super.preStartup();
78 // XXX check for updates before starting up.
79 // If an update is performed, restart.
80 // if (P2Util.checkForUpdates())
81 // PlatformUI.getWorkbench().restart();
82 }
83
84 /**
85 * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=234252
86 */
87 // public void initialize(IWorkbenchConfigurer configurer) {
88 //
89 // WorkbenchAdapterBuilder.registerAdapters();
90 //
91 // final String ICONS_PATH = "icons/full/";
92 // final String PATH_OBJECT = ICONS_PATH + "obj16/";
93 // Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH);
94 // declareWorkbenchImage(configurer, ideBundle,
95 // IDE.SharedImages.IMG_OBJ_PROJECT, PATH_OBJECT + "prj_obj.gif",
96 // true);
97 // declareWorkbenchImage(configurer, ideBundle,
98 // IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED, PATH_OBJECT
99 // + "cprj_obj.gif", true);
100 //
101 // }
102 //
103 // private void declareWorkbenchImage(IWorkbenchConfigurer configurer_p,
104 // Bundle ideBundle, String symbolicName, String path, boolean shared) {
105 // URL url = ideBundle.getEntry(path);
106 // ImageDescriptor desc = ImageDescriptor.createFromURL(url);
107 // configurer_p.declareImage(symbolicName, desc, shared);
108 // }
109 //
110 // public IAdaptable getDefaultPageInput() {
111 // IWorkspace workspace = ResourcesPlugin.getWorkspace();
112 // return workspace.getRoot();
113 // }
114
115 // @Override
116 // public void eventLoopException(Throwable exception) {
117 // super.eventLoopException(exception);
118 // StoreUtil.messageDialog("Unexpected error", null, exception.getMessage(), exception);
119 // }
120
121
122 /* (non-Javadoc)
123 * @see org.eclipse.ui.application.WorkbenchAdvisor#getWorkbenchErrorHandler()
124 */
125 @Override
126 public synchronized AbstractStatusHandler getWorkbenchErrorHandler() {
127 if (cdmStatusHandler == null) {
128 cdmStatusHandler = new CdmStatusHandler();
129 }
130 return cdmStatusHandler;
131 }
132
133
134 /**
135 * Custom status handler for handling scenarios which are
136 * not handled by the editor (e.g. runtime exceptions)
137 *
138 * @author cmathew
139 *
140 */
141 class CdmStatusHandler extends AbstractStatusHandler {
142
143 /* (non-Javadoc)
144 * @see org.eclipse.ui.statushandlers.AbstractStatusHandler#handle(org.eclipse.ui.statushandlers.StatusAdapter, int)
145 */
146 @Override
147 public void handle(StatusAdapter statusAdapter, int style)
148 {
149 if(statusAdapter.getStatus().matches(IStatus.ERROR)) {
150
151 IStatus status = statusAdapter.getStatus();
152 Throwable t = statusAdapter.getStatus().getException();
153 // NOTE : Currently we only allow RuntimeExceptions since
154 // allowing all kinds of exceptions would also include
155 // those in generated status objects coming from from logging triggers
156 // leading to a recursive infinite loop of :
157 // initial exception thrown -> status handling -> dialog opening + logging of status ->
158 // status handling -> dialog opening + logging of status ... and so on
159 if(t != null && t instanceof RuntimeException) {
160 MessagingUtils.errorDialog("Unexpected error",
161 null,
162 MessagingUtils.UNEXPECTED_ERROR_MESSAGE + MessagingUtils.CONTACT_MESSAGE,
163 statusAdapter.getStatus().getPlugin(),
164 t);
165 }
166 }
167 }
168 }
169
170
171
172 }