]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore.ui/src/org/simantics/db/procore/ui/internal/UI.java
00cca9311329ff43eaf8afe4ae0835d69139a0d4
[simantics/platform.git] / bundles / org.simantics.db.procore.ui / src / org / simantics / db / procore / ui / internal / UI.java
1 package org.simantics.db.procore.ui.internal;
2
3 import java.io.File;
4 import java.lang.reflect.Method;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.eclipse.swt.widgets.Display;
9 import org.eclipse.swt.widgets.Shell;
10 import org.simantics.db.common.utils.Logger;
11 import org.simantics.db.exception.InternalException;
12 import org.simantics.db.server.DatabaseCorruptedException;
13 import org.simantics.db.server.DatabaseLastExitException;
14 import org.simantics.db.server.DatabaseProtocolException;
15 import org.simantics.db.server.DatabaseStartException;
16 import org.simantics.db.server.DatabaseVersionException;
17 import org.simantics.db.server.GuardFileVersionException;
18 import org.simantics.db.server.ProCoreException;
19
20 public class UI {
21     public static boolean delete(Shell shell, File folder) {
22         return HandlerUtil.delete(shell, folder, "Database Delete", null);
23     }
24     public static boolean purge(Shell shell, File folder) {
25         return HandlerUtil.purge(shell, folder, "Database Purge", null);
26     }
27     public static boolean handleStart(Shell shell, InternalException e) throws ProCoreException {
28         if (!(e instanceof ProCoreException))
29             return false; // Can not fix this exception.
30         ProCoreException pce = (ProCoreException)e;
31         Handler handler = getStart(pce);
32         assert(null != handler);
33         return handler.start(shell, pce);
34     }
35     public static Display getDisplay() {
36         Display d = Display.getCurrent();
37         if (d == null)
38             d = Display.getDefault();
39         return d;
40     }
41     static Map<Long, Handler> startHandlers = new HashMap<Long, Handler>();
42     static {
43         startHandlers.put(GuardFileVersionException.getHandlerId(), new GuardFileVersionHandler());
44         startHandlers.put(DatabaseCorruptedException.getHandlerId(), new DatabaseCorruptedHandler());
45         startHandlers.put(DatabaseStartException.getHandlerId(), new DatabaseStartHandler());
46         startHandlers.put(DatabaseVersionException.getHandlerId(), new DatabaseVersionHandler());
47         startHandlers.put(DatabaseLastExitException.getHandlerId(), new DatabaseLastExitHandler());
48         startHandlers.put(DatabaseProtocolException.getHandlerId(), new DatabaseProtocolHandler());
49     }
50     private static <E extends ProCoreException> long getId(E pe) {
51         long id = 0;
52         try {
53             Method m = pe.getClass().getMethod("getHandlerId");
54             Object value = m.invoke(null);
55             id = (long)value;
56         } catch (RuntimeException e) {
57             Logger.defaultLogError(e);
58         } catch (Exception e) {
59             Logger.defaultLogError(e);
60         }
61         return id;
62     }
63     private static <E extends ProCoreException> Handler getStart(E e) {
64        Handler h = startHandlers.get(getId(e));
65        if (null == h)
66            return new DefaultHandler();
67        else
68            return h;
69     }
70 }