📄 addhvacunitpage.java
字号:
package net.sf.dz.setup.core;import java.awt.GridBagConstraints;import java.awt.GridBagLayout; import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.SortedSet;import java.util.StringTokenizer;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JCheckBox;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import org.freehold.jukebox.logger.Logger;import net.sf.dz.pnp.MulticastClient;import net.sf.dz.pnp.MulticastEvent;import net.sf.dz.pnp.MulticastEventListener;import net.sf.dz.util.wizard.DoublePanel;import net.sf.dz.util.wizard.Wizard;import net.sf.dz.util.wizard.WizardPage;/** * Wizard panel that handles HVAC unit configuration management. * * @author Copyright © <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2004 * @version $Id: AddHvacUnitPage.java,v 1.12 2004/07/28 07:22:52 vtt Exp $ */class AddHvacUnitPage extends WizardPage implements MulticastEventListener { /** * List of configured HVAC units. */ private JList unitList = new JList(); /** * 'Add the unit' button. */ private JButton addButton; /** * 'Delete the unit' button. */ private JButton deleteButton; /** * A helper panel container. */ private DoublePanel dp; /** * A placeholder for situations when no unit is selected. */ JLabel noUnitLabel = new JLabel("Nothing to configure", JLabel.CENTER); /** * A HVAC unit configuration panel. */ HvacUnitConfPanel hup = null; private Map eventBySource = new HashMap(); /** * Create an instance. * * @param logger Logger to use. * * @param owner Wizard that owns this page. */ public AddHvacUnitPage(Logger logger, Wizard owner) { super(logger, owner, "Add HVAC Unit"); List buttonNames = new LinkedList(); buttonNames.add("Add"); buttonNames.add("Delete"); dp = new DoublePanel(getContentPane(), this, buttonNames); addButton = dp.getButton(0); deleteButton = dp.getButton(1); deleteButton.setEnabled(false); dp.leftPanel.setBorder(BorderFactory.createTitledBorder("HVAC Units")); dp.rightPanel.setBorder(BorderFactory.createTitledBorder("Unit Properties")); dp.add(dp.rightPanel, noUnitLabel); JScrollPane leftScroller = new JScrollPane(unitList); leftScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); leftScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); dp.add(dp.leftPanel, leftScroller); unitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); unitList.addListSelectionListener(this); } public void activate() { parseUnits(); } public String getHelpURL() { return "FIXME"; } /** * Validate the panel state. * * At least one unit must be configured before we can proceed. */ public String validate() { if ( getContextValues("unit.").isEmpty() ) { return "At least one unit must be defined"; } if ( isVisible() ) { deleteButton.setEnabled(!getContextValues("unit.").isEmpty() && unitList.getSelectedValue() != null); } return ""; } public boolean isEnabled() { return true; } /** * Handle the button presses. * * Buttons handled are {@link #addButton add}, {@link #deleteButton * delete}. * * @param e Event to handle. */ public void actionPerformed2(ActionEvent e) { Object source = e.getSource(); if ( source == addButton ) { String name = (String)JOptionPane.showInputDialog( getContentPane(), "Enter a unit name", "Add a HVAC Unit", JOptionPane.QUESTION_MESSAGE); complain(LOG_DEBUG, CH_WP, "New unit: " + name); if ( name != null && !"".equals(name) ) { // See if it is a dupe if ( getOwner().getContext().get("unit." + name) != null ) { complain(LOG_DEBUG, CH_WP, "Duplicate name: " + name); return; } // All right, let's create it UnitDescriptor ud = new UnitDescriptor(name); getOwner().getContext().put("unit." + name, ud); parseUnits(); unitList.setSelectedValue(ud, true); // And create a configuration panel if necessary if ( hup == null ) { hup = new HvacUnitConfPanel(this, ud); dp.rightPanel.removeAll(); dp.add(dp.rightPanel, hup); } } } else if ( source == deleteButton ) { UnitDescriptor ud = (UnitDescriptor)unitList.getSelectedValue(); getOwner().getContext().remove("unit." + ud.name); parseUnits(); if ( getContextValues("unit.").isEmpty() ) { hup = null; dp.rightPanel.removeAll(); dp.add(dp.rightPanel, noUnitLabel); } for ( Iterator i = ud.zoneSet.iterator(); i.hasNext(); ) { ZoneDescriptor zd = (ZoneDescriptor)i.next(); zd.unit = null; i.remove(); } } } /** * Handle the list selection change. * * Upon selecting a unit in the {@link #unitList unit list}, the right * panel is populated with the unit configuration. * * @param e Event to handle. */ public void valueChanged2(ListSelectionEvent e) { UnitDescriptor ud = (UnitDescriptor)unitList.getSelectedValue(); if ( ud == null ) { hup = null; dp.rightPanel.removeAll(); dp.add(dp.rightPanel, noUnitLabel); dp.rightPanel.validate(); dp.rightPanel.repaint(); return; } if ( e.getValueIsAdjusting() ) { // This prevents double triggering return; } if ( ud == null ) { throw new IllegalStateException("Huh?"); } if ( hup == null ) { hup = new HvacUnitConfPanel(this, ud); dp.rightPanel.removeAll(); dp.add(dp.rightPanel, hup); dp.rightPanel.validate(); dp.rightPanel.repaint(); } hup.syncFrom(ud); } public void itemStateChanged2(ItemEvent e) { syncConf2Unit(); } private void parseUnits() { Set unitSet = getSortedContextValues("unit."); Object selected = unitList.getSelectedValue(); unitList.setListData(new Vector(unitSet)); if ( selected != null ) { unitList.setSelectedValue(selected, true); } else if ( !unitSet.isEmpty() ) { unitList.setSelectedIndex(0); } } /** * Clone the configuration from the {@link #hup HVAC unit panel} into a * unit descriptor for the currently selected HVAC unit in the {@link * #unitList list}. */ private void syncConf2Unit() { UnitDescriptor ud = (UnitDescriptor)unitList.getSelectedValue(); if ( hup == null ) { complain(LOG_WARNING, CH_WP, "Null hup?"); return; } hup.syncTo(ud); } public void multicastEventReceived(MulticastClient source, MulticastEvent e) { //complain(LOG_INFO, CH_WP, "PnP: " + e); eventBySource.put(e.getSource(), e); parseEvents(); } /** * Parse {@link #eventBySource event map} into objects * displayable in a list. */ private synchronized void parseEvents() { boolean modified = false; for ( Iterator i = eventBySource.keySet().iterator(); i.hasNext(); ) { Object key = i.next(); MulticastEvent e = (MulticastEvent)eventBySource.get(key); for ( StringTokenizer st = new StringTokenizer(e.getMessage(), " "); st.hasMoreTokens(); ) { String address = st.nextToken(); if ( getOwner().getContext().get("switch." + address) == null ) { // All right, we haven't seen this before SwitchDescriptor sd = new SwitchDescriptor(address); getOwner().getContext().put("switch." + address, sd); modified = true; } } } // If we're not modified, there's nothing to update if ( !modified ) { return; } if ( hup != null ) { SortedSet deviceSet = getSortedContextValues("switch."); // Take care to preserve existing selection, if any Object selected = hup.driverPanel.switchList.getSelectedValue(); hup.driverPanel.switchList.setListData(new Vector(deviceSet)); if ( selected != null ) { hup.driverPanel.switchList.setSelectedValue(selected, true); } } } /** * A panel that handles the HVAC unit configuration. */ protected class HvacUnitConfPanel extends JPanel implements ItemListener { /** * HVAC unit type. * * Known types at this point are: air conditioner, heat pump, * furnace, boiler. */ public final JComboBox hvacTypeCombo = new JComboBox(); /** * Energize mode selector. * * Some HVAC units require to be energized to enter the cooling * mode, some to enter the heating mode. This selector allows to * configure that. * * <p> * * This control is applicable only for a heat pump. */ public final JComboBox energizeCombo = new JComboBox(); /** * Default HVAC unit mode. * * This determines which mode (heating or cooling) the unit will be * put into on DZ startup. * * <p> * * This control is applicable only for a heat pump. */ public final JComboBox defaultModeCombo = new JComboBox(); /** * Emergency heat availability selector.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -