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.Color;
027: import java.awt.Component;
028: import java.awt.Container;
029: import java.awt.Dimension;
030: import java.awt.FlowLayout;
031: import java.awt.GraphicsConfiguration;
032: import java.awt.GraphicsDevice;
033: import java.awt.GraphicsEnvironment;
034: import java.awt.Rectangle;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.WindowEvent;
038: import java.awt.event.WindowListener;
039: import java.util.ArrayList;
040: import java.util.Collection;
041: import java.util.HashSet;
042: import java.util.Iterator;
043:
044: import javax.swing.BorderFactory;
045: import javax.swing.Box;
046: import javax.swing.BoxLayout;
047: import javax.swing.JButton;
048: import javax.swing.JDialog;
049: import javax.swing.JLabel;
050: import javax.swing.JOptionPane;
051: import javax.swing.JPanel;
052: import javax.swing.JScrollPane;
053: import javax.swing.JTabbedPane;
054: import javax.swing.JTextField;
055: import javax.swing.JTextPane;
056: import javax.swing.text.BadLocationException;
057: import javax.swing.text.Style;
058: import javax.swing.text.StyleConstants;
059: import javax.swing.text.StyleContext;
060: import javax.swing.text.StyledDocument;
061:
062: import org.objectweb.salome_tmf.api.Api;
063: import org.objectweb.salome_tmf.data.Action;
064: import org.objectweb.salome_tmf.data.DataConstants;
065: import org.objectweb.salome_tmf.data.ManualTest;
066: import org.objectweb.salome_tmf.data.Parameter;
067: import org.objectweb.salome_tmf.ihm.IHMConstants;
068: import org.objectweb.salome_tmf.ihm.languages.Language;
069: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
070: import org.objectweb.salome_tmf.ihm.main.plugins.PluginsTools;
071: import org.objectweb.salome_tmf.ihm.tools.Tools;
072: import org.objectweb.salome_tmf.plugins.UICompCst;
073:
074: /**
075: * Classe qui représente la vue pour ajouter/éditer une action
076:
077: */
078: public class AskNewAction extends JDialog implements DataConstants,
079: IHMConstants {
080:
081: /**
082: * La zone texte pour saisir le nom de l'action
083: */
084: JTextField nameField;
085:
086: /**
087: * La zone texte pour saisir la description
088: */
089: JTextPane descriptionArea;
090:
091: /**
092: * La zone texte pour saisir le résultat attendu
093: */
094: JTextPane awaitedResultArea;
095:
096: Style paramStyleDescription;
097: Style paramStyleAwaitedResult;
098:
099: Style def;
100:
101: Style regularDescription;
102: Style regularAwaitedResult;
103: StyledDocument descriptionDoc;
104:
105: StyledDocument awaitedResultDoc;
106:
107: /**
108: * L'action crée
109: */
110: Action action;
111:
112: AttachmentView attachmentPanel;
113:
114: /**************************************************************************/
115: /** METHODES PUBLIQUES ***/
116: /**************************************************************************/
117:
118: /**
119: * Constructeur de la vue pour ajouter/éditer une action
120: * @param parent le composant père
121: * @param title le titre du composant
122: * @param name le nom de l'action
123: * @param descr la description de l'action
124: * @param result le résultat de l'action
125: * @param attach la liste des attachements
126: */
127: public AskNewAction(Component parent, String title, Action oldAction) {
128:
129: //Pour bloquer le focus sur la boite de dialogue
130: super (SalomeTMFContext.getInstance().ptrFrame, true);
131: int t_x = 1024 - 100;
132: int t_y = 768 / 3 * 2 - 50;
133: int t_x2 = 1024;
134: int t_y2 = 768;
135: try {
136: GraphicsEnvironment ge = GraphicsEnvironment
137: .getLocalGraphicsEnvironment();
138: GraphicsDevice[] gs = ge.getScreenDevices();
139: GraphicsDevice gd = gs[0];
140: GraphicsConfiguration[] gc = gd.getConfigurations();
141: Rectangle r = gc[0].getBounds();
142: t_x = r.width - 100;
143: t_y = r.height / 3 * 2 - 50;
144: t_x2 = r.width;
145: t_y2 = r.height;
146: } catch (Exception E) {
147:
148: }
149:
150: nameField = new JTextField(10);
151: descriptionArea = new JTextPane();
152: awaitedResultArea = new JTextPane();
153: descriptionDoc = descriptionArea.getStyledDocument();
154: awaitedResultDoc = descriptionArea.getStyledDocument();
155: //JPanel page = new JPanel();
156:
157: // Cas ou le test est utilisé dans une campagne de test
158: String message = DataModel.getCurrentProject()
159: .getCampaignWithExecResultWhereTestIsUse(
160: DataModel.getCurrentTest());
161: if ((!message.equals("")) && (Api.isLockExecutedTest())) {
162: nameField.setEditable(false);
163: descriptionArea.setEditable(false);
164: awaitedResultArea.setEditable(false);
165: }
166:
167: JLabel testNameLabel = new JLabel(Language.getInstance()
168: .getText("Nom_de_l'action_:_"));
169:
170: JPanel giveName = new JPanel(new FlowLayout(FlowLayout.LEFT));
171: giveName.add(Box.createHorizontalGlue());
172: giveName.add(testNameLabel);
173: giveName.add(Box.createRigidArea(new Dimension(10, 0)));
174: giveName.add(nameField);
175:
176: JScrollPane descriptionScrollPane = new JScrollPane(
177: descriptionArea);
178: descriptionScrollPane.setBorder(BorderFactory
179: .createTitledBorder(BorderFactory
180: .createLineBorder(Color.BLACK), Language
181: .getInstance().getText("Description")));
182: //descriptionScrollPane.setPreferredSize(new Dimension(500,100));
183:
184: JScrollPane awaitedResultScrollPane = new JScrollPane(
185: awaitedResultArea);
186: awaitedResultScrollPane.setBorder(BorderFactory
187: .createTitledBorder(BorderFactory
188: .createLineBorder(Color.BLACK), Language
189: .getInstance().getText("Résultat_attendu")));
190: //awaitedResultScrollPane.setPreferredSize(new Dimension(500,100));
191:
192: JButton okButton = new JButton(Language.getInstance().getText(
193: "Valider"));
194: okButton.setToolTipText(Language.getInstance().getText(
195: "Valider"));
196: okButton.addActionListener(new ActionListener() {
197: public void actionPerformed(ActionEvent e) {
198: if (!nameField.getText().trim().equals("")) {
199: if (!((ManualTest) DataModel.getCurrentTest())
200: .hasActionNameInModel(nameField.getText()
201: .trim())
202: && !nameField.getText().trim().equals(
203: DataModel.EMPTY_NAME)
204: && !Action.isInBase(DataModel
205: .getCurrentTest(), nameField
206: .getText().trim())) {
207: createAndQuit();
208: } else if (((ManualTest) DataModel.getCurrentTest())
209: .getActionFromModel(nameField.getText()
210: .trim()) != null
211: && ((ManualTest) DataModel.getCurrentTest())
212: .getActionFromModel(
213: nameField.getText().trim())
214: .equals(action)) {
215: createAndQuit();
216: } else {
217: JOptionPane
218: .showMessageDialog(
219: AskNewAction.this ,
220: Language
221: .getInstance()
222: .getText(
223: "Ce_nom_d'action_existe_déjà_!"),
224: Language.getInstance().getText(
225: "Erreur_!"),
226: JOptionPane.ERROR_MESSAGE);
227: }
228: } else {
229: JOptionPane
230: .showMessageDialog(
231: AskNewAction.this ,
232: Language
233: .getInstance()
234: .getText(
235: "Il_faut_obligatoirement_donner_un_nom_à_l'action_!"),
236: Language.getInstance().getText(
237: "Attention_!"),
238: JOptionPane.WARNING_MESSAGE);
239: }
240: }
241: });
242:
243: JButton cancelButton = new JButton(Language.getInstance()
244: .getText("Annuler"));
245: cancelButton.setToolTipText(Language.getInstance().getText(
246: "Annuler"));
247: cancelButton.addActionListener(new ActionListener() {
248: public void actionPerformed(ActionEvent e) {
249: action = null;
250: DataModel.initAttachmentTable(DataModel
251: .getCurrentTest().getAttachmentMapFromModel()
252: .values());
253: AskNewAction.this .dispose();
254: }
255: });
256:
257: JButton descriptionAddParameterButton = new JButton(Language
258: .getInstance().getText("Paramètre"));
259: descriptionAddParameterButton.setIcon(Tools
260: .createAppletImageIcon(PATH_TO_PARAM_ICON, ""));
261: descriptionAddParameterButton.setToolTipText(Language
262: .getInstance().getText(
263: "Ajouter_un_paramètre_à_la_description"));
264: descriptionAddParameterButton
265: .addActionListener(new ActionListener() {
266: public void actionPerformed(ActionEvent e) {
267: addParameterInText(descriptionArea);
268: }
269: });
270: // Cas ou le test est utilisé dans une campagne de test
271: if ((!message.equals("")) && (Api.isLockExecutedTest())) {
272: descriptionAddParameterButton.setEnabled(false);
273: }
274:
275: JButton resultAddParameterButton = new JButton(Language
276: .getInstance().getText("Paramètre"));
277: resultAddParameterButton.setIcon(Tools.createAppletImageIcon(
278: PATH_TO_PARAM_ICON, ""));
279: resultAddParameterButton.setToolTipText(Language.getInstance()
280: .getText("Ajouter_un_paramètre_au_résultat_attendu"));
281: resultAddParameterButton
282: .addActionListener(new ActionListener() {
283: public void actionPerformed(ActionEvent e) {
284: addParameterInText(awaitedResultArea);
285: }
286: });
287: // Cas ou le test est utilisé dans une campagne de test
288: if ((!message.equals("")) && (Api.isLockExecutedTest())) {
289: resultAddParameterButton.setEnabled(false);
290: }
291:
292: JPanel completeDescriptionPane = new JPanel();
293: completeDescriptionPane.setLayout(new BoxLayout(
294: completeDescriptionPane, BoxLayout.X_AXIS));
295: completeDescriptionPane.add(descriptionScrollPane);
296: descriptionScrollPane.setPreferredSize(new Dimension(t_x,
297: t_y / 3 * 2));
298: completeDescriptionPane.add(descriptionAddParameterButton);
299: completeDescriptionPane.setPreferredSize(new Dimension(t_x,
300: t_y / 3 * 2));
301:
302: JPanel completeAwaitedResultPane = new JPanel();
303: completeAwaitedResultPane.setLayout(new BoxLayout(
304: completeAwaitedResultPane, BoxLayout.X_AXIS));
305: completeAwaitedResultPane.add(awaitedResultScrollPane);
306: awaitedResultScrollPane.setPreferredSize(new Dimension(t_x,
307: t_y / 3 * 2));
308: completeAwaitedResultPane.add(resultAddParameterButton);
309: completeAwaitedResultPane.setPreferredSize(new Dimension(t_x,
310: t_y / 3 * 2));
311:
312: JPanel buttonPanel = new JPanel(new FlowLayout(
313: FlowLayout.CENTER));
314: buttonPanel.add(okButton);
315: buttonPanel.add(cancelButton);
316:
317: /*JPanel textPanel = new JPanel();
318: textPanel.setLayout(new BoxLayout(textPanel,BoxLayout.Y_AXIS));
319: textPanel.add(Box.createRigidArea(new Dimension(0,10)));
320: textPanel.add(giveName);
321: textPanel.add(Box.createRigidArea(new Dimension(1,30)));
322: textPanel.add(completeDescriptionPane);
323: textPanel.add(Box.createRigidArea(new Dimension(1,30)));
324: textPanel.add(completeAwaitedResultPane);
325: textPanel.add(Box.createRigidArea(new Dimension(1,30)));
326: */
327:
328: JPanel textPanel = new JPanel();
329: textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
330: textPanel.add(Box.createRigidArea(new Dimension(1, 1)));
331: textPanel.add(completeDescriptionPane);
332: textPanel.add(Box.createRigidArea(new Dimension(1, 1)));
333: textPanel.add(completeAwaitedResultPane);
334: //textPanel.add(Box.createRigidArea(new Dimension(1,1)));
335:
336: //attachmentPanel = new AttachmentView(DataModel.getApplet(), ACTION, ACTION, oldAction, SMALL_SIZE_FOR_ATTACH, null, null, null);
337: attachmentPanel = new AttachmentView(DataModel.getApplet(),
338: ACTION, ACTION, oldAction, SMALL_SIZE_FOR_ATTACH, this );
339: attachmentPanel.setBorder(BorderFactory.createTitledBorder(
340: BorderFactory.createLineBorder(Color.BLACK), Language
341: .getInstance().getText("Attachements")));
342: //textPanel.add(attachmentPanel);
343:
344: /* page.setLayout(new BorderLayout());
345: page.add(textPanel);
346: page.add(buttonPanel, BorderLayout.SOUTH);
347: */
348:
349: //Container contentPaneFrame = this.getContentPane();
350: //contentPaneFrame.add(page, BorderLayout.CENTER);
351:
352: def = StyleContext.getDefaultStyleContext().getStyle(
353: StyleContext.DEFAULT_STYLE);
354:
355: regularDescription = descriptionDoc.addStyle("regularD", def);
356: StyleConstants.setForeground(regularDescription, Color.BLACK);
357: // Style souligné
358: paramStyleDescription = descriptionDoc.addStyle("parameterD",
359: regularDescription);
360: StyleConstants.setForeground(paramStyleDescription, Color.red);
361:
362: regularAwaitedResult = descriptionDoc
363: .addStyle("regularAR", def);
364: StyleConstants.setForeground(regularAwaitedResult, Color.BLACK);
365: // Style souligné
366: paramStyleAwaitedResult = descriptionDoc.addStyle(
367: "parameterAR", regularAwaitedResult);
368: StyleConstants
369: .setForeground(paramStyleAwaitedResult, Color.red);
370:
371: initData(oldAction);
372:
373: this .addWindowListener(new WindowListener() {
374: public void windowClosing(WindowEvent e) {
375: action = null;
376: DataModel.initAttachmentTable(DataModel
377: .getCurrentTest().getAttachmentMapFromModel()
378: .values());
379: }
380:
381: public void windowDeiconified(WindowEvent e) {
382: }
383:
384: public void windowOpened(WindowEvent e) {
385: }
386:
387: public void windowActivated(WindowEvent e) {
388: }
389:
390: public void windowDeactivated(WindowEvent e) {
391: }
392:
393: public void windowClosed(WindowEvent e) {
394: }
395:
396: public void windowIconified(WindowEvent e) {
397: }
398: });
399:
400: /*
401: Container contentPane = this.getContentPane();
402: Dimension d = giveName.getSize();
403: contentPane.add(giveName);
404: giveName.setMaximumSize(new Dimension(t_x2, 40));
405: contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
406:
407: textPanel.setPreferredSize(new Dimension(t_x, t_y/3*2 - d.height));
408: contentPane.add(textPanel);
409:
410: attachmentPanel.setMaximumSize(new Dimension(t_x2, t_y2/3));
411: contentPane.add(attachmentPanel);
412:
413: buttonPanel.setMaximumSize(new Dimension(t_x2, 40));
414: contentPane.add(buttonPanel);
415: */
416: JTabbedPane pJTabbedPane = new JTabbedPane();
417: Container contentPane = this .getContentPane();
418: contentPane.setLayout(new BoxLayout(contentPane,
419: BoxLayout.Y_AXIS));
420:
421: Dimension d = giveName.getSize();
422: JPanel actionDetail = new JPanel();
423: actionDetail.setLayout(new BoxLayout(actionDetail,
424: BoxLayout.Y_AXIS));
425: giveName.setMaximumSize(new Dimension(t_x2, 40));
426: actionDetail.add(giveName);
427: textPanel.setPreferredSize(new Dimension(t_x, t_y / 3 * 2
428: - d.height));
429: actionDetail.add(textPanel);
430:
431: attachmentPanel.setMaximumSize(new Dimension(t_x2, t_y2 / 3));
432: pJTabbedPane.add(Language.getInstance().getText("Action"),
433: actionDetail);
434: pJTabbedPane.add(
435: Language.getInstance().getText("Attachements"),
436: attachmentPanel);
437:
438: SalomeTMFContext.getInstance().addToUIComponentsMap(
439: UICompCst.ACTION_NEW_TAB, pJTabbedPane);
440: // Activation des plugins associés
441: PluginsTools.activateAssociatedPlgs(UICompCst.ACTION_NEW_TAB);
442:
443: buttonPanel.setMaximumSize(new Dimension(t_x2, 40));
444: contentPane.add(pJTabbedPane);
445: contentPane.add(buttonPanel);
446:
447: setSize(t_x, t_y);
448: this .setTitle(title);
449:
450: /*this.setLocationRelativeTo(this.getParent());
451: this.pack();
452: this.setVisible(true);*/
453: centerScreen();
454:
455: } // Fin du constructeur AddAction/6
456:
457: void centerScreen() {
458: Dimension dim = getToolkit().getScreenSize();
459: this .pack();
460: Rectangle abounds = getBounds();
461: setLocation((dim.width - abounds.width) / 2,
462: (dim.height - abounds.height) / 2);
463: this .setVisible(true);
464: requestFocus();
465: }
466:
467: /**
468: * Constructeur
469: * @param parent le composant père
470: * @param title le titre du composant
471: */
472: public AskNewAction(Component parent, String title) {
473: this (parent, title, null);
474: } // Fin du constructeur AddAction/2
475:
476: /**************************************************************************/
477: /** METHODES PUBLIQUES ***/
478: /**************************************************************************/
479:
480: /**
481: * Accesseur de l'action construite
482: * @return une action
483: */
484: public Action getAction() {
485: return action;
486: } // Fin de la méthode getAction/0
487:
488: private void initData(Action oldAction) {
489: if (oldAction != null) {
490: action = oldAction;
491: //Initialisation pour édition
492: nameField.setText(oldAction.getNameFromModel());
493: descriptionArea
494: .setText(oldAction.getDescriptionFromModel());
495: awaitedResultArea.setText(oldAction
496: .getAwaitedResultFromModel());
497: Collection col = oldAction.getAttachmentMapFromModel()
498: .values();
499: DataModel.initAttachmentTable(col);
500: } else {
501: ManualTest test = (ManualTest) DataModel.getCurrentTest();
502: //action = new Action(test.getIdBDD());
503:
504: int i = DataModel.getActionTableModel().getRowCount();
505: while (test.getActionListFromModel(false).contains(
506: test.getActionFromModel("A" + i))) {
507: i++;
508: }
509: action = new Action(test, "A" + i, "");
510: nameField.setText("A" + i);
511: DataModel.getAttachmentTableModel().clearTable();
512: }
513: DataModel.setCurrentAction(action);
514:
515: }
516:
517: /**
518: *
519: * @param area
520: */
521: private void addParameterInText(JTextPane textPane) {
522: ParameterInsertionView insertView = new ParameterInsertionView();
523: if (insertView.getParameter() != null) {
524: // action.addParameter(insertView.getParameter());
525: action.setUseParamInModel(insertView.getParameter());
526: try {
527: StyledDocument doc = (StyledDocument) textPane
528: .getDocument();
529: if (textPane.equals(awaitedResultArea)) {
530:
531: doc.insertString(textPane.getCaretPosition(), "$"
532: + insertView.getParameter()
533: .getNameFromModel() + "$", doc
534: .getStyle("parameterAR"));
535: doc.insertString(doc.getLength(), " ", doc
536: .getStyle("regularAR"));
537: } else {
538: doc.insertString(textPane.getCaretPosition(), "$"
539: + insertView.getParameter()
540: .getNameFromModel() + "$", doc
541: .getStyle("parameterD"));
542: doc.insertString(doc.getLength(), " ", doc
543: .getStyle("regularD"));
544: }
545: } catch (BadLocationException ble) {
546: }
547: }
548: } // Fin de la méthode addParameterInText/1
549:
550: private void createAndQuit() {
551:
552: action.updateInModel(nameField.getText().trim(),
553: descriptionArea.getText(), awaitedResultArea.getText());
554: //action.getAttachmentMapFromModel().clear();
555: /*Collection values = attachmentPanel.getAttachmentMap().values();
556: for (Iterator iter = values.iterator(); iter.hasNext();) {
557: action.addAttachementInModel((Attachment)iter.next());
558: }*/
559: ArrayList paramInDesc = Tools.getParametersInDescription(
560: descriptionArea.getText(), action
561: .getParameterHashSetFromModel());
562: ArrayList paramInAwaitedResult = Tools
563: .getParametersInDescription(
564: awaitedResultArea.getText(), action
565: .getParameterHashSetFromModel());
566: ArrayList toBeDelete = new ArrayList();
567: HashSet paramSet = new HashSet(action
568: .getParameterHashSetFromModel().values());//ADD - Hashtable2HashSet
569: for (Iterator iter = paramSet.iterator(); iter.hasNext();) {
570: Parameter param = (Parameter) iter.next();
571: if (!paramInDesc.contains(param)
572: && !paramInAwaitedResult.contains(param))
573: toBeDelete.add(param);
574: }
575: for (int i = 0; i < toBeDelete.size(); i++) {
576: action.deleteUseParamInModel((Parameter) toBeDelete.get(i));
577: }
578: DataModel.initAttachmentTable(DataModel.getCurrentTest()
579: .getAttachmentMapFromModel().values());
580:
581: AskNewAction.this .dispose();
582: }
583:
584: } // Fin de la classe AddAction
|