Search in sources :

Example 81 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class BeanTableDataModel method copyName.

public void copyName(int row, int column) {
    NamedBean nBean = getBySystemName(sysNameList.get(row));
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection name = new StringSelection(nBean.getUserName());
    clipboard.setContents(name, null);
}
Also used : NamedBean(jmri.NamedBean) Clipboard(java.awt.datatransfer.Clipboard) StringSelection(java.awt.datatransfer.StringSelection)

Example 82 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class BeanTableDataModel method setValueAt.

@Override
public void setValueAt(Object value, int row, int col) {
    switch(col) {
        case USERNAMECOL:
            // check to see if user name already exists
            if (((String) value).equals("")) {
                value = null;
            } else {
                NamedBean nB = getByUserName((String) value);
                if (nB != null) {
                    log.error("User name is not unique " + value);
                    String msg = Bundle.getMessage("WarningUserName", new Object[] { ("" + value) });
                    JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            NamedBean nBean = getBySystemName(sysNameList.get(row));
            nBean.setUserName((String) value);
            if (nbMan.inUse(sysNameList.get(row), nBean)) {
                String msg = Bundle.getMessage("UpdateToUserName", new Object[] { getBeanType(), value, sysNameList.get(row) });
                int optionPane = JOptionPane.showConfirmDialog(null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
                if (optionPane == JOptionPane.YES_OPTION) {
                    //This will update the bean reference from the systemName to the userName
                    try {
                        nbMan.updateBeanFromSystemToUser(nBean);
                    } catch (JmriException ex) {
                    //We should never get an exception here as we already check that the username is not valid
                    }
                }
            }
            fireTableRowsUpdated(row, row);
            break;
        case COMMENTCOL:
            getBySystemName(sysNameList.get(row)).setComment((String) value);
            fireTableRowsUpdated(row, row);
            break;
        case VALUECOL:
            // button fired, swap state
            NamedBean t = getBySystemName(sysNameList.get(row));
            clickOn(t);
            break;
        case DELETECOL:
            // button fired, delete Bean
            deleteBean(row, col);
            break;
        default:
            break;
    }
}
Also used : NamedBean(jmri.NamedBean) JmriException(jmri.JmriException)

Example 83 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class SignalHeadTableAction method createModel.

/**
     * Create the JTable DataModel, along with the changes for the specific case
     * of SignalHeads.
     */
@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int LITCOL = NUMCOLUMN;

        public static final int HELDCOL = LITCOL + 1;

        public static final int EDITCOL = HELDCOL + 1;

        @Override
        public int getColumnCount() {
            return NUMCOLUMN + 3;
        }

        @Override
        public String getColumnName(int col) {
            if (col == VALUECOL) {
                // override default title, correct name SignalHeadAppearance i.e. "Red"
                return Bundle.getMessage("SignalMastAppearance");
            } else if (col == LITCOL) {
                return Bundle.getMessage("ColumnHeadLit");
            } else if (col == HELDCOL) {
                return Bundle.getMessage("ColumnHeadHeld");
            } else if (col == EDITCOL) {
                // no heading on "Edit"
                return "";
            } else {
                return super.getColumnName(col);
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == VALUECOL) {
                // Use a JPanel containing a custom Appearance ComboBox
                return RowComboBoxPanel.class;
            } else if (col == LITCOL) {
                return Boolean.class;
            } else if (col == HELDCOL) {
                return Boolean.class;
            } else if (col == EDITCOL) {
                return JButton.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            if (col == LITCOL) {
                return new JTextField(4).getPreferredSize().width;
            } else if (col == HELDCOL) {
                return new JTextField(4).getPreferredSize().width;
            } else if (col == EDITCOL) {
                return new JTextField(7).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == LITCOL) {
                return true;
            } else if (col == HELDCOL) {
                return true;
            } else if (col == EDITCOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= sysNameList.size()) {
                log.debug("row is greater than name list");
                return "error";
            }
            String name = sysNameList.get(row);
            SignalHead s = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
            if (s == null) {
                // if due to race condition, the device is going away
                return Boolean.valueOf(false);
            }
            if (col == LITCOL) {
                boolean val = s.getLit();
                return Boolean.valueOf(val);
            } else if (col == HELDCOL) {
                boolean val = s.getHeld();
                return Boolean.valueOf(val);
            } else if (col == EDITCOL) {
                return Bundle.getMessage("ButtonEdit");
            } else if (col == VALUECOL) {
                try {
                    return s.getAppearanceName();
                } catch (java.lang.NullPointerException e) {
                    //Appearance (head) not set
                    log.debug("Appearance for head {} not set", row);
                    // use place holder string in table
                    return Bundle.getMessage("BeanStateUnknown");
                }
            } else {
                return super.getValueAt(row, col);
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            String name = sysNameList.get(row);
            SignalHead s = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
            if (s == null) {
                // device is going away anyway
                return;
            }
            if (col == VALUECOL) {
                if ((String) value != null) {
                    //row = table.convertRowIndexToModel(row); // find the right row in model instead of table (not needed here)
                    log.debug("SignalHead setValueAt (rowConverted={}; value={})", row, value);
                    // convert from String (selected item) to int
                    int newState = 99;
                    // Array of valid appearance names
                    String[] stateNameList = s.getValidStateNames();
                    // Array of valid appearance numbers
                    int[] validStateList = s.getValidStates();
                    for (int i = 0; i < stateNameList.length; i++) {
                        if (value.equals(stateNameList[i])) {
                            newState = validStateList[i];
                            break;
                        }
                    }
                    if (newState == 99) {
                        if (stateNameList.length == 0) {
                            newState = SignalHead.DARK;
                            log.warn("New signal state not found so setting to Dark " + s.getDisplayName());
                        } else {
                            newState = validStateList[0];
                            log.warn("New signal state not found so setting to the first available " + s.getDisplayName());
                        }
                    }
                    if (log.isDebugEnabled()) {
                        String oldAppearanceName = s.getAppearanceName();
                        log.debug("Signal Head set from: {} to: {} [{}]", oldAppearanceName, value, newState);
                    }
                    s.setAppearance(newState);
                    fireTableRowsUpdated(row, row);
                }
            } else if (col == LITCOL) {
                boolean b = ((Boolean) value).booleanValue();
                s.setLit(b);
            } else if (col == HELDCOL) {
                boolean b = ((Boolean) value).booleanValue();
                s.setHeld(b);
            } else if (col == EDITCOL) {
                // button clicked - edit
                editSignal(row);
            } else {
                super.setValueAt(value, row, col);
            }
        }

        @Override
        public String getValue(String name) {
            SignalHead s = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
            if (s == null) {
                // if due to race condition, the device is going away
                return "<lost>";
            }
            String val = null;
            try {
                val = s.getAppearanceName();
            } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                log.error(e.getLocalizedMessage(), e);
            }
            if (val != null) {
                return val;
            } else {
                return "Unexpected null value";
            }
        }

        @Override
        public Manager getManager() {
            return InstanceManager.getDefault(jmri.SignalHeadManager.class);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
        }

        @Override
        public NamedBean getByUserName(String name) {
            return InstanceManager.getDefault(jmri.SignalHeadManager.class).getByUserName(name);
        }

        /*public int getDisplayDeleteMsg() { return InstanceManager.getDefault(jmri.UserPreferencesManager.class).getMultipleChoiceOption(getClassName(),"delete"); }
             public void setDisplayDeleteMsg(int boo) { InstanceManager.getDefault(jmri.UserPreferencesManager.class).setMultipleChoiceOption(getClassName(), "delete", boo); }*/
        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        // no longer used since 4.7.1, but have to override
        @Deprecated
        @Override
        public void clickOn(NamedBean t) {
            int oldState = ((SignalHead) t).getAppearance();
            int newState = 99;
            // getValidAppearances((String)
            int[] stateList = ((SignalHead) t).getValidStates();
            for (int i = 0; i < stateList.length; i++) {
                if (oldState == stateList[i]) {
                    if (i < stateList.length - 1) {
                        newState = stateList[i + 1];
                        break;
                    } else {
                        newState = stateList[0];
                        break;
                    }
                }
            }
            if (newState == 99) {
                if (stateList.length == 0) {
                    newState = SignalHead.DARK;
                    log.warn("New signal state not found so setting to Dark " + t.getDisplayName());
                } else {
                    newState = stateList[0];
                    log.warn("New signal state not found so setting to the first available " + t.getDisplayName());
                }
            }
            log.debug("was " + oldState + " becomes " + newState);
            ((SignalHead) t).setAppearance(newState);
        }

        /**
             * Set column width.
             * @return a button to fit inside the VALUE column
             */
        @Override
        public JButton configureButton() {
            // pick a large size
            // about the longest Appearance string
            JButton b = new JButton(Bundle.getMessage("SignalHeadStateYellow"));
            b.putClientProperty("JComponent.sizeVariant", "small");
            b.putClientProperty("JButton.buttonType", "square");
            return b;
        }

        @Override
        public boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            if (e.getPropertyName().indexOf("Lit") >= 0 || e.getPropertyName().indexOf("Held") >= 0 || e.getPropertyName().indexOf("ValidStatesChanged") >= 0) {
                return true;
            } else {
                return super.matchPropertyName(e);
            }
        }

        @Override
        protected String getBeanType() {
            return Bundle.getMessage("BeanNameSignalHead");
        }

        /**
             * Respond to change from bean. Prevent Appearance change when Signal Head is set to Hold or Unlit.
             * @param e A property change of any bean
             */
        @Override
        public // At present, does not work/change when head Lit/Held checkboxes are (de)activated
        void propertyChange(java.beans.PropertyChangeEvent e) {
            if (e.getPropertyName().indexOf("Lit") < 0 || e.getPropertyName().indexOf("Held") >= 0 || e.getPropertyName().indexOf("ValidStatesChanged") >= 0) {
                if (e.getSource() instanceof NamedBean) {
                    String name = ((NamedBean) e.getSource()).getSystemName();
                    if (log.isDebugEnabled()) {
                        log.debug("Update cell {}, {} for {}", sysNameList.indexOf(name), VALUECOL, name);
                    }
                    // since we can add columns, the entire row is marked as updated
                    int row = sysNameList.indexOf(name);
                    this.fireTableRowsUpdated(row, row);
                    // activate this method below
                    clearAppearanceVector(row);
                }
            }
            super.propertyChange(e);
        }

        /**
             * Customize the SignalHead Value (Appearance) column to show an appropriate ComboBox of available Appearances
             * when the TableDataModel is being called from ListedTableAction.
             * @param table a JTable of Signal Head
             */
        @Override
        protected void configValueColumn(JTable table) {
            // have the value column hold a JPanel with a JComboBox for Appearances
            setColumnToHoldButton(table, VALUECOL, configureButton());
            // add extras, override BeanTableDataModel
            log.debug("Head configValueColumn (I am {})", super.toString());
            table.setDefaultEditor(RowComboBoxPanel.class, new AppearanceComboBoxPanel());
            // use same class for the renderer
            table.setDefaultRenderer(RowComboBoxPanel.class, new AppearanceComboBoxPanel());
        // Set more things?
        }

        /**
             * A row specific Appearance combobox cell editor/renderer.
             */
        class AppearanceComboBoxPanel extends RowComboBoxPanel {

            @Override
            protected final void eventEditorMousePressed() {
                // add editorBox to JPanel
                this.editor.add(getEditorBox(table.convertRowIndexToModel(this.currentRow)));
                this.editor.revalidate();
                SwingUtilities.invokeLater(this.comboBoxFocusRequester);
                log.debug("eventEditorMousePressed in row: {})", this.currentRow);
            }

            /**
                 * Call the method in the surrounding method for the SignalHeadTable.
                 * @param row the user clicked on in the table
                 * @return an appropriate combobox for this signal head
                 */
            @Override
            protected JComboBox getEditorBox(int row) {
                return getAppearanceEditorBox(row);
            }
        }

        // Methods to display VALUECOL (appearance) ComboBox in the Signal Head Table
        // Derived from the SignalMastJTable class (deprecated since 4.5.5):
        // All row values are in terms of the Model, not the Table as displayed.
        /**
             * Clear the old appearance comboboxes and force them to be rebuilt.
             * Used with the Single Output Signal Head to capture reconguration.
             * @param row Index of the signal mast (in TableDataModel) to be rebuilt in the Hashtables
             */
        public void clearAppearanceVector(int row) {
            boxMap.remove(this.getValueAt(row, SYSNAMECOL));
            editorMap.remove(this.getValueAt(row, SYSNAMECOL));
        }

        // Hashtables for Editors; not used for Renderer)
        /**
             * Provide a JComboBox element to display inside the JPanel CellEditor.
             * When not yet present, create, store and return a new one.
             * @param row Index number (in TableDataModel)
             * @return A combobox containing the valid appearance names for this mast
             */
        public JComboBox getAppearanceEditorBox(int row) {
            JComboBox editCombo = editorMap.get(this.getValueAt(row, SYSNAMECOL));
            if (editCombo == null) {
                // create a new one with correct appearances
                editCombo = new JComboBox<String>(getRowVector(row));
                editorMap.put(this.getValueAt(row, SYSNAMECOL), editCombo);
            }
            return editCombo;
        }

        Hashtable<Object, JComboBox> editorMap = new Hashtable<Object, JComboBox>();

        /**
             * returns a list of all the valid appearances that have not been disabled
             * @param head the name of the signal head
             * @return List of valid signal head appearance names
             */
        public Vector<String> getValidAppearances(String head) {
            // convert String[] validStateNames to Vector
            String[] app = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(head).getValidStateNames();
            Vector<String> v = new Vector<String>();
            for (int i = 0; i < app.length; i++) {
                String appearance = app[i];
                v.add(appearance);
            }
            return v;
        }

        /**
             * Holds a Hashtable of valid appearances per signal head,
             * used by getEditorBox()
             * @param row Index number (in TableDataModel)
             * @return The Vector of valid appearance names for this mast to show in the JComboBox
             */
        Vector<String> getRowVector(int row) {
            Vector<String> comboappearances = boxMap.get(this.getValueAt(row, SYSNAMECOL));
            if (comboappearances == null) {
                // create a new one with right appearance
                Vector<String> v = getValidAppearances((String) this.getValueAt(row, SYSNAMECOL));
                comboappearances = v;
                boxMap.put(this.getValueAt(row, SYSNAMECOL), comboappearances);
            }
            return comboappearances;
        }

        Hashtable<Object, Vector<String>> boxMap = new Hashtable<Object, Vector<String>>();
    };
}
Also used : NamedBean(jmri.NamedBean) JButton(javax.swing.JButton) QuadOutputSignalHead(jmri.implementation.QuadOutputSignalHead) DccSignalHead(jmri.implementation.DccSignalHead) DoubleTurnoutSignalHead(jmri.implementation.DoubleTurnoutSignalHead) TripleTurnoutSignalHead(jmri.implementation.TripleTurnoutSignalHead) SignalHead(jmri.SignalHead) SingleTurnoutSignalHead(jmri.implementation.SingleTurnoutSignalHead) TripleOutputSignalHead(jmri.implementation.TripleOutputSignalHead) JTextField(javax.swing.JTextField) InstanceManager(jmri.InstanceManager) Manager(jmri.Manager) Vector(java.util.Vector) JComboBox(javax.swing.JComboBox) Hashtable(java.util.Hashtable) JTable(javax.swing.JTable)

Example 84 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class SignalMastLogicTableAction method createModel.

@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int SOURCECOL = 0;

        public static final int SOURCEAPPCOL = 1;

        public static final int DESTCOL = 2;

        public static final int DESTAPPCOL = 3;

        public static final int COMCOL = 4;

        public static final int DELCOL = 5;

        public static final int ENABLECOL = 6;

        public static final int EDITLOGICCOL = 7;

        //We have to set a manager first off, but this gets replaced.
        @Override
        protected SignalMastLogicManager getManager() {
            return InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
        }

        /*public EcosLocoAddress getByDccAddress(int address) {return getManager().getByDccAddress(address);}*/
        @Override
        public String getValue(String s) {
            return "Set";
        }

        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        @Override
        public void clickOn(jmri.NamedBean t) {
        }

        @Override
        protected synchronized void updateNameList() {
            // first, remove listeners from the individual objects
            if (signalMastLogicList != null) {
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    // if object has been deleted, it's not here; ignore it
                    Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                    Enumeration<SignalMastLogic> en = b.keys();
                    while (en.hasMoreElements()) {
                        SignalMastLogic sm = en.nextElement();
                        SignalMast dest = b.get(sm);
                        sm.removePropertyChangeListener(this);
                        sm.getSourceMast().removePropertyChangeListener(this);
                        dest.removePropertyChangeListener(this);
                    }
                }
            }
            ArrayList<SignalMastLogic> source = getManager().getSignalMastLogicList();
            signalMastLogicList = new ArrayList<Hashtable<SignalMastLogic, SignalMast>>();
            for (int i = 0; i < source.size(); i++) {
                ArrayList<SignalMast> destList = source.get(i).getDestinationList();
                source.get(i).addPropertyChangeListener(this);
                source.get(i).getSourceMast().addPropertyChangeListener(this);
                for (int j = 0; j < destList.size(); j++) {
                    Hashtable<SignalMastLogic, SignalMast> hash = new Hashtable<SignalMastLogic, SignalMast>(1);
                    hash.put(source.get(i), destList.get(j));
                    destList.get(j).addPropertyChangeListener(this);
                    signalMastLogicList.add(hash);
                }
            }
        }

        //Will need to redo this so that we work out the row number from looking in the signalmastlogiclist.
        @Override
        public void propertyChange(java.beans.PropertyChangeEvent e) {
            if (suppressUpdate) {
                return;
            }
            // updateNameList();
            if (e.getPropertyName().equals("length") || e.getPropertyName().equals("updatedDestination") || e.getPropertyName().equals("updatedSource")) {
                updateNameList();
                log.debug("Table changed length to " + signalMastLogicList.size());
                fireTableDataChanged();
            } else if (e.getSource() instanceof SignalMastLogic) {
                SignalMastLogic logic = (SignalMastLogic) e.getSource();
                if (matchPropertyName(e)) {
                    for (int i = 0; i < signalMastLogicList.size(); i++) {
                        Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                        Enumeration<SignalMastLogic> en = b.keys();
                        while (en.hasMoreElements()) {
                            SignalMastLogic sm = en.nextElement();
                            if (sm == logic) {
                                fireTableRowsUpdated(i, i);
                            }
                        }
                    }
                }
            } else if (e.getSource() instanceof jmri.SignalMast) {
                jmri.SignalMast sigMast = (jmri.SignalMast) e.getSource();
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                    Enumeration<SignalMastLogic> en = b.keys();
                    while (en.hasMoreElements()) {
                        SignalMastLogic sm = en.nextElement();
                        //SignalMast dest = b.get(sm);
                        if (sm.getSourceMast() == sigMast) {
                            fireTableRowsUpdated(i, i);
                        }
                    }
                }
            }
        }

        //}
        /**
             * Is this property event announcing a change this table should
             * display?
             * <P>
             * Note that events will come both from the NamedBeans and also from
             * the manager
             */
        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return ((e.getPropertyName().indexOf("Comment") >= 0) || (e.getPropertyName().indexOf("Enable") >= 0));
        }

        @Override
        public int getColumnCount() {
            return EDITLOGICCOL + 1;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if (col == COMCOL) {
                getLogicFromRow(row).setComment((String) value, getDestMastFromRow(row));
            } else if (col == EDITLOGICCOL) {
                class WindowMaker implements Runnable {

                    int row;

                    WindowMaker(int r) {
                        row = r;
                    }

                    @Override
                    public void run() {
                        editLogic(row, 0);
                    }
                }
                WindowMaker t = new WindowMaker(row);
                javax.swing.SwingUtilities.invokeLater(t);
            } else if (col == DELCOL) {
                // button fired, delete Bean
                deleteLogic(row, col);
            } else if (col == ENABLECOL) {
                boolean enable = ((Boolean) value).booleanValue();
                if (enable) {
                    getLogicFromRow(row).setEnabled(getDestMastFromRow(row));
                } else {
                    getLogicFromRow(row).setDisabled(getDestMastFromRow(row));
                }
            }
        }

        @Override
        public String getColumnName(int col) {
            switch(col) {
                case SOURCECOL:
                    return Bundle.getMessage("Source");
                case DESTCOL:
                    return Bundle.getMessage("Destination");
                case SOURCEAPPCOL:
                    return Bundle.getMessage("LabelAspectType");
                case DESTAPPCOL:
                    return Bundle.getMessage("LabelAspectType");
                case COMCOL:
                    return Bundle.getMessage("Comment");
                case DELCOL:
                    // override default, no title for Delete column
                    return "";
                case EDITLOGICCOL:
                    // override default, no title for Edit column
                    return "";
                case ENABLECOL:
                    return Bundle.getMessage("ColumnHeadEnabled");
                default:
                    return "unknown";
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            switch(col) {
                case SOURCECOL:
                case DESTCOL:
                case SOURCEAPPCOL:
                case COMCOL:
                case DESTAPPCOL:
                    return String.class;
                case ENABLECOL:
                    return Boolean.class;
                case EDITLOGICCOL:
                case DELCOL:
                    return JButton.class;
                default:
                    return null;
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            switch(col) {
                case COMCOL:
                case EDITLOGICCOL:
                case DELCOL:
                case ENABLECOL:
                    return true;
                default:
                    return false;
            }
        }

        void editLogic(int row, int col) {
            sigLog.setMast(getLogicFromRow(row).getSourceMast(), getDestMastFromRow(row));
            sigLog.actionPerformed(null);
        }

        void deleteLogic(int row, int col) {
            //This needs to be looked at
            InstanceManager.getDefault(jmri.SignalMastLogicManager.class).removeSignalMastLogic(getLogicFromRow(row), getDestMastFromRow(row));
        }

        public SignalMast getDestMastFromRow(int row) {
            // if object has been deleted, it's not here; ignore it
            Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(row);
            Enumeration<SignalMastLogic> en = b.keys();
            while (en.hasMoreElements()) {
                return b.get(en.nextElement());
            }
            return null;
        }

        public SignalMastLogic getLogicFromRow(int row) {
            Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(row);
            Enumeration<SignalMastLogic> en = b.keys();
            while (en.hasMoreElements()) {
                return en.nextElement();
            }
            return null;
        }

        @Override
        public int getPreferredWidth(int col) {
            switch(col) {
                case SOURCECOL:
                    return new JTextField(10).getPreferredSize().width;
                case COMCOL:
                    return 75;
                case DESTCOL:
                    return new JTextField(10).getPreferredSize().width;
                case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
                EDITLOGICCOL:
                    return new JTextField(6).getPreferredSize().width;
                case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
                DELCOL:
                    return new JTextField(5).getPreferredSize().width;
                case DESTAPPCOL:
                    return new JTextField(10).getPreferredSize().width;
                case SOURCEAPPCOL:
                    return new JTextField(10).getPreferredSize().width;
                case ENABLECOL:
                    return new JTextField(5).getPreferredSize().width;
                default:
                    //log.warn("Unexpected column in getPreferredWidth: "+col);
                    return new JTextField(8).getPreferredSize().width;
            }
        }

        @Override
        public void configureTable(JTable table) {
            setColumnToHoldButton(table, EDITLOGICCOL, new JButton(Bundle.getMessage("ButtonEdit")));
            table.getTableHeader().setReorderingAllowed(true);
            // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            // resize columns as requested
            for (int i = 0; i < table.getColumnCount(); i++) {
                int width = getPreferredWidth(i);
                table.getColumnModel().getColumn(i).setPreferredWidth(width);
            }
            table.sizeColumnsToFit(-1);
            // configValueColumn(table);
            configDeleteColumn(table);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return null;
        }

        @Override
        public NamedBean getByUserName(String name) {
            return null;
        }

        @Override
        public synchronized void dispose() {
            getManager().removePropertyChangeListener(this);
            if (signalMastLogicList != null) {
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    SignalMastLogic b = getLogicFromRow(i);
                    if (b != null) {
                        b.removePropertyChangeListener(this);
                    }
                }
            }
        }

        @Override
        public int getRowCount() {
            return signalMastLogicList.size();
        }

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= signalMastLogicList.size()) {
                log.debug("row index is greater than signalMastLogicList size");
                return null;
            }
            SignalMastLogic b = getLogicFromRow(row);
            switch(col) {
                case SOURCECOL:
                    return getLogicFromRow(row).getSourceMast().getDisplayName();
                case // return user name
                DESTCOL:
                    // sometimes, the TableSorter invokes this on rows that no longer exist, so we check
                    return (b != null) ? getDestMastFromRow(row).getDisplayName() : null;
                case //
                SOURCEAPPCOL:
                    return (b != null) ? b.getSourceMast().getAspect() : null;
                case //
                DESTAPPCOL:
                    return (b != null) ? getDestMastFromRow(row).getAspect() : null;
                case COMCOL:
                    return (b != null) ? b.getComment(getDestMastFromRow(row)) : null;
                case DELCOL:
                    return Bundle.getMessage("ButtonDelete");
                case EDITLOGICCOL:
                    return Bundle.getMessage("ButtonEdit");
                case ENABLECOL:
                    return (b != null) ? b.isEnabled(getDestMastFromRow(row)) : null;
                default:
                    //log.error("internal state inconsistent with table requst for "+row+" "+col);
                    return null;
            }
        }

        @Override
        protected void configDeleteColumn(JTable table) {
            // have the delete column hold a button
            setColumnToHoldButton(table, DELCOL, new JButton(Bundle.getMessage("ButtonDelete")));
        }

        @Override
        protected String getBeanType() {
            return "Signal Mast Logic";
        }

        @Override
        protected void showPopup(MouseEvent e) {
        }
    };
}
Also used : NamedBean(jmri.NamedBean) ArrayList(java.util.ArrayList) JButton(javax.swing.JButton) SignalMast(jmri.SignalMast) JTextField(javax.swing.JTextField) NamedBean(jmri.NamedBean) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMast(jmri.SignalMast) PropertyChangeEvent(java.beans.PropertyChangeEvent) Enumeration(java.util.Enumeration) MouseEvent(java.awt.event.MouseEvent) Hashtable(java.util.Hashtable) SignalMastLogic(jmri.SignalMastLogic) JTable(javax.swing.JTable)

Example 85 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class SignalGroupTableAction method createModel.

/**
     * Create the JTable DataModel, along with the changes for the specific case
     * of SignalGroups.
     */
@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int COMMENTCOL = 2;

        public static final int DELETECOL = 3;

        public static final int ENABLECOL = 4;

        // default name: SETCOL
        public static final int EDITCOL = 5;

        @Override
        public int getColumnCount() {
            return 6;
        }

        @Override
        public String getColumnName(int col) {
            if (col == EDITCOL) {
                // no heading on "Edit" column
                return "";
            }
            if (col == ENABLECOL) {
                return Bundle.getMessage("ColumnHeadEnabled");
            }
            if (col == COMMENTCOL) {
                return Bundle.getMessage("ColumnComment");
            }
            if (col == DELETECOL) {
                return "";
            } else {
                return super.getColumnName(col);
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == EDITCOL) {
                return JButton.class;
            }
            if (col == ENABLECOL) {
                return Boolean.class;
            }
            if (col == DELETECOL) {
                return JButton.class;
            }
            if (col == COMMENTCOL) {
                return String.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            if (col == EDITCOL) {
                return new JTextField(Bundle.getMessage("ButtonEdit")).getPreferredSize().width;
            }
            if (col == ENABLECOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            if (col == COMMENTCOL) {
                return new JTextField(30).getPreferredSize().width;
            }
            if (col == DELETECOL) {
                return new JTextField(Bundle.getMessage("ButtonDelete")).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == COMMENTCOL) {
                return true;
            }
            if (col == EDITCOL) {
                return true;
            }
            if (col == ENABLECOL) {
                return true;
            }
            if (col == DELETECOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public Object getValueAt(int row, int col) {
            NamedBean b;
            if (col == EDITCOL) {
                return Bundle.getMessage("ButtonEdit");
            } else if (col == ENABLECOL) {
                return Boolean.valueOf(((SignalGroup) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getEnabled());
            //return true;
            } else if (col == COMMENTCOL) {
                b = getBySystemName(sysNameList.get(row));
                return (b != null) ? b.getComment() : null;
            } else if (//
            col == DELETECOL) {
                return Bundle.getMessage("ButtonDelete");
            } else {
                return super.getValueAt(row, col);
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if (col == EDITCOL) {
                // set up to Edit. Use separate Runnable so window is created on top
                class WindowMaker implements Runnable {

                    int row;

                    WindowMaker(int r) {
                        row = r;
                    }

                    @Override
                    public void run() {
                        // set up add/edit panel addFrame (starts as Add pane)
                        addPressed(null);
                        _systemName.setText((String) getValueAt(row, SYSNAMECOL));
                        // adjust addFrame for Edit
                        editPressed(null);
                    }
                }
                WindowMaker t = new WindowMaker(row);
                javax.swing.SwingUtilities.invokeLater(t);
            } else if (col == ENABLECOL) {
                // alternate
                SignalGroup r = (SignalGroup) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                boolean v = r.getEnabled();
                r.setEnabled(!v);
            } else if (col == COMMENTCOL) {
                getBySystemName(sysNameList.get(row)).setComment((String) value);
                fireTableRowsUpdated(row, row);
            } else if (col == DELETECOL) {
                // button fired, delete Bean
                deleteBean(row, col);
            } else {
                super.setValueAt(value, row, col);
            }
        }

        @Override
        public void configureTable(JTable table) {
            table.setDefaultRenderer(Boolean.class, new EnablingCheckboxRenderer());
            table.setDefaultRenderer(JComboBox.class, new jmri.jmrit.symbolicprog.ValueRenderer());
            table.setDefaultEditor(JComboBox.class, new jmri.jmrit.symbolicprog.ValueEditor());
            super.configureTable(table);
        }

        @Override
        protected void configDeleteColumn(JTable table) {
            // have the delete column hold a button
            SignalGroupTableAction.this.setColumnToHoldButton(table, DELETECOL, new JButton(Bundle.getMessage("ButtonDelete")));
        }

        /**
             * Delete the bean after all the checking has been done.
             * <P>
             * (Deactivate the Signal Group), then use the superclass to delete it.
             */
        @Override
        void doDelete(NamedBean bean) {
            //((SignalGroup)bean).deActivateSignalGroup();
            super.doDelete(bean);
        }

        // want to update when enabled parameter changes
        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            if (e.getPropertyName().equals("Enabled")) {
                return true;
            } else {
                return super.matchPropertyName(e);
            }
        }

        @Override
        public Manager getManager() {
            return jmri.InstanceManager.getDefault(jmri.SignalGroupManager.class);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return jmri.InstanceManager.getDefault(jmri.SignalGroupManager.class).getBySystemName(name);
        }

        @Override
        public NamedBean getByUserName(String name) {
            return jmri.InstanceManager.getDefault(jmri.SignalGroupManager.class).getByUserName(name);
        }

        @Override
        public int getDisplayDeleteMsg() {
            return 0x00;
        /*return InstanceManager.getDefault(jmri.UserPreferencesManager.class).getWarnDeleteSignalGroup();*/
        }

        @Override
        public void setDisplayDeleteMsg(int boo) {
        /*InstanceManager.getDefault(jmri.UserPreferencesManager.class).setWarnDeleteSignalGroup(boo); */
        }

        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        @Override
        public void clickOn(NamedBean t) {
        // mute action
        //((SignalGroup)t).setSignalGroup();
        }

        @Override
        public String getValue(String s) {
            // not directly used but should be present to implement abstract class
            return "Set";
        }

        /*            public JButton configureButton() {
                return new JButton(" Set ");
            }*/
        @Override
        protected String getBeanType() {
            return "Signal Group";
        }
    };
}
Also used : SignalGroup(jmri.SignalGroup) NamedBean(jmri.NamedBean) JComboBox(javax.swing.JComboBox) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) InstanceManager(jmri.InstanceManager) Manager(jmri.Manager) JTable(javax.swing.JTable)

Aggregations

NamedBean (jmri.NamedBean)104 ArrayList (java.util.ArrayList)18 JTextField (javax.swing.JTextField)12 JmriException (jmri.JmriException)12 SignalMast (jmri.SignalMast)12 JTable (javax.swing.JTable)11 Manager (jmri.Manager)9 Sensor (jmri.Sensor)9 JButton (javax.swing.JButton)8 InstanceManager (jmri.InstanceManager)8 JComboBox (javax.swing.JComboBox)7 MouseEvent (java.awt.event.MouseEvent)6 Hashtable (java.util.Hashtable)6 SignalHead (jmri.SignalHead)6 Turnout (jmri.Turnout)6 OBlock (jmri.jmrit.logix.OBlock)5 File (java.io.File)4 JLabel (javax.swing.JLabel)4 TableCellEditor (javax.swing.table.TableCellEditor)4 TableCellRenderer (javax.swing.table.TableCellRenderer)4