mapzonespage.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 201 行

JAVA
201
字号
package net.sf.dz.setup.core;import java.awt.event.ActionEvent;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.SortedSet;import java.util.Vector;import javax.swing.JButton;import javax.swing.JList;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import org.freehold.jukebox.logger.Logger;import net.sf.dz.util.wizard.DoubleListPanel;import net.sf.dz.util.wizard.Wizard;import net.sf.dz.util.wizard.WizardPage;class MapZonesPage extends WizardPage {    private JList zoneList;    private JList unitList;        private JButton assignButton;    private JButton detachButton;        private DoubleListPanel dlp;        public MapZonesPage(Logger logger, Wizard owner) {            super(logger, owner, "Map Servos to Temperature Zones");        List buttonNames = new LinkedList();                buttonNames.add("Assign >>");        buttonNames.add("<< Detach");                dlp = new DoubleListPanel(getContentPane(), this,                                  "Temperature Zones",                                  "Units",                                  buttonNames);                assignButton = dlp.getButton(0);        detachButton = dlp.getButton(1);                zoneList = dlp.leftList;        unitList = dlp.rightList;                zoneList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        unitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);                assignButton.setEnabled(false);        detachButton.setEnabled(false);    }    public void activate() {        SortedSet zoneSet = getSortedContextValues("zone.");        SortedSet unitSet = getSortedContextValues("unit.");                // Take care to preserve existing selection, if any                Object selected = null;                if ( zoneList.getSelectedIndex() != -1 ) {                    selected = zoneList.getSelectedValue();        }                    zoneList.setListData(new Vector(zoneSet));                if ( selected != null ) {                    zoneList.setSelectedValue(selected, true);        } else if ( !zoneSet.isEmpty() ) {                    zoneList.setSelectedIndex(0);        }                if ( unitList.getSelectedIndex() != -1 ) {                    selected = unitList.getSelectedValue();        }                unitList.setListData(new Vector(unitSet));                if ( selected != null ) {                    unitList.setSelectedValue(selected, true);        } else if ( !unitSet.isEmpty() ) {                    unitList.setSelectedIndex(0);        }            }        public String validate() {            UnitDescriptor ud = null;        ZoneDescriptor zd = null;                // Check which buttons have to be enabled must happen before the        // validation check, otherwise the buttons will just stay        // disabled        if (zoneList.getSelectedIndex() != -1) {                    zd =  (ZoneDescriptor)zoneList.getSelectedValue();        }                if (unitList.getSelectedIndex() != -1) {                    ud = (UnitDescriptor)unitList.getSelectedValue();        }                assignButton.setEnabled(ud != null && zd != null                             && !ud.zoneSet.contains(zd) && zd.unit == null);        detachButton.setEnabled(zd != null && zd.unit != null);                // Validation succeeds when there are no unassigned zones, and        // no units that don't have any zones assigned to them.                Map context = getOwner().getContext();                for ( Iterator i = context.keySet().iterator(); i.hasNext(); ) {                    String key = i.next().toString();                        if ( key.startsWith("zone.") ) {                            zd = (ZoneDescriptor)context.get(key);                                if ( zd.unit == null ) {                                    return "Some zones (at least'" + zd.name + "') aren't assigned - assign, or delete zone";                }            }        }                for ( Iterator i = context.keySet().iterator(); i.hasNext(); ) {                    String key = i.next().toString();                        if ( key.startsWith("unit.") ) {                            ud = (UnitDescriptor)context.get(key);                                if ( ud.zoneSet.isEmpty() ) {                                    return "Some units (at least '" + ud.name + "') don't have zones assigned - assign, or delete unit";                }            }        }                return "";    }        public boolean isEnabled() {            return true;    }        public String getHelpURL() {            return "FIXME";    }        public void actionPerformed2(ActionEvent e) {                Object source = e.getSource();        UnitDescriptor ud = (UnitDescriptor)unitList.getSelectedValue();        ZoneDescriptor zd = (ZoneDescriptor)zoneList.getSelectedValue();                if ( assignButton == source ) {                    ud.zoneSet.add(zd);            zd.unit = ud;                } else if ( detachButton == source ) {                    zd.unit.zoneSet.remove(zd);            zd.unit = null;        }                zoneList.validate();        unitList.validate();                zoneList.repaint();        unitList.repaint();    }        public void valueChanged2(ListSelectionEvent e) {        }}

⌨️ 快捷键说明

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