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