start.java

来自「Hibernate开发及整合应用大全 蔡雪焘编著 本书用典型的示例剖析Hiber」· Java 代码 · 共 806 行 · 第 1/2 页

JAVA
806
字号
/* * Created on 29-03-2003 * */package net.sf.hibernate.console;import java.awt.Component;import java.awt.Container;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.sql.Driver;import java.sql.DriverManager;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.WeakHashMap;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.JCheckBoxMenuItem;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTable;import javax.swing.JTree;import javax.swing.KeyStroke;import javax.swing.UIManager;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.plaf.basic.BasicSplitPaneDivider;import javax.swing.plaf.basic.BasicSplitPaneUI;import javax.swing.tree.DefaultTreeModel;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.cfg.Configuration;import net.sf.hibernate.console.ConfigurationUI.ConsoleConfiguration;import net.sf.hibernate.console.docking.ElegantDockingPort;import net.sf.hibernate.console.docking.ElegantPanel;import net.sf.hibernate.console.node.BaseNode;import net.sf.hibernate.console.node.NodeFactory;import net.sf.hibernate.console.views.GraphView;import net.sf.hibernate.console.views.ObjectGraphView;import net.sf.hibernate.console.views.ObjectPropertySheetView;import net.sf.hibernate.console.views.QueryCommanderView;import net.sf.hibernate.console.views.QueryPageView;import net.sf.hibernate.console.views.SessionFactoryView;import net.sf.hibernate.console.views.SessionView;import net.sf.hibernate.console.views.StaticModelView;import net.sf.hibernate.console.views.View;import net.sf.hibernate.tool.hbm2ddl.SchemaExport;import net.sf.hibernate.util.ReflectHelper;import org.pf.joi.Inspector;import com.jgoodies.clearlook.ClearLookManager;import com.jgoodies.clearlook.ClearLookMode;import com.jgoodies.plaf.plastic.PlasticXPLookAndFeel;/** * Main class for Hibernate Console. Either use directly by runing main() or do a Start.startWith(configuration); *  * @author max * */public class Start implements QueryExecutor, SessionController {	JSplitPane horizontalSplit;	JSplitPane vertSplitLeft;	JSplitPane vertSplitRight;		Map fakeDrivers = new HashMap();	Map views = new HashMap();	int execcount;	List sessionListeners = new ArrayList();	List factoryListeners = new ArrayList();		final static String dockingSettingsFileName = "hibernateconsole-docking.xml";	private JMenu namedQueries;	private ErrorListModel errorList;	private JFrame mainWindow;	Configuration configuration;	SessionFactory sf;	private Action executeAction;    		private URLClassLoader configurationClassLoader;	private Map previousLoaders = new WeakHashMap();	private QueryCommanderView queryCommander;	        Start() {			}	void reconfig(ConsoleConfiguration config) {		File configfile = config.getXmlconfig();		Properties props = config.getProperties();		List mappings = config.getMappingFiles();		List paths = config.getCustomClasspath();		File hbm = null;		try {									Iterator maps = paths.iterator();						List urls = new ArrayList();						while (maps.hasNext()) {				File dir = (File) maps.next();				urls.add(dir.toURL());										}			if(urls.size()>0) {                configurationClassLoader = new URLClassLoader((URL[]) urls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader());                installLoader();						}						configuration = new Configuration();                    			configuration = configuration.setProperties(props);			if (configfile != null) {				configuration = configuration.configure(configfile);			}			Iterator hbms = mappings.iterator();					while (hbms.hasNext()) {				hbm = (File) hbms.next();				configuration = configuration.addFile(hbm.toString());			}			//	convince DriverManager that you can use our specified driver!			String driverClassName = configuration.getProperty("hibernate.connection.driver_class");			if(driverClassName!=null) {				try {					Class driverClass = ReflectHelper.classForName(driverClassName);					if(!fakeDrivers.containsKey(driverClassName)) { // To avoid "double registration"						FakeDelegatingDriver fakeDelegatingDriver = new FakeDelegatingDriver((Driver) driverClass.newInstance());						DriverManager.registerDriver(fakeDelegatingDriver);						fakeDrivers.put(driverClassName,fakeDelegatingDriver);					}									} catch (ClassNotFoundException e1) {					reportException(e1);									} catch (InstantiationException e) {					reportException(e);				} catch (IllegalAccessException e) {					reportException(e);				} catch (SQLException e) {					reportException(e);					}			}						initialize();		} catch (HibernateException e) {			reportException(e);  //TODO: report which hbm file is doing the error.		} catch (MalformedURLException e) {			reportException(e);					} finally {			uninstallLoader();		}	}		/**	 * @param thread	 * @param prevCl	 */	private void uninstallLoader() {		ClassLoader cl = (ClassLoader) previousLoaders.get(Thread.currentThread());		if(cl!=null) {			Thread.currentThread().setContextClassLoader(cl);		}	}	private void initialize() throws HibernateException {		try {			installLoader();			if (configuration != null) {				sf = configuration.buildSessionFactory();							//registerMBean();								//JGraphUtilities.applyLayout(graph, new AnnealingLayoutAlgorithm());				buildMenuOfAvailableQueries();				fireFactoryCreated(configuration, sf);		}		} finally {			uninstallLoader();		}	}	private void fireSessionCreated(Session session) {		Iterator i = sessionListeners.iterator();		while (i.hasNext()) {			SessionView view = (SessionView) i.next();			view.sessionOpened(session);		}				}	private void fireQueryPageCreated(QueryPage qp) {		Iterator i = sessionListeners.iterator();		while (i.hasNext()) {			SessionView view = (SessionView) i.next();			view.queryPageCreated(qp);		}			}	protected void fireObjectUpdated(Session session, Object o) {		Iterator i = sessionListeners.iterator();		while (i.hasNext()) {			SessionView view = (SessionView) i.next();			view.objectUpdated(session, o);		}			}	private void fireFactoryCreated(Configuration cfg, SessionFactory mySf) {		Iterator i = factoryListeners.iterator();		while (i.hasNext()) {			SessionFactoryView view = (SessionFactoryView) i.next();			view.factoryCreated(cfg, sf);		}	}		private void fireFactoryUpdated(SessionFactory sf) {		Iterator i = factoryListeners.iterator();		while (i.hasNext()) {			SessionFactoryView view = (SessionFactoryView) i.next();			view.factoryUpdated(sf);		}	}	/**	 * 	 */	private void installLoader() {		if(configurationClassLoader!=null) {			previousLoaders.put(Thread.currentThread(), Thread.currentThread().getContextClassLoader());			Thread.currentThread().setContextClassLoader(configurationClassLoader);		}			}					public static void startWith(Configuration c, boolean lookGood) throws HibernateException {			if (lookGood) setupSwing();			Start h8ide = new Start();			h8ide.setConfiguration(c);			h8ide.start();		}		public static void startWith(Configuration c) throws HibernateException {		startWith(c, false);	}	public static void main(String[] args) throws HibernateException {		startWith(null,true);	}		private static void setupSwing() {		try {			if (System.getProperty("sun.awt.exception.handler")==null) {				System.setProperty("sun.awt.exception.handler", AWTExceptionHandler.class.getName());						} else {				System.err.println("could not install AWT handler ;(");			}			ClearLookManager.setMode(ClearLookMode.ON);			UIManager.setLookAndFeel(new PlasticXPLookAndFeel());			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());					} catch (Exception e) { // noop 					}	}	/**	 * @param cfg	 */	private void setConfiguration(Configuration cfg) {		configuration = cfg;	}	private void start() throws HibernateException {			mainWindow = setupUI();				mainWindow.show();				horizontalSplit.setDividerLocation(0.3d);		vertSplitLeft.setDividerLocation(0.75d);		vertSplitRight.setDividerLocation(0.75d);					}	private JFrame setupUI() throws HibernateException {		ElegantDockingPort bottomRight;		ElegantDockingPort topRight;		ElegantDockingPort bottomLeft;		ElegantDockingPort topLeft;				final JFrame main = new JFrame("Hibernate Console 2.1");		main.setIconImage(Toolkit.getDefaultToolkit().getImage(Start.class.getResource("images/hibernate_icon.jpg")));					createActions();		buildViews();				horizontalSplit = createSplitPane(JSplitPane.HORIZONTAL_SPLIT);		vertSplitLeft = createSplitPane(JSplitPane.VERTICAL_SPLIT);		vertSplitRight = createSplitPane(JSplitPane.VERTICAL_SPLIT);						horizontalSplit.setLeftComponent(vertSplitLeft);		horizontalSplit.setRightComponent(vertSplitRight);		topLeft = new ElegantDockingPort("top.left");		bottomLeft = new ElegantDockingPort("bottom.left");		topRight = new ElegantDockingPort("top.right");		bottomRight = new ElegantDockingPort("bottom.right");				topLeft.add((ElegantPanel) views.get("static.graph"));		topLeft.add((ElegantPanel) views.get("static.tree"));						bottomLeft.add((ElegantPanel) views.get("messages"));		bottomLeft.add((ElegantPanel) views.get("dynamic.graph"));		bottomLeft.add((ElegantPanel) views.get("properties"));						topRight.add((ElegantPanel) views.get("query"));						bottomRight.add((ElegantPanel) views.get("result"));						vertSplitLeft.setLeftComponent(topLeft);		vertSplitLeft.setRightComponent(bottomLeft);		vertSplitRight.setLeftComponent(topRight);		vertSplitRight.setRightComponent(bottomRight);						horizontalSplit.setLeftComponent(vertSplitLeft);		horizontalSplit.setRightComponent(vertSplitRight);						main.setContentPane(horizontalSplit);						JMenuBar bar = buildMenuBar();		main.setJMenuBar(bar);		main.addWindowListener(new WindowAdapter() {			public void windowClosing(WindowEvent e) {								super.windowClosing(e);				/*DockingConfiguration dockCfg = ConfigurationManager.createDockingConfiguration();				ConfigurationManager.store(dockCfg, new File(dockingSettingsFileName));*/			}		});		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		main.setSize(800, 600);		//main.setExtendedState(Frame.MAXIMIZED_BOTH);						/*File dockingSettingsFile = new File(dockingSettingsFileName);		if(dockingSettingsFile.exists() && dockingSettingsFile.canRead() && dockingSettingsFile.isFile()) {						DockingConfiguration config = ConfigurationManager.load(dockingSettingsFile);			ConfigurationManager.applyConfiguration(config);		}		*/		initialize();							return main;	}    	private void buildViews() {		views.put("static.graph", new ElegantPanel("static.graph","Static graph",createStrippedScrollPane(buildConfigGraph().getContainer())));		views.put("static.tree",new ElegantPanel("static.tree","Static tree",createStrippedScrollPane(buildConfigTree().getContainer())));		views.put("messages", new ElegantPanel("messages","Messages", buildMessagePanel()));		views.put("dynamic.graph",new ElegantPanel("dynamic.graph","Object graph", createStrippedScrollPane(buildObjectGraph().getContainer())));		views.put("properties", new ElegantPanel("properties","Properties", buildPropertySheet().getContainer()));		views.put("query", new ElegantPanel("query","Query", createStrippedScrollPane(buildHQLCommander().getContainer())));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?