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.BorderLayout;
027: import java.awt.Dimension;
028: import java.awt.GridLayout;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033: import java.util.Observable;
034: import java.util.Observer;
035: import java.util.Vector;
036:
037: import javax.swing.BorderFactory;
038: import javax.swing.JButton;
039: import javax.swing.JOptionPane;
040: import javax.swing.JPanel;
041: import javax.swing.JScrollPane;
042: import javax.swing.JTabbedPane;
043: import javax.swing.JTable;
044: import javax.swing.JTree;
045: import javax.swing.ListSelectionModel;
046: import javax.swing.event.ChangeEvent;
047: import javax.swing.event.ChangeListener;
048: import javax.swing.event.ListSelectionEvent;
049: import javax.swing.event.ListSelectionListener;
050: import javax.swing.event.TreeSelectionEvent;
051: import javax.swing.event.TreeSelectionListener;
052: import javax.swing.tree.DefaultMutableTreeNode;
053: import javax.swing.tree.TreePath;
054:
055: import org.objectweb.salome_tmf.data.AutomaticTest;
056: import org.objectweb.salome_tmf.data.ManualTest;
057: import org.objectweb.salome_tmf.data.Test;
058: import org.objectweb.salome_tmf.ihm.languages.Language;
059: import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
060: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
061: import org.objectweb.salome_tmf.ihm.models.DynamicTree;
062: import org.objectweb.salome_tmf.ihm.tools.Tools;
063:
064: import salomeTMF_plug.requirements.ReqPlugin;
065: import salomeTMF_plug.requirements.data.ReqLeaf;
066: import salomeTMF_plug.requirements.data.Requirement;
067: import salomeTMF_plug.requirements.sqlWrapper.ReqWrapper;
068: import salomeTMF_plug.requirements.sqlWrapper.SQLWrapper;
069:
070: public class RequirementActionPanel extends JPanel implements
071: ActionListener, ListSelectionListener, Observer {
072:
073: JButton viewReqButton;
074:
075: JButton delReqButton;
076:
077: JButton addReqButton;
078:
079: JTable reqTable;
080:
081: JPanel buttonsPanel;
082: JScrollPane tablePane;
083: JTabbedPane salomeParent;
084: JTree salomeDynamicTree;
085:
086: RequirementTree pReqTree;
087: RequirementCampPanel pRequirementCampPanel;
088: //JTree reqTree;
089:
090: Test pCurrentTest;
091: Vector reqCovered;
092: Vector filtredreqCovered;
093:
094: int type; /* 0 for Manual Test 1 for AutomatedTest */
095: FiltrePanel pFiltrePanel;
096:
097: public RequirementActionPanel(int _type, RequirementTree _pReqTree,
098: RequirementCampPanel _RequirementCampPanel) {
099: type = _type;
100: pReqTree = _pReqTree;
101: pRequirementCampPanel = _RequirementCampPanel;
102: initComponent();
103: }
104:
105: void initComponent() {
106: reqCovered = new Vector();
107: filtredreqCovered = new Vector();
108: viewReqButton = new JButton(Language.getInstance().getText(
109: "Visualiser"));
110: addReqButton = new JButton(Language.getInstance().getText(
111: "Utiliser"));
112: delReqButton = new JButton(Language.getInstance().getText(
113: "Supprimer"));
114:
115: viewReqButton.addActionListener(this );
116: addReqButton.addActionListener(this );
117: delReqButton.addActionListener(this );
118:
119: viewReqButton.setEnabled(false);
120: addReqButton.setEnabled(true);
121: delReqButton.setEnabled(false);
122:
123: buttonsPanel = new JPanel(new GridLayout(1, 3));
124: buttonsPanel.add(addReqButton);
125: buttonsPanel.add(delReqButton);
126: buttonsPanel.add(viewReqButton);
127:
128: SimpleTableModel model = new SimpleTableModel();
129:
130: reqTable = new JTable(model);
131: try {
132: //reqTable.setDefaultRenderer(Class.forName( "java.lang.Object" ), new PriorityTableCellRenderer(reqCovered));
133: reqTable.setDefaultRenderer(Class
134: .forName("java.lang.Object"),
135: new PriorityTableCellRenderer(filtredreqCovered));
136: } catch (Exception e) {
137: e.printStackTrace();
138: }
139: model.addColumn("id");
140: model.addColumn(Language.getInstance().getText("Nom"));
141: model.addColumn(Language.getInstance().getText("Categorie"));
142: model.addColumn(Language.getInstance().getText("Complexite"));
143: model.addColumn(Language.getInstance().getText("Etat"));
144:
145: //model.addColumn(Language.getInstance().getText("Description"));
146:
147: reqTable.setPreferredScrollableViewportSize(new Dimension(600,
148: 200));
149: reqTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
150: ListSelectionModel rowSM = reqTable.getSelectionModel();
151: rowSM.addListSelectionListener(this );
152:
153: tablePane = new JScrollPane(reqTable);
154: tablePane.setBorder(BorderFactory.createRaisedBevelBorder());
155:
156: pFiltrePanel = new FiltrePanel(null, this );
157:
158: setLayout(new BorderLayout());
159: add(buttonsPanel, BorderLayout.NORTH);
160: add(tablePane, BorderLayout.CENTER);
161: add(pFiltrePanel, BorderLayout.SOUTH);
162: //addFocusListener(new requirementTestFocusListener());
163: }
164:
165: public void setParent(JTabbedPane _salomeParent) {
166:
167: if (salomeParent != null) {
168: return;
169: } else {
170: salomeParent = _salomeParent;
171: salomeParent.addChangeListener(new ChangeListener() {
172: public void stateChanged(ChangeEvent e) {
173: if (((JTabbedPane) e.getSource())
174: .getSelectedComponent().equals(
175: RequirementActionPanel.this )) {
176: System.out.println("Update Requiment for test");
177: InitData(DataModel.getCurrentTest());
178: }
179: }
180: });
181: }
182: }
183:
184: public void setTestTree(DynamicTree pDynamicTree) {
185: if (salomeDynamicTree == null) {
186: salomeDynamicTree = pDynamicTree.getTree();
187: salomeDynamicTree
188: .addTreeSelectionListener(new TreeSelectionListener() {
189: public void valueChanged(TreeSelectionEvent e) {
190: DefaultMutableTreeNode node = (DefaultMutableTreeNode) salomeDynamicTree
191: .getLastSelectedPathComponent();
192:
193: if (node == null)
194: return;
195:
196: Object nodeInfo = node.getUserObject();
197: /* React to the node selection. */
198: if ((nodeInfo instanceof ManualTest)
199: && type == 0) {
200: if (salomeParent
201: .getSelectedComponent()
202: .equals(
203: RequirementActionPanel.this )) {
204: InitData((Test) nodeInfo);
205: }
206: } else if ((nodeInfo instanceof AutomaticTest)
207: && type == 1) {
208: if (salomeParent
209: .getSelectedComponent()
210: .equals(
211: RequirementActionPanel.this )) {
212: InitData((Test) nodeInfo);
213: }
214: }
215: }
216: });
217: }
218: }
219:
220: public void InitData(Test pTest) {
221:
222: if (pTest == pCurrentTest || pTest == null) {
223: return;
224: }
225: pCurrentTest = pTest;
226: Vector reqCoveredWrapper;
227: reqCovered.clear();
228:
229: try {
230: reqCoveredWrapper = Requirement
231: .getReqWrapperCoveredByTest(pTest.getIdBdd());
232: int size = reqCoveredWrapper.size();
233: for (int i = 0; i < size; i++) {
234: ReqWrapper pReqWrapper = (ReqWrapper) reqCoveredWrapper
235: .elementAt(i);
236: DefaultMutableTreeNode node = pReqTree
237: .findRequirementFromParent(pReqWrapper);
238: Requirement pReq = null;
239: if (node != null) {
240: pReq = (Requirement) node.getUserObject();
241: reqCovered.add(pReq);
242: } else {
243: pReqTree.reload();
244: node = pReqTree
245: .findRequirementFromParent(pReqWrapper);
246: if (node != null) {
247: pReq = (Requirement) node.getUserObject();
248: reqCovered.add(pReq);
249: } else {
250: //Hum Data corruption !!!
251: Tools
252: .ihmExceptionView(new Exception(
253: "Hum ??, it seem that data integrity are corrupted"));
254:
255: }
256: }
257: }
258: // Init table data
259: //updateReqUse(reqCovered, false);
260: updateTable(reqCovered);
261: } catch (Exception e) {
262: Tools.ihmExceptionView(e);
263: }
264: }
265:
266: void updateReqUse(Vector reqCovered, boolean onBDD) {
267: int transNumber = -1;
268: try {
269: int reqSize = reqCovered.size();
270: if (onBDD) {
271: transNumber = SQLWrapper.beginTransaction();
272: Requirement.deleteAllCoverForTest(pCurrentTest
273: .getIdBdd());
274: }
275: //IHM
276: //((SimpleTableModel)reqTable.getModel()).clearTable();
277:
278: for (int i = 0; i < reqSize; i++) {
279: Requirement pReq = (Requirement) reqCovered
280: .elementAt(i);
281:
282: if (onBDD) {
283: // Bdd
284: pReq.addTestCoverInDB(pCurrentTest.getIdBdd(),
285: false);
286: }
287:
288: // IHM
289: /*Vector data = new Vector();
290: data.add(pReq.getNameFromModel());
291: if (pReq.getDescriptionFromModel().length()> 60){
292: data.add(pReq.getDescriptionFromModel().substring(0, 59)+"...");
293: } else {
294: data.add(pReq.getDescriptionFromModel());
295: }
296: ((SimpleTableModel)reqTable.getModel()).insertRow(i,data);
297: */
298:
299: }
300: if (onBDD) {
301: SQLWrapper.commitTrans(transNumber);
302: }
303: // ihm
304: updateTable(reqCovered);
305: } catch (Exception e) {
306: try {
307: if (onBDD) {
308: SQLWrapper.rollBackTrans(transNumber);
309: }
310: } catch (Exception e2) {
311: }
312: }
313:
314: }
315:
316: void updateTable(Vector reqCovered) {
317: filtredreqCovered.clear();
318: int filtre = pFiltrePanel.getFiltre();
319: int reqSize = reqCovered.size();
320: ((SimpleTableModel) reqTable.getModel()).clearTable();
321: int compteur = 0;
322: for (int i = 0; i < reqSize; i++) {
323: Requirement pReq = (Requirement) reqCovered.elementAt(i);
324:
325: if (pReq instanceof ReqLeaf) {
326: if (!filtredreqCovered.contains(pReq)) {
327: int reqP = ((ReqLeaf) pReq).getPriorityFromModel();
328: if ((reqP & filtre) > 0) {
329: filtredreqCovered.add(pReq);
330: Vector data = new Vector();
331: data.add("" + pReq.getIdBdd());
332: data.add(pReq.getLongName());
333: /*if (pReq.getDescriptionFromModel().length()> 60){
334: data.add(pReq.getDescriptionFromModel().substring(0, 59)+"...");
335: } else {
336: data.add(pReq.getDescriptionFromModel());
337: }*/
338: data.add(ReqWrapper
339: .getCatString(((ReqLeaf) pReq)
340: .getCatFromModel()));
341: data.add(ReqWrapper
342: .getComplexString(((ReqLeaf) pReq)
343: .getComplexeFromModel()));
344: data.add(ReqWrapper
345: .getStateString(((ReqLeaf) pReq)
346: .getStateFromModel()));
347:
348: ((SimpleTableModel) reqTable.getModel())
349: .insertRow(compteur, data);
350: compteur++;
351: }
352: }
353: }
354: }
355: }
356:
357: public void actionPerformed(ActionEvent e) {
358: if (e.getSource().equals(addReqButton)) {
359: if (addReqPerformed(e)) {
360: pReqTree.refreshCurrent();
361: if (pRequirementCampPanel != null) {
362: pRequirementCampPanel.refreshCurrentCover(false);
363: }
364: }
365: } else if (e.getSource().equals(delReqButton)) {
366: if (deleteConfirme(Language.getInstance().getText(
367: "Couverture"))) {
368: delReqPerformed(e);
369: pReqTree.refreshCurrent();
370: if (pRequirementCampPanel != null) {
371: pRequirementCampPanel.refreshCurrentCover(false);
372: }
373: }
374: } else if (e.getSource().equals(viewReqButton)) {
375: viewReqPerformed(e);
376: }
377: }
378:
379: boolean deleteConfirme(String quoi) {
380: Object[] options = {
381: org.objectweb.salome_tmf.ihm.languages.Language
382: .getInstance().getText("Oui"),
383: org.objectweb.salome_tmf.ihm.languages.Language
384: .getInstance().getText("Non") };
385: int choice = -1;
386:
387: choice = SalomeTMFContext.getInstance().askQuestion(
388: org.objectweb.salome_tmf.ihm.languages.Language
389: .getInstance().getText(
390: "confimation_suppression2")
391: + " " + quoi + " ?",
392: org.objectweb.salome_tmf.ihm.languages.Language
393: .getInstance().getText("Attention_!"),
394: JOptionPane.WARNING_MESSAGE, options);
395:
396: if (choice == JOptionPane.YES_OPTION) {
397: return true;
398: } else {
399: return false;
400: }
401: }
402:
403: // Fonction qui d?clenche !
404: boolean addReqPerformed(ActionEvent e) {
405:
406: SelectRequirement pSelectRequirement = new SelectRequirement(
407: pReqTree.getCopy(true), Language.getInstance().getText(
408: "Selection"), reqCovered, true, null);
409: Hashtable selected = pSelectRequirement.getSelection();
410: if (selected != null) {
411: reqCovered.clear();
412: Enumeration enumE = selected.elements();
413: while (enumE.hasMoreElements()) {
414: reqCovered.add(enumE.nextElement());
415: }
416: updateReqUse(reqCovered, true);
417: return true;
418: }
419: return false;
420: }
421:
422: void delReqPerformed(ActionEvent e) {
423: int selectedRow = reqTable.getSelectedRow();
424: if (selectedRow > -1) {
425: //if (reqCovered != null) {
426: if (filtredreqCovered != null) {
427: //Requirement pReq = (Requirement)reqCovered.elementAt(selectedRow);
428: Requirement pReq = (Requirement) filtredreqCovered
429: .elementAt(selectedRow);
430: try {
431: // Bdd
432: pReq.deleteCoverForTest(pCurrentTest.getIdBdd());
433:
434: //IHM
435: reqCovered.remove(selectedRow);
436: //updateReqUse(reqCovered, false);
437: updateTable(reqCovered);
438: } catch (Exception ex) {
439:
440: }
441: }
442: }
443: }
444:
445: void viewReqPerformed(ActionEvent e) {
446: int selectedRow = reqTable.getSelectedRow();
447:
448: if (selectedRow > -1) {
449: //if (reqCovered != null) {
450: if (filtredreqCovered != null) {
451: //Requirement pReq = (Requirement)reqCovered.elementAt(selectedRow);
452: Requirement pReq = (Requirement) filtredreqCovered
453: .elementAt(selectedRow);
454: DefaultMutableTreeNode node = pReqTree
455: .findRequirementFromParent(pReq);
456: if (node != null) {
457: //pReqTree.refreshNode(node);
458: ReqPlugin.selectReqTab();
459: pReqTree.setSelectionPath(new TreePath(node
460: .getPath()));
461:
462: }
463: }
464: }
465: }
466:
467: public void valueChanged(ListSelectionEvent e) {
468: //Ignore extra messages.
469: if (e.getValueIsAdjusting()) {
470: return;
471: }
472:
473: ListSelectionModel lsm = (ListSelectionModel) e.getSource();
474: if (lsm.isSelectionEmpty()) {
475: //no rows are selected
476: delReqButton.setEnabled(false);
477: viewReqButton.setEnabled(false);
478: } else {
479: //int selectedRow = lsm.getMinSelectionIndex();
480: delReqButton.setEnabled(true);
481: viewReqButton.setEnabled(true);
482: //selectedRow is selected
483: }
484: }
485:
486: /********************************* Observer *************************/
487:
488: public void update(Observable o, Object arg) {
489: if (arg instanceof ReqEvent) {
490: ReqEvent event = (ReqEvent) arg;
491: if (event.code == ReqEvent.FILTRE_REQ_CHANGE) {
492: //refreshCurrent(false);
493: updateTable(reqCovered);
494: }
495: }
496: }
497: }
|