Search in sources :

Example 51 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class DefaultConditionalAction method setDeviceName.

@Override
public void setDeviceName(String deviceName) {
    _deviceName = deviceName;
    NamedBean bean = getIndirectBean(_deviceName);
    if (bean == null) {
        bean = getActionBean(_deviceName);
    }
    if (bean != null) {
        _namedBean = nbhm.getNamedBeanHandle(_deviceName, bean);
    } else {
        _namedBean = null;
    }
}
Also used : NamedBean(jmri.NamedBean)

Example 52 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class LightTableAction method createModel.

/**
     * Create the JTable DataModel, along with the changes for the specific case
     * of Lights.
     */
@Override
protected void createModel() {
    // load graphic state column display preference
    _graphicState = InstanceManager.getDefault(GuiLafPreferencesManager.class).isGraphicTableState();
    m = new BeanTableDataModel() {

        public static final int ENABLECOL = NUMCOLUMN;

        public static final int INTENSITYCOL = ENABLECOL + 1;

        public static final int EDITCOL = INTENSITYCOL + 1;

        protected String enabledString = Bundle.getMessage("ColumnHeadEnabled");

        protected String intensityString = Bundle.getMessage("ColumnHeadIntensity");

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

        @Override
        public String getColumnName(int col) {
            if (col == EDITCOL) {
                // no heading on "Edit"
                return "";
            }
            if (col == INTENSITYCOL) {
                return intensityString;
            }
            if (col == ENABLECOL) {
                return enabledString;
            } else {
                return super.getColumnName(col);
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == EDITCOL) {
                return JButton.class;
            }
            if (col == INTENSITYCOL) {
                return Double.class;
            }
            if (col == ENABLECOL) {
                return Boolean.class;
            } else if (col == VALUECOL && _graphicState) {
                // use an image to show light state
                return JLabel.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            // override default value for UserName column
            if (col == USERNAMECOL) {
                return new JTextField(16).getPreferredSize().width;
            }
            if (col == EDITCOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            if (col == INTENSITYCOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            if (col == ENABLECOL) {
                return new JTextField(6).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == EDITCOL) {
                return true;
            }
            if (col == INTENSITYCOL) {
                return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).isIntensityVariable();
            }
            if (col == ENABLECOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public String getValue(String name) {
            Light l = lightManager.getBySystemName(name);
            if (l == null) {
                return ("Failed to find " + name);
            }
            int val = l.getState();
            switch(val) {
                case Light.ON:
                    return Bundle.getMessage("LightStateOn");
                case Light.INTERMEDIATE:
                    return Bundle.getMessage("LightStateIntermediate");
                case Light.OFF:
                    return Bundle.getMessage("LightStateOff");
                case Light.TRANSITIONINGTOFULLON:
                    return Bundle.getMessage("LightStateTransitioningToFullOn");
                case Light.TRANSITIONINGHIGHER:
                    return Bundle.getMessage("LightStateTransitioningHigher");
                case Light.TRANSITIONINGLOWER:
                    return Bundle.getMessage("LightStateTransitioningLower");
                case Light.TRANSITIONINGTOFULLOFF:
                    return Bundle.getMessage("LightStateTransitioningToFullOff");
                default:
                    return "Unexpected value: " + val;
            }
        }

        @Override
        public Object getValueAt(int row, int col) {
            switch(col) {
                case EDITCOL:
                    return Bundle.getMessage("ButtonEdit");
                case INTENSITYCOL:
                    return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getTargetIntensity();
                case ENABLECOL:
                    return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getEnabled();
                default:
                    return super.getValueAt(row, col);
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            switch(col) {
                case EDITCOL:
                    // 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 to edit
                            addPressed(null);
                            fixedSystemName.setText((String) getValueAt(row, SYSNAMECOL));
                            // don't really want to stop Light w/o user action
                            editPressed();
                        }
                    }
                    WindowMaker t = new WindowMaker(row);
                    javax.swing.SwingUtilities.invokeLater(t);
                    break;
                case INTENSITYCOL:
                    // alternate
                    try {
                        Light l = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                        double intensity = ((Double) value);
                        if (intensity < 0) {
                            intensity = 0;
                        }
                        if (intensity > 1.0) {
                            intensity = 1.0;
                        }
                        l.setTargetIntensity(intensity);
                    } catch (IllegalArgumentException e1) {
                        status1.setText(Bundle.getMessage("LightError16"));
                    }
                    break;
                case ENABLECOL:
                    // alternate
                    Light l = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                    boolean v = l.getEnabled();
                    l.setEnabled(!v);
                    break;
                case VALUECOL:
                    if (_graphicState) {
                        // respond to clicking on ImageIconRenderer CellEditor
                        Light ll = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                        clickOn(ll);
                        fireTableRowsUpdated(row, row);
                        break;
                    }
                //$FALL-THROUGH$
                default:
                    super.setValueAt(value, row, col);
                    break;
            }
        }

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

        // all properties update for now
        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return true;
        }

        @Override
        public Manager getManager() {
            return lightManager;
        }

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

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

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

        @Override
        public void clickOn(NamedBean t) {
            int oldState = ((Light) t).getState();
            int newState;
            switch(oldState) {
                case Light.ON:
                    newState = Light.OFF;
                    break;
                case Light.OFF:
                    newState = Light.ON;
                    break;
                default:
                    newState = Light.OFF;
                    log.warn("Unexpected Light state " + oldState + " becomes OFF");
                    break;
            }
            ((Light) t).setState(newState);
        }

        @Override
        public JButton configureButton() {
            return new JButton(" " + Bundle.getMessage("LightStateOff") + " ");
        }

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

        /**
             * Customize the light table Value (State) column to show an appropriate graphic for the light state
             * if _graphicState = true, or (default) just show the localized state text
             * when the TableDataModel is being called from ListedTableAction.
             *
             * @param table a JTable of Lights
             */
        @Override
        protected void configValueColumn(JTable table) {
            // have the value column hold a JPanel (icon)
            //setColumnToHoldButton(table, VALUECOL, new JLabel("123456")); // for small round icon, but cannot be converted to JButton
            // add extras, override BeanTableDataModel
            log.debug("Light configValueColumn (I am {})", super.toString());
            if (_graphicState) {
                // load icons, only once
                // editor
                table.setDefaultEditor(JLabel.class, new ImageIconRenderer());
                // item class copied from SwitchboardEditor panel
                table.setDefaultRenderer(JLabel.class, new ImageIconRenderer());
            } else {
                // classic text style state indication
                super.configValueColumn(table);
            }
        }

        /**
             * Visualize state in table as a graphic, customized for Lights (2 states + ... for transitioning).
             * Renderer and Editor are identical, as the cell contents are not actually edited,
             * only used to toggle state using {@link #clickOn(NamedBean)}.
             * @see jmri.jmrit.beantable.sensor.SensorTableDataModel.ImageIconRenderer
             * @see jmri.jmrit.beantable.BlockTableAction#createModel()
             * @see jmri.jmrit.beantable.TurnoutTableAction#createModel()
             */
        class ImageIconRenderer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

            protected JLabel label;

            // also used in display.switchboardEditor
            protected String rootPath = "resources/icons/misc/switchboard/";

            // for Light
            protected char beanTypeChar = 'L';

            protected String onIconPath = rootPath + beanTypeChar + "-on-s.png";

            protected String offIconPath = rootPath + beanTypeChar + "-off-s.png";

            protected BufferedImage onImage;

            protected BufferedImage offImage;

            protected ImageIcon onIcon;

            protected ImageIcon offIcon;

            protected int iconHeight = -1;

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                log.debug("Renderer Item = {}, State = {}", row, value);
                if (iconHeight < 0) {
                    // load resources only first time, either for renderer or editor
                    loadIcons();
                    log.debug("icons loaded");
                }
                return updateLabel((String) value, row);
            }

            @Override
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                log.debug("Renderer Item = {}, State = {}", row, value);
                if (iconHeight < 0) {
                    // load resources only first time, either for renderer or editor
                    loadIcons();
                    log.debug("icons loaded");
                }
                return updateLabel((String) value, row);
            }

            public JLabel updateLabel(String value, int row) {
                if (iconHeight > 0) {
                // if necessary, increase row height;
                //table.setRowHeight(row, Math.max(table.getRowHeight(), iconHeight - 5)); // TODO adjust table row height for Lights
                }
                if (value.equals(Bundle.getMessage("LightStateOff")) && offIcon != null) {
                    label = new JLabel(offIcon);
                    label.setVerticalAlignment(JLabel.BOTTOM);
                    log.debug("offIcon set");
                } else if (value.equals(Bundle.getMessage("LightStateOn")) && onIcon != null) {
                    label = new JLabel(onIcon);
                    label.setVerticalAlignment(JLabel.BOTTOM);
                    log.debug("onIcon set");
                } else if (value.equals(Bundle.getMessage("BeanStateInconsistent"))) {
                    // centered text alignment
                    label = new JLabel("X", JLabel.CENTER);
                    label.setForeground(Color.red);
                    log.debug("Light state inconsistent");
                    iconHeight = 0;
                } else if (value.equals(Bundle.getMessage("LightStateIntermediate"))) {
                    // centered text alignment
                    label = new JLabel("...", JLabel.CENTER);
                    log.debug("Light state in transition");
                    iconHeight = 0;
                } else {
                    // failed to load icon
                    // centered text alignment
                    label = new JLabel(value, JLabel.CENTER);
                    log.warn("Error reading icons for LightTable");
                    iconHeight = 0;
                }
                label.setToolTipText(value);
                label.addMouseListener(new MouseAdapter() {

                    @Override
                    public final void mousePressed(MouseEvent evt) {
                        log.debug("Clicked on icon in row {}", row);
                        stopCellEditing();
                    }
                });
                return label;
            }

            @Override
            public Object getCellEditorValue() {
                log.debug("getCellEditorValue, me = {})", this.toString());
                return this.toString();
            }

            /**
                 * Read and buffer graphics. Only called once for this table.
                 * @see #getTableCellEditorComponent(JTable, Object, boolean, int, int)
                 */
            protected void loadIcons() {
                try {
                    onImage = ImageIO.read(new File(onIconPath));
                    offImage = ImageIO.read(new File(offIconPath));
                } catch (IOException ex) {
                    log.error("error reading image from {} or {}", onIconPath, offIconPath, ex);
                }
                log.debug("Success reading images");
                int imageWidth = onImage.getWidth();
                int imageHeight = onImage.getHeight();
                // scale icons 50% to fit in table rows
                Image smallOnImage = onImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_DEFAULT);
                Image smallOffImage = offImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_DEFAULT);
                onIcon = new ImageIcon(smallOnImage);
                offIcon = new ImageIcon(smallOffImage);
                iconHeight = onIcon.getIconHeight();
            }
        }
    };
