Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/org/openlcb/EventNameStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@
*/
public interface EventNameStore {

/**
* @param eventName Either a previously stored event name that is
* is associated to an event ID, or the dotted-hex form of an Event ID
* @return an eventID from tne matching name, if any
* otherwise directly return the doted-hex input.
*/
public EventID getEventID(String eventName);

/**
* @param eventID A valid event ID, not null
* @return If a name has been associated with this event ID, return that name,
* otherwise an event ID from parsing the eventName as dotted-hex.
*/
public String getEventName(EventID eventID);

}
2 changes: 1 addition & 1 deletion src/org/openlcb/cdi/cmd/BackupConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void visitInt(ConfigRepresentation.IntegerEntry e) {

@Override
public void visitEvent(ConfigRepresentation.EventEntry e) {
writeEntry(finalWriter, e.key, e.getValue());
writeEntry(finalWriter, e.key, e.getNumericalEventValue());
}
}
);
Expand Down
30 changes: 27 additions & 3 deletions src/org/openlcb/cdi/impl/ConfigRepresentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.logging.Logger;
import org.openlcb.DefaultPropertyListenerSupport;
import org.openlcb.EventID;
import org.openlcb.EventNameStore;
import org.openlcb.NodeID;
import org.openlcb.OlcbInterface;
import org.openlcb.Utilities;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class ConfigRepresentation extends DefaultPropertyListenerSupport {
// Last time the progressbar was updated from the load.
private long lastProgress;

public org.openlcb.cdi.swing.CdiPanel.GuiItemFactory factory = null;
public EventNameStore eventNameStore;

/**
* Connects to a node, populates the cache by fetching and parsing the CDI.
Expand Down Expand Up @@ -744,13 +745,29 @@ protected void updateVisibleValue() {
}
}

/**
* @return if `eventNameStore` exists, return the name of the contained event
* otherwise return the numerical event ID in dotted-hex form.
*/
public String getValue() {
MemorySpaceCache cache = getCacheForSpace(space);
byte[] b = cache.read(origin, size);
if (b == null) return null;
EventID eid = new EventID(b);
if (factory == null) return eid.toShortString();
return factory.getStringFromEventID(eid);
if (eventNameStore == null) return eid.toShortString();
return eventNameStore.getEventName(eid);
}

/**
* @return the numerical event ID in dotted-hex form,
* ignoring any conversion eventNameStore that might exist
*/
public String getNumericalEventValue() {
MemorySpaceCache cache = getCacheForSpace(space);
byte[] b = cache.read(origin, size);
if (b == null) return null;
EventID eid = new EventID(b);
return eid.toShortString();
}

public void setValue(EventID event) {
Expand All @@ -759,6 +776,13 @@ public void setValue(EventID event) {
if (b == null) return;
cache.write(origin, b, this);
}

public void setValue(String eventName) {
EventID event;
if (eventNameStore == null) event = new EventID(eventName);
else event = eventNameStore.getEventID(eventName);
setValue(event);
}
}

/**
Expand Down
35 changes: 10 additions & 25 deletions src/org/openlcb/cdi/swing/CdiPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ public void initComponents(ConfigRepresentation rep, GuiItemFactory factory) {
setAlignmentX(Component.LEFT_ALIGNMENT);
this.rep = rep;
this.factory = factory;
rep.factory = factory;

contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
Expand Down Expand Up @@ -2328,7 +2327,7 @@ protected void additionalButtons() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
NodeID node = rep.getConnection().getNodeId();
EventID ev = factory.getEventIDFromString(textField.getText());
EventID ev = rep.eventNameStore.getEventID(textField.getText());
rep.getConnection().getOutputConnection().put(new ProducerConsumerEventReportMessage(node, ev), rep.getConnection().getOutputConnection());
}
});
Expand Down Expand Up @@ -2398,15 +2397,18 @@ private void showEventidMoreFunctionsMenu() {

@Override
protected void writeDisplayTextToNode() {
entry.setValue(factory.getEventIDFromString(textField.getText()));
entry.setValue(rep.eventNameStore.getEventID(textField.getText()));
_changeMade = true;
notifyTabColorRefresh();
}

@Override
protected void updateDisplayText(@NonNull String value) {
EventID eid = factory.getEventIDFromString(value);
String retval = factory.getStringFromEventID(eid);
String retval = "";
if (!value.isEmpty()) {
EventID eid = rep.eventNameStore.getEventID(value);
retval = rep.eventNameStore.getEventName(eid);
}
textField.setText(retval);
}

Expand All @@ -2430,8 +2432,7 @@ void updateColor() {
EventID id;

try {
// id = new EventID(s);
id = factory.getEventIDFromString(s);
id = rep.eventNameStore.getEventID(s);
} catch (RuntimeException e) {
// Event is not in the right format. Ignore.
return;
Expand Down Expand Up @@ -2461,6 +2462,8 @@ private void releaseListener() {
}
}



// represent a slider with an optional text view
private class SliderWithView extends JPanel {
JSlider slider = null;
Expand Down Expand Up @@ -3232,24 +3235,6 @@ public JTextField handleStringValue(JTextField value) {
public JTextArea handleEditorValue(JTextArea value) {
return value;
}

/** Convert a String into an EventID, doing any additional local
* dealiasing required.
* @param content Content to convert, e.g. from a text component
* @return eventID that represents the content
*/
public EventID getEventIDFromString(String content) {
return new EventID(content);
}

/** Convert an EventID into a String, doing any additional local
* aliasing required.
* @param event EventID to convert, e.g. from reading a node
* @return local representation fo that EventID, often just the dotted hex
*/
public String getStringFromEventID(EventID event) {
return event.toShortString();
}
}

/**
Expand Down