001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Mikael MARCHE, Fayçal SOUGRATI, Vincent PAUTRET
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package salomeTMF_plug.requirements.ihm;
025:
026: import java.awt.GridLayout;
027: import java.awt.Toolkit;
028:
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTree;
032: import javax.swing.ScrollPaneConstants;
033: import javax.swing.tree.DefaultMutableTreeNode;
034: import javax.swing.tree.MutableTreeNode;
035: import javax.swing.tree.TreePath;
036: import javax.swing.tree.TreeSelectionModel;
037:
038: import org.objectweb.salome_tmf.api.ApiConstants;
039: import org.objectweb.salome_tmf.data.DataConstants;
040: import org.objectweb.salome_tmf.data.Family;
041: import org.objectweb.salome_tmf.data.Test;
042: import org.objectweb.salome_tmf.data.TestList;
043: import org.objectweb.salome_tmf.ihm.models.TestTreeModel;
044: import org.objectweb.salome_tmf.ihm.models.TreeRenderer;
045:
046: public class SimpleDynamicTestTree extends JPanel implements
047: ApiConstants, DataConstants {
048:
049: /**
050: * La racine de l'arbre
051: */
052: protected DefaultMutableTreeNode rootNode;
053:
054: /**
055: * Mod?le de l'arbre
056: */
057: protected TestTreeModel treeModel;
058:
059: /**
060: * L'arbre
061: */
062: protected JTree tree;
063:
064: /**
065: *
066: */
067: private Toolkit toolkit = Toolkit.getDefaultToolkit();
068:
069: /**
070: * Le noeud selectionn?
071: */
072: private DefaultMutableTreeNode selectedNode;
073:
074: /**
075: * Type de l'arbre "campagne" ou "suite"
076: */
077: private int treeType;
078:
079: /**************************************************************************/
080: /** CONSTRUCTEUR ***/
081: /**************************************************************************/
082: /**
083: * Le constructeur de l'arbre
084: */
085: public SimpleDynamicTestTree(String rootName, int type) {
086: super (new GridLayout(1, 0));
087:
088: // Le support graphique sur lequel est fond? l'arbre
089: TreeRenderer renderer = new TreeRenderer();
090:
091: treeType = type;
092:
093: // la racine de l'arbre de test
094: rootNode = new DefaultMutableTreeNode(rootName);
095:
096: tree = new JTree();
097: treeModel = new TestTreeModel(rootNode, tree, null);
098: tree.setModel(treeModel);
099:
100: //tree.setEditable(true);
101: tree.setCellRenderer(renderer);
102: tree.getSelectionModel().setSelectionMode(
103: TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
104: tree.setShowsRootHandles(true);
105:
106: JScrollPane scrollPane = new JScrollPane(tree,
107: ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
108: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
109: add(scrollPane);
110: } // Fin du constructeur DynamicTree/0
111:
112: public SimpleDynamicTestTree(DefaultMutableTreeNode _rootNode,
113: int type) {
114: super (new GridLayout(1, 0));
115:
116: // Le support graphique sur lequel est fond? l'arbre
117: TreeRenderer renderer = new TreeRenderer();
118:
119: treeType = type;
120:
121: // la racine de l'arbre de test
122: rootNode = _rootNode;
123:
124: tree = new JTree();
125: treeModel = new TestTreeModel(rootNode, tree, null);
126: tree.setModel(treeModel);
127:
128: //tree.setEditable(true);
129: tree.setCellRenderer(renderer);
130: tree.getSelectionModel().setSelectionMode(
131: TreeSelectionModel.SINGLE_TREE_SELECTION);
132: tree.setShowsRootHandles(true);
133:
134: JScrollPane scrollPane = new JScrollPane(tree,
135: ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
136: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
137: add(scrollPane);
138: } // Fin du constructeur DynamicTree/0
139:
140: /**
141: * Supprime tout de l'arbre, sauf la racine
142: */
143: public void clear() {
144: rootNode.removeAllChildren();
145: treeModel.reload();
146: //treeModel.reload();
147: } // Fin de la m?thode clear/0
148:
149: public void removeNode(DefaultMutableTreeNode node) {
150: treeModel.removeNodeFromParent(node);
151: }
152:
153: /**
154: * Cherche de mani?re r?cursive et supprime le noeud correspond ? celui
155: * pass? en param?tre. A l'appel le premier param?tre est le noeud de
156: * d?but de la recherche.
157: * @param currentNode le noeud courant
158: * @param node le noeud ? retirer.
159: */
160: public void searchAndRemove(DefaultMutableTreeNode currentNode,
161: DefaultMutableTreeNode node) {
162: if (currentNode.equals(node)) {
163: MutableTreeNode parent = (MutableTreeNode) (currentNode
164: .getParent());
165: if (parent != null) {
166: treeModel.removeNodeFromParent(currentNode);
167: }
168: } else {
169: for (int i = 0; i < currentNode.getChildCount(); i++) {
170: searchAndRemove((DefaultMutableTreeNode) currentNode
171: .getChildAt(i), node);
172: }
173: }
174: } // Fin de la m?thode searchAndRemove/2
175:
176: /**
177: * M?thode d'ajout d'un noeud dans l'arbre
178: * @param child l'objet ajout? dans l'arbre
179: * @return le nouveau noeud de l'arbre
180: */
181: public DefaultMutableTreeNode addObject(Object child) {
182: DefaultMutableTreeNode parentNode = null;
183: TreePath parentPath = tree.getSelectionPath();
184:
185: if (parentPath == null) {
186: parentNode = rootNode;
187: } else {
188: parentNode = (DefaultMutableTreeNode) (parentPath
189: .getLastPathComponent());
190: }
191: return addObject(parentNode, child, true);
192: } // Fin de la classe addObject/1
193:
194: /**
195: * M?thode d'ajout d'un noeud invisible sous un parent
196: * @param parent le parent
197: * @param child le noeud ajout?
198: * @return le nouveau noeud de l'arbre
199: */
200: public DefaultMutableTreeNode addObject(
201: DefaultMutableTreeNode parent, Object child) {
202: return addObject(parent, child, false);
203: } // Fin de la classe addObject/2
204:
205: /**
206: * M?thode d'ajout d'un noeud dans l'arbre sous le parent.
207: * @param parent le parent
208: * @param child le noeud ? ajouter
209: * @param shouldBeVisible visible ou non
210: * @return le nouveau noeud de l'arbre
211: */
212: public DefaultMutableTreeNode addObject(
213: DefaultMutableTreeNode parent, Object child,
214: boolean shouldBeVisible) {
215: // System.out.println("Ajout de : " + child + " ? " + parent);
216: DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
217: child);
218: if (parent == null) {
219: parent = rootNode;
220: }
221: // Insertion du noeud
222: treeModel.insertNodeInto(childNode, parent, parent
223: .getChildCount());
224:
225: // on s'assure que le noeud est visible
226: if (shouldBeVisible) {
227: tree.scrollPathToVisible(new TreePath(childNode.getPath()));
228: }
229:
230: return childNode;
231: } // Fin de la classe addObject/3
232:
233: /**
234: * M?thode qui retourne le noeud de l'arbre qui est s?lectionn?
235: * @return un noeud de l'arbre
236: */
237: public DefaultMutableTreeNode getSelectedNode() {
238: return selectedNode;
239: } // Fin de la m?thode getSelectedNode/0
240:
241: /**
242: * Retourne la racine de l'arbre
243: * @return la racine de l'arbre
244: */
245: public DefaultMutableTreeNode getRoot() {
246: return rootNode;
247: } // Fin de la m?thode getRoot/0
248:
249: /**
250: * Retourne le type de l'arbre : "campagne" ou "suite"
251: * @return le type de l'arbre
252: */
253: public int getTreeType() {
254: return treeType;
255: } // Fin de la m?thode getTreeType/0
256:
257: /**
258: * Retourne le noeud correspondant ? la famille dont le nom est pass? en
259: * param?tre. Retourne <code>null</code>, si le nom ne correspond pas ? une
260: * famille.
261: * @param familyName un nom
262: * @return le noeud correspondant ? la famille dont le nom est pass? en
263: * param?tre. Retourne <code>null</code>, si le nom ne correspond pas ? une
264: * famille.
265: */
266: public DefaultMutableTreeNode findRemoveFamilyNode(
267: String familyName, boolean toRemove) {
268: // DefaultMutableTreeNode root = (DefaultMutableTreeNode)treeModel.getRoot();
269: DefaultMutableTreeNode root = rootNode;
270: for (int i = 0; i < root.getChildCount(); i++) {
271: DefaultMutableTreeNode familyNode = (DefaultMutableTreeNode) root
272: .getChildAt(i);
273: if (familyNode.getUserObject() instanceof Family
274: && ((Family) familyNode.getUserObject())
275: .getNameFromModel().equals(familyName)) {
276: if (toRemove) {
277: familyNode.removeFromParent();
278: }
279: return familyNode;
280: }
281: }
282: return null;
283: } // Fin de la m?thode findFamilyNode/1
284:
285: /**
286: * Retourne le noeud correspondant ? la suite de tests dont le nom est
287: * pass? en param?tre. Retourne <code>null</code>, si le nom ne
288: * correspond pas ? une suite de tests
289: * @param testListName un nom
290: * @param familyName un nom de famille
291: * @param toRemove pour indiquer que le noeud doit ?tre supprim?
292: * @return le noeud correspondant ? la suite de tests dont le nom est
293: * pass? en param?tre. Retourne <code>null</code>, si le nom ne
294: * correspond pas ? une suite de tests.
295: */
296: public DefaultMutableTreeNode findRemoveTestListNode(
297: String testListName, String familyName, boolean toRemove) {
298: // DefaultMutableTreeNode root = (DefaultMutableTreeNode)treeModel.getRoot();
299: DefaultMutableTreeNode root = rootNode;
300: for (int i = 0; i < root.getChildCount(); i++) {
301: DefaultMutableTreeNode familyNode = (DefaultMutableTreeNode) root
302: .getChildAt(i);
303: if (familyNode.getUserObject() instanceof Family
304: && ((Family) familyNode.getUserObject())
305: .getNameFromModel().equals(familyName)) {
306: for (int j = 0; j < familyNode.getChildCount(); j++) {
307: DefaultMutableTreeNode testListNode = (DefaultMutableTreeNode) familyNode
308: .getChildAt(j);
309: if (testListNode.getUserObject() instanceof TestList
310: && ((TestList) testListNode.getUserObject())
311: .getNameFromModel().equals(
312: testListName)) {
313: if (toRemove) {
314: testListNode.removeFromParent();
315: }
316: return testListNode;
317: }
318: }
319: }
320: }
321: return null;
322: } // Fin de la m?thode findFamilyNode/1
323:
324: /**
325: * Retourne le noeud correspondant ? la suite de tests dont le nom est
326: * pass? en param?tre. Retourne <code>null</code>, si le nom ne
327: * correspond pas ? une suite de tests
328: * @param testListName un nom
329: * @param toRemove pour supprimer le noeud une fois trouv?
330: * @return le noeud correspondant ? la suite de tests dont le nom est
331: * pass? en param?tre. Retourne <code>null</code>, si le nom ne
332: * correspond pas ? une suite de tests.
333: */
334: public DefaultMutableTreeNode findRemoveTestNode(String testName,
335: String testListName, String familyName, boolean toRemove) {
336: // DefaultMutableTreeNode root = (DefaultMutableTreeNode)treeModel.getRoot();
337: DefaultMutableTreeNode root = rootNode;
338: for (int i = 0; i < root.getChildCount(); i++) {
339: DefaultMutableTreeNode familyNode = (DefaultMutableTreeNode) root
340: .getChildAt(i);
341: if (familyNode.getUserObject() instanceof Family
342: && ((Family) familyNode.getUserObject())
343: .getNameFromModel().equals(familyName)) {
344: for (int j = 0; j < familyNode.getChildCount(); j++) {
345: DefaultMutableTreeNode testListNode = (DefaultMutableTreeNode) familyNode
346: .getChildAt(j);
347: if (testListNode.getUserObject() instanceof TestList
348: && ((TestList) testListNode.getUserObject())
349: .getNameFromModel().equals(
350: testListName)) {
351: for (int k = 0; k < testListNode
352: .getChildCount(); k++) {
353: DefaultMutableTreeNode testNode = (DefaultMutableTreeNode) testListNode
354: .getChildAt(k);
355: if (testNode.getUserObject() instanceof Test
356: && ((Test) testNode.getUserObject())
357: .getNameFromModel().equals(
358: testName)) {
359: if (toRemove) {
360: DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) testNode
361: .getParent();
362: parentNode.remove(testNode);
363: }
364: return testNode;
365: }
366: }
367:
368: }
369: }
370: }
371: }
372: return null;
373: } // Fin de la m?thode findFamilyNode/1
374:
375: /**
376: * Retourne le mod?le de donn?es de l'arbre
377: * @return le mod?le de donn?es de l'arbre
378: */
379: public TestTreeModel getModel() {
380: return treeModel;
381: } // Fin de la m?thode getModel/0
382:
383: /**
384: * @return
385: */
386: public JTree getTree() {
387: return tree;
388: }
389:
390: } // Fin de la classe DynamicTree
|