public class UnitSelector extends ResizeLabel implements ChangeListener, MouseListener,
ItemSelectable {
- private final DoubleModel model;
+ private DoubleModel model;
private final Action[] extraActions;
private UnitGroup unitGroup;
if (model != null) {
this.unitGroup = model.getUnitGroup();
this.currentUnit = model.getCurrentUnit();
- } else {
+ } else if (group != null) {
this.unitGroup = group;
this.currentUnit = group.getDefaultUnit();
+ } else {
+ this.unitGroup = UnitGroup.UNITS_NONE;
+ this.currentUnit = UnitGroup.UNITS_NONE.getDefaultUnit();
}
this.extraActions = actions;
withinBorder = new CompoundBorder(new LineBorder(new Color(0f, 0f, 0f, 0.6f)),
new EmptyBorder(1, 1, 1, 1));
+ // Add model listener if showing value
+ if (showValue)
+ this.model.addChangeListener(this);
+
setBorder(normalBorder);
updateText();
}
public UnitSelector(DoubleModel model, boolean showValue, Action... actions) {
this(model, showValue, null, actions);
-
- // Add model listener
- this.model.addChangeListener(this);
}
return model;
}
+
+ /**
+ * Set the current double model.
+ *
+ * @param model the model to set, <code>null</code> is NOT allowed.
+ */
+ public void setModel(DoubleModel model) {
+ if (this.model != null && showValue) {
+ this.model.removeChangeListener(this);
+ }
+ this.model = model;
+ this.unitGroup = model.getUnitGroup();
+ this.currentUnit = model.getCurrentUnit();
+ if (showValue) {
+ this.model.addChangeListener(this);
+ }
+ updateText();
+ }
+
+
/**
* Return the unit group that is being shown, or <code>null</code>. Either this method