]> git.argeo.org Git - gpl/argeo-slc.git/blob - workbench/WorkbenchUiPlugin.java
Prepare next development cycle
[gpl/argeo-slc.git] / workbench / WorkbenchUiPlugin.java
1 package org.argeo.cms.ui.workbench;
2
3 import java.io.IOException;
4 import java.util.ResourceBundle;
5
6 import javax.security.auth.callback.Callback;
7 import javax.security.auth.callback.CallbackHandler;
8 import javax.security.auth.callback.UnsupportedCallbackException;
9
10 import org.argeo.api.cms.CmsLog;
11 import org.argeo.cms.CmsException;
12 import org.eclipse.core.runtime.ILogListener;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.ui.plugin.AbstractUIPlugin;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceRegistration;
20
21 /** The activator class controls the plug-in life cycle */
22 public class WorkbenchUiPlugin extends AbstractUIPlugin implements ILogListener {
23 private final static CmsLog log = CmsLog.getLog(WorkbenchUiPlugin.class);
24
25 // The plug-in ID
26 public final static String PLUGIN_ID = "org.argeo.cms.ui.workbench"; //$NON-NLS-1$
27 public final static String THEME_PLUGIN_ID = "org.argeo.cms.ui.theme"; //$NON-NLS-1$
28
29 private ResourceBundle messages;
30 private static BundleContext bundleContext;
31
32 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
33
34 @Override
35 protected Display initialValue() {
36 return Display.getCurrent();
37 }
38 };
39
40 final static String CONTEXT_KEYRING = "KEYRING";
41
42 private CallbackHandler defaultCallbackHandler;
43 private ServiceRegistration<CallbackHandler> defaultCallbackHandlerReg;
44
45 // The shared instance
46 private static WorkbenchUiPlugin plugin;
47
48 public void start(BundleContext context) throws Exception {
49 super.start(context);
50 bundleContext = context;
51 defaultCallbackHandler = new DefaultCallbackHandler();
52 defaultCallbackHandlerReg = context.registerService(CallbackHandler.class, defaultCallbackHandler, null);
53
54 plugin = this;
55 messages = ResourceBundle.getBundle(PLUGIN_ID + ".messages");
56 Platform.addLogListener(this);
57 if (log.isTraceEnabled())
58 log.trace("Eclipse logging now directed to standard logging");
59 }
60
61 public void stop(BundleContext context) throws Exception {
62 bundleContext = null;
63 defaultCallbackHandlerReg.unregister();
64 }
65
66 public static BundleContext getBundleContext() {
67 return bundleContext;
68 }
69
70 /*
71 * Returns the shared instance
72 *
73 * @return the shared instance
74 */
75 public static WorkbenchUiPlugin getDefault() {
76 return plugin;
77 }
78
79 protected class DefaultCallbackHandler implements CallbackHandler {
80 public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
81
82 // if (display != null) // RCP
83 Display displayToUse = display.get();
84 if (displayToUse == null)// RCP
85 displayToUse = Display.getDefault();
86 displayToUse.syncExec(new Runnable() {
87 public void run() {
88 DefaultLoginDialog dialog = new DefaultLoginDialog(display.get().getActiveShell());
89 try {
90 dialog.handle(callbacks);
91 } catch (IOException e) {
92 throw new CmsException("Cannot open dialog", e);
93 }
94 }
95 });
96 // else {// RAP
97 // DefaultLoginDialog dialog = new DefaultLoginDialog();
98 // dialog.handle(callbacks);
99 // }
100 }
101
102 }
103
104 public static ImageDescriptor getImageDescriptor(String path) {
105 return imageDescriptorFromPlugin(THEME_PLUGIN_ID, path);
106 }
107
108 /** Returns the internationalized label for the given key */
109 public static String getMessage(String key) {
110 try {
111 return getDefault().messages.getString(key);
112 } catch (NullPointerException npe) {
113 log.warn(key + " not found.");
114 return key;
115 }
116 }
117
118 /**
119 * Gives access to the internationalization message bundle. Returns null in case
120 * this UiPlugin is not started (for JUnit tests, by instance)
121 */
122 public static ResourceBundle getMessagesBundle() {
123 if (getDefault() != null)
124 // To avoid NPE
125 return getDefault().messages;
126 else
127 return null;
128 }
129
130 public void logging(IStatus status, String plugin) {
131 CmsLog pluginLog = CmsLog.getLog(plugin);
132 Integer severity = status.getSeverity();
133 if (severity == IStatus.ERROR)
134 pluginLog.error(status.getMessage(), status.getException());
135 else if (severity == IStatus.WARNING)
136 pluginLog.warn(status.getMessage(), status.getException());
137 else if (severity == IStatus.INFO)
138 pluginLog.info(status.getMessage(), status.getException());
139 else if (severity == IStatus.CANCEL)
140 if (pluginLog.isDebugEnabled())
141 pluginLog.debug(status.getMessage(), status.getException());
142 }
143 }