📄 addhvacunitpage.java
字号:
driverCombo.addItem("1-Wire (SwitchContainer)"); driverCombo.addItem("Serial (DTR)"); driverCombo.addItemListener(this); driverCombo.setSelectedIndex(0); setBorder(BorderFactory.createTitledBorder("Driver Configuration")); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; cs.gridheight = 1; cs.weightx = 1; cs.weighty = 0; cs.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(driverCombo, cs); add(driverCombo); cs.gridy++; cs.weighty = 1; cs.fill = GridBagConstraints.BOTH; layout.setConstraints(onewirePanel, cs); add(onewirePanel); layout.setConstraints(dtrPanel, cs); add(dtrPanel); renderOneWirePanel(); switchList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); switchList.addListSelectionListener(this); modeButton.addActionListener(this); compressorButton.addActionListener(this); fanButton.addActionListener(this); } public void syncFrom(UnitDescriptor ud) { Set switchSet = getSortedContextValues("switch."); Object selected = null; if ( switchList.getSelectedIndex() != -1 ) { selected = switchList.getSelectedValue(); } switchList.setListData(new Vector(switchSet)); // Some of these values may be undefined at this point modeAddressLabel.setText((String)ud.propertyMap.get("switch.mode.address")); compressorAddressLabel.setText((String)ud.propertyMap.get("switch.stage.address")); fanAddressLabel.setText((String)ud.propertyMap.get("switch.fan.address")); syncButtons(); // Need to restore the list selection after the button and label // content is in place, so the buttons get proper text if ( selected != null ) { switchList.setSelectedValue(selected, true); } else if ( !switchSet.isEmpty() ) { // We get here if there was no selection, but there were some // switchs - in other words, the first time switchList.setSelectedIndex(0); } } public void syncTo(UnitDescriptor ud) { String modeSwitchAddress = modeAddressLabel.getText(); String compressorSwitchAddress = compressorAddressLabel.getText(); String fanSwitchAddress = fanAddressLabel.getText(); modeSwitchAddress = ("".equals(modeSwitchAddress)) ? null : modeSwitchAddress; compressorSwitchAddress = ("".equals(compressorSwitchAddress)) ? null : compressorSwitchAddress; fanSwitchAddress = ("".equals(fanSwitchAddress)) ? null : fanSwitchAddress; ud.propertyMap.put("switch.mode.address", modeSwitchAddress); ud.propertyMap.put("switch.stage.address", compressorSwitchAddress); ud.propertyMap.put("switch.fan.address", fanSwitchAddress); } /** * Make sure the button texts correspond to address label contents. */ public void syncButtons() { checkButton(modeButton, modeAddressLabel); checkButton(compressorButton, compressorAddressLabel); checkButton(fanButton, fanAddressLabel); } private void checkButton(JButton button, JLabel addressLabel) { if ( addressLabel.getText() == null || "".equals(addressLabel.getText()) ) { // VT: FIXME: Additional checks required to see if a switch // is selected, and whether the switch is already assigned // to other buttons. button.setText("Assign"); } else { button.setText("Release"); } } private void renderOneWirePanel() { GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); onewirePanel.setLayout(layout); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; cs.gridheight = 1; cs.weightx = 0; cs.weighty = 0; cs.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(modeLabel, cs); onewirePanel.add(modeLabel); cs.gridx++; cs.weightx = 1; layout.setConstraints(modeAddressLabel, cs); onewirePanel.add(modeAddressLabel); cs.gridx++; cs.weightx = 0; layout.setConstraints(modeButton, cs); onewirePanel.add(modeButton); cs.gridx = 0; cs.gridy++; cs.weightx = 0; layout.setConstraints(compressorLabel, cs); onewirePanel.add(compressorLabel); cs.gridx++; cs.weightx = 1; layout.setConstraints(compressorAddressLabel, cs); onewirePanel.add(compressorAddressLabel); cs.gridx++; cs.weightx = 0; layout.setConstraints(compressorButton, cs); onewirePanel.add(compressorButton); cs.gridx = 0; cs.gridy++; cs.weightx = 0; layout.setConstraints(fanLabel, cs); onewirePanel.add(fanLabel); cs.gridx++; cs.weightx = 1; layout.setConstraints(fanAddressLabel, cs); onewirePanel.add(fanAddressLabel); cs.gridx++; cs.weightx = 0; layout.setConstraints(fanButton, cs); onewirePanel.add(fanButton); cs.gridx = 0; cs.gridy++; cs.weightx = 1; cs.weighty = 1; cs.gridwidth = 3; cs.fill = GridBagConstraints.BOTH; JScrollPane switchScroller = new JScrollPane(switchList); switchScroller.setBorder(BorderFactory.createTitledBorder("Switches available")); switchScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); switchScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); layout.setConstraints(switchScroller, cs); onewirePanel.add(switchScroller); modeButton.setEnabled(false); compressorButton.setEnabled(false); fanButton.setEnabled(false); modeAddressLabel.setHorizontalAlignment(JLabel.CENTER); compressorAddressLabel.setHorizontalAlignment(JLabel.CENTER); fanAddressLabel.setHorizontalAlignment(JLabel.CENTER); } /** * Handle a button press. * * @param e Event to handle. */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); checkButton(source, modeButton, modeAddressLabel); checkButton(source, compressorButton, compressorAddressLabel); checkButton(source, fanButton, fanAddressLabel); enable(modeAddressLabel, modeButton); enable(compressorAddressLabel, compressorButton); enable(fanAddressLabel, fanButton); // Now that we have the things properly displayed, let's make // sure the unit descriptor is current UnitDescriptor ud = (UnitDescriptor)unitList.getSelectedValue(); syncTo(ud); } /** * Check if the button pressed is the button checked, and then set * the address label and button text accordingly. * * @param source Button pressed. * * @param button Button to possibly change the text for ("Assign" or "Release"). * * @param addressLabel label to possibly change the text for (switch * address or empty string). */ private void checkButton(Object source, JButton button, JLabel addressLabel) { if ( source != button ) { return; } SwitchDescriptor sd = (SwitchDescriptor)switchList.getSelectedValue(); if ( addressLabel.getText() == null || "".equals(addressLabel.getText()) ) { // Assign addressLabel.setText(sd.address); button.setText("Release"); button.setEnabled(true); } else { // Release addressLabel.setText(""); button.setText("Assign"); } } public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if ( source == driverCombo ) { onewirePanel.setVisible(driverCombo.getSelectedIndex() == 0); dtrPanel.setVisible(driverCombo.getSelectedIndex() == 1); } } public void valueChanged(ListSelectionEvent e) { enable(modeAddressLabel, modeButton); enable(compressorAddressLabel, compressorButton); enable(fanAddressLabel, fanButton); } private void enable(JLabel addressLabel, JButton button) { if ( "".equals(addressLabel.getText()) ) { button.setText("Assign"); button.setEnabled(isAvailable()); } else { button.setText("Release"); button.setEnabled(true); } } /** * Check whether the currently selected switch is available (not * assigned to any other functions). * * @return true if it is available. */ private boolean isAvailable() { if ( switchList.getSelectedIndex() == -1 ) { return false; } SwitchDescriptor sd = (SwitchDescriptor)switchList.getSelectedValue(); // It is safe to check all the labels because we're being called // from the context where the label in question is certainly // empty if ( sd.address.equals(modeAddressLabel.getText()) || sd.address.equals(compressorAddressLabel.getText()) || sd.address.equals(fanAddressLabel.getText()) ) { return false; } return true; } public void setType(int type) { // VT: NOTE: Again, this is redundant // VT: FIXME: Need to decide what to do if it so happens that // first the switch address is selected, and then state changed // and address becomes locked into a disabled state // VT: FIXME: Buttons enabled state must be handled differently // - it depends on whether the address had been assigned // already, and whether there are available switches in the list switch ( type ) { case 0: // A/C - there's no mode switch modeLabel.setEnabled(false); modeAddressLabel.setEnabled(false); modeButton.setEnabled(false); compressorLabel.setEnabled(true); compressorAddressLabel.setEnabled(true); compressorButton.setEnabled(true); fanLabel.setEnabled(true); fanAddressLabel.setEnabled(true); fanButton.setEnabled(true); break; case 1: // Heat Pump modeLabel.setEnabled(true); modeAddressLabel.setEnabled(true); modeButton.setEnabled(true); compressorLabel.setEnabled(true); compressorAddressLabel.setEnabled(true); compressorButton.setEnabled(true); fanLabel.setEnabled(true); fanAddressLabel.setEnabled(true); fanButton.setEnabled(true); break; case 2: // Furnace - there's no mode switch modeLabel.setEnabled(false); modeAddressLabel.setEnabled(false); modeButton.setEnabled(false); compressorLabel.setEnabled(true); compressorAddressLabel.setEnabled(true); compressorButton.setEnabled(true); fanLabel.setEnabled(true); fanAddressLabel.setEnabled(true); fanButton.setEnabled(true); break; case 3: // Boiler - there's no mode and no fan switch, just // compressor (actually, stage) modeLabel.setEnabled(false); modeAddressLabel.setEnabled(false); modeButton.setEnabled(false); compressorLabel.setEnabled(true); compressorAddressLabel.setEnabled(true); compressorButton.setEnabled(true); fanLabel.setEnabled(false); fanAddressLabel.setEnabled(false); fanButton.setEnabled(false); break; default: throw new IllegalArgumentException("Invalid HVAC type (" + type + "), valid are 0..3"); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -