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 Fayçal SOUGRATI, Vincent Pautret, Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.ihm.main;
025:
026: import java.awt.BorderLayout;
027: import java.awt.Cursor;
028: import java.awt.FlowLayout;
029: import java.awt.GridLayout;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.io.File;
033: import java.io.FileNotFoundException;
034: import java.sql.Date;
035: import java.util.Hashtable;
036:
037: import javax.swing.BorderFactory;
038: import javax.swing.BoxLayout;
039: import javax.swing.JButton;
040: import javax.swing.JLabel;
041: import javax.swing.JMenu;
042: import javax.swing.JMenuBar;
043: import javax.swing.JMenuItem;
044: import javax.swing.JOptionPane;
045: import javax.swing.JPanel;
046:
047: import org.java.plugin.Extension;
048: import org.objectweb.salome_tmf.api.Api;
049: import org.objectweb.salome_tmf.api.ApiConstants;
050: import org.objectweb.salome_tmf.api.Permission;
051: import org.objectweb.salome_tmf.api.Util;
052: import org.objectweb.salome_tmf.data.AutomaticTest;
053: import org.objectweb.salome_tmf.data.Script;
054: import org.objectweb.salome_tmf.ihm.languages.Language;
055: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
056: import org.objectweb.salome_tmf.ihm.tools.Tools;
057: import org.objectweb.salome_tmf.plugins.UICompCst;
058: import org.objectweb.salome_tmf.plugins.core.TestDriver;
059:
060: /**
061: * @author teaml039
062: * @version : 0.1
063: */
064: public class AutomaticTestScriptView extends JPanel implements
065: ApiConstants, ActionListener {
066:
067: static JLabel tscName;
068: static JLabel plugArgLabel;
069: static JLabel extensionLabel;
070:
071: static JButton openFile;
072: static JButton delFile;
073:
074: static JMenuBar scriptMenuBar;
075: static JMenu scriptMenu;
076: static JMenuItem modifyScriptItem;
077: static JMenuItem actualItem;
078: static JMenuItem setUpEngineItem;
079: static JMenuItem aboutItem;
080:
081: String extension;
082: String plugArg;
083: static AutomaticTest pTest = null;
084: TestDriver driver = null;
085: File file;
086:
087: /**
088: *
089: *
090: */
091: public AutomaticTestScriptView() {
092: pTest = null;
093: //script = new Script();
094:
095: tscName = new JLabel(Language.getInstance().getText(
096: "Nom_du_script_:"));
097: plugArgLabel = new JLabel(Language.getInstance().getText(
098: "Arguments_du_driver_:"));
099: extensionLabel = new JLabel(Language.getInstance().getText(
100: "extension_:"));
101:
102: openFile = new JButton(Language.getInstance()
103: .getText("Ajouter"));
104: openFile.setToolTipText(Language.getInstance().getText(
105: "Ajouter_un_fichier"));
106: openFile.addActionListener(this );
107:
108: delFile = new JButton(Language.getInstance().getText(
109: "Supprimer"));
110: delFile.setToolTipText(Language.getInstance().getText(
111: "Supprimer_le_fichier_de_script"));
112: delFile.setEnabled(false);
113: delFile.addActionListener(this );
114:
115: scriptMenu = new JMenu(Language.getInstance().getText("Script"));
116: modifyScriptItem = new JMenuItem(Language.getInstance()
117: .getText("Modifier_le_Script"));
118: modifyScriptItem.addActionListener(this );
119: modifyScriptItem.setEnabled(false);
120:
121: actualItem = new JMenuItem(Language.getInstance().getText(
122: "Actualiser_le_Script"));
123: actualItem.addActionListener(this );
124: actualItem.setEnabled(false);
125:
126: setUpEngineItem = new JMenuItem(Language.getInstance().getText(
127: "SetUp_Driver"));
128: setUpEngineItem.addActionListener(this );
129: setUpEngineItem.setEnabled(false);
130:
131: aboutItem = new JMenuItem(Language.getInstance().getText(
132: "A_propos_du_driver"));
133: aboutItem.addActionListener(this );
134: aboutItem.setEnabled(true);
135:
136: scriptMenu.add(modifyScriptItem);
137: scriptMenu.add(actualItem);
138: scriptMenu.add(setUpEngineItem);
139: scriptMenu.add(aboutItem);
140:
141: scriptMenuBar = new JMenuBar();
142: scriptMenuBar.add(scriptMenu);
143:
144: //JPanel allButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));
145: JPanel allButtons = new JPanel(new GridLayout(1, 3));
146: allButtons.add(openFile);
147: allButtons.add(delFile);
148: allButtons.add(scriptMenuBar);
149:
150: allButtons.setBorder(BorderFactory.createRaisedBevelBorder());
151:
152: // Mapping entre composants graphiques et constantes
153: SalomeTMFContext.getInstance().addToUIComponentsMap(
154: UICompCst.AUTOMATED_TEST_SCRIPT_BUTTONS_PANEL,
155: allButtons);
156: // Add this component as static component
157: UICompCst.staticUIComps
158: .add(UICompCst.AUTOMATED_TEST_SCRIPT_BUTTONS_PANEL);
159:
160: JPanel extensionPanel = new JPanel(new FlowLayout(
161: FlowLayout.LEFT));
162: extensionPanel.add(extensionLabel);
163:
164: // Mapping entre composants graphiques et constantes
165: SalomeTMFContext.getInstance().addToUIComponentsMap(
166: UICompCst.AUTOMATED_TEST_SCRIPT_EXTENSION_PANEL,
167: extensionPanel);
168: // Add this component as static component
169: UICompCst.staticUIComps
170: .add(UICompCst.AUTOMATED_TEST_SCRIPT_EXTENSION_PANEL);
171:
172: JPanel classPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
173: classPanel.add(plugArgLabel);
174:
175: // Mapping entre composants graphiques et constantes
176: SalomeTMFContext.getInstance().addToUIComponentsMap(
177: UICompCst.AUTOMATED_TEST_SCRIPT_CLASSNAME_PANEL,
178: classPanel);
179: // Add this component as static component
180: UICompCst.staticUIComps
181: .add(UICompCst.AUTOMATED_TEST_SCRIPT_CLASSNAME_PANEL);
182:
183: JPanel fileNamePanel = new JPanel(new FlowLayout(
184: FlowLayout.LEFT));
185: fileNamePanel.add(tscName);
186:
187: // Mapping entre objets graphiques et constantes
188: SalomeTMFContext.getInstance().addToUIComponentsMap(
189: UICompCst.AUTOMATED_TEST_SCRIPT_FILENAME_PANEL,
190: fileNamePanel);
191: // Add this component as static component
192: UICompCst.staticUIComps
193: .add(UICompCst.AUTOMATED_TEST_SCRIPT_FILENAME_PANEL);
194:
195: JPanel globalPanel = new JPanel();
196: globalPanel.setLayout(new BoxLayout(globalPanel,
197: BoxLayout.Y_AXIS));
198: globalPanel.add(fileNamePanel, BorderLayout.NORTH);
199: globalPanel.add(classPanel);
200: globalPanel.add(extensionPanel);
201:
202: JPanel hh = new JPanel(new BorderLayout());
203: hh.add(globalPanel, BorderLayout.NORTH);
204:
205: this .setLayout(new BorderLayout());
206: this .add(allButtons, BorderLayout.NORTH);
207: this .add(hh, BorderLayout.CENTER);
208:
209: } // Fin du constructeur
210:
211: /**
212: * @return
213: */
214: public static JLabel getTscName() {
215: return tscName;
216: }
217:
218: /**
219: * @param label
220: */
221: public static void setTscName(JLabel label) {
222: tscName = label;
223: }
224:
225: /**
226: * @return
227: */
228: public static JLabel getPlugArgLabel() {
229: return plugArgLabel;
230: }
231:
232: /**
233: * @return
234: */
235: public static JLabel getExtensionLabel() {
236: return extensionLabel;
237: }
238:
239: public static void giveAccessToIhmScriptView() {
240: if (!Permission.canCreateTest()) {
241: openFile.setEnabled(false);
242: }
243: if (!Permission.canDeleteTest()) {
244: delFile.setEnabled(false);
245: }
246: if (!Permission.canUpdateTest()) {
247: modifyScriptItem.setEnabled(false);
248: aboutItem.setEnabled(false);
249: setUpEngineItem.setEnabled(false);
250: }
251: }
252:
253: /**
254: * @return
255: */
256: public static JButton getDelFile() {
257: return delFile;
258: }
259:
260: /* Invoked when test is selected in the treewiew*/
261: public static void setTest(AutomaticTest test) {
262: pTest = test;
263: if (test.getScriptFromModel() != null) {
264: tscName.setText(Language.getInstance().getText(
265: "Nom_du_script_:_")
266: + test.getScriptFromModel().getNameFromModel());
267: if (Permission.canUpdateTest()) {
268: setUpEngineItem.setEnabled(true);
269: modifyScriptItem.setEnabled(true);
270: }
271:
272: if (Permission.canDeleteTest()) {
273: delFile.setEnabled(true);
274:
275: setUpEngineItem.setEnabled(true);
276: modifyScriptItem.setEnabled(true);
277: }
278: if (test.getScriptFromModel().getScriptExtensionFromModel() == null) {
279: extensionLabel.setText(Language.getInstance().getText(
280: "Extension_:_"));
281: } else {
282: extensionLabel.setText(Tools.createHtmlString(Language
283: .getInstance().getText("Extension_:_")
284: + test.getScriptFromModel()
285: .getScriptExtensionFromModel(), 100));
286: }
287: if (test.getScriptFromModel().getPlugArgFromModel() != null) {
288: plugArgLabel.setText(Language.getInstance().getText(
289: "Arguments_du_driver_:_")
290: + test.getScriptFromModel()
291: .getPlugArgFromModel());
292: } else {
293: plugArgLabel.setText(Language.getInstance().getText(
294: "Arguments_du_driver_:_"));
295: }
296: } else {
297: tscName.setText(Language.getInstance().getText(
298: "Nom_du_script_:_"));
299: extensionLabel.setText(Language.getInstance().getText(
300: "Extension_:_"));
301: plugArgLabel.setText(Language.getInstance().getText(
302: "Arguments_du_driver_:_"));
303: delFile.setEnabled(false);
304:
305: modifyScriptItem.setEnabled(false);
306: setUpEngineItem.setEnabled(false);
307: }
308: }
309:
310: public void actionPerformed(ActionEvent e) {
311: //pTest = (AutomaticTest) DataModel.getCurrentTest();
312: Object source = e.getSource();
313: if (pTest == null) {
314: JOptionPane
315: .showMessageDialog(
316: AutomaticTestScriptView.this ,
317: Language
318: .getInstance()
319: .getText(
320: "Impossible_de_trouver_le_test_correspond_dans_le_plan_de_test"),
321: Language.getInstance().getText("Erreur_!"),
322: JOptionPane.ERROR_MESSAGE);
323: return;
324: }
325: Util
326: .log("Activate Extention with : "
327: + (Extension) SalomeTMFContext.getInstance().associatedExtension
328: .get(pTest.getExtensionFromModel()));
329: Util.log("Activate Extention with : "
330: + SalomeTMFContext.getInstance().urlSalome);
331: Util.log("Activate Extention with : "
332: + SalomeTMFContext.getInstance().jpf);
333:
334: driver = pTest.ActivateExtention((Extension) SalomeTMFContext
335: .getInstance().associatedExtension.get(pTest
336: .getExtensionFromModel()), SalomeTMFContext
337: .getInstance().urlSalome, SalomeTMFContext
338: .getInstance().jpf);
339: if (driver == null
340: && (!(source.equals(delFile)) || !(source
341: .equals(openFile)))) {
342: JOptionPane
343: .showMessageDialog(
344: AutomaticTestScriptView.this ,
345: Language
346: .getInstance()
347: .getText(
348: "Impossible_d'activer_le_plugin_d'exécution_de_tests_")
349: + pTest.getExtensionFromModel(),
350: Language.getInstance().getText("Erreur_!"),
351: JOptionPane.ERROR_MESSAGE);
352: return;
353: }
354:
355: if (source.equals(openFile)) {
356: onpenFilePerformed(e);
357: } else if (source.equals(delFile)) {
358: delFilePerformed(e);
359: } else if (source.equals(modifyScriptItem)) {
360: modifyPerformed(e);
361: } else if (source.equals(actualItem)) {
362: commitPerformed(e);
363: } else if (source.equals(setUpEngineItem)) {
364: modifyArgPerformed(e);
365: } else if (source.equals(aboutItem)) {
366: plugInfoPerformed(e);
367: }
368:
369: }
370:
371: private void onpenFilePerformed(ActionEvent e) {
372: File file = driver.choiceTest(pTest);
373: Script script = null;
374: boolean update = false;
375: if (pTest.getScriptFromModel() != null) {
376: script = pTest.getScriptFromModel();
377: update = true;
378: } else {
379: script = new Script(file.getName(), "");
380: }
381: if (file != null) {
382:
383: tscName.setText(Language.getInstance().getText(
384: "Nom_du_script_:_")
385: + file.getName());
386: script.setLocalisation(file.getAbsolutePath());
387: script.setTypeInModel(TEST_SCRIPT);
388: script.setScriptExtensionInModel(pTest
389: .getExtensionFromModel());
390: //String[] fileNamePart = file.getName().split("[.]");
391: if (!update)
392: plugArg = driver.getDefaultTestDiverAgument();
393:
394: script.updatePlugArgInModel(plugArg);
395:
396: try {
397: if (!file.exists()) {
398: file.createNewFile();
399: }
400: if (update) {
401: Util
402: .log("[AutomaticTestView:onpenFilePerformed] : is update");
403: script.updateInDB(file);
404: /*script.updateNameInDB(file.getName());
405: script.updateContentInDB(file.getAbsolutePath());
406: script.updateDateInDB(new Date(file.lastModified()));
407: script.updateLengthInDB(file.length());
408: */
409: } else {
410: ((AutomaticTest) DataModel.getCurrentTest())
411: .addScriptInDBAndModel(script, file);
412: }
413: //((AutomaticTest)DataModel.getCurrentTest()).setScript(script);
414: plugArgLabel.setText(Language.getInstance().getText(
415: "Arguments_du_driver_:_")
416: + plugArg);
417:
418: if (plugArg == null || plugArg.equals("")) {
419: setUpEngineItem.setEnabled(false);
420: } else {
421: setUpEngineItem.setEnabled(true);
422: }
423: delFile.setEnabled(true);
424: modifyScriptItem.setEnabled(true);
425: } catch (Exception exception) {
426: Tools.ihmExceptionView(exception);
427: }
428: }
429:
430: }
431:
432: private void delFilePerformed(ActionEvent e) {
433: driver.onDeleteTestScript(pTest);
434: Script script = pTest.getScriptFromModel();
435: if (script == null)
436: return;
437: int choice = -1;
438: Object[] options = { Language.getInstance().getText("Oui"),
439: Language.getInstance().getText("Non") };
440: choice = SalomeTMFContext.getInstance().askQuestion(
441: Language.getInstance().getText(
442: "Supprimer_le_fichier_de_script")
443: + " : " + script.getNameFromModel() + " ?",
444: Language.getInstance().getText("Attention_!"),
445: JOptionPane.WARNING_MESSAGE, options);
446: if (choice != JOptionPane.YES_OPTION) {
447: return;
448: }
449:
450: try {
451: // BdD
452: ((AutomaticTest) pTest).deleteScriptInDBAndModel();
453: //((AutomaticTest)DataModel.getCurrentTest()).deleteScriptFromDB(script.getName());
454:
455: // IHM
456: //pTest.setScript(null);
457: tscName.setText(Language.getInstance().getText(
458: "Nom_du_script_:_"));
459: plugArgLabel.setText(Language.getInstance().getText(
460: "Arguments_du_driver_:"));
461: extensionLabel.setText(Language.getInstance().getText(
462: "extension_:_"));
463: modifyScriptItem.setEnabled(false);
464: actualItem.setEnabled(false);
465: setUpEngineItem.setEnabled(true);
466: } catch (Exception exception) {
467: Tools.ihmExceptionView(exception);
468: }
469: }
470:
471: private void modifyPerformed(ActionEvent e) {
472: try {
473: Cursor c = new Cursor(Cursor.WAIT_CURSOR);
474: AutomaticTestScriptView.this .setCursor(c);
475: Hashtable param = new Hashtable();
476: String plugSeting = "";
477: param.put("salome_projectName", DataModel
478: .getCurrentProject().getNameFromModel());
479: param.put("salome_ProjectObject", DataModel
480: .getCurrentProject());
481: param.put("salome_debug", new Boolean(true));
482: param.put("salome_CampagneName", "");
483: param.put("salome_ExecName", ""); // Add MM
484: param.put("salome_environmentName", "");
485: param.put("salome_TestName", DataModel.getCurrentTest()
486: .getNameFromModel());
487: param.put("salome_SuiteTestName", DataModel
488: .getCurrentTest().getTestListFromModel()
489: .getNameFromModel());
490: param.put("salome_FamilyName", DataModel.getCurrentTest()
491: .getTestListFromModel().getFamilyFromModel()
492: .getNameFromModel());
493: param.put("testLog", "");
494: param.put("Verdict", "");
495:
496: plugSeting = pTest.getScriptFromModel()
497: .getPlugArgFromModel();
498:
499: if (Api.isConnected()) {
500: //file = ((AutomaticTest)DataModel.getCurrentTest()).getTestScriptFromDB(((AutomaticTest)DataModel.getCurrentTest()).getScriptFromModel().getNameFromModel());
501: file = ((AutomaticTest) DataModel.getCurrentTest())
502: .getTestScriptFromDB();
503: driver.editTest(Tools
504: .speedpurge(file.getAbsolutePath()), pTest,
505: param, plugSeting);
506: } else {
507: Script script = pTest.getScriptFromModel();
508: if (script != null)
509: driver.editTest(Tools.speedpurge(script
510: .getNameFromModel()), pTest, param,
511: plugSeting);
512: }
513: actualItem.setEnabled(true);
514: } catch (Exception ex) {
515: JOptionPane
516: .showMessageDialog(
517: AutomaticTestScriptView.this ,
518: Language
519: .getInstance()
520: .getText(
521: "Erreur_pour_lancer_l'éditeur_de_script_:_\n")
522: + ex.getMessage(), Language
523: .getInstance().getText("Erreur_!"),
524: JOptionPane.ERROR_MESSAGE);
525:
526: ex.printStackTrace();
527: //return;
528: }
529:
530: try {
531: Cursor c = new Cursor(Cursor.DEFAULT_CURSOR);
532: AutomaticTestScriptView.this .setCursor(c);
533: } catch (Exception ee) {
534: ee.printStackTrace();
535: }
536: //commitButton.setEnabled(true);
537:
538: }
539:
540: private void commitPerformed(ActionEvent e) {
541: if (Api.isConnected()) {
542: Date date = new Date(file.lastModified());
543: try {
544:
545: Script script = ((AutomaticTest) DataModel
546: .getCurrentTest()).getScriptFromModel();
547: script.updateInDB(file);
548: /*script.updateContentInDB(file.getAbsolutePath());
549: script.updateDateInDB(date);
550: script.updateLengthInDB(file.length());*/
551:
552: JOptionPane
553: .showMessageDialog(
554: AutomaticTestScriptView.this ,
555: Language
556: .getInstance()
557: .getText(
558: "Le_fichier_a_été_correctement_archivé."),
559: Language.getInstance().getText(
560: "Info..."),
561: JOptionPane.INFORMATION_MESSAGE);
562: actualItem.setEnabled(false);
563:
564: } catch (FileNotFoundException fe) {
565: JOptionPane
566: .showMessageDialog(
567: AutomaticTestScriptView.this ,
568: Language
569: .getInstance()
570: .getText(
571: "Impossible_de_trouver_le_fichier!\n_Vous_devez_l'importer_en_cliquant_sur_le_bouton_\"Modifier\""),
572: Language.getInstance().getText(
573: "Erreur_!"),
574: JOptionPane.ERROR_MESSAGE);
575: } catch (Exception exception) {
576: Tools.ihmExceptionView(exception);
577: }
578: }
579: }
580:
581: public void modifyArgPerformed(ActionEvent e) {
582: Script script = pTest.getScriptFromModel();
583: String newArg = driver.modifyTestDiverAgument(script
584: .getPlugArgFromModel());
585:
586: if (newArg != null && !newArg.equals("")) {
587:
588: try {
589: script.updatePlugArgInDBAndModel(newArg);
590:
591: plugArgLabel.setText(Language.getInstance().getText(
592: "Arguments_du_driver_:_")
593: + newArg);
594:
595: } catch (Exception exception) {
596: Tools.ihmExceptionView(exception);
597: }
598: }
599: }
600:
601: public void plugInfoPerformed(ActionEvent e) {
602: driver.getHelp();
603: }
604:
605: } // Fin de la classe AutomaticTestScriptView
|