package variableEditorComponents;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import valueTypes.ColorValue;
import variableEditorUI.VariableEditorUIUpdateThread;
import variables.Variable;
import expressionConsole.ExpressionConsoleModel;
/**
* A panel to edit a color which is bound to a Variable. When the user picks a
* new color by clicking on it, the content of the Variable is updated with the
* new color. When the Variable is changed from another source, the state of the
* box is updated with the new contents of the Variable.
*
* @author Curran Kelleher
*
*/
public class VariableBoundColorBox extends JPanel implements Observer,
VariableEditorComponent, MouseListener {
private static final long serialVersionUID = -7777499408692975081L;
/**
* The Variable to which this VariableBoundColorBox is bound.
*/
Variable variable;
/**
* The Value that is currently being displayed. This is checked against the
* actual value of the Variable periodically to decide whether or not to
* update the display. This String is parseable into the actual value. It is
* generated by the method Value.toParseableString()
*/
String displayedValue = "";
/**
* Construct a VariableBoundColorBox which is bound to the specified
* Variable.
*
* @param variable
* the variable to edit
*/
public VariableBoundColorBox(Variable variable) {
// set the variable reference
this.variable = variable;
// get notifications every second.
VariableEditorUIUpdateThread.getInstance().addObserver(this);
// initialize the Color box
updateWithCurrentVariableValue();
// set up the action listener
addMouseListener(this);
}
/**
* Updates the content of the Color box to reflect the current value of the
* variable.
*
*/
public void updateWithCurrentVariableValue() {
setBackground(ColorValue.extractColor(variable.evaluate()));
displayedValue = variable.evaluate().toParseableString();
}
/**
* Get the update from the VariableEditorUIUpdateThread
*/
public void update(Observable o, Object arg) {
if (o == VariableEditorUIUpdateThread.getInstance())
// if this component is no longer usable, remove it as an Observer.
if (!isDisplayable())
VariableEditorUIUpdateThread.getInstance().deleteObserver(this);
else if (!variable.evaluate().toParseableString().equals(
displayedValue))
updateWithCurrentVariableValue();
}
/**
* This method doesn't do anything.
*/
public void bindToVariableEditorComponent(
VariableEditorComponent componentToUpdate) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
public void mouseReleased(MouseEvent e) {
if (contains(e.getPoint())) {
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
Color color = JColorChooser.showDialog(this, "Choose Color",
getBackground());
if (color != null) {
ExpressionConsoleModel.getInstance().enterExpression(
variable.toString() + " = "
+ new ColorValue(color).toParseableString());
// refresh the Color
updateWithCurrentVariableValue();
}
}
}
public void mouseEntered(MouseEvent e) {
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
}
public void mouseExited(MouseEvent e) {
setBorder(null);
}
}