Search in sources :

Example 1 with SystemConnectionAction

use of jmri.jmrix.swing.SystemConnectionAction in project JMRI by JMRI.

the class AbstractActionModelFactory method getDialogMessage.

private JPanel getDialogMessage(JList<String> actions, JComboBox<String> connections) {
    // NOI18N
    JLabel connectionsLabel = new JLabel(Bundle.getMessage("AbstractActionModelFactory.getDialogMessage.connectionsLabel", JLabel.LEADING));
    actions.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
        if (!e.getValueIsAdjusting()) {
            connections.removeAllItems();
            connections.setEnabled(false);
            connectionsLabel.setEnabled(false);
            String name = actions.getSelectedValue();
            if (name != null) {
                String className = StartupActionModelUtil.getDefault().getClassName(name);
                if (className != null && StartupActionModelUtil.getDefault().isSystemConnectionAction(className)) {
                    try {
                        Action action = (Action) Class.forName(className).newInstance();
                        if (SystemConnectionAction.class.isAssignableFrom(action.getClass())) {
                            ((SystemConnectionAction) action).getSystemConnectionMemoClasses().stream().forEach((clazz) -> {
                                InstanceManager.getList(SystemConnectionMemo.class).stream().forEach((memo) -> {
                                    if (clazz.isAssignableFrom(memo.getClass())) {
                                        connections.addItem(memo.getUserName());
                                        connections.setEnabled(true);
                                        connectionsLabel.setEnabled(true);
                                    }
                                });
                            });
                        }
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
                        log.error("Unable to create Action", ex);
                    }
                }
            }
        }
    });
    connections.setEnabled(false);
    connectionsLabel.setEnabled(false);
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.add(new JLabel(this.getEditModelMessage(), JLabel.LEADING));
    panel.add(new JScrollPane(actions));
    panel.add(connectionsLabel);
    panel.add(connections);
    return panel;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) SystemConnectionAction(jmri.jmrix.swing.SystemConnectionAction) Action(javax.swing.Action) BoxLayout(javax.swing.BoxLayout) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JLabel(javax.swing.JLabel) SystemConnectionAction(jmri.jmrix.swing.SystemConnectionAction)

Example 2 with SystemConnectionAction

use of jmri.jmrix.swing.SystemConnectionAction in project JMRI by JMRI.

the class AbstractActionModel method performAction.

@Override
public void performAction() throws JmriException {
    log.debug("Invoke Action from {}", className);
    try {
        Action action = (Action) Class.forName(className).newInstance();
        if (SystemConnectionAction.class.isAssignableFrom(action.getClass())) {
            SystemConnectionMemo memo = ConnectionNameFromSystemName.getSystemConnectionMemoFromSystemPrefix(this.getSystemPrefix());
            if (memo != null) {
                ((SystemConnectionAction) action).setSystemConnectionMemo(memo);
            } else {
                log.warn("Connection \"{}\" does not exist and cannot be assigned to action {}\nThis warning can be silenced by configuring the connection associated with the startup action.", this.getSystemPrefix(), className);
            }
        }
        this.performAction(action);
    } catch (ClassNotFoundException ex) {
        log.error("Could not find specified class: {}", className);
    } catch (IllegalAccessException ex) {
        log.error("Unexpected access exception for class: {}", className, ex);
        throw new JmriException(ex);
    } catch (InstantiationException ex) {
        log.error("Could not instantiate specified class: {}", className, ex);
        throw new JmriException(ex);
    } catch (Exception ex) {
        log.error("Error while performing startup action for class: {}", className, ex);
        throw new JmriException(ex);
    }
}
Also used : SystemConnectionAction(jmri.jmrix.swing.SystemConnectionAction) Action(javax.swing.Action) SystemConnectionMemo(jmri.jmrix.SystemConnectionMemo) JmriException(jmri.JmriException) SystemConnectionAction(jmri.jmrix.swing.SystemConnectionAction) JmriException(jmri.JmriException)

Aggregations

Action (javax.swing.Action)2 SystemConnectionAction (jmri.jmrix.swing.SystemConnectionAction)2 BoxLayout (javax.swing.BoxLayout)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 JmriException (jmri.JmriException)1 SystemConnectionMemo (jmri.jmrix.SystemConnectionMemo)1