1   package wbKerho;
2   
3   import java.awt.BorderLayout;
4   import java.awt.FlowLayout;
5   import java.awt.Frame;
6   
7   import javax.swing.JButton;
8   import javax.swing.JComponent;
9   import javax.swing.JDialog;
10  import javax.swing.JPanel;
11  import javax.swing.KeyStroke;
12  import javax.swing.border.EmptyBorder;
13  
14  import fi.jyu.mit.gui.EditPanel;
15  
16  import java.awt.event.ActionListener;
17  import java.awt.event.ActionEvent;
18  import java.awt.event.KeyEvent;
19  
20  /**
21   * Dialogi kerhon nimelle
22   * @author vesal
23   * @version 4.1.2011
24   *
25   */
26  public class KerhonNimiDialog extends JDialog {
27  
28      private static final long serialVersionUID = 1L;
29  
30      private final JPanel contentPanel = new JPanel();
31      private EditPanel editPanel = new EditPanel();
32  
33  
34      /**
35       * Käynnistä dialogi ja kysy kerhon nimi
36       * @param defName ehdotettava nimi
37       * @return käyttäjän antama nimi
38       */
39      public static String askName(String defName) {
40          String ret = null;
41          try {
42              KerhonNimiDialog dialog = new KerhonNimiDialog(null);
43              dialog.editPanel.setText(defName);
44              dialog.setVisible(true);
45              ret = dialog.getResult();
46              dialog.dispose();
47          } catch (Exception e) {
48              e.printStackTrace();
49          }
50          return ret;
51      }
52  
53  
54      private String result = null;
55  
56  
57      /**
58       * Palautetana dialogista saatu tulos. null jos poistuttiin
59       * @return dialogiin kirjoitettu jono, null jos lopetettiin
60       */
61      public String getResult() {
62          return result;
63      }
64  
65  
66      /**
67       * Apumetodi tuloksen asettamiseksi sisäistä luokista 
68       * @param value
69       */
70      protected void setResult(String value) {
71          result = value;
72      }
73  
74      /**
75       * Palautetaan editPanel sisäluokille
76       * @return editPanel
77       */
78      protected EditPanel getEditPanel() {
79          return editPanel;
80      }
81  
82  
83  
84  
85      /**
86       * Luodaan kerhon nimen kysyvä dialogi
87       * @param parent mikä framen alta käynnistettiin
88       */
89      public KerhonNimiDialog(Frame parent) {
90          super(parent,"Nimi",true);
91          setTitle("Kerho");
92          setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
93          setBounds(100, 100, 371, 268);
94          getContentPane().setLayout(new BorderLayout());
95          contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
96  
97          contentPanel.registerKeyboardAction(new ActionListener() {
98              @Override
99              public void actionPerformed(ActionEvent actionEvent) {
100                 setResult(null);
101                 setVisible(false);
102             }
103         }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
104 
105         getContentPane().add(contentPanel, BorderLayout.SOUTH);
106         contentPanel.setLayout(new BorderLayout(0, 0));
107 
108         editPanel.setColumns(20);
109         editPanel.setCaption("Anna kerhon nimi");
110         contentPanel.add(editPanel, BorderLayout.NORTH);
111         JPanel buttonPane = new JPanel();
112         contentPanel.add(buttonPane, BorderLayout.SOUTH);
113         buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
114 
115         JButton okButton = new JButton("OK");
116         okButton.addActionListener(new ActionListener() {
117             @Override
118             public void actionPerformed(ActionEvent arg0) {
119                 setResult(getEditPanel().getText());
120                 setVisible(false); 
121             }
122         });
123         buttonPane.add(okButton);
124         getRootPane().setDefaultButton(okButton);
125 
126         JButton cancelButton = new JButton("Cancel");
127         cancelButton.addActionListener(new ActionListener() {
128             @Override
129             public void actionPerformed(ActionEvent arg0) {
130                 setResult(null);
131                 setVisible(false);
132             }
133         });
134         cancelButton.setActionCommand("Cancel");
135         buttonPane.add(cancelButton);
136 
137         TiedotPanel tiedotPanel = new TiedotPanel();
138         getContentPane().add(tiedotPanel, BorderLayout.CENTER);
139     }
140 
141 
142 
143 }
144