KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > HibernateConsolePlugin


1 package org.hibernate.eclipse.console;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.MissingResourceException JavaDoc;
6 import java.util.ResourceBundle JavaDoc;
7
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.MultiStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.jface.dialogs.ErrorDialog;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.ui.plugin.AbstractUIPlugin;
14 import org.hibernate.console.ConsoleConfigurationPreferences;
15 import org.hibernate.console.HibernateConsoleRuntimeException;
16 import org.hibernate.console.KnownConfigurations;
17 import org.hibernate.eclipse.EclipseLogger;
18 import org.hibernate.eclipse.console.wizards.EclipseConsoleConfigurationPreferences;
19 import org.osgi.framework.BundleContext;
20
21 /**
22  * The main plugin class to be used in the desktop.
23  */

24 public class HibernateConsolePlugin extends AbstractUIPlugin {
25     
26     public static final String JavaDoc ID = "org.hibernate.eclipse.console";
27     
28     //The shared instance.
29
private static HibernateConsolePlugin plugin;
30     //Resource bundle.
31
private ResourceBundle JavaDoc resourceBundle;
32     private EclipseLogger logger = new EclipseLogger(ID);
33     
34     /**
35      * The constructor.
36      */

37     public HibernateConsolePlugin() {
38         super();
39         plugin = this;
40     }
41
42     /**
43      * This method is called upon plug-in activation
44      */

45     public void start(BundleContext context) throws Exception JavaDoc {
46         super.start(context);
47         
48         HibernateConsoleSaveParticipant participant = new HibernateConsoleSaveParticipant();
49         participant.doStart(this);
50     }
51
52     
53     /**
54      * This method is called when the plug-in is stopped
55      */

56     public void stop(BundleContext context) throws Exception JavaDoc {
57         super.stop(context);
58         plugin = null;
59         resourceBundle = null;
60     }
61
62     /**
63      * Returns the shared instance.
64      */

65     public static HibernateConsolePlugin getDefault() {
66         return plugin;
67     }
68
69     /**
70      * Returns the string from the plugin's resource bundle,
71      * or 'key' if not found.
72      */

73     public static String JavaDoc getResourceString(String JavaDoc key) {
74         ResourceBundle JavaDoc bundle = HibernateConsolePlugin.getDefault().getResourceBundle();
75         try {
76             return (bundle != null) ? bundle.getString(key) : key;
77         } catch (MissingResourceException JavaDoc e) {
78             return key;
79         }
80     }
81
82     /**
83      * Returns the plugin's resource bundle,
84      */

85     public ResourceBundle JavaDoc getResourceBundle() {
86         try {
87             if (resourceBundle == null)
88                 resourceBundle = ResourceBundle.getBundle("org.hibernate.eclipse.console.HibernateConsolePluginResources");
89         } catch (MissingResourceException JavaDoc x) {
90             resourceBundle = null;
91         }
92         return resourceBundle;
93     }
94     
95     /**
96      * Logs the specified status with this plug-in's log.
97      *
98      * @param status status to log
99      */

100     public void log(IStatus status) {
101         logger.log(status);
102     }
103     
104     
105     /**
106      * Logs an internal info with the specified message.
107      *
108      * @param message the error message to log
109      */

110     public void log(String JavaDoc message) {
111         log(new Status(IStatus.INFO, HibernateConsolePlugin.ID, 0, message, null));
112     }
113     
114     /**
115      * Logs an internal error with the specified message.
116      *
117      * @param message the error message to log
118      */

119     public void logErrorMessage(String JavaDoc message, Throwable JavaDoc t) {
120         log(new MultiStatus(HibernateConsolePlugin.ID, IStatus.ERROR , new IStatus[] { throwableToStatus(t) }, message, t));
121     }
122     
123     static IStatus throwableToStatus(Throwable JavaDoc t) {
124         ArrayList JavaDoc causes = new ArrayList JavaDoc();
125         Throwable JavaDoc temp = t;
126         while(temp!=null && temp.getCause()!=temp) {
127             causes.add(new Status(IStatus.ERROR, ID, 150, temp.getMessage()==null?"<no message>":temp.getMessage(), temp));
128             temp = temp.getCause();
129         }
130         String JavaDoc msg = "<No message>";
131         if(t!=null && t.getMessage()!=null) {
132             msg = t.getMessage();
133         }
134         
135         return new MultiStatus(ID, IStatus.ERROR,(IStatus[]) causes.toArray(new IStatus[causes.size()]), msg, t);
136         
137     }
138     
139     public void logErrorMessage(String JavaDoc message, Throwable JavaDoc t[]) {
140         IStatus[] children = new IStatus[t.length];
141         for (int i = 0; i < t.length; i++) {
142             Throwable JavaDoc throwable = t[i];
143             children[i] = throwableToStatus(throwable);
144         }
145         
146         IStatus s = new MultiStatus(ID, IStatus.ERROR,children, message, null);
147         log(s);
148     }
149
150     /**
151      * Logs an internal error with the specified throwable
152      *
153      * @param e the exception to be logged
154      */

155     public void log(Throwable JavaDoc e) {
156         log(new Status(IStatus.ERROR, ID, 150, "Hibernate Console Internal Error", e)); //$NON-NLS-1$
157
}
158
159     void readStateFrom(File JavaDoc f) {
160         try {
161             EclipseConsoleConfigurationPreferences[] preferences = EclipseConsoleConfigurationPreferences.readStateFrom(f);
162             
163             for (int i = 0; i < preferences.length; i++) {
164                 ConsoleConfigurationPreferences prefs = preferences[i];
165                 KnownConfigurations.getInstance().addConfiguration(new EclipseConsoleConfiguration(prefs), true); // TODO: do we need to broadcast every time when reading state ?
166
}
167         } catch(HibernateConsoleRuntimeException hcr) {
168             logErrorMessage("Error while reading console configuration", hcr);
169         }
170         
171     }
172
173     void writeStateTo(File JavaDoc f) {
174         System.out.println("write state to" + f);
175         KnownConfigurations.getInstance().writeStateTo(f);
176     }
177
178     /**
179      *
180      */

181     public void showError(Shell shell, String JavaDoc message, Throwable JavaDoc he) {
182         logErrorMessage(message, he);
183         String JavaDoc string = he==null?"<No message>":he.getClass().getName() + ":" + he.getMessage();
184         IStatus warning = new Status(IStatus.WARNING,
185                   HibernateConsolePlugin.ID, 1, string , he);
186                ErrorDialog.openError(shell,
187                   "Hibernate Console", message, warning);
188     }
189     
190     
191 }
192
Popular Tags