// end of custom data model
}
Also used : ImageIcon(javax.swing.ImageIcon) NamedBean(jmri.NamedBean) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) LightManager(jmri.LightManager) InstanceManager(jmri.InstanceManager) GuiLafPreferencesManager(apps.gui.GuiLafPreferencesManager) Manager(jmri.Manager) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Light(jmri.Light) AbstractCellEditor(javax.swing.AbstractCellEditor) TableCellEditor(javax.swing.table.TableCellEditor) TableCellRenderer(javax.swing.table.TableCellRenderer) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) IOException(java.io.IOException) JTable(javax.swing.JTable) File(java.io.File)

Example 53 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class SectionTableAction method createModel.

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

        public static final int BEGINBLOCKCOL = NUMCOLUMN;

        public static final int ENDBLOCKCOL = BEGINBLOCKCOL + 1;

        public static final int EDITCOL = ENDBLOCKCOL + 1;

        @Override
        public String getValue(String name) {
            return "";
        }

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

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

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

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

        @Override
        public void clickOn(NamedBean t) {
        }

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

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= sysNameList.size()) {
                log.debug("row is greater than name list");
                return "";
            }
            if (col == BEGINBLOCKCOL) {
                Section z = (Section) getBySystemName(sysNameList.get(row));
                if (z != null) {
                    return z.getBeginBlockName();
                }
                return "  ";
            } else if (col == ENDBLOCKCOL) {
                Section z = (Section) getBySystemName(sysNameList.get(row));
                if (z != null) {
                    return z.getEndBlockName();
                }
                return "  ";
            } else if (col == VALUECOL) {
                Section z = (Section) getBySystemName(sysNameList.get(row));
                if (z == null) {
                    return "";
                } else {
                    int state = z.getState();
                    if (state == Section.FREE) {
                        return (rbx.getString("SectionFree"));
                    } else if (state == Section.FORWARD) {
                        return (rbx.getString("SectionForward"));
                    } else if (state == Section.REVERSE) {
                        return (rbx.getString("SectionReverse"));
                    }
                }
            } else if (col == EDITCOL) {
                return Bundle.getMessage("ButtonEdit");
            } else {
                return super.getValueAt(row, col);
            }
            return null;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if ((col == BEGINBLOCKCOL) || (col == ENDBLOCKCOL)) {
                return;
            } else if (col == EDITCOL) {
                class WindowMaker implements Runnable {

                    int row;

                    WindowMaker(int r) {
                        row = r;
                    }

                    @Override
                    public void run() {
                        String sName = (String) getValueAt(row, SYSNAMECOL);
                        editPressed(sName);
                    }
                }
                WindowMaker t = new WindowMaker(row);
                javax.swing.SwingUtilities.invokeLater(t);
            } else if (col == DELETECOL) {
                deleteSectionPressed(sysNameList.get(row));
            } else {
                super.setValueAt(value, row, col);
            }
        }

        @Override
        public String getColumnName(int col) {
            if (col == BEGINBLOCKCOL) {
                return (rbx.getString("SectionFirstBlock"));
            }
            if (col == ENDBLOCKCOL) {
                return (rbx.getString("SectionLastBlock"));
            }
            if (col == EDITCOL) {
                // no namne on Edit column
                return "";
            }
            return super.getColumnName(col);
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == VALUECOL) {
                // not a button
                return String.class;
            }
            if (col == BEGINBLOCKCOL) {
                // not a button
                return String.class;
            }
            if (col == ENDBLOCKCOL) {
                // not a button
                return String.class;
            }
            if (col == EDITCOL) {
                return JButton.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == BEGINBLOCKCOL) {
                return false;
            }
            if (col == ENDBLOCKCOL) {
                return false;
            }
            if (col == VALUECOL) {
                return false;
            }
            if (col == EDITCOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            // override default value for SystemName and UserName columns
            if (col == SYSNAMECOL) {
                return new JTextField(9).getPreferredSize().width;
            }
            if (col == USERNAMECOL) {
                return new JTextField(17).getPreferredSize().width;
            }
            if (col == VALUECOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            // new columns
            if (col == BEGINBLOCKCOL) {
                return new JTextField(15).getPreferredSize().width;
            }
            if (col == ENDBLOCKCOL) {
                return new JTextField(15).getPreferredSize().width;
            }
            if (col == EDITCOL) {
                return new JTextField(6).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public void configValueColumn(JTable table) {
        // value column isn't button, so config is null
        }

        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return true;
        // return (e.getPropertyName().indexOf("alue")>=0);
        }

        @Override
        protected String getBeanType() {
            return "Section";
        }
    };
}
Also used : NamedBean(jmri.NamedBean) SectionManager(jmri.SectionManager) BlockManager(jmri.BlockManager) Manager(jmri.Manager) JTextField(javax.swing.JTextField) Section(jmri.Section) EntryPoint(jmri.EntryPoint) SectionManager(jmri.SectionManager) JTable(javax.swing.JTable)

Example 54 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class AddSignalMastPanel method checkUserName.

boolean checkUserName(String nam) {
    if (!((nam == null) || (nam.equals("")))) {
        // user name changed, check if new name already exists
        NamedBean nB = InstanceManager.getDefault(jmri.SignalMastManager.class).getByUserName(nam);
        if (nB != null) {
            log.error("User Name is not unique " + nam);
            String msg = Bundle.getMessage("WarningUserName", new Object[] { ("" + nam) });
            JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        //Check to ensure that the username doesn't exist as a systemname.
        nB = InstanceManager.getDefault(jmri.SignalMastManager.class).getBySystemName(nam);
        if (nB != null) {
            log.error("User Name is not unique " + nam + " It already exists as a System name");
            String msg = Bundle.getMessage("WarningUserNameAsSystem", new Object[] { ("" + nam) });
            JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    return true;
}
Also used : NamedBean(jmri.NamedBean)

Example 55 with NamedBean

use of jmri.NamedBean in project JMRI by JMRI.

the class WarrantTableAction method sharedTO.

@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "OBlock extends Block")
private static boolean sharedTO(OPath myPath, OPath path) {
    List<BeanSetting> myTOs = myPath.getSettings();
    Iterator<BeanSetting> iter = myTOs.iterator();
    List<BeanSetting> tos = path.getSettings();
    boolean ret = false;
    while (iter.hasNext()) {
        BeanSetting mySet = iter.next();
        NamedBean myTO = mySet.getBean();
        int myState = mySet.getSetting();
        Iterator<BeanSetting> it = tos.iterator();
        while (it.hasNext()) {
            BeanSetting set = it.next();
            NamedBean to = set.getBean();
            if (myTO.equals(to)) {
                // turnouts are equal.  check if settings are compatible.
                OBlock myBlock = (OBlock) myPath.getBlock();
                int state = set.getSetting();
                OBlock block = (OBlock) path.getBlock();
                //                  String note = "WARNING: ";
                if (myState != state) {
                    ret = myBlock.addSharedTurnout(myPath, block, path);
                /*                       _textArea.append(note+Bundle.getMessage("sharedTurnout", myPath.getName(), myBlock.getDisplayName(),
                             myTO.getDisplayName(), (myState==jmri.Turnout.CLOSED ? "Closed":"Thrown"),
                             path.getName(), block.getDisplayName(), to.getDisplayName(),
                             (state==jmri.Turnout.CLOSED ? "Closed":"Thrown")));
                      _textArea.append("\n");
                    } else {
                        note = "Note: "; */
                }
            }
        }
    }
    return ret;
}
Also used : NamedBean(jmri.NamedBean) BeanSetting(jmri.BeanSetting) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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