Skip to content

Commit

Permalink
Merge pull request #268 from bobjacobsen/nodeselector-update-with-API…
Browse files Browse the repository at this point in the history
…-change

NodeSelector GUI improvements
  • Loading branch information
dpharris authored Aug 18, 2024
2 parents 28fbac0 + b40c4a2 commit 63a5ac6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
33 changes: 18 additions & 15 deletions src/org/openlcb/swing/NodeSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
*
* @author Bob Jacobsen Copyright (C) 2012
*/
public class NodeSelector extends JPanel {
public class NodeSelector extends JComboBox<NodeSelector.ModelEntry> {

private final PropertyChangeListener propertyChangeListener;
MimicNodeStore store;
JComboBox<ModelEntry> box;
private DefaultComboBoxModel<ModelEntry> model = new DefaultComboBoxModel<ModelEntry>();
private boolean seenLight = false;
private int termCount = 2; // how many terms to keep in ID string
Expand All @@ -59,14 +58,11 @@ public NodeSelector(MimicNodeStore store) {
* @param termCount Number of ID terms to include in the displayed ID
*/
public NodeSelector(MimicNodeStore store, int termCount) {
super();
this.store = store;
this.termCount = termCount;

this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

box = new JComboBox<ModelEntry>(model);
add(box);

this.setModel(model);
// listen for newly arrived nodes
propertyChangeListener = new PropertyChangeListener() {
@Override
Expand All @@ -89,9 +85,9 @@ public void propertyChange(PropertyChangeEvent e) {

// If there are no nodes added, manually set the size
// to a reasonable value
if (box.getItemCount() == 0) {
box.setPrototypeDisplayValue(new ModelEntry(new String(new char[70])));
}
//if (getItemCount() == 0) {
setPrototypeDisplayValue(new ModelEntry("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"));
//}

addHierarchyListener(new HierarchyListener() {
@Override
Expand All @@ -113,7 +109,6 @@ protected class ModelEntry implements Comparable<ModelEntry>, PropertyChangeList

ModelEntry(MimicNodeStore.NodeMemo memo) {
this.nodeMemo = memo;
//log.finest("registering listener for " + memo.getNodeID() + " " + memo.toString());
memo.addPropertyChangeListener(this);
updateDescription();
}
Expand Down Expand Up @@ -191,6 +186,8 @@ public String toString() {
"vector.")
public boolean equals(Object o) {
if (o instanceof ModelEntry) {
if (nodeMemo == null && ((ModelEntry) o).nodeMemo == null) return true;
if (nodeMemo == null || ((ModelEntry) o).nodeMemo == null) return false;
return getNodeID().equals(((ModelEntry) o).getNodeID());
}
if (o instanceof NodeID) {
Expand All @@ -213,15 +210,14 @@ public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
}

public void dispose() {
//log.warning("dispose of " + nodeMemo.getNodeID().toString());
nodeMemo.removePropertyChangeListener(this);
}
}

// Notifies that the contents ofa given entry have changed. This will delete and re-add the
// entry to the model, forcing a refresh of the box.
private void updateComboBoxModelEntry(ModelEntry modelEntry) {
int idx = model.getIndexOf(modelEntry.getNodeID());
int idx = model.getIndexOf(modelEntry);
if (idx < 0) {
return;
}
Expand Down Expand Up @@ -249,6 +245,11 @@ private void newNodeInList(MimicNodeStore.NodeMemo memo) {
++i;
}
model.insertElementAt(e, i);

if (getSelectedItem() == null) setSelectedItem(e);

setPrototypeDisplayValue(e);
invalidate();
}

// Removes all entries from the model list, disposing them in the process.
Expand All @@ -264,8 +265,10 @@ public void dispose() {
store.removePropertyChangeListener(propertyChangeListener);
}

public NodeID getSelectedItem() {
return ((ModelEntry) box.getSelectedItem()).getNodeID();
// There should always be a node selected
public NodeID getSelectedNodeID() {
ModelEntry me = (ModelEntry) super.getSelectedItem();
return me.getNodeID();
}

private static final Logger log = Logger.getLogger(NodeSelector.class.getName());
Expand Down
4 changes: 2 additions & 2 deletions test/org/openlcb/swing/NodeSelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public void tearDown() {

String getAllItems() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nodeSelector.box.getItemCount(); ++i) {
sb.append(nodeSelector.box.getItemAt(i).toString());
for (int i = 0; i < nodeSelector.getItemCount(); ++i) {
sb.append(nodeSelector.getItemAt(i).toString());
sb.append(';');
}
return sb.toString();
Expand Down

0 comments on commit 63a5ac6

Please sign in to comment.