configurationui.java
来自「Hibernate开发及整合应用大全 蔡雪焘编著 本书用典型的示例剖析Hiber」· Java 代码 · 共 394 行
JAVA
394 行
/* * Created on 30-03-2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package net.sf.hibernate.console;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.StringTokenizer;import javax.swing.AbstractAction;import javax.swing.BoxLayout;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.ListModel;import javax.swing.ListSelectionModel;import javax.swing.SwingUtilities;import javax.swing.filechooser.FileFilter;import com.jgoodies.forms.builder.DefaultFormBuilder;import com.jgoodies.forms.layout.FormLayout;/** * @author max * */public class ConfigurationUI extends JPanel { private static final String CUSTOM_CLASSPATH_PROPERTY = "console.custom.classpath"; private static final String HBM_XML_PROPERTY = "console.hbm.xml"; private static final String HIBERNATE_CFG_XML_PROPERTY = "console.hibernate.cfg.xml"; private static final String PROPERTYFILENAME_PROPERTY = "console.hibernate.properties"; interface ConfigurationUIListener { public void configurationChanged(ConsoleConfiguration config); } static public class ConsoleConfiguration { private File xmlconfig; private Properties properties; private List mappingFiles; private List customClasspath; public ConsoleConfiguration(File xmlconfig, Properties properties, List mappingFiles, List customClasspath) { this.xmlconfig = xmlconfig; this.properties = properties; this.mappingFiles = mappingFiles; this.customClasspath = customClasspath; } public List getCustomClasspath() { return customClasspath; } public void setCustomClasspath(List customClasspath) { this.customClasspath = customClasspath; } public List getMappingFiles() { return mappingFiles; } public void setMappingFiles(List mappingFiles) { this.mappingFiles = mappingFiles; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public File getXmlconfig() { return xmlconfig; } public void setXmlconfig(File xmlconfig) { this.xmlconfig = xmlconfig; } } ConfigurationUIListener listener = null; JFileChooser chooser = new JFileChooser(new File(".").getAbsolutePath()); JTextField configField; //DefaultListModel mappings = new DefaultListModel(); JTextField propertyFileField; private JList hbmList; private JList pathList; ConfigurationUI(ConfigurationUIListener cul) { super(); listener = cul; setupUI(); restoreConfiguration(); } void setupUI() { propertyFileField = new JTextField(); JButton but = new JButton(new OpenProperties("...", "properties", "Property files", propertyFileField)); configField = new JTextField(); JButton but2 = new JButton(new OpenProperties("...", "cfg.xml", "Hibernate cfg.xml files", configField)); FileFilter filters[] = new SimpleFileFilter[] { new SimpleFileFilter("hbm.xml", "Hibernate mapping files")}; FileFilter jars[] = new SimpleFileFilter[] { new SimpleFileFilter("jar", "Java ARchives"), new SimpleFileFilter("zip", "ZIPped java archies")}; JPanel mapPanel = createFileDirEditor(hbmList = new JList(), filters); JPanel pathPanel = createFileDirEditor(pathList = new JList(), jars); FormLayout fl = new FormLayout("left:min, 3dlu, max(400dlu;min), 3dlu, 15dlu" // 1st major colum ,"top:pref, 3dlu,top:pref, 3dlu,top:pref, 3dlu,top:pref, 3dlu"); DefaultFormBuilder builder = new DefaultFormBuilder(new JPanel(),fl); builder.append("Properties:", propertyFileField, but); builder.nextLine(2); builder.append("Config file:", configField, but2); builder.nextLine(2); builder.append("Mapping files:", mapPanel); builder.nextLine(2); builder.append("Class path:", pathPanel); builder.nextLine(2); builder.append(new JButton(new ApplyConfig(hbmList, pathList))); this.setLayout(new BorderLayout()); this.add(builder.getPanel(), BorderLayout.CENTER); } /** * @return */ private JPanel createFileDirEditor(JList aList, FileFilter[] filters) { JPanel p = new JPanel(new BorderLayout()); aList.setModel(new DefaultListModel()); p.add(new JScrollPane(aList), BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); buttonPanel.add(new JButton(new AddFile("Add...", aList, filters))); buttonPanel.add(new JButton(new RemoveItem("Remove", aList))); buttonPanel.add(new JButton(new MoveItem("Up", true, aList))); buttonPanel.add(new JButton(new MoveItem("Down", false, aList))); p.add(buttonPanel, BorderLayout.EAST); return p; } /* (non-Javadoc) * @see javax.swing.JComponent#getMinimumSize() */ public Dimension getMinimumSize() { return new Dimension(200, 100); } public class AddFile extends AbstractAction { private JList list; FileFilter[] filters; public AddFile(String string, JList list, FileFilter[] filters) { super(string); this.list = list; this.filters = filters; } public void actionPerformed(ActionEvent e) { chooser.resetChoosableFileFilters(); for (int i = 0; i < filters.length; i++) { FileFilter filter = filters[i]; chooser.addChoosableFileFilter(filter); } chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); chooser.setMultiSelectionEnabled(true); int returnVal = chooser.showOpenDialog( SwingUtilities.getWindowAncestor(ConfigurationUI.this)); if (returnVal == JFileChooser.APPROVE_OPTION) { File[] files = chooser.getSelectedFiles(); for (int i = 0; i < files.length; i++) { ((DefaultListModel)list.getModel()).addElement(files[i]); } } } } public class OpenProperties extends AbstractAction { private JTextField field; private String prefixName; private String prefix; public OpenProperties(String string, String prefix, String name, JTextField field) { super(string); this.prefix = prefix; this.prefixName = name; this.field = field; } public void actionPerformed(ActionEvent e) { chooser.setMultiSelectionEnabled(true); chooser.resetChoosableFileFilters(); SimpleFileFilter sff = new SimpleFileFilter(prefix, prefixName); chooser.addChoosableFileFilter(sff); int returnVal = chooser.showOpenDialog( SwingUtilities.getWindowAncestor(ConfigurationUI.this)); if (returnVal == JFileChooser.APPROVE_OPTION) { field.setText(chooser.getSelectedFile().toString()); } } } public class RemoveItem extends AbstractAction { private JList list; RemoveItem(String str, JList list) { super(str); this.list = list; } /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { while (!list.isSelectionEmpty()) { ((DefaultListModel)list.getModel()).remove(list.getSelectedIndex()); } } } public class MoveItem extends AbstractAction { JList list = null; boolean up; MoveItem(String str, boolean up, JList mappingsList) { super(str); this.up = up; this.list = mappingsList; } public void actionPerformed(ActionEvent e) { ListSelectionModel sm = list.getSelectionModel(); DefaultListModel mappings = (DefaultListModel) list.getModel(); int si = sm.getMinSelectionIndex(); if (up && si > 0) { Object object = mappings.remove(si); mappings.add(si - 1, object); sm.setSelectionInterval(si-1, si-1); } else if (!up && si < mappings.size()-1) { Object object = mappings.remove(si); mappings.add(si + 1, object); sm.setSelectionInterval(si+1, si+1); } } } /** * @author max * */ public class ApplyConfig extends AbstractAction { private JList list; private JList pathsList; ApplyConfig(JList mappings, JList paths) { super("Apply"); this.list = mappings; this.pathsList = paths; } public void actionPerformed(ActionEvent e) { ListModel mappings = list.getModel(); if (listener!=null) { List mappingFiles = new ArrayList(); for(int i = 0; i<mappings.getSize(); i++) { mappingFiles.add(mappings.getElementAt(i)); } ListModel paths = pathsList.getModel(); List customClasspath = new ArrayList(); for(int i = 0; i<paths.getSize(); i++) { customClasspath.add(paths.getElementAt(i)); } Properties properties = new Properties(); if(propertyFileField.getText()!=null && propertyFileField.getText().trim().length()>0) { try { properties.load(new FileInputStream(propertyFileField.getText())); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } String cfg = configField.getText(); File xmlconfig = null; if(cfg.trim().length()!=0) { xmlconfig = new File(cfg); } saveConfiguration(); listener.configurationChanged(new ConsoleConfiguration(xmlconfig,properties,mappingFiles,customClasspath)); } } } /** * Will save the .properties and .hbm.xml files currently selected */ public void saveConfiguration() { File f = new File("hibernateconsole.properties"); try { Properties config = new Properties(); config.setProperty(PROPERTYFILENAME_PROPERTY, propertyFileField.getText()); config.setProperty(HIBERNATE_CFG_XML_PROPERTY, configField.getText()); String mappingFiles = ""; ListModel mappings = hbmList.getModel(); for(int i = 0; i < mappings.getSize(); i++) { mappingFiles += mappings.getElementAt(i) + ";"; } config.put(HBM_XML_PROPERTY, mappingFiles); String pathNames = ""; ListModel paths = pathList.getModel(); for(int i = 0; i < paths.getSize(); i++) { pathNames+= paths.getElementAt(i) + ";"; } config.put(CUSTOM_CLASSPATH_PROPERTY, pathNames); config.save(new FileOutputStream(f), "Hibernate Console properties"); } catch(Exception e) { System.err.println("Problems while saving configuration to " + f); e.printStackTrace(); } } /** * Will restore the .properties and .hbm.xml files currently selected */ public void restoreConfiguration() { Properties props = new Properties(); File f = new File("hibernateconsole.properties"); if(f.exists()) { try { props.load(new FileInputStream(f)); } catch (Exception e) { System.err.println("Could not load from " + f); e.printStackTrace(); } } propertyFileField.setText(props.getProperty(PROPERTYFILENAME_PROPERTY)); configField.setText(props.getProperty(HIBERNATE_CFG_XML_PROPERTY, "")); String mappingFiles = props.getProperty(HBM_XML_PROPERTY, ""); StringTokenizer st = new StringTokenizer(mappingFiles, ";"); while(st.hasMoreTokens()) { String file = st.nextToken(); ((DefaultListModel)hbmList.getModel()).addElement(new File(file)); } mappingFiles = props.getProperty(CUSTOM_CLASSPATH_PROPERTY, ""); st = new StringTokenizer(mappingFiles, ";"); while(st.hasMoreTokens()) { String file = st.nextToken(); ((DefaultListModel)pathList.getModel()).addElement(new File(file)); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?