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 Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package salomeTMF_plug.requirements.ihm;
025:
026: import java.awt.Color;
027: import java.awt.Component;
028: import java.util.Observer;
029: import java.util.Vector;
030:
031: import javax.swing.ImageIcon;
032: import javax.swing.JTree;
033: import javax.swing.tree.DefaultMutableTreeNode;
034: import javax.swing.tree.DefaultTreeCellRenderer;
035: import javax.swing.tree.DefaultTreeModel;
036: import javax.swing.tree.TreeModel;
037: import javax.swing.tree.TreePath;
038:
039: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
040: import org.objectweb.salome_tmf.ihm.models.PopupListener;
041:
042: import salomeTMF_plug.requirements.ReqPlugin;
043: import salomeTMF_plug.requirements.data.DataLoader;
044: import salomeTMF_plug.requirements.data.IReqFilter;
045: import salomeTMF_plug.requirements.data.ReqFamily;
046: import salomeTMF_plug.requirements.data.ReqFilter;
047: import salomeTMF_plug.requirements.data.ReqLeaf;
048: import salomeTMF_plug.requirements.data.Requirement;
049: import salomeTMF_plug.requirements.sqlWrapper.ReqWrapper;
050:
051: public class RequirementTree extends JTree {
052:
053: protected DefaultMutableTreeNode parent;
054: protected DefaultTreeCellRenderer renderer = null;
055: DefaultTreeModel treeModel;
056: RequirementTreeObservable pRequirementTreeObservable;
057: IReqFilter pPriorityFilter;
058:
059: public RequirementTree() {
060: super ();
061: pRequirementTreeObservable = new RequirementTreeObservable();
062: addTreeSelectionListener(pRequirementTreeObservable);
063: parent = new DefaultMutableTreeNode(new ReqFamily(
064: "Requirements_"
065: + ReqPlugin.getProjectRef().getNameFromModel(),
066: ""));
067: //treeModel = new DefaultTreeModel(parent);
068:
069: pPriorityFilter = new ReqFilter(1007);
070: treeModel = new FilterReqTreeModel(pPriorityFilter, parent,
071: this );
072: pRequirementTreeObservable.currentNode = parent;
073:
074: setModel(treeModel);
075: setCellRenderer(getRender());
076: treeModel.setRoot(parent);
077: setSelectionPath(new TreePath(parent.getPath()));
078: addMouseListener(new PopupListener(new PopPopTreeMenu(this ),
079: this ));
080: }
081:
082: IReqFilter getFilter() {
083: return pPriorityFilter;
084: }
085:
086: void setStatTools(StatRequirement _pStatRequirement,
087: FiltrePanel _pFiltrePanel) {
088: pRequirementTreeObservable.setStatTools(_pStatRequirement,
089: _pFiltrePanel);
090: }
091:
092: void setPathToRoot() {
093: //setSelectionPath(new TreePath(parent.getPath()));
094: expandPath(new TreePath(parent.getPath()));
095: }
096:
097: public void reload() {
098: try {
099: parent.removeAllChildren();
100: DataLoader.loadData(this );
101: treeModel.reload();
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: }
106:
107: public void print() {
108: //pJTree = new JTree(new DefaultTreeModel(parent));
109: print(parent, 0);
110: }
111:
112: static public void print(DefaultMutableTreeNode node, int tab) {
113: Requirement preq = (Requirement) node.getUserObject();
114: int nbChild = node.getChildCount();
115: if (nbChild == 0) {
116: for (int t = 0; t < tab; t++) {
117: System.out.print("\t");
118: }
119: System.out.println("Reaqleaf : " + preq);
120: return;
121: } else {
122: int i = 0;
123: for (int t = 0; t < tab; t++) {
124: System.out.print("\t");
125: }
126: System.out.println("Reqfamily : " + preq + "["
127: + ((ReqFamily) preq).getFistLeaf() + "]");
128: while (i < nbChild) {
129: print((DefaultMutableTreeNode) node.getChildAt(i),
130: tab + 1);
131: i++;
132: }
133: }
134: return;
135: }
136:
137: /*public void refresh(){
138: treeModel.reload();
139: }*/
140:
141: public void refreshCurrent() {
142: DefaultMutableTreeNode node = pRequirementTreeObservable.currentNode;
143: if (node != null) {
144: treeModel.reload(node);
145: pRequirementTreeObservable.refresh();
146: }
147: }
148:
149: public void refreshNode(DefaultMutableTreeNode node) {
150: if (node != null) {
151: treeModel.reload(node);
152: }
153: }
154:
155: public JTree getCopy(boolean reload) {
156: JTree pJTree = null;
157: if (reload) {
158: reload();
159: }
160: //pJTree = new JTree(new DefaultTreeModel(parent));
161: pJTree = new JTree(new DefaultTreeModel(getCopy(parent)));
162: pJTree.setCellRenderer(getRender());
163: return pJTree;
164: }
165:
166: public void copyIn(JTree pJTree, boolean reload) {
167: if (reload) {
168: reload();
169: }
170: TreeModel pTreeModel = pJTree.getModel();
171: DefaultMutableTreeNode pParent = (DefaultMutableTreeNode) pTreeModel
172: .getRoot();
173: pParent.removeAllChildren();
174: int nbChild = parent.getChildCount();
175: int i = 0;
176: while (i < nbChild) {
177: DefaultMutableTreeNode child_copy = getCopy((DefaultMutableTreeNode) parent
178: .getChildAt(i));
179: pParent.insert(child_copy, i);
180: i++;
181: }
182: }
183:
184: private DefaultMutableTreeNode getCopy(DefaultMutableTreeNode node) {
185: DefaultMutableTreeNode copy_node = new DefaultMutableTreeNode(
186: node.getUserObject());
187: int nbChild = node.getChildCount();
188: if (nbChild == 0) {
189: return copy_node;
190: } else {
191: int i = 0;
192: while (i < nbChild) {
193: DefaultMutableTreeNode child_copy = getCopy((DefaultMutableTreeNode) node
194: .getChildAt(i));
195: copy_node.insert(child_copy, i);
196: i++;
197: }
198: }
199:
200: return copy_node;
201: }
202:
203: public void addRequirementToCurrent(Requirement req)
204: throws Exception {
205: Requirement pReq = (Requirement) pRequirementTreeObservable.currentNode
206: .getUserObject();
207: if (pReq instanceof ReqFamily) {
208: req.setParent(pReq);
209: DefaultMutableTreeNode pNode = new DefaultMutableTreeNode(
210: req);
211: insertNode(pRequirementTreeObservable.currentNode, pNode);
212: //currentNode.add(pNode);
213: //scrollPathToVisible(new TreePath(pNode.getPath()));
214: setSelectionPath(new TreePath(pNode.getPath()));
215: } else {
216: throw new Exception(
217: "[RequirementTree->addRequirementToCurrent] no requiment family found");
218: }
219: }
220:
221: public void addRequirementToReqNode(Requirement req,
222: Requirement reqFamily, boolean updatePath) throws Exception {
223: if (reqFamily instanceof ReqFamily) {
224: req.setParent(reqFamily);
225: DefaultMutableTreeNode _parent = search(parent, reqFamily);
226: DefaultMutableTreeNode pNode = new DefaultMutableTreeNode(
227: req);
228: insertNode(_parent, pNode);
229: if (updatePath) {
230: setSelectionPath(new TreePath(pNode.getPath()));
231: }
232: } else {
233: throw new Exception(
234: "[RequirementTree->addRequirementToReqNode] no requiment family found");
235: }
236: }
237:
238: public void addRequirementToReqNodeInTree(Requirement pReq,
239: DefaultMutableTreeNode _parent, boolean updatePath) {
240: DefaultMutableTreeNode pNode = new DefaultMutableTreeNode(pReq);
241: insertNode(_parent, pNode);
242: if (pReq instanceof ReqFamily) {
243: ReqFamily pReq2 = (ReqFamily) pReq;
244: Vector reqFams = pReq2.getFirstFamily();
245: int size = reqFams.size();
246: for (int i = 0; i < size; i++) {
247: Requirement pReq2Add = (Requirement) reqFams
248: .elementAt(i);
249: addRequirementToReqNodeInTree(pReq2Add, pNode, false);
250: }
251: Vector reqLeafs = pReq2.getFistLeaf();
252: size = reqLeafs.size();
253: for (int i = 0; i < size; i++) {
254: Requirement pReq2Add = (Requirement) reqLeafs
255: .elementAt(i);
256: addRequirementToReqNodeInTree(pReq2Add, pNode, false);
257: }
258: }
259: if (updatePath) {
260: setSelectionPath(new TreePath(pNode.getPath()));
261: }
262:
263: }
264:
265: void insertNode(DefaultMutableTreeNode parent,
266: DefaultMutableTreeNode childNode) {
267: //Desactiver le filtre
268: boolean reactive = false;
269: if (pPriorityFilter.isActived()) {
270: pPriorityFilter.setActived(false);
271: reactive = true;
272: ((FilterReqTreeModel) treeModel).setFiltered(false);
273: }
274: treeModel.insertNodeInto(childNode, parent, parent
275: .getChildCount());
276: if (reactive) {
277: pPriorityFilter.setActived(true);
278: ((FilterReqTreeModel) treeModel).setFiltered(true);
279: }
280: }
281:
282: public void deleteCurrentRequirement() {
283: if (pRequirementTreeObservable.currentNode.equals(parent)) {
284: return;
285:
286: }
287: if (getCurrentNode() == null || getCurrentNode().equals(parent)) {
288: return;
289: }
290: DefaultMutableTreeNode p_parent;
291:
292: Requirement pReq = (Requirement) pRequirementTreeObservable.currentNode
293: .getUserObject();
294: p_parent = (DefaultMutableTreeNode) pRequirementTreeObservable.currentNode
295: .getParent();
296: try {
297: //if (pReq instanceof ReqFamily){
298: pReq.deleteInDBAndModel();
299: //}
300: treeModel
301: .removeNodeFromParent(pRequirementTreeObservable.currentNode);
302: setSelectionPath(new TreePath(p_parent.getPath()));
303: } catch (Exception e) {
304: e.printStackTrace();
305: }
306: }
307:
308: public void deleteRequirementInModelAndDB(Requirement pReq) {
309: DefaultMutableTreeNode node2del = findRequirementFromParent(pReq);
310:
311: if (node2del == null || node2del.equals(parent)) {
312: return;
313: }
314: DefaultMutableTreeNode p_parent;
315:
316: p_parent = (DefaultMutableTreeNode) node2del.getParent();
317: try {
318:
319: pReq.deleteInDBAndModel();
320: treeModel
321: .removeNodeFromParent(pRequirementTreeObservable.currentNode);
322: setSelectionPath(new TreePath(p_parent.getPath()));
323: } catch (Exception e) {
324: e.printStackTrace();
325: }
326: }
327:
328: DefaultMutableTreeNode getCurrentNode() {
329: return pRequirementTreeObservable.currentNode;
330: }
331:
332: DefaultMutableTreeNode getParentNode() {
333: return parent;
334: }
335:
336: public Requirement getCurrentRequirement() {
337: if (pRequirementTreeObservable.currentNode != null) {
338: return (Requirement) pRequirementTreeObservable.currentNode
339: .getUserObject();
340: }
341: return null;
342: }
343:
344: public Requirement getRootRequirement() {
345: if (parent != null) {
346: return (Requirement) parent.getUserObject();
347: }
348: return null;
349: }
350:
351: DefaultMutableTreeNode search(DefaultMutableTreeNode pTempNode,
352: Requirement pReq) {
353: Requirement pTempReq = (Requirement) pTempNode.getUserObject();
354: DefaultMutableTreeNode retNode = null;
355: if (pTempReq.equals(pReq)) {
356: return pTempNode;
357: } else {
358: int nbChild = pTempNode.getChildCount();
359: boolean trouve = false;
360: int i = 0;
361: while (i < nbChild && !trouve) {
362: retNode = search((DefaultMutableTreeNode) pTempNode
363: .getChildAt(i), pReq);
364: if (retNode != null) {
365: trouve = true;
366: }
367: i++;
368: }
369: return retNode;
370: }
371: }
372:
373: DefaultMutableTreeNode searchWrapper(
374: DefaultMutableTreeNode pTempNode, ReqWrapper pReq) {
375: Requirement pTempReq = (Requirement) pTempNode.getUserObject();
376: DefaultMutableTreeNode retNode = null;
377: if (pTempReq.getIdBdd() == pReq.getIdBDD()) {
378: return pTempNode;
379: } else {
380: int nbChild = pTempNode.getChildCount();
381: boolean trouve = false;
382: int i = 0;
383: while (i < nbChild && !trouve) {
384: retNode = searchWrapper(
385: (DefaultMutableTreeNode) pTempNode
386: .getChildAt(i), pReq);
387: if (retNode != null) {
388: trouve = true;
389: }
390: i++;
391: }
392: return retNode;
393: }
394: }
395:
396: DefaultMutableTreeNode searchRegx(DefaultMutableTreeNode pTempNode,
397: String reqex, boolean id) {
398: Requirement pTempReq = (Requirement) pTempNode.getUserObject();
399: DefaultMutableTreeNode retNode = null;
400:
401: if (id) {
402: String idReq = (pTempReq.getIdBdd() + "").trim();
403: if (idReq.equals(reqex)) {
404: return pTempNode;
405: }
406: } else {
407: if (pTempReq.getNameFromModel().matches(reqex)) {
408: return pTempNode;
409: }
410: }
411:
412: int nbChild = pTempNode.getChildCount();
413: boolean trouve = false;
414: int i = 0;
415: while (i < nbChild && !trouve) {
416: retNode = searchRegx((DefaultMutableTreeNode) pTempNode
417: .getChildAt(i), reqex, id);
418: if (retNode != null) {
419: trouve = true;
420: }
421: i++;
422: }
423: return retNode;
424:
425: /*String idReq = (pTempReq.getIdBdd()+"").trim();
426: if (idReq.equals(reqex) || pTempReq.getNameFromModel().matches(reqex)){
427: return pTempNode;
428: }
429: else {
430: int nbChild = pTempNode.getChildCount();
431: boolean trouve = false;
432: int i = 0;
433: while (i< nbChild && !trouve){
434: retNode = searchRegx((DefaultMutableTreeNode)pTempNode.getChildAt(i), reqex, id);
435: if (retNode != null){
436: trouve = true;
437: }
438: i++;
439: }
440: return retNode;
441: }*/
442: }
443:
444: DefaultMutableTreeNode searchString(
445: DefaultMutableTreeNode pTempNode, String name) {
446: Requirement pTempReq = (Requirement) pTempNode.getUserObject();
447: DefaultMutableTreeNode retNode = null;
448: if (pTempReq.getNameFromModel().equals(name)) {
449: return pTempNode;
450: } else {
451: int nbChild = pTempNode.getChildCount();
452: boolean trouve = false;
453: int i = 0;
454: while (i < nbChild && !trouve) {
455: retNode = searchString(
456: (DefaultMutableTreeNode) pTempNode
457: .getChildAt(i), name);
458: if (retNode != null) {
459: trouve = true;
460: }
461: i++;
462: }
463: return retNode;
464: }
465: }
466:
467: public DefaultMutableTreeNode findRequirementFromCurrent(
468: Requirement pReq) {
469: return search(pRequirementTreeObservable.currentNode, pReq);
470: }
471:
472: public DefaultMutableTreeNode findRequirementFromParent(
473: Requirement pReq) {
474: return search(parent, pReq);
475: }
476:
477: public DefaultMutableTreeNode findRequirementFromParent(
478: ReqWrapper pReq) {
479: return searchWrapper(parent, pReq);
480: }
481:
482: public DefaultMutableTreeNode findRequirementFromCurrent(
483: String regex, boolean id) {
484: DefaultMutableTreeNode pReqNode = searchRegx(
485: pRequirementTreeObservable.currentNode, regex, id);
486: if (pReqNode != null) {
487: setSelectionPath(new TreePath(pReqNode.getPath()));
488: } else {
489: //Pas trouve
490: //TODO
491: }
492: return pReqNode;
493: }
494:
495: /*public DefaultMutableTreeNode findRequirementFromParent(String regex) {
496: DefaultMutableTreeNode pReqNode = searchRegx(parent, regex);
497: if (pReqNode != null){
498: setSelectionPath(new TreePath(pReqNode.getPath()));
499: } else {
500: //Pas trouve
501: //TODO
502: }
503: return pReqNode;
504: }*/
505:
506: public DefaultMutableTreeNode findRequirementByNameFromParent(
507: String name) {
508: DefaultMutableTreeNode pReqNode = searchString(parent, name);
509: if (pReqNode != null) {
510: setSelectionPath(new TreePath(pReqNode.getPath()));
511: } else {
512: //Pas trouve
513: //TODO
514: }
515: return pReqNode;
516: }
517:
518: DefaultTreeCellRenderer getRender() {
519: if (renderer == null) {
520: renderer = new myReqRenderer();
521: }
522: return renderer;
523: }
524:
525: void addObserver(Observer o) {
526: try {
527: pRequirementTreeObservable.addObserver(o);
528: } catch (Exception e) {
529:
530: }
531: }
532:
533: class myReqRenderer extends DefaultTreeCellRenderer {
534: //ImageIcon ReqFamIcon = new javax.swing.ImageIcon(getClass().getResource("/salomeTMF_plug/requirements/resources/images/reqFam.png"));
535: //ImageIcon ReqLeafIncon = new javax.swing.ImageIcon(getClass().getResource("/salomeTMF_plug/requirements/resources/images/req.png"));
536: ImageIcon ReqFamIcon = createImageIcon("/salomeTMF_plug/requirements/resources/images/reqFam.png");
537: ImageIcon ReqLeafIncon = createImageIcon("/salomeTMF_plug/requirements/resources/images/req.png");
538:
539: myReqRenderer() {
540: super ();
541: }
542:
543: public Component getTreeCellRendererComponent(JTree tree,
544: Object value, boolean sel, boolean expanded,
545: boolean leaf, int row, boolean hasFocus) {
546:
547: //System.out.println("Get renderer for " + value);
548: super .getTreeCellRendererComponent(tree, value, sel,
549: expanded, leaf, row, hasFocus);
550:
551: if (value instanceof DefaultMutableTreeNode) {
552: Requirement req = (Requirement) ((DefaultMutableTreeNode) value)
553: .getUserObject();
554: if (req.isFamilyReq()) {
555: setIcon(ReqFamIcon);
556: } else {
557: setIcon(ReqLeafIncon);
558: ReqLeaf pReqLeaf = (ReqLeaf) req;
559: if (pReqLeaf.getPriorityFromModel() == ReqFilter.P_HIGHT) {
560: setForeground(Color.red);
561: } else if (pReqLeaf.getPriorityFromModel() == ReqFilter.P_MEDIUM) {
562: setForeground(Color.blue);
563: } else if (pReqLeaf.getPriorityFromModel() == ReqFilter.P_LOW) {
564: setForeground(Color.BLACK);
565: } else {
566: setForeground(Color.gray);
567: }
568:
569: }
570: //setName(req.getNameFromModel());
571: }
572: setEnabled(true);
573: return this ;
574: }
575:
576: }
577:
578: protected static ImageIcon createImageIcon(String path) {
579: java.net.URL imgURL = RequirementTree.class.getResource(path);
580: if (imgURL != null) {
581: return new ImageIcon(imgURL);
582: } else {
583: System.err.println("Couldn't find file: " + path);
584: return null;
585: }
586: }
587:
588: }
|