use of gov.sandia.n2a.host.Host.Factory 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;
}
Aggregations