]> git.argeo.org Git - lgpl/argeo-commons.git/blob - workbench/WorkbenchUiPlugin.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 /** The activator class controls the plug-in life cycle */
39 public class WorkbenchUiPlugin extends AbstractUIPlugin implements ILogListener {
40 private final static Log log = LogFactory.getLog(WorkbenchUiPlugin.class);
41
42 // The plug-in ID
43 public final static String PLUGIN_ID = "org.argeo.cms.ui.workbench"; //$NON-NLS-1$
44
45 private ResourceBundle messages;
46 private static BundleContext bundleContext;
47
48 public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
49
50 @Override
51 protected Display initialValue() {
52 return Display.getCurrent();
53 }
54 };
55
56
57 final static String CONTEXT_KEYRING = "KEYRING";
58
59 private CallbackHandler defaultCallbackHandler;
60 private ServiceRegistration<CallbackHandler> defaultCallbackHandlerReg;
61
62 // The shared instance
63 private static WorkbenchUiPlugin plugin;
64
65 public void start(BundleContext context) throws Exception {
66 super.start(context);
67 bundleContext = context;
68 defaultCallbackHandler = new DefaultCallbackHandler();
69 defaultCallbackHandlerReg = context.registerService(
70 CallbackHandler.class, defaultCallbackHandler, null);
71
72 plugin = this;
73 messages = ResourceBundle.getBundle(PLUGIN_ID + ".messages");
74 Platform.addLogListener(this);
75 if (log.isTraceEnabled())
76 log.trace("Eclipse logging now directed to standard logging");
77 }
78
79 public void stop(BundleContext context) throws Exception {
80 bundleContext = null;
81 defaultCallbackHandlerReg.unregister();
82 }
83
84 public static BundleContext getBundleContext() {
85 return bundleContext;
86 }
87
88 /*
89 * Returns the shared instance
90 *
91 * @return the shared instance
92 */
93 public static WorkbenchUiPlugin getDefault() {
94 return plugin;
95 }
96
97 protected class DefaultCallbackHandler implements CallbackHandler {
98 public void handle(final Callback[] callbacks) throws IOException,
99 UnsupportedCallbackException {
100
101 // if (display != null) // RCP
102 Display displayToUse = display.get();
103 if (displayToUse == null)// RCP
104 displayToUse = Display.getDefault();
105 displayToUse.syncExec(new Runnable() {
106 public void run() {
107 DefaultLoginDialog dialog = new DefaultLoginDialog(display
108 .get().getActiveShell());
109 try {
110 dialog.handle(callbacks);
111 } catch (IOException e) {
112 throw new CmsException("Cannot open dialog", e);
113 }
114 }
115 });
116 // else {// RAP
117 // DefaultLoginDialog dialog = new DefaultLoginDialog();
118 // dialog.handle(callbacks);
119 // }
120 }
121
122 }
123
124 public static ImageDescriptor getImageDescriptor(String path) {
125 return imageDescriptorFromPlugin(PLUGIN_ID, path);
126 }
127
128 /** Returns the internationalized label for the given key */
129 public static String getMessage(String key) {
130 try {
131 return getDefault().messages.getString(key);
132 } catch (NullPointerException npe) {
133 log.warn(key + " not found.");
134 return key;
135 }
136 }
137
138 /**
139 * Gives access to the internationalization message bundle. Returns null in
140 * case this UiPlugin is not started (for JUnit tests, by instance)
141 */
142 public static ResourceBundle getMessagesBundle() {
143 if (getDefault() != null)
144 // To avoid NPE
145 return getDefault().messages;
146 else
147 return null;
148 }
149
150 public void logging(IStatus status, String plugin) {
151 Log pluginLog = LogFactory.getLog(plugin);
152 Integer severity = status.getSeverity();
153 if (severity == IStatus.ERROR)
154 pluginLog.error(status.getMessage(), status.getException());
155 else if (severity == IStatus.WARNING)
156 pluginLog.warn(status.getMessage(), status.getException());
157 else if (severity == IStatus.INFO)
158 pluginLog.info(status.getMessage(), status.getException());
159 else if (severity == IStatus.CANCEL)
160 if (pluginLog.isDebugEnabled())
161 pluginLog.debug(status.getMessage(), status.getException());
162 }
163 }