1 package org.hibernate.eclipse.console; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 import java.util.MissingResourceException ; 6 import java.util.ResourceBundle ; 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 24 public class HibernateConsolePlugin extends AbstractUIPlugin { 25 26 public static final String ID = "org.hibernate.eclipse.console"; 27 28 private static HibernateConsolePlugin plugin; 30 private ResourceBundle resourceBundle; 32 private EclipseLogger logger = new EclipseLogger(ID); 33 34 37 public HibernateConsolePlugin() { 38 super(); 39 plugin = this; 40 } 41 42 45 public void start(BundleContext context) throws Exception { 46 super.start(context); 47 48 HibernateConsoleSaveParticipant participant = new HibernateConsoleSaveParticipant(); 49 participant.doStart(this); 50 } 51 52 53 56 public void stop(BundleContext context) throws Exception { 57 super.stop(context); 58 plugin = null; 59 resourceBundle = null; 60 } 61 62 65 public static HibernateConsolePlugin getDefault() { 66 return plugin; 67 } 68 69 73 public static String getResourceString(String key) { 74 ResourceBundle bundle = HibernateConsolePlugin.getDefault().getResourceBundle(); 75 try { 76 return (bundle != null) ? bundle.getString(key) : key; 77 } catch (MissingResourceException e) { 78 return key; 79 } 80 } 81 82 85 public ResourceBundle getResourceBundle() { 86 try { 87 if (resourceBundle == null) 88 resourceBundle = ResourceBundle.getBundle("org.hibernate.eclipse.console.HibernateConsolePluginResources"); 89 } catch (MissingResourceException x) { 90 resourceBundle = null; 91 } 92 return resourceBundle; 93 } 94 95 100 public void log(IStatus status) { 101 logger.log(status); 102 } 103 104 105 110 public void log(String message) { 111 log(new Status(IStatus.INFO, HibernateConsolePlugin.ID, 0, message, null)); 112 } 113 114 119 public void logErrorMessage(String message, Throwable t) { 120 log(new MultiStatus(HibernateConsolePlugin.ID, IStatus.ERROR , new IStatus[] { throwableToStatus(t) }, message, t)); 121 } 122 123 static IStatus throwableToStatus(Throwable t) { 124 ArrayList causes = new ArrayList (); 125 Throwable 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 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 message, Throwable t[]) { 140 IStatus[] children = new IStatus[t.length]; 141 for (int i = 0; i < t.length; i++) { 142 Throwable 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 155 public void log(Throwable e) { 156 log(new Status(IStatus.ERROR, ID, 150, "Hibernate Console Internal Error", e)); } 158 159 void readStateFrom(File 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); } 167 } catch(HibernateConsoleRuntimeException hcr) { 168 logErrorMessage("Error while reading console configuration", hcr); 169 } 170 171 } 172 173 void writeStateTo(File f) { 174 System.out.println("write state to" + f); 175 KnownConfigurations.getInstance().writeStateTo(f); 176 } 177 178 181 public void showError(Shell shell, String message, Throwable he) { 182 logErrorMessage(message, he); 183 String 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 |