📄 configurator.java
字号:
package net.sf.dz.setup;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.PrintWriter;import java.net.URL;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.awt.Dimension;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTextField;import javax.swing.JTextPane;import javax.swing.JTree;import javax.swing.WindowConstants;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.text.Document;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.MutableTreeNode;import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.conf.ConfigurationFactory;import org.freehold.jukebox.conf.TextConfiguration;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.service.PassiveService;/** * The Configurator. * * <p> * * Allows to create the configuration files and some scripts that depend on * the configuration, as well as RRD database, from scratch. * * @author Copyright © <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001-2002 * @version $Id: Configurator.java,v 1.21 2004/06/29 21:18:21 vtt Exp $ */public class Configurator extends PassiveService implements ActionListener, TreeSelectionListener { public static final LogChannel CH_CF = new LogChannel("Configurator"); /** * The configuration to work with. */ private String targetCfURL; /** * The main frame. */ private JFrame mainFrame; /** * The main menu bar. */ private JMenuBar mainMenuBar; private JMenuItem file_save; private JMenuItem file_exit; private JMenuItem help_about; /** * The help split pane. * * The top pane contains the {@link #mainSplit, mainSplit}, the lower * the {@link #error error pane}. */ private JSplitPane helpSplit; /** * The main split pane. */ private JSplitPane mainSplit; /** * The tree that displays the configuration. */ private JTree cfTree; /** * The scroll pane containing the {@link #cfTree configuration tree}. */ private JScrollPane cfTreeScroll; /** * The right pane split pane. */ private JSplitPane rightSplit; /** * The container for the node display. */ private JPanel nodeDisplay; /** * The text pane containing the description for the selected tree node. */ private JTextPane helpPane; /** * The scroll pane containing the {@link #helpPane help pane}. */ private JScrollPane helpPaneScroll; /** * The component available to the subclasses to submit the error * messages and hints to. */ protected JTextPane error; /** * A wrapper for the error pane. */ private JScrollPane errorScroll; /** * The target configuration. */ private Configuration targetCf; /** * Content map for <code>UniqueStringAnchor</code>. * * The key is the meta, the value is the set of existing instances * with that meta. * * <p> * * VT: FIXME: This is not nice - the spec forbids inner classes to have * static variables, so this is a quick and dirty fix. Have to beautify * it someday, possibly by passing it to the UniqueStringAnchor * constructor as a parameter. */ static Map contentMap = new HashMap(); protected void configure() throws Throwable { } protected void startup() throws Throwable { String cfURL = System.getProperty("conf.URL"); if ( cfURL == null || "".equals(cfURL) ) { throw new IllegalArgumentException("You must set the 'conf.URL' environment variable to point to the configuration"); } Configuration cf = (new ConfigurationFactory()).getConfiguration(new URL(cfURL)); if ( cf == null ) { throw new IllegalArgumentException("Configuration couldn't be created from " + cfURL); } configure("configurator", cf); targetCfURL = System.getProperty("target.conf.URL"); if ( targetCfURL == null || "".equals(targetCfURL) ) { throw new IllegalArgumentException("You must set the 'target.conf.URL' environment variable to point to the target configuration"); } URL targetURL = new URL(targetCfURL); try { targetCf = (new ConfigurationFactory()).getConfiguration(targetURL); } catch ( Throwable t ) { complain(LOG_WARNING, CH_CF, "Configuration not found at '" + targetCfURL + "', new one will be created. The cause was:" + t.getMessage()); targetCf = new TextConfiguration(); } if ( !targetCfURL.startsWith("file:") ) { complain(LOG_WARNING, CH_CF, "Writing to '" + targetURL.getProtocol() + "' protocol URL is not supported, you will not be able to save the configuration"); } mainFrame = new JFrame("DIY Zoning Configurator: " + targetCfURL); mainFrame.setSize(new Dimension(800, 600)); mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); mainMenuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); file_save = new JMenuItem("Save"); file_exit = new JMenuItem("Exit"); fileMenu.add(file_save); fileMenu.addSeparator(); fileMenu.add(file_exit); mainMenuBar.add(fileMenu); JMenu helpMenu = new JMenu("Help"); help_about = new JMenuItem("About"); helpMenu.add(help_about); // VT: FIXME: It is possible to add the "Help" submenu at the right // by inserting a BoxLayout element, but the hell with it for now - // there are more important things to do mainMenuBar.add(helpMenu); mainFrame.setJMenuBar(mainMenuBar); file_save.addActionListener(this); file_exit.addActionListener(this); help_about.addActionListener(this); cfTree = createTree(); cfTree.addTreeSelectionListener(this); cfTreeScroll = new JScrollPane(cfTree); cfTreeScroll.setMinimumSize(new Dimension(200, 300)); helpPane = new JTextPane(); helpPane.setEditable(false); helpPaneScroll = new JScrollPane(helpPane); helpPaneScroll.setMinimumSize(new Dimension(400, 200)); nodeDisplay = new JPanel(); nodeDisplay.setMinimumSize(new Dimension(400, 200)); rightSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, nodeDisplay, helpPaneScroll); rightSplit.setOneTouchExpandable(true); mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, cfTreeScroll, rightSplit); mainSplit.setDividerLocation(200); mainSplit.setOneTouchExpandable(true); // VT: NOTE: Can't do this before the rest of the stuff is // initialized cfTree.setSelectionRow(0); // VT: FIXME: Expand the tree so the first three levels (house, // unit, zone) are visible error = new JTextPane(); errorScroll = new JScrollPane(error); errorScroll.setMinimumSize(new Dimension(300, 50)); errorScroll.setBorder(BorderFactory.createTitledBorder("Status")); helpSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, mainSplit, errorScroll); mainFrame.getContentPane().add(helpSplit); //mainFrame.pack(); mainFrame.setVisible(true); } protected void shutdown(Throwable cause) throws Throwable { } /** * Create the configuration tree. * * @return The complete configuration tree. May be empty (if the * configuration didn't exist). */ private JTree createTree() { HouseAnchor ha = new HouseAnchor(targetCf.getString("dz.name", "My House")); DefaultMutableTreeNode root = new DefaultMutableTreeNode(ha); JTree cfTree = new JTree(root); cfTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); List unitList = targetCf.getList("dz.unit"); complain(LOG_INFO, CH_CF, "Units found: " + unitList); for ( Iterator i = unitList.iterator(); i.hasNext(); ) { createUnit(i.next().toString(), root); } return cfTree; } /** * Create the tree node and attach it to the tree, based on configuration. * * @param unitName Unit name to look up in the configuration. * * @param root Tree node to attach it to. */ private void createUnit(String unitName, DefaultMutableTreeNode root) { DefaultMutableTreeNode unitNode = new DefaultMutableTreeNode(new UnitAnchor(unitName)); createUnitController(unitName, unitNode); createZoneController(unitName, unitNode); List zoneList = targetCf.getList("dz.unit." + unitName + ".zone"); complain(LOG_INFO, CH_CF, "Zones found in '" + unitName + "': " + zoneList); for ( Iterator i = zoneList.iterator(); i.hasNext(); ) { createZone(unitName, i.next().toString(), unitNode); } root.add(unitNode); } /** * Create a new unit subtree from scratch. * * It will contain a HVAC Controller subtree and Zone Controller * subtree, but no zones. The name will default to "New Unit". */ private void createNewUnit() { // Find a root TreePath rootPath = cfTree.getPathForRow(0); DefaultMutableTreeNode root = (DefaultMutableTreeNode)rootPath.getLastPathComponent(); // Bold assumption: root is not null, even if the tree panel is // collapsed. String unitName = "New Unit"; DefaultMutableTreeNode unitNode = new DefaultMutableTreeNode(new UnitAnchor(unitName)); createNewUnitController(unitName, unitNode); createNewZoneController(unitName, unitNode); ((DefaultTreeModel)cfTree.getModel()).insertNodeInto(unitNode, root, root.getChildCount()); cfTree.scrollPathToVisible(new TreePath(unitNode.getPath())); } private void createUnitController(String unitName, DefaultMutableTreeNode unitNode) { DefaultMutableTreeNode unitControllerNode = new DefaultMutableTreeNode(new ImmutableAnchor("HVAC Controller", "hvac-controller.html")); DefaultMutableTreeNode unitControllerClassNode = new DefaultMutableTreeNode(new ClassAnchor(targetCf.getString("dz.unit." + unitName + ".class", "N/A"), "hvac-controller-class.html", "configurator.ac.controller.class")); DefaultMutableTreeNode unitDriverNode = new DefaultMutableTreeNode(new ImmutableAnchor("HVAC Driver", "hvac-driver.html")); DefaultMutableTreeNode unitDriverClassNode = new DefaultMutableTreeNode(new ClassAnchor(targetCf.getString("dz.unit." + unitName + ".driver.class", "N/A"), "hvac-driver-class.html", "configurator.ac.driver.class")); unitDriverNode.add(unitDriverClassNode); unitControllerNode.add(unitControllerClassNode); unitControllerNode.add(unitDriverNode); unitNode.add(unitControllerNode); } private void createNewUnitController(String unitName, DefaultMutableTreeNode unitNode) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -