View Javadoc

1   package net.sf.mmapps.applications.developer.dialog;
2   
3   import java.awt.*;
4   import java.awt.event.*;
5   import java.util.Hashtable;
6   
7   import javax.swing.*;
8   
9   /***
10   * Default dialog makes it possible to create simple dialogs.
11   * @author Kees Jongenburger
12   * @version $Id: DefaultDialog.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
13   **/
14  public class DefaultDialog extends JDialog implements ActionListener{
15      private JLabel dialogCaption;
16      private JPanel inputFieldsPanel;
17      private JPanel cancelOkPanel;
18      private JButton okButton;
19      private JButton cancelButton;
20      public static int ACTION_CANCEL =1;
21      public static int ACTION_OK =2;
22      public static int ACTION_UNINITIALIZED =-1;
23      int action = ACTION_UNINITIALIZED;
24      
25      Hashtable inputs;
26      
27      public void addInput(String name){
28          JLabel label = new JLabel();
29          JTextField textField = new JTextField(20);
30          inputs.put(name,textField);
31          
32          label.setText(name);
33          GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
34          inputFieldsPanel.add(label, gridBagConstraints);
35          
36          //textfield.setText("admin");
37          gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
38          gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
39          inputFieldsPanel.add(textField, gridBagConstraints);
40          
41          pack();
42      }
43      
44      public void show(){
45          
46          //Toolkit tk = Toolkit.getDefaultToolkit();
47          //Dimension dim = tk.getScreenSize();
48          Dimension dim = getOwner().getSize();
49          Point point = getOwner().getLocation();
50          setLocation(point.x + (dim.width /2) - getWidth() /2, point.y +(dim.height /2) - getHeight()/ 2);
51          super.show();
52      }
53  
54      public String getInput(String name){
55          JTextField t = (JTextField)inputs.get(name);
56          return t.getText();
57      }
58      
59      public void setInput(String name,String value){
60          JTextField t = (JTextField)inputs.get(name);
61          t.setText(value);
62      }
63      
64  
65      /*** Creates new form DefaultDialog */
66      public DefaultDialog(java.awt.Frame parent, boolean modal,String title) {
67          super(parent, modal);
68          inputs = new Hashtable();
69          dialogCaption = new JLabel();
70          inputFieldsPanel = new JPanel();
71          
72          cancelOkPanel = new JPanel();
73          okButton = new JButton();
74          cancelButton = new JButton();
75          
76          getContentPane().setLayout(new java.awt.GridBagLayout());
77          java.awt.GridBagConstraints gridBagConstraints1;
78          
79          addWindowListener(new java.awt.event.WindowAdapter() {
80              public void windowClosing(java.awt.event.WindowEvent evt) {
81                  closeDialog(evt);
82              }
83          });
84          
85          setName(title);
86          setTitle(title);
87          dialogCaption.setText(title);
88          gridBagConstraints1 = new java.awt.GridBagConstraints();
89          gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
90          gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
91          getContentPane().add(dialogCaption, gridBagConstraints1);
92          
93          inputFieldsPanel.setLayout(new java.awt.GridBagLayout());
94          getContentPane().add(inputFieldsPanel, gridBagConstraints1);
95          
96          cancelOkPanel.setLayout(new java.awt.GridBagLayout());
97          java.awt.GridBagConstraints gridBagConstraints2;
98          
99          okButton.setText("OK");
100         okButton.addActionListener(this);
101         gridBagConstraints2 = new java.awt.GridBagConstraints();
102         cancelOkPanel.add(okButton, gridBagConstraints2);
103         
104         cancelButton.setText("Cancel");
105         cancelButton.addActionListener(this);
106         gridBagConstraints2 = new java.awt.GridBagConstraints();
107         cancelOkPanel.add(cancelButton, gridBagConstraints2);
108         
109         gridBagConstraints1 = new java.awt.GridBagConstraints();
110         getContentPane().add(cancelOkPanel, gridBagConstraints1);
111         
112     }
113     public void actionPerformed(ActionEvent actionEvent){
114         if (actionEvent.getSource() == okButton){
115             action = ACTION_OK;
116             setVisible(false);
117             dispose();
118         } else if (actionEvent.getSource() == cancelButton){
119             action = ACTION_CANCEL;
120             setVisible(false);
121             dispose();
122         }
123     }
124     
125     public int getAction(){
126         return action;
127     }
128     public void setCancelLabel(String value){
129         cancelButton.setText(value);
130     }
131     
132     public void setOkLabel(String value){
133         okButton.setText(value);
134     }
135     
136     /*** Closes the dialog */
137     private void closeDialog(java.awt.event.WindowEvent evt) {
138         setVisible(false);
139         dispose();
140     }
141     
142     
143 }