001: /*
002: * $Id: JGraphEditorKit.java,v 1.3 2006/06/05 19:48:25 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.editor;
011:
012: import java.util.ArrayList;
013: import java.util.Hashtable;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017:
018: /**
019: * Holds references to actions and mouse tools and updates their states.
020: */
021: public class JGraphEditorKit {
022:
023: /**
024: * Holds the (name, action) and (name, tool) pairs respectively.
025: */
026: protected Map actions = new Hashtable(), tools = new Hashtable();
027:
028: /**
029: * Holds all action bundles.
030: */
031: protected List bundles = new ArrayList(2);
032:
033: /**
034: * Adds the specified action to the kit.
035: *
036: * @param action
037: * The action to add.
038: * @return Returns the previous action for the same name.
039: *
040: * @see JGraphEditorAction#getName()
041: */
042: public Object addAction(JGraphEditorAction action) {
043: if (action != null)
044: return actions.put(action.getName(), action);
045: return null;
046: }
047:
048: /**
049: * Adds all actions in the specified bundle and stores a reference to the
050: * bundle for later update of the actions.
051: *
052: * @param bundle
053: * The bundle to add the actions from.
054: */
055: public void addBundle(JGraphEditorAction.Bundle bundle) {
056: JGraphEditorAction[] a = bundle.getActions();
057: for (int i = 0; i < a.length; i++)
058: addAction(a[i]);
059: bundles.add(bundle);
060: }
061:
062: /**
063: * Returns the action for the specified name or <code>null</code> if no
064: * such action exists.
065: *
066: * @param name
067: * The name that identifies the action.
068: * @return Returns the action with the name <code>name</code> or
069: * <code>null</code>.
070: */
071: public JGraphEditorAction getAction(String name) {
072: if (name != null)
073: return (JGraphEditorAction) actions.get(name);
074: return null;
075: }
076:
077: /**
078: * Adds the specified tool to the kit.
079: *
080: * @param tool
081: * The tool to add.
082: * @return Returns the previous tool for the same name.
083: *
084: * @see JGraphEditorTool#getName()
085: */
086: public Object addTool(JGraphEditorTool tool) {
087: if (tool != null) {
088: return tools.put(tool.getName(), tool);
089: }
090: return null;
091: }
092:
093: /**
094: * Returns the tool for the specified name or <code>null</code> if no such
095: * tool exists.
096: *
097: * @param name
098: * The name that identifies the tool.
099: * @return Returns the tool with the name <code>name</code> or
100: * <code>null</code>.
101: */
102: public JGraphEditorTool getTool(String name) {
103: if (name != null)
104: return (JGraphEditorTool) tools.get(name);
105: return null;
106: }
107:
108: /**
109: * This is messaged from the application when the kit should update the
110: * state of its actions and tools. This implementation updates all
111: * registered bundles.
112: *
113: * @see JGraphEditorAction.Bundle#update()
114: */
115: public void update() {
116: Iterator it = bundles.iterator();
117: while (it.hasNext())
118: ((JGraphEditorAction.Bundle) it.next()).update();
119: }
120:
121: /**
122: * @return Returns the actions.
123: */
124: public Map getActions() {
125: return actions;
126: }
127:
128: /**
129: * @param actions The actions to set.
130: */
131: public void setActions(Map actions) {
132: this .actions = actions;
133: }
134:
135: /**
136: * @return Returns the bundles.
137: */
138: public List getBundles() {
139: return bundles;
140: }
141:
142: /**
143: * @param bundles The bundles to set.
144: */
145: public void setBundles(List bundles) {
146: this .bundles = bundles;
147: }
148:
149: /**
150: * @return Returns the tools.
151: */
152: public Map getTools() {
153: return tools;
154: }
155:
156: /**
157: * @param tools The tools to set.
158: */
159: public void setTools(Map tools) {
160: this.tools = tools;
161: }
162: }
|