]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/WorkbenchUiPlugin.java
Improve workbench layer
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / 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.eclipse.ui.workbench;
17
18 import java.util.ResourceBundle;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.eclipse.core.runtime.ILogListener;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.ui.plugin.AbstractUIPlugin;
27 import org.osgi.framework.BundleContext;
28
29 /**
30 * The activator class controls the plug-in life cycle
31 */
32 public class WorkbenchUiPlugin extends AbstractUIPlugin implements ILogListener {
33 private final static Log log = LogFactory.getLog(WorkbenchUiPlugin.class);
34 private ResourceBundle messages;
35
36 // The plug-in ID
37 public static final String ID = "org.argeo.eclipse.ui.workbench"; //$NON-NLS-1$
38
39 // The shared instance
40 private static WorkbenchUiPlugin plugin;
41
42 /**
43 * The constructor
44 */
45 public WorkbenchUiPlugin() {
46 }
47
48 public void start(BundleContext context) throws Exception {
49 super.start(context);
50 plugin = this;
51 messages = ResourceBundle.getBundle(ID + ".messages");
52 Platform.addLogListener(this);
53 if (log.isTraceEnabled())
54 log.trace("Eclipse logging now directed to standard logging");
55 }
56
57 public void stop(BundleContext context) throws Exception {
58 try {
59 Platform.removeLogListener(this);
60 if (log.isTraceEnabled())
61 log.trace("Eclipse logging not directed anymore to standard logging");
62 plugin = null;
63 } finally {
64 super.stop(context);
65 }
66 }
67
68 /**
69 * Returns the shared instance
70 *
71 * @return the shared instance
72 */
73 public static WorkbenchUiPlugin getDefault() {
74 return plugin;
75 }
76
77 public static ImageDescriptor getImageDescriptor(String path) {
78 return imageDescriptorFromPlugin(ID, path);
79 }
80
81 /** Returns the internationalized label for the given key */
82 public static String getMessage(String key) {
83 try {
84 return getDefault().messages.getString(key);
85 } catch (NullPointerException npe) {
86 log.warn(key + " not found.");
87 return key;
88 }
89 }
90
91 /**
92 * Gives access to the internationalization message bundle. Returns null in
93 * case this UiPlugin is not started (for JUnit tests, by instance)
94 */
95 public static ResourceBundle getMessagesBundle() {
96 if (getDefault() != null)
97 // To avoid NPE
98 return getDefault().messages;
99 else
100 return null;
101 }
102
103 public void logging(IStatus status, String plugin) {
104 Log pluginLog = LogFactory.getLog(plugin);
105 Integer severity = status.getSeverity();
106 if (severity == IStatus.ERROR)
107 pluginLog.error(status.getMessage(), status.getException());
108 else if (severity == IStatus.WARNING)
109 pluginLog.warn(status.getMessage(), status.getException());
110 else if (severity == IStatus.INFO)
111 pluginLog.info(status.getMessage(), status.getException());
112 else if (severity == IStatus.CANCEL)
113 if (pluginLog.isDebugEnabled())
114 pluginLog.debug(status.getMessage(), status.getException());
115 }
116 }