]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/WorkbenchUiPlugin.java
a4e5ac39484d041b06d30649ac5aea96eb4bef65
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / WorkbenchUiPlugin.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.ui.workbench;
17
18 import java.io.IOException;
19 import java.util.ResourceBundle;
20
21 import javax.security.auth.callback.Callback;
22 import javax.security.auth.callback.CallbackHandler;
23 import javax.security.auth.callback.UnsupportedCallbackException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.cms.CmsException;
28 import org.argeo.cms.widgets.auth.DefaultLoginDialog;
29 import org.eclipse.core.runtime.ILogListener;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.plugin.AbstractUIPlugin;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37
38 /**
39 * The activator class controls the plug-in life cycle
40 */
41 public class WorkbenchUiPlugin extends AbstractUIPlugin implements ILogListener {
42
43 private final static Log log = LogFactory.getLog(WorkbenchUiPlugin.class);
44
45 private ResourceBundle messages;
46
47 private static BundleContext bundleContext;
48 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
49
50 @Override
51 protected Display initialValue() {
52 return Display.getCurrent();
53 }
54 };
55
56 // The plug-in ID
57 // public final static String PLUGIN_ID = "org.argeo.security.ui"; //$NON-NLS-1$
58 public final static String PLUGIN_ID = "org.argeo.cms.ui.workbench"; //$NON-NLS-1$
59
60 final static String CONTEXT_KEYRING = "KEYRING";
61
62 private CallbackHandler defaultCallbackHandler;
63 private ServiceRegistration<CallbackHandler> defaultCallbackHandlerReg;
64
65 // The shared instance
66 private static WorkbenchUiPlugin plugin;
67
68 public void start(BundleContext context) throws Exception {
69 super.start(context);
70
71 bundleContext = context;
72
73 defaultCallbackHandler = new DefaultCallbackHandler();
74 defaultCallbackHandlerReg = context.registerService(
75 CallbackHandler.class, defaultCallbackHandler, null);
76
77 plugin = this;
78 messages = ResourceBundle.getBundle(PLUGIN_ID + ".messages");
79 Platform.addLogListener(this);
80 if (log.isTraceEnabled())
81 log.trace("Eclipse logging now directed to standard logging");
82
83 }
84
85 public void stop(BundleContext context) throws Exception {
86 bundleContext = null;
87 defaultCallbackHandlerReg.unregister();
88 }
89
90 public static BundleContext getBundleContext() {
91 return bundleContext;
92 }
93
94 /*
95 * Returns the shared instance
96 *
97 * @return the shared instance
98 */
99 public static WorkbenchUiPlugin getDefault() {
100 return plugin;
101 }
102
103 protected class DefaultCallbackHandler implements CallbackHandler {
104 public void handle(final Callback[] callbacks) throws IOException,
105 UnsupportedCallbackException {
106
107 // if (display != null) // RCP
108 Display displayToUse = display.get();
109 if (displayToUse == null)// RCP
110 displayToUse = Display.getDefault();
111 displayToUse.syncExec(new Runnable() {
112 public void run() {
113 DefaultLoginDialog dialog = new DefaultLoginDialog(display
114 .get().getActiveShell());
115 try {
116 dialog.handle(callbacks);
117 } catch (IOException e) {
118 throw new CmsException("Cannot open dialog", e);
119 }
120 }
121 });
122 // else {// RAP
123 // DefaultLoginDialog dialog = new DefaultLoginDialog();
124 // dialog.handle(callbacks);
125 // }
126 }
127
128 }
129
130 public static ImageDescriptor getImageDescriptor(String path) {
131 return imageDescriptorFromPlugin(PLUGIN_ID, path);
132 }
133
134 /** Returns the internationalized label for the given key */
135 public static String getMessage(String key) {
136 try {
137 return getDefault().messages.getString(key);
138 } catch (NullPointerException npe) {
139 log.warn(key + " not found.");
140 return key;
141 }
142 }
143
144 /**
145 * Gives access to the internationalization message bundle. Returns null in
146 * case this UiPlugin is not started (for JUnit tests, by instance)
147 */
148 public static ResourceBundle getMessagesBundle() {
149 if (getDefault() != null)
150 // To avoid NPE
151 return getDefault().messages;
152 else
153 return null;
154 }
155
156 public void logging(IStatus status, String plugin) {
157 Log pluginLog = LogFactory.getLog(plugin);
158 Integer severity = status.getSeverity();
159 if (severity == IStatus.ERROR)
160 pluginLog.error(status.getMessage(), status.getException());
161 else if (severity == IStatus.WARNING)
162 pluginLog.warn(status.getMessage(), status.getException());
163 else if (severity == IStatus.INFO)
164 pluginLog.info(status.getMessage(), status.getException());
165 else if (severity == IStatus.CANCEL)
166 if (pluginLog.isDebugEnabled())
167 pluginLog.debug(status.getMessage(), status.getException());
168 }
169 }