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.Dimension;
027: import java.awt.Frame;
028: import java.awt.Rectangle;
029: import java.awt.event.ComponentAdapter;
030: import java.awt.event.ComponentEvent;
031: import java.awt.event.WindowAdapter;
032: import java.awt.event.WindowEvent;
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035:
036: import javax.swing.JDialog;
037: import javax.swing.JOptionPane;
038: import javax.swing.JTextField;
039:
040: import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
041:
042: /* 1.4 example used by DialogDemo.java. */
043: class CustomDialog extends JDialog implements PropertyChangeListener {
044: private String typedText = null;
045: private JTextField textField;
046:
047: private String btnOk = "Enter";
048: private String btnCancel = "Cancel";
049:
050: String result;
051:
052: private JOptionPane optionPane;
053:
054: public String getValidatedText() {
055: return typedText;
056: }
057:
058: /** Creates the reusable dialog. */
059: public CustomDialog(Frame aFrame, String title, String question,
060: String defaultValue) {
061: //super(true);
062: super (SalomeTMFContext.getInstance().getSalomeFrame());
063: setModal(true);
064: setResizable(false);
065:
066: setTitle(title);
067:
068: textField = new JTextField(10);
069: if (defaultValue != null && !defaultValue.equals("")) {
070: textField.setText(defaultValue);
071: }
072: setResizable(false);
073: Object[] array = { question, textField };
074:
075: //Create an array specifying the number of dialog buttons
076: //and their text.
077: Object[] options = { btnOk, btnCancel };
078:
079: //Create the JOptionPane.
080: optionPane = new JOptionPane(array,
081: JOptionPane.QUESTION_MESSAGE,
082: JOptionPane.YES_NO_OPTION, null, options, options[0]);
083:
084: //Make this dialog display it.
085: setContentPane(optionPane);
086:
087: //Handle window closing correctly.
088: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
089: addWindowListener(new WindowAdapter() {
090: public void windowClosing(WindowEvent we) {
091: optionPane.setValue(new Integer(
092: JOptionPane.CLOSED_OPTION));
093: }
094: });
095:
096: //Ensure the text field always gets the first focus.
097: addComponentListener(new ComponentAdapter() {
098: public void componentShown(ComponentEvent ce) {
099: textField.requestFocusInWindow();
100: }
101: });
102:
103: optionPane.addPropertyChangeListener(this );
104: //this.setLocation(300,200);
105: //this.setLocationRelativeTo(this.getParent());
106: //pack();
107: centerScreen();
108: }
109:
110: void centerScreen() {
111:
112: Dimension dim = getToolkit().getScreenSize();
113: this .pack();
114: Rectangle abounds = getBounds();
115: setLocation((dim.width - abounds.width) / 2,
116: (dim.height - abounds.height) / 2);
117: requestFocus();
118: }
119:
120: /** This method handles events for the text field. */
121: /*public void actionPerformed(ActionEvent e) {
122:
123: optionPane.setValue(btnString1);
124: System.out.println("[CustomDialog->actionPerformed] : " +e );
125: * Object value = optionPane.getValue();
126: * if (value == JOptionPane.UNINITIALIZED_VALUE) {
127: //ignore reset
128: return;
129: }
130:
131: }*/
132:
133: public void propertyChange(PropertyChangeEvent e) {
134: //System.out.println("[CustomDialog->PropertyChangeEvent] : " +e );
135: String prop = e.getPropertyName();
136: if (isVisible()
137: && (e.getSource() == optionPane)
138: && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
139: .equals(prop))) {
140:
141: Object value = optionPane.getValue();
142: //System.out.println("[CustomDialog->PropertyChangeEvent] value " + value );
143: if (value.equals(btnOk)) {
144: result = textField.getText();
145: //System.out.println("[CustomDialog->PropertyChangeEvent] OK : " + value);
146: clearAndHide();
147: } else if (value.equals(btnCancel)) {
148: //System.out.println("[CustomDialog->PropertyChangeEvent] CANCEL");
149: result = null;
150: clearAndHide();
151: } else {
152: //System.out.println("[CustomDialog->PropertyChangeEvent] default");
153: result = null;
154: }
155: }
156: }
157:
158: /** This method clears the dialog and hides it. */
159: public void clearAndHide() {
160: //textField.setText(null);
161: setVisible(false);
162: }
163:
164: public String getValue() {
165: return result;
166: }
167: }
|