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 org.objectweb.salome_tmf.ihm.models;
025:
026: import java.awt.Frame;
027: import java.util.Hashtable;
028:
029: import javax.swing.JOptionPane;
030: import javax.swing.JTree;
031: import javax.swing.tree.DefaultMutableTreeNode;
032: import javax.swing.tree.DefaultTreeModel;
033: import javax.swing.tree.TreePath;
034:
035: import org.objectweb.salome_tmf.api.ApiConstants;
036: import org.objectweb.salome_tmf.api.Permission;
037: import org.objectweb.salome_tmf.data.Campaign;
038: import org.objectweb.salome_tmf.data.DataConstants;
039: import org.objectweb.salome_tmf.data.Family;
040: import org.objectweb.salome_tmf.data.ManualTest;
041: import org.objectweb.salome_tmf.data.SimpleData;
042: import org.objectweb.salome_tmf.data.Test;
043: import org.objectweb.salome_tmf.data.TestList;
044: import org.objectweb.salome_tmf.ihm.IHMConstants;
045: import org.objectweb.salome_tmf.ihm.filtre.ISimpleDataFilter;
046: import org.objectweb.salome_tmf.ihm.languages.Language;
047: import org.objectweb.salome_tmf.ihm.main.SalomeTMFPanels;
048: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
049: import org.objectweb.salome_tmf.ihm.tools.Tools;
050:
051: /**
052: * Classe qui d?finit le mod?le de donn?es de l'arbre.
053: * @author teaml039
054: * @version 0.1
055: */
056: public class TestTreeModel extends DefaultTreeModel implements
057: ApiConstants, IHMConstants {
058:
059: /******************************************************************************/
060: /** CONSTRUCTEUR ***/
061: /******************************************************************************/
062: protected ISimpleDataFilter m_filter;
063: protected JTree testTree;
064:
065: /**
066: * Constructeur du mod?le de donn?es de l'arbre
067: * @param root la racine de l'arbre
068: */
069: public TestTreeModel(DefaultMutableTreeNode root, JTree testTree,
070: ISimpleDataFilter filter) {
071: super (root);
072: m_filter = filter;
073: this .testTree = testTree;
074: } // Fin du constructeur TreeModel/1
075:
076: /******************************************************************************/
077: /** METHODES PUBLIQUES ***/
078: /******************************************************************************/
079:
080: /**
081: * On surcharge la m?thode de DefaultTreeModel afin de prendre en compte
082: * la nature sp?cifique des noeuds de l'arbre.
083: */
084: public void valueForPathChanged(TreePath path, Object newValue) {
085:
086: DefaultMutableTreeNode aNode = (DefaultMutableTreeNode) path
087: .getLastPathComponent();
088: if (Permission.canUpdateTest()
089: && ((DefaultMutableTreeNode) this .getRoot())
090: .getUserObject().equals(TEST_ROOT_NAME)) {
091:
092: if (aNode.getUserObject() instanceof Test) {
093:
094: if (!DataModel.getCurrentProject().containsTestInModel(
095: ((TestList) ((DefaultMutableTreeNode) aNode
096: .getParent()).getUserObject()),
097: (String) newValue)
098: && !Test
099: .isInBase(
100: (TestList) ((DefaultMutableTreeNode) aNode
101: .getParent())
102: .getUserObject(),
103: (String) newValue)) {
104: //String oldName = ((Test)aNode.getUserObject()).getName();
105: try {
106: ((Test) aNode.getUserObject())
107: .updateInDBAndModel(
108: (String) newValue,
109: ((Test) aNode.getUserObject())
110: .getDescriptionFromModel());
111:
112: if (aNode.getUserObject() instanceof ManualTest) {
113: //SalomeTMFPanels.getManualTestNameLabel().setText(Language.getInstance().getText("Nom_du_test_:_") + (String)newValue);
114: SalomeTMFPanels.setTestPanelTestInfo(
115: DataConstants.MANUAL_TEST,
116: (String) newValue, null, null);
117: } else {
118: //SalomeTMFPanels.getAutomaticTestNameLabel().setText(Language.getInstance().getText("Nom_du_test_:_") + (String)newValue);
119: SalomeTMFPanels.setTestPanelTestInfo(
120: DataConstants.AUTOMATIC_TEST,
121: (String) newValue, null, null);
122: }
123: //((Test)aNode.getUserObject()).setName((String)newValue);
124: nodeChanged(aNode);
125: } catch (Exception exception) {
126: Tools.ihmExceptionView(exception);
127: }
128: } else if (DataModel
129: .getCurrentProject()
130: .getTestFromModel(
131: ((TestList) ((DefaultMutableTreeNode) aNode
132: .getParent()).getUserObject())
133: .getFamilyFromModel()
134: .getNameFromModel(),
135: ((TestList) ((DefaultMutableTreeNode) aNode
136: .getParent()).getUserObject())
137: .getNameFromModel(),
138: (String) newValue) == null
139: || !DataModel
140: .getCurrentProject()
141: .getTestFromModel(
142: ((TestList) ((DefaultMutableTreeNode) aNode
143: .getParent())
144: .getUserObject())
145: .getFamilyFromModel()
146: .getNameFromModel(),
147: ((TestList) ((DefaultMutableTreeNode) aNode
148: .getParent())
149: .getUserObject())
150: .getNameFromModel(),
151: (String) newValue).equals(
152: aNode.getUserObject())) {
153: JOptionPane
154: .showMessageDialog(
155: new Frame(),
156: Language
157: .getInstance()
158: .getText(
159: "Ce_nom_de_test_existe_déjà_dans_cette_suite_!"),
160: Language.getInstance().getText(
161: "Erreur_!"),
162: JOptionPane.ERROR_MESSAGE);
163: }
164:
165: } else if (aNode.getUserObject() instanceof TestList
166: && !((TestList) aNode.getUserObject())
167: .equals(DEFAULT_TESTLIST_NAME)) {
168:
169: if (!DataModel.getCurrentProject()
170: .containsTestListInModel(
171: ((TestList) aNode.getUserObject())
172: .getFamilyFromModel(),
173: (String) newValue)
174: && !TestList.isInBase(((TestList) aNode
175: .getUserObject()).getFamilyFromModel(),
176: (String) newValue)) {
177: //String oldName = ((TestList)aNode.getUserObject()).getName();
178: try {
179: ((TestList) aNode.getUserObject())
180: .updateInDBAndModel(
181: (String) newValue,
182: ((TestList) aNode
183: .getUserObject())
184: .getDescriptionFromModel());
185: //((TestList)aNode.getUserObject()).setName((String)newValue);
186: nodeChanged(aNode);
187: } catch (Exception exception) {
188: Tools.ihmExceptionView(exception);
189: }
190: } else if (DataModel.getCurrentProject()
191: .getTestListFromModel(
192: ((TestList) aNode.getUserObject())
193: .getFamilyFromModel()
194: .getNameFromModel(),
195: (String) newValue) == null
196: || !DataModel.getCurrentProject()
197: .getTestListFromModel(
198: ((TestList) aNode
199: .getUserObject())
200: .getFamilyFromModel()
201: .getNameFromModel(),
202: (String) newValue).equals(
203: aNode.getUserObject())) {
204: JOptionPane
205: .showMessageDialog(
206: new Frame(),
207: Language
208: .getInstance()
209: .getText(
210: "Le_nom_de_la_suite_existe_déjà_dans_cette_famille_!"),
211: Language.getInstance().getText(
212: "Erreur_!"),
213: JOptionPane.ERROR_MESSAGE);
214: }
215:
216: } else if (aNode.getUserObject() instanceof Family
217: && !((Family) aNode.getUserObject())
218: .getNameFromModel().equals(
219: DEFAULT_FAMILY_NAME)) {
220: if (!DataModel.getCurrentProject()
221: .containsFamilyInModel((String) newValue)
222: && !Family
223: .isInBase(
224: DataModel.getCurrentProject(),
225: (String) newValue)) {
226: //String oldName = ((Family)aNode.getUserObject()).getName();
227: try {
228: ((Family) aNode.getUserObject())
229: .updateInDBAndModel(
230: (String) newValue,
231: ((Family) aNode.getUserObject())
232: .getDescriptionFromModel());
233: //((Family)aNode.getUserObject()).setName((String)newValue);
234: nodeChanged(aNode);
235: } catch (Exception exception) {
236: Tools.ihmExceptionView(exception);
237: }
238: } else if (DataModel.getCurrentProject()
239: .getFamilyFromModel((String) newValue) == null
240: || !DataModel.getCurrentProject()
241: .getFamilyFromModel((String) newValue)
242: .equals(aNode.getUserObject())) {
243: JOptionPane.showMessageDialog(new Frame(), Language
244: .getInstance().getText(
245: "Cette_famille_existe_déjà_!"),
246: Language.getInstance().getText("Erreur_!"),
247: JOptionPane.ERROR_MESSAGE);
248: }
249:
250: }
251: } else if (Permission.canUpdateCamp()
252: && ((DefaultMutableTreeNode) this .getRoot())
253: .getUserObject().equals(CAMPAIGN_ROOT_NAME)) {
254: if (aNode.getUserObject() instanceof Campaign) {
255: if (!DataModel.getCurrentProject()
256: .containsCampaignInModel((String) newValue)
257: && !Campaign
258: .isInBase(
259: DataModel.getCurrentProject(),
260: (String) newValue)) {
261: //SalomeTMFPanels.getCampaignNameLabel().setText(Language.getInstance().getText("Nom_de_la_campagne_:_") +(String)newValue);
262: SalomeTMFPanels.setCampPanelInfo((String) newValue,
263: null, null);
264: //String oldName = ((Campaign)aNode.getUserObject()).getName();
265: try {
266: Campaign camp = (Campaign) aNode
267: .getUserObject();
268:
269: //BdD
270: camp.updateInDBAndModel((String) newValue, camp
271: .getDescriptionFromModel());
272:
273: //IHM
274: nodeChanged(aNode);
275:
276: } catch (Exception exception) {
277: Tools.ihmExceptionView(exception);
278: }
279: } else if (DataModel.getCurrentProject()
280: .getCampaignFromModel((String) newValue) == null
281: || !DataModel
282: .getCurrentProject()
283: .getCampaignFromModel((String) newValue)
284: .equals(aNode.getUserObject())) {
285: //((Campaign)aNode.getUserObject()).setName(oldName);
286: JOptionPane
287: .showMessageDialog(
288: new Frame(),
289: Language
290: .getInstance()
291: .getText(
292: "Le_nom_de_la_campagne_existe_déjà_!"),
293: Language.getInstance().getText(
294: "Erreur_!"),
295: JOptionPane.ERROR_MESSAGE);
296: }
297: }
298: }
299: } // Fin de la m?thode valueForPathChanged
300:
301: /*************** Methode pour le filtrage des éléments *****************************/
302: public void setFilter(ISimpleDataFilter filter) {
303: m_filter = filter;
304: }
305:
306: public void setFiltered(boolean pass) {
307: TreePath pTreePath = testTree.getSelectionPath();
308: Hashtable<TreePath, Boolean> state = getExpansionState(testTree);
309: //System.out.println("TreePath : " + pTreePath);
310: m_filter.setActived(pass);
311: Object[] path = { root };
312: int[] childIndices = new int[root.getChildCount()];
313: Object[] children = new Object[root.getChildCount()];
314: for (int i = 0; i < root.getChildCount(); i++) {
315: childIndices[i] = i;
316: children[i] = root.getChildAt(i);
317: }
318: fireTreeStructureChanged(this , path, childIndices, children);
319: if (pTreePath != null) {
320:
321: DefaultMutableTreeNode pNode = (DefaultMutableTreeNode) pTreePath
322: .getLastPathComponent();
323:
324: if (pNode.getUserObject() instanceof SimpleData) {
325: SimpleData data = (SimpleData) pNode.getUserObject();
326:
327: testTree.setSelectionPath(pTreePath);
328: if (!m_filter.isFiltred(data)) {
329: boolean trouve = false;
330: while (!trouve && pTreePath != null) {
331: pTreePath = pTreePath.getParentPath();
332: if (pTreePath != null) {
333: pNode = (DefaultMutableTreeNode) pTreePath
334: .getLastPathComponent();
335: Object obj = pNode.getUserObject();
336: if (obj instanceof SimpleData) {
337: data = (SimpleData) obj;
338: if (m_filter.isFiltred(data)) {
339: trouve = true;
340: }
341: }
342: }
343: }
344: testTree.setSelectionPath(pTreePath);
345: }
346: }
347: }
348: restoreExpanstionState(testTree, state);
349: }
350:
351: public int getChildCount(Object parent) {
352: if (m_filter == null) {
353: return super .getChildCount(parent);
354: }
355: int realCount = super .getChildCount(parent), filterCount = 0;
356: ;
357: for (int i = 0; i < realCount; i++) {
358: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) super
359: .getChild(parent, i);
360: if (m_filter.pass(dmtn))
361: filterCount++;
362: }
363: return filterCount;
364: }
365:
366: public Object getChild(Object parent, int index) {
367: if (m_filter == null) {
368: return super .getChild(parent, index);
369: }
370: int cnt = -1;
371: for (int i = 0; i < super .getChildCount(parent); i++) {
372: Object child = super .getChild(parent, i);
373: if (m_filter.pass(child))
374: cnt++;
375: if (cnt == index)
376: return child;
377: }
378: return null;
379: }
380:
381: /********************* UTIL **************************/
382:
383: // is path1 descendant of path2
384: public boolean isDescendant(TreePath path1, TreePath path2) {
385: int count1 = path1.getPathCount();
386: int count2 = path2.getPathCount();
387: if (count1 <= count2)
388: return false;
389: while (count1 != count2) {
390: path1 = path1.getParentPath();
391: count1--;
392: }
393: return path1.equals(path2);
394: }
395:
396: public Hashtable<TreePath, Boolean> getExpansionState(JTree tree) {
397: Hashtable<TreePath, Boolean> statePath = new Hashtable<TreePath, Boolean>();
398: int nbRow = testTree.getRowCount();
399: for (int row = 0; row < nbRow; row++) {
400: TreePath rowPath = tree.getPathForRow(row);
401: int rowCount = tree.getRowCount();
402: for (int i = row; i < rowCount; i++) {
403: TreePath path = tree.getPathForRow(i);
404: if (i == row || isDescendant(path, rowPath)) {
405: if (tree.isExpanded(path)) {
406: statePath.put(path, true);
407: }
408: } else
409: break;
410: }
411: }
412: return statePath;
413: }
414:
415: public void restoreExpanstionState(JTree tree,
416: Hashtable<TreePath, Boolean> statePath) {
417: int nbRow = testTree.getRowCount();
418: for (int row = 0; row < nbRow; row++) {
419: //TreePath rowPath = tree.getPathForRow(row);
420: int rowCount = tree.getRowCount();
421: for (int i = row; i < rowCount; i++) {
422: TreePath path = tree.getPathForRow(i);
423: Boolean expanded = statePath.get(path);
424: if (expanded != null) {
425: tree.expandRow(tree.getRowForPath(path));
426: }
427: }
428: }
429: }
430: }// Fin de la classe TreeModel
|