package research;
import research.tool.Tool;
import research.toolAction.ToolAction;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
/**
* author: zhangwei
* Date: 2003-5-10
* Time: 23:57:53
*/
public class ToolActionController {
public final static String DEFAULT_POLICY = "DEFAULT_POLICY";
public final static String CONTINUE_POLICY = "CONTINUE_POLICY";
protected java.util.List actions = new ArrayList();
protected final PropertyChangeListener toolActionPropertyChangeListener = new ToolActionPropertyChangeListener();
protected final PropertyChangeListener toolPropertyChangeListener = new ToolPropertyChangeListener();
protected ToolAction defaultAction = null;
protected ToolAction currentAction = null;
protected DrawingEditor drawingEditor = null;
protected String policy = DEFAULT_POLICY;
public ToolActionController() {
defaultAction = null;
currentAction = null;
}
public void setPolicy(String policy){
if (!DEFAULT_POLICY.equals(policy) && !CONTINUE_POLICY.equals(policy)){
return;
}
this.policy = policy;
}
public String getPolicy(){
return policy;
}
public void setDrawingEditor(DrawingEditor drawingEditor) {
this.drawingEditor = drawingEditor;
if (currentAction != null) {
this.drawingEditor.setTool((Tool) currentAction.getValue(ToolAction.TOOL));
} else {
this.drawingEditor.setTool(null);
}
}
public DrawingEditor getDrawingEditor() {
return drawingEditor;
}
public synchronized void addToolAction(ToolAction toolAction) {
if (actions.contains(toolAction))
return;
toolAction.addPropertyChangeListener(toolActionPropertyChangeListener);
actions.add(toolAction);
Tool tool = (Tool) toolAction.getValue(ToolAction.TOOL);
if (tool != null) {
tool.addPropertyChangeListener(Tool.TOOL_DONE, toolPropertyChangeListener);
}
}
public synchronized void removeToolAction(ToolAction toolAction) {
if (!actions.contains(toolAction))
return;
Tool tool = (Tool) toolAction.getValue(ToolAction.TOOL);
if (tool != null) {
tool.removePropertyChangeListener(Tool.TOOL_DONE, toolPropertyChangeListener);
}
actions.remove(toolAction);
toolAction.removePropertyChangeListener(toolActionPropertyChangeListener);
if (toolAction == defaultAction) {
defaultAction = null;
}
}
public void setDefaultToolAction(ToolAction toolAction) {
if (!actions.contains(toolAction))
return;
if (defaultAction != null) {
defaultAction.setDefault(false);
}
defaultAction = toolAction;
defaultAction.setDefault(true);
}
protected class ToolPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
if (Tool.TOOL_DONE.equals(name)) {
if(e.getSource() != currentAction.getValue(ToolAction.TOOL))
return;
if (policy.equals(ToolActionController.CONTINUE_POLICY)) {
currentAction.activate();
} else {
if (currentAction != defaultAction)
currentAction.setSelected(false);
else {
//reactivate the defaultAction
currentAction.activate();
}
}
}
}
}
protected class ToolActionPropertyChangeListener implements PropertyChangeListener {
protected boolean processing = false;
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
if (name.equals("selected")) {
if (processing) return;
processing = true;
boolean newValue = ((Boolean) e.getNewValue()).booleanValue();
ToolAction action = (ToolAction) e.getSource();
if (newValue) {
ToolAction ta = null;
for (int i = 0; i < actions.size(); i++) {
ta = (ToolAction) actions.get(i);
if (ta == action) continue;
if (ta.isSelected()) ta.setSelected(false);
}
currentAction = action;
} else {
ToolAction ta = null;
for (int i = 0; i < actions.size(); i++) {
ta = (ToolAction) actions.get(i);
if (ta == action) continue;
if (ta.isSelected()) ta.setSelected(false);
}
if (defaultAction != null) {
defaultAction.setSelected(true);
currentAction = defaultAction;
} else {
currentAction = null;
}
}
if (drawingEditor != null) {
if (currentAction != null) {
drawingEditor.setTool((Tool) currentAction.getValue(ToolAction.TOOL));
} else {
drawingEditor.setTool(null);
}
}
processing = false;
} else if (name.equals(ToolAction.TOOL)) {
Tool oldTool = (Tool) e.getOldValue();
Tool newTool = (Tool) e.getNewValue();
if (oldTool != null)
oldTool.removePropertyChangeListener(Tool.TOOL_DONE, toolPropertyChangeListener);
if (newTool != null)
newTool.addPropertyChangeListener(Tool.TOOL_DONE, toolPropertyChangeListener);
}
}
}
}