Search in sources :

Example 1 with MTextField

use of gov.sandia.n2a.ui.MTextField in project n2a by frothga.

the class SettingsHost method getPanel.

@Override
public Component getPanel() {
    if (scrollPane != null)
        return scrollPane;
    JPanel view = new JPanel();
    scrollPane = new JScrollPane(view);
    // About one line of text. Typically, one "click" of the wheel does 3 steps, so about 45px or 3 lines of text.
    scrollPane.getVerticalScrollBar().setUnitIncrement(15);
    for (Host h : Host.getHosts()) if (h instanceof Remote)
        model.addElement(h);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setToolTipText("Press Insert to create host. Press Delete to remove host.");
    InputMap inputMap = list.getInputMap();
    inputMap.put(KeyStroke.getKeyStroke("INSERT"), "add");
    inputMap.put(KeyStroke.getKeyStroke("ctrl shift EQUALS"), "add");
    inputMap.put(KeyStroke.getKeyStroke("DELETE"), "delete");
    inputMap.put(KeyStroke.getKeyStroke("BACK_SPACE"), "delete");
    ActionMap actionMap = list.getActionMap();
    actionMap.put("add", new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            // Create new record
            String hostname = Host.uniqueName();
            // Get default class. The user can change it later.
            Host h = Host.create(hostname, "");
            // Focus record in UI
            int index = list.getSelectedIndex();
            if (index < 0)
                index = model.getSize();
            model.add(index, h);
            // Assumption: this triggers a selection change event, which will in turn call displayRecord().
            list.setSelectedIndex(index);
            fieldName.requestFocusInWindow();
        }
    });
    actionMap.put("delete", new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            // Remove current record
            int index = list.getSelectedIndex();
            Host h = list.getSelectedValue();
            if (h == null)
                return;
            Host.remove(h, true);
            // Focus another record, or clear UI
            model.remove(index);
            index = Math.min(index, model.size() - 1);
            // triggers selection change event, resulting in call to displayRecord()
            if (index >= 0)
                list.setSelectedIndex(index);
        }
    });
    list.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;
            displayRecord();
        }
    });
    fieldName = new MTextField(null, "", "", 20, true);
    fieldName.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            Host h = list.getSelectedValue();
            String newName = fieldName.getText();
            Host.rename(h, newName);
            list.repaint();
            if (editor instanceof NameChangeListener)
                ((NameChangeListener) editor).nameChanged(h.name);
        }
    });
    for (ExtensionPoint ext : PluginManager.getExtensionsForPoint(Factory.class)) {
        Factory f = (Factory) ext;
        if (f instanceof FactoryRemote)
            comboClass.addItem(f.className());
    }
    comboClass.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String newClass = comboClass.getSelectedItem().toString();
            // Change the class. This requires destroying the current instance and constructing
            // a new one that is bound to the same record.
            int index = list.getSelectedIndex();
            Host h = list.getSelectedValue();
            if (h == null)
                return;
            String originalClass = h.getClassName();
            if (newClass.equals(originalClass))
                return;
            Host.remove(h, false);
            Host h2 = Host.create(h.name, newClass);
            h.transferJobsTo(h2);
            model.set(index, h2);
            displayRecord();
        }
    });
    editorHolder.setLayout(new BorderLayout());
    JPanel panelList = Lay.BL("C", list, "pref=[100,200]");
    panelList.setBorder(LineBorder.createBlackLineBorder());
    panelList = (JPanel) Lay.eb(Lay.BL("C", panelList), "5");
    Lay.BLtg(view, "N", Lay.BL("W", Lay.BxL("H", Lay.BL("N", panelList), Box.createHorizontalStrut(5), Lay.BL("N", Lay.BxL(Lay.FL(Lay.lb("Name"), fieldName), Lay.FL(Lay.lb("Class"), comboClass), editorHolder)))));
    scrollPane.setFocusCycleRoot(true);
    if (list.getModel().getSize() > 0)
        list.setSelectedIndex(0);
    return scrollPane;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) ActionMap(javax.swing.ActionMap) ActionEvent(java.awt.event.ActionEvent) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint) ListSelectionEvent(javax.swing.event.ListSelectionEvent) Remote(gov.sandia.n2a.host.Remote) FactoryRemote(gov.sandia.n2a.host.Host.FactoryRemote) Factory(gov.sandia.n2a.host.Host.Factory) Host(gov.sandia.n2a.host.Host) MTextField(gov.sandia.n2a.ui.MTextField) ListSelectionListener(javax.swing.event.ListSelectionListener) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener) BorderLayout(java.awt.BorderLayout) InputMap(javax.swing.InputMap) ChangeListener(javax.swing.event.ChangeListener) AbstractAction(javax.swing.AbstractAction) FactoryRemote(gov.sandia.n2a.host.Host.FactoryRemote)

Aggregations

Host (gov.sandia.n2a.host.Host)1 Factory (gov.sandia.n2a.host.Host.Factory)1 FactoryRemote (gov.sandia.n2a.host.Host.FactoryRemote)1 Remote (gov.sandia.n2a.host.Remote)1 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)1 MTextField (gov.sandia.n2a.ui.MTextField)1 BorderLayout (java.awt.BorderLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 AbstractAction (javax.swing.AbstractAction)1 ActionMap (javax.swing.ActionMap)1 InputMap (javax.swing.InputMap)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 ChangeEvent (javax.swing.event.ChangeEvent)1 ChangeListener (javax.swing.event.ChangeListener)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 ListSelectionListener (javax.swing.event.ListSelectionListener)1