Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved handling of identically-named formula parameters. #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 26 additions & 6 deletions src/ucar/unidata/data/DataOperand.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class DataOperand {
/** Data categories */
private List categories;

/** Data Choice associated with this operand. May be {@code null}. */
private DerivedDataChoice dataChoice;

/** associated data */
private Object data;
Expand All @@ -98,17 +100,18 @@ public class DataOperand {
*
*/
public DataOperand(String name) {
this(name, null);
this(null, name, null);
}

/**
* Create a new DataOperand
*
* Create a new DataOperand.
*
* @param choice Data choice associated with this operand. May be {@code null}.
* @param name name for this object
* @param data associated data
*
*/
public DataOperand(String name, Object data) {
public DataOperand(DerivedDataChoice choice, String name, Object data) {
this.dataChoice = choice;
this.name = name;
this.data = data;
int idx1 = name.indexOf("[");
Expand Down Expand Up @@ -461,7 +464,24 @@ public List getTimeIndices() {
return timeIndices;
}


/**
* Returns the Data Choice associated with this operand.
*
* @return Either the associated data choice or {@code null}.
*/
public DerivedDataChoice getDataChoice() {
return dataChoice;
}

/**
* Sets the Data Choice associated with this operand.
*
* @param choice Associated data choice. May be {@code null}.
*/
public void setDataChoice(DerivedDataChoice choice) {
dataChoice = choice;
}

/**
* Test
*
Expand Down
21 changes: 10 additions & 11 deletions src/ucar/unidata/data/DerivedDataChoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public String getFullDescription() {
/**
* A utility to to add (uniquely) the given opName into the list of ops.
*
* @param choice The data choice associated with the operand being created.
* @param opName The operand name (e.g., D1)
* @param data The data associated with the operand.
* @param operands The list of operand ({@link DataOperand}) objects.
Expand All @@ -295,23 +296,21 @@ public String getFullDescription() {
* @return The instance of the {@link DataOperand} that holds
* the opName/data pair.
*/
private static DataOperand addOperand(String opName, Object data,
List operands, Hashtable opsSoFar) {
private static DataOperand addOperand(DerivedDataChoice choice, String opName, Object data,
List operands, Hashtable opsSoFar) {
DataOperand dataOperand = (DataOperand) opsSoFar.get(opName);
if (dataOperand != null) {
if ( !dataOperand.isBound()) {
dataOperand.setData(data);
}
return dataOperand;
}
dataOperand = new DataOperand(opName, data);
dataOperand = new DataOperand(choice, opName, data);
opsSoFar.put(opName, dataOperand);
operands.add(dataOperand);
return dataOperand;
}



/**
* Add the given operand op into the list of ops if it has not been
* placed into the seen table and if it is not one of the jython keywords.
Expand Down Expand Up @@ -427,7 +426,7 @@ private List collectOperands(DataCategory category,
for (int i = 0; i < childrenChoices.size(); i++) {
DataChoice dc = (DataChoice) childrenChoices.get(i);
if (dc instanceof UserDataChoice) {
DataOperand operand = new DataOperand(dc.getName(),
DataOperand operand = new DataOperand(this, dc.getName(),
((UserDataChoice) dc).getValue());
allUserOperands.add(operand);
if ( !operand.isBound()) {
Expand Down Expand Up @@ -475,22 +474,22 @@ private List collectOperands(DataCategory category,
String opName = dc.getName();
if (dc instanceof UnboundDataChoice) {
// System.err.println ("unbound choice");
addOperand(opName, null, operands, operandsSoFar);
addOperand(this, opName, null, operands, operandsSoFar);
} else if (dc instanceof UserDataChoice) {
// System.err.println ("UDC");
UserDataChoice userChoice = (UserDataChoice) dc;
addOperand(alias, userChoice.getValue(), operands,
addOperand(this, alias, userChoice.getValue(), operands,
operandsSoFar);
if ( !userChoice.persistent) {
userChoice.setValue(null);
}
} else {
//Here, put the DataChoice in as the data (sort of as a place holder for later).
// System.err.println ("addOperand " + opName + " alias = " + alias );
addOperand(opName, dc, operands, operandsSoFar);
addOperand(this, opName, dc, operands, operandsSoFar);
if (userSelectedChoices.get(alias) == null) {
// System.err.println ("addOperand-alias " + alias);
addOperand(alias, dc, operands, operandsSoFar);
addOperand(this, alias, dc, operands, operandsSoFar);
}
}
}
Expand All @@ -503,7 +502,7 @@ private List collectOperands(DataCategory category,
for (int i = 0; i < nonUserOperands.size(); i++) {
DataOperand operand = (DataOperand) nonUserOperands.get(i);
// System.err.println ("addOperand-nonUser ");
addOperand(operand.getName(), null, operands, operandsSoFar);
addOperand(this, operand.getName(), null, operands, operandsSoFar);
}


Expand Down
49 changes: 38 additions & 11 deletions src/ucar/unidata/idv/ui/IdvUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5662,13 +5662,33 @@ public List selectUserChoices(String msg, List userOperands) {
if (fieldType == null) {
fieldType = FIELDTYPE_TEXT;
}
String label = operand.getLabel();
Object dflt = operand.getUserDefault();
Object cacheKey = Misc.newList(label, fieldType);
Object cachedOperand = operandCache.get(cacheKey);

DerivedDataChoice formula = operand.getDataChoice();
String description = operand.getDescription();
if (formula != null) {
description = formula.toString();
}

String label = operand.getLabel();
Object dflt = operand.getUserDefault();
Object cacheKeyNewStyle = Misc.newList(description, label, fieldType);
Object cacheKey = Misc.newList(label, fieldType);

Object cachedOperand = null;
boolean oldStyle = operandCache.containsKey(cacheKey);
boolean newStyle = operandCache.containsKey(cacheKeyNewStyle);

// if new style, always use that and ignore old style
// if no new style, proceed as before.
if (newStyle) {
cachedOperand = operandCache.get(cacheKeyNewStyle);
} else if (oldStyle) {
cachedOperand = operandCache.get(cacheKey);
}
if (cachedOperand != null) {
dflt = cachedOperand;
}

JCheckBox cbx = new JCheckBox("", operand.isPersistent());
persistentCbxs.add(cbx);
JComponent field = null;
Expand Down Expand Up @@ -5781,11 +5801,18 @@ public void actionPerformed(ActionEvent ae) {
}
List values = new ArrayList();
for (int i = 0; i < userOperands.size(); i++) {
DataOperand operand = (DataOperand) userOperands.get(i);
String label = operand.getLabel();
Object field = fields.get(i);
Object value = null;
Object cacheValue = null;
DataOperand operand = (DataOperand) userOperands.get(i);
String description = operand.getDescription();
DerivedDataChoice formula = operand.getDataChoice();
String label = operand.getLabel();
Object field = fields.get(i);
Object value = null;
Object cacheValue = null;

if (formula != null) {
description = formula.toString();
}

if (field instanceof JTextComponent) {
value = ((JTextComponent) field).getText().trim();
} else if (field instanceof JCheckBox) {
Expand All @@ -5808,13 +5835,13 @@ public void actionPerformed(ActionEvent ae) {
if (fieldType == null) {
fieldType = "text";
}
Object cacheKey = Misc.newList(label, fieldType);

Object cacheKey = Misc.newList(description, label, fieldType);
operandCache.put(cacheKey, cacheValue);
values.add(new UserOperandValue(value, cbx.isSelected()));
}
getStore().putEncodedFile("operandcache.xml", operandCache);
return values;

}


Expand Down