KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > graph > dialog > SelectGraphDialog


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.graph.dialog;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.GridBagConstraints JavaDoc;
11 import java.awt.GridBagLayout JavaDoc;
12 import java.awt.GridLayout JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.ActionListener JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import javax.swing.JButton JavaDoc;
19 import javax.swing.JComboBox JavaDoc;
20 import javax.swing.JDialog JavaDoc;
21 import javax.swing.JLabel JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23
24 /**
25  * @author Laurent Etiemble
26  * @version $Revision: 1.5 $
27  */

28 public class SelectGraphDialog extends JDialog JavaDoc
29 {
30    /** Description of the Field */
31    protected JComboBox JavaDoc choices;
32    /** Description of the Field */
33    protected Object JavaDoc initialSelectionValue;
34    /** Description of the Field */
35    protected Object JavaDoc selectedValue = null;
36    /** Description of the Field */
37    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService");
38
39
40    /**
41     *Constructor for the CustomJOptionPane object
42     *
43     * @param parentComponent Description of the Parameter
44     * @param selectionValues Description of the Parameter
45     * @param initialSelectionValue Description of the Parameter
46     */

47    public SelectGraphDialog(Component JavaDoc parentComponent, Object JavaDoc[] selectionValues, Object JavaDoc initialSelectionValue)
48    {
49       super();
50       this.setModal(true);
51       this.setTitle(resources.getString("graph.dialog.title"));
52
53       JPanel JavaDoc main = new JPanel JavaDoc(new GridBagLayout JavaDoc());
54       GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
55       constraints.insets = new Insets JavaDoc(5, 5, 5, 5);
56
57       constraints.gridwidth = GridBagConstraints.REMAINDER;
58       constraints.weightx = 1.0;
59       constraints.fill = GridBagConstraints.HORIZONTAL;
60       constraints.anchor = GridBagConstraints.WEST;
61       JLabel JavaDoc messageLabel = new JLabel JavaDoc(resources.getString("graph.dialog.text.description"));
62       main.add(messageLabel, constraints);
63
64       this.initialSelectionValue = initialSelectionValue;
65
66       this.choices = new JComboBox JavaDoc(selectionValues);
67       this.choices.setSelectedItem(this.initialSelectionValue);
68       this.choices.setEditable(true);
69       if (selectionValues.length == 0)
70       {
71          choices.getEditor().setItem(this.initialSelectionValue);
72       }
73
74       main.add(choices, constraints);
75
76       JPanel JavaDoc buttons = new JPanel JavaDoc(new GridLayout JavaDoc(1, 2, 2, 2));
77
78       JButton JavaDoc buttonSelect = new JButton JavaDoc(resources.getString("graph.dialog.button.select"));
79       buttons.add(buttonSelect);
80       buttonSelect.addActionListener(
81          new ActionListener JavaDoc()
82          {
83             /**
84              * @param e Description of the Parameter
85              */

86             public void actionPerformed(ActionEvent JavaDoc e)
87             {
88                select();
89                dispose();
90             }
91          });
92
93       JButton JavaDoc buttonCancel = new JButton JavaDoc(resources.getString("graph.dialog.button.cancel"));
94       buttons.add(buttonCancel);
95       buttonCancel.addActionListener(
96          new ActionListener JavaDoc()
97          {
98             /**
99              * @param e Description of the Parameter
100              */

101             public void actionPerformed(ActionEvent JavaDoc e)
102             {
103                selectedValue = null;
104                dispose();
105             }
106          });
107
108       constraints.fill = GridBagConstraints.NONE;
109       constraints.anchor = GridBagConstraints.SOUTH;
110       main.add(buttons, constraints);
111
112       this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
113       this.getContentPane().add(main);
114       this.pack();
115       this.setLocationRelativeTo(parentComponent);
116    }
117
118
119    /**
120     * Returns the selectedValue.
121     *
122     * @return Object
123     */

124    public Object JavaDoc getSelectedValue()
125    {
126       return selectedValue;
127    }
128
129
130    /** Description of the Method */
131    private void select()
132    {
133       boolean found = false;
134
135       Object JavaDoc choice = this.choices.getSelectedItem();
136       Object JavaDoc text = this.choices.getEditor().getItem();
137
138       for (int i = 0; i < this.choices.getModel().getSize(); i++)
139       {
140          choice = this.choices.getModel().getElementAt(i);
141          if (choice.toString().equals(text.toString()))
142          {
143             found = true;
144             break;
145          }
146       }
147       if (!found)
148       {
149          choice = text;
150       }
151
152       this.selectedValue = choice;
153    }
154 }
155
Popular Tags