001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.model.tree;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import javax.swing.event.TreeModelEvent;
024: import javax.swing.event.TreeModelListener;
025: import javax.swing.tree.TreeModel;
026: import javax.swing.tree.TreePath;
027:
028: import org.apache.log4j.Logger;
029:
030: import com.eviware.soapui.model.ModelItem;
031: import com.eviware.soapui.model.tree.nodes.WorkspaceTreeNode;
032: import com.eviware.soapui.model.workspace.Workspace;
033:
034: /**
035: * The Navigator TreeModel
036: *
037: * @author Ole.Matzura
038: */
039:
040: public class SoapUITreeModel implements TreeModel {
041: private Set<TreeModelListener> listeners = new HashSet<TreeModelListener>();
042: private SoapUITreeNode workspaceNode;
043: private final static Logger logger = Logger
044: .getLogger(SoapUITreeModel.class);
045: private Map<ModelItem, SoapUITreeNode> modelItemMap = new HashMap<ModelItem, SoapUITreeNode>();
046:
047: public SoapUITreeModel(Workspace workspace) {
048: workspaceNode = new WorkspaceTreeNode(workspace, this );
049: mapModelItem(workspaceNode);
050: }
051:
052: public Object getRoot() {
053: return workspaceNode;
054: }
055:
056: public Object getChild(Object parent, int index) {
057: SoapUITreeNode treeNode = (SoapUITreeNode) parent;
058: return treeNode.getChildNode(index);
059: }
060:
061: public int getChildCount(Object parent) {
062: SoapUITreeNode treeNode = (SoapUITreeNode) parent;
063: return treeNode.getChildCount();
064: }
065:
066: public boolean isLeaf(Object node) {
067: SoapUITreeNode treeNode = (SoapUITreeNode) node;
068: return treeNode.isLeaf();
069: }
070:
071: public void valueForPathChanged(TreePath path, Object newValue) {
072: SoapUITreeNode treeNode = (SoapUITreeNode) path
073: .getLastPathComponent();
074: if (treeNode.valueChanged(newValue)) {
075: // not implemented.. need to expose setName in ModelItem
076: }
077: }
078:
079: public int getIndexOfChild(Object parent, Object child) {
080: SoapUITreeNode treeNode = (SoapUITreeNode) parent;
081: return treeNode.getIndexOfChild(child);
082: }
083:
084: public void addTreeModelListener(TreeModelListener l) {
085: listeners.add(l);
086: }
087:
088: public void removeTreeModelListener(TreeModelListener l) {
089: listeners.remove(l);
090: }
091:
092: public void mapModelItem(SoapUITreeNode soapUITreeNode) {
093: modelItemMap.put(soapUITreeNode.getModelItem(), soapUITreeNode);
094: }
095:
096: public void unmapModelItem(ModelItem modelItem) {
097: if (modelItemMap.containsKey(modelItem))
098: modelItemMap.remove(modelItem);
099: else
100: logger.error("Failed to unmap model item ["
101: + modelItem.getName() + "]");
102: }
103:
104: public void notifyNodesInserted(TreeModelEvent e) {
105: Iterator<TreeModelListener> i = listeners.iterator();
106: while (i.hasNext()) {
107: i.next().treeNodesInserted(e);
108: }
109: }
110:
111: public void notifyNodesRemoved(TreeModelEvent e) {
112: Iterator<TreeModelListener> i = listeners.iterator();
113: while (i.hasNext()) {
114: i.next().treeNodesRemoved(e);
115: }
116: }
117:
118: public void notifyStructureChanged(TreeModelEvent e) {
119: Iterator<TreeModelListener> i = listeners.iterator();
120: while (i.hasNext()) {
121: i.next().treeStructureChanged(e);
122: }
123: }
124:
125: public void notifyNodesChanged(TreeModelEvent e) {
126: Iterator<TreeModelListener> i = listeners.iterator();
127: while (i.hasNext()) {
128: i.next().treeNodesChanged(e);
129: }
130: }
131:
132: public TreePath getPath(SoapUITreeNode treeNode) {
133: //SoapUITreeNode treeNode = modelItemMap.get( modelItem );
134: //if( treeNode == null )
135: // throw new RuntimeException( "Missing mapping for modelItem " + modelItem.getName() );
136:
137: List<Object> nodes = new ArrayList<Object>();
138: nodes.add(treeNode);
139:
140: treeNode = treeNode.getParentTreeNode();
141: while (treeNode != null) {
142: nodes.add(0, treeNode);
143: treeNode = treeNode.getParentTreeNode();
144: }
145:
146: return new TreePath(nodes.toArray());
147: }
148:
149: public void notifyNodeChanged(SoapUITreeNode treeNode) {
150: SoapUITreeNode parent = treeNode.getParentTreeNode();
151: if (parent == null) {
152: notifyNodesChanged(new TreeModelEvent(this ,
153: new Object[] { treeNode }));
154: return;
155: }
156:
157: int ix = parent.getIndexOfChild(treeNode);
158:
159: if (ix == -1) {
160: logger.error("Changed node [" + treeNode
161: + "] not found in parent [" + parent + "]");
162: return;
163: }
164:
165: notifyNodesChanged(new TreeModelEvent(this , getPath(parent),
166: new int[] { ix }, new Object[] { parent
167: .getChildNode(ix) }));
168: }
169:
170: public void notifyNodeInserted(SoapUITreeNode treeNode) {
171: SoapUITreeNode parent = treeNode.getParentTreeNode();
172: int ix = parent.getIndexOfChild(treeNode);
173:
174: if (ix == -1) {
175: logger.error("Inserted node [" + treeNode
176: + "] not found in parent [" + parent + "]");
177: return;
178: }
179:
180: mapModelItem(treeNode);
181:
182: notifyNodesInserted(new TreeModelEvent(this , getPath(parent),
183: new int[] { ix }, new Object[] { parent
184: .getChildNode(ix) }));
185: }
186:
187: public void notifyNodeRemoved(SoapUITreeNode treeNode) {
188: SoapUITreeNode parent = treeNode.getParentTreeNode();
189: int ix = parent.getIndexOfChild(treeNode);
190:
191: if (ix == -1) {
192: logger.error("Removed node [" + treeNode
193: + "] not found in parent [" + parent + "]");
194: return;
195: }
196:
197: notifyNodesRemoved(new TreeModelEvent(this , getPath(parent),
198: new int[] { ix }, new Object[] { parent
199: .getChildNode(ix) }));
200:
201: treeNode.release();
202: }
203:
204: public SoapUITreeNode getTreeNode(ModelItem parentItem) {
205: return modelItemMap.get(parentItem);
206: }
207:
208: public TreePath getPath(ModelItem modelItem) {
209: return getPath(modelItemMap.get(modelItem));
210: }
211:
212: public void mapModelItems(List<? extends SoapUITreeNode> treeNodes) {
213: Iterator<? extends SoapUITreeNode> iterator = treeNodes
214: .iterator();
215: while (iterator.hasNext()) {
216: SoapUITreeNode item = iterator.next();
217: mapModelItem(item);
218: }
219: }
220:
221: }
|