Skip to content

Commit

Permalink
Merge pull request #233 from bobjacobsen/bobjacobsen-repname-update-f…
Browse files Browse the repository at this point in the history
…rom-candp

Process <repname> as described in draft documents
  • Loading branch information
dpharris authored Nov 4, 2023
2 parents d758ab3 + 3522365 commit 0eaf235
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/org/openlcb/cdi/CdiRep.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static interface Item {
public static interface Group extends Item {
public java.util.List<Item> getItems();
public int getReplication();
public String getRepName();
public String getRepName(int index, int replications);
}

public static interface Map {
Expand Down
81 changes: 76 additions & 5 deletions src/org/openlcb/cdi/jdom/JdomCdiRep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.openlcb.cdi.jdom;

import edu.umd.cs.findbugs.annotations.NonNull;

import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Element;
import org.openlcb.cdi.CdiRep;
Expand Down Expand Up @@ -246,12 +250,79 @@ public int getOffset() {
} catch (org.jdom2.DataConversionException e1) { return 0; }
}

/**
* Provides the name for this replication. See the CDI TN for the
* algorithm being used.
* @param index a 1-based index of an element within the group
* @return a default "Group" string if no repname elements exist
*/
@Override
public String getRepName() {
Element d = e.getChild("repname");
if (d==null) return null;
return d.getText();
}
@NonNull
public String getRepName(int index, int replications) {
if (index < 1) throw new IllegalArgumentException("index "+index+" must be >= 1");

List<Element> repnames = e.getChildren("repname");

// no repnames, return default
if (repnames == null || repnames.size() == 0) return DEFAULT_REP_PREFIX;

Element d;

// more than one repname element and index refers to not last, use the appropriate one
if (index < repnames.size()) { // index is 1-based as is size()
// not last element, use the name from the repname directly
d = repnames.get(index-1); // index is 1-based
return d.getText();
}

// Check for the case that this index == number of
// repnames _and_ index == rep count. That should not be extended.
if (index == repnames.size() && index == replications) {
// this the special case of _not_ extending the last repname
d = repnames.get(index-1); // index is 1-based
return d.getText();
}

// in this case, we have to extend the last repname
d = repnames.get(repnames.size()-1);
String name = d.getText();

int firstTrailingDigit = indexOfFirstTrailingDigit(name);
if (firstTrailingDigit == -1) {
// no final digits
// append appropriate index taking into account prior repname elements
int trailingNumber = index - (repnames.size()-1);
return name+trailingNumber; // as recommended by TN, this does not add whitespace in between
}

// now we need to extract the trailing digits and index off them
String digits = name.substring(firstTrailingDigit);

int initialValue = Integer.parseInt(digits);
int trailingNumber = initialValue + ((index-1) - (repnames.size()-1));
return name.substring(0,firstTrailingDigit)+trailingNumber;
}

static final private String DEFAULT_REP_PREFIX = "Group";

// Find the trailing digit characters, if any, in a String
// Return the offset of the 1st digit character
// Return -1 if not found
int indexOfFirstTrailingDigit(String input) {
if ( input.isEmpty() ) return -1;
if (! Character.isDigit(input.charAt(input.length()-1)) ) return -1;

// so there is at least one digit at end, scan for first non-digit
for (int first = input.length()-1; first>=0; first--) {
if (! Character.isDigit(input.charAt(first))) {
return first+1;
}
}

// here if its all digits!
return 0;
}

}

public static class EventID extends Item implements CdiRep.EventID {
Expand Down
3 changes: 1 addition & 2 deletions src/org/openlcb/cdi/swing/CdiPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,7 @@ public void visitGroupRep(final ConfigRepresentation.GroupRep e) {
currentPane.setLayout(new BoxLayout(currentPane, BoxLayout.Y_AXIS));
currentPane.setAlignmentX(Component.LEFT_ALIGNMENT);
CdiRep.Group item = e.group;
final String name = (item.getRepName() != null ? (item.getRepName()) : "Group") + " "
+ (e.index);
final String name = item.getRepName(e.index, item.getReplication());
//currentPane.setBorder(BorderFactory.createTitledBorder(name));

// set the name of this pane, which names the tab
Expand Down
4 changes: 2 additions & 2 deletions test/org/openlcb/cdi/swing/CdiPanelDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void displayFile(String fileName) {
File file;
if (fileName == null) {
// find file & load file
fci.setDialogTitle("Find desired script file");
fci.setDialogTitle("Find desired CDI file");
fci.rescanCurrentDirectory();

int retVal = fci.showOpenDialog(null);
Expand Down Expand Up @@ -70,7 +70,7 @@ public JButton handleReadButton(JButton button) {
* We always use the same file chooser in this class, so that the user's
* last-accessed directory remains available.
*/
static JFileChooser fci = new JFileChooser();
static JFileChooser fci = new JFileChooser(".");

// Main entry point
static public void main(String[] args) {
Expand Down

0 comments on commit 0eaf235

Please sign in to comment.