KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ConfigDialog


1
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.io.FileOutputStream JavaDoc;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8
9 /** Configuration frame.
10 A frame that sets the config properties.
11 * @see WhisperIM#Config*/

12 public final class ConfigDialog{
13     private static final int GAP=10; // gap in pixels between components
14

15     final JFrame frame=new JFrame(Lang.gs("config title"));
16     final JPanel flPanel=new JPanel(new GridLayout(4,1,GAP,GAP));
17     final JPanel otherPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
18     final JPanel oPanel=new JPanel(new GridLayout(1,1,GAP,GAP));
19     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.TRAILING,GAP,GAP));
20     
21     final ButtonGroup accountLocation=new ButtonGroup();
22     final JRadioButton userhomeRB=new JRadioButton(Lang.gs("user home"));
23     final JRadioButton applicationRB=new JRadioButton(Lang.gs("application dir"));
24     final JRadioButton otherRB=new JRadioButton(Lang.gs("other dir"));
25     final JTextField other=new JTextField(30);
26     final JButton browseButton=new JButton(Lang.gs("browse_tt"),Icons.BROWSE);
27     JFileChooser dirChooser;
28     
29     final JLabel timeoutLbl=new JLabel(Lang.gs("time out"));
30     
31     final int start=((Integer.parseInt(WhisperIM.Config.getProperty("timeout")))/1000);
32     final JSpinner timeout=new JSpinner( new SpinnerNumberModel(start,0,60,1));
33     
34     final JButton saveBtn=new JButton(Lang.gs("save"),Icons.SAVE);
35     final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL);
36     final JButton helpBtn=new JButton(Lang.gs("help"),Icons.HELP);
37     
38     final BtnClick click=new BtnClick(); // button action listener
39
final RadioBtnClick radioBtnClick=new RadioBtnClick(); // RadioButton listener
40

41     /** Creates the config frame.*/
42     public ConfigDialog(){
43         frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
44         frame.setIconImage(Icons.CONFIG.getImage());
45         Container cp=frame.getContentPane();
46         cp.setLayout(new BorderLayout(GAP,GAP));
47         // file location panel
48
flPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK),Lang.gs("account location")));
49         flPanel.setToolTipText(Lang.gs("account location_tt"));
50         accountLocation.add(userhomeRB);
51         accountLocation.add(applicationRB);
52         accountLocation.add(otherRB);
53         flPanel.add(userhomeRB);
54         flPanel.add(applicationRB);
55         flPanel.add(otherRB);
56         browseButton.setActionCommand("browse");
57         browseButton.setToolTipText(Lang.gs("browse_tt"));
58         browseButton.addActionListener(click);
59         otherPanel.add(other);
60         otherPanel.add(browseButton);
61         flPanel.add(otherPanel);
62         userhomeRB.addActionListener(radioBtnClick);
63         applicationRB.addActionListener(radioBtnClick);
64         otherRB.addActionListener(radioBtnClick);
65         if(WhisperIM.Config.getProperty("accountLocation").compareTo("userhome")==0){
66             userhomeRB.setSelected(true);
67         }
68         else{
69             if(WhisperIM.Config.getProperty("accountLocation").compareTo("application")==0){
70                 applicationRB.setSelected(true);
71             }
72             else{
73                 other.setText(WhisperIM.Config.getProperty("accountLocation"));
74                 otherRB.setSelected(true);
75             }
76         }
77         if(other.getText().equals("")){
78             other.setText(System.getProperties().getProperty("user.home"));
79         }
80         other.setEnabled(otherRB.isSelected());
81         dirChooser=new JFileChooser(other.getText());
82         dirChooser.setDialogTitle(Lang.gs("select account files directory"));
83         dirChooser.setApproveButtonText(Lang.gs("select"));
84         dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
85         dirChooser.setMultiSelectionEnabled(false);
86         browseButton.setEnabled(otherRB.isSelected());
87         // other options panel
88
oPanel.setBorder(new EmptyBorder(GAP,GAP,GAP,GAP));
89         final Box timeoutBox=new Box(BoxLayout.X_AXIS);
90         timeout.setToolTipText(Lang.gs("time out_tt"));
91         timeoutLbl.setToolTipText(Lang.gs("time out_tt"));
92         timeoutLbl.setLabelFor(timeout);
93         timeoutLbl.setDisplayedMnemonic(Lang.s2k("time out_mn"));
94         timeoutBox.add(timeoutLbl);
95         timeoutBox.add(Box.createGlue());
96         timeoutBox.add(timeout);
97         oPanel.add(timeoutBox);
98         // buttons panel
99
saveBtn.setDefaultCapable(true);
100         saveBtn.setMnemonic(Lang.s2k("save_mn"));
101         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
102         helpBtn.setMnemonic(Lang.s2k("help_mn"));
103         btnPanel.add(helpBtn);
104         btnPanel.add(saveBtn);
105         btnPanel.add(cancelBtn);
106         frame.getRootPane().setDefaultButton(saveBtn);
107         helpBtn.addActionListener(click);
108         saveBtn.addActionListener(click);
109         cancelBtn.addActionListener(click);
110         // add panels and pack
111
cp.add(flPanel,BorderLayout.NORTH);
112         cp.add(oPanel,BorderLayout.CENTER);
113         cp.add(btnPanel,BorderLayout.SOUTH);
114         frame.pack();
115         frame.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
116
frame.getRootPane().getActionMap().put("help",new HelpAction(frame,"config.html"));
117         frame.setLocationRelativeTo(null);
118     }
119     
120     /** Shows the frame.*/
121     public void show(){
122         frame.show();
123     }
124     
125     /** Disposes the dialog.*/
126     public void dispose(){
127         frame.dispose();
128     }
129     
130     final class BtnClick implements ActionListener{
131         public void actionPerformed(ActionEvent ae){
132             //get button
133
String JavaDoc b=ae.getActionCommand();
134             // call method depending on button clicked
135
if(b.equals(cancelBtn.getText())){
136                 frame.dispose();
137             }
138             if(b.equals(helpBtn.getText())){
139                 WhisperIM.Help_Window.show(frame,"config.html");
140                 return;
141             }
142             if(b.equals("browse")){
143                 int r=dirChooser.showDialog(frame,Lang.gs("select"));
144                 if(r==JFileChooser.APPROVE_OPTION){
145                     try{
146                         other.setText(dirChooser.getSelectedFile().getCanonicalPath());
147                     }
148                     catch(Exception JavaDoc e){
149                         // leave the text field as it is
150
}
151                 }
152                 return;
153             }
154             if(b.equals(saveBtn.getText())){
155                 // VALIDATE
156
// nothing to do
157
// SET CONFIG PROPERTIES
158
// timeout
159
int timeoutvalue=( (Integer JavaDoc) timeout.getValue()).intValue();
160                 timeoutvalue=timeoutvalue*1000; // setting is in milliseconds but control is for seconds
161
WhisperIM.Config.setProperty("timeout",timeoutvalue+"");
162                 // accountLocation
163
boolean needreboot=false;
164                 String JavaDoc currentAL=WhisperIM.Config.getProperty("accountLocation");
165                 if(userhomeRB.isSelected()){
166                     if(currentAL.compareTo("userhome")!=0){
167                         WhisperIM.Config.setProperty("accountLocation","userhome");
168                         needreboot=true;
169                     }
170                 }
171                 else{
172                     if(applicationRB.isSelected()){
173                         if(currentAL.compareTo("application")!=0){
174                             WhisperIM.Config.setProperty("accountLocation","application");
175                             needreboot=true;
176                         }
177                     }
178                     else{
179                         if(currentAL.compareTo(other.getText())!=0){
180                             WhisperIM.Config.setProperty("accountLocation",other.getText());
181                             needreboot=true;
182                         }
183                     }
184                 }
185                 // try and save
186
try{
187                     WhisperIM.Config.store(new FileOutputStream JavaDoc(WhisperIM.CONFIG_FILE,false),"Whisper IM Config");
188                 }
189                 catch(Exception JavaDoc e){
190                     JOptionPane.showMessageDialog(frame,Lang.gs("cannot save config file")+e.toString(),Lang.gs("error"),
191                 JOptionPane.ERROR_MESSAGE);
192                     return;
193                 }
194                 // display message
195
if(needreboot){
196                     if(WhisperIM.New_Account_Window!=null){
197                         WhisperIM.New_Account_Window.dispose();
198                         WhisperIM.New_Account_Window=null;
199                     }
200                     WhisperIM.Login_Window.dispose();
201                     try{
202                         WhisperIM.setAccountDir();
203                     }
204                     catch(Exception JavaDoc e){
205                         GUI.showError(frame,"config title",null,e.getMessage());
206                     }
207                     WhisperIM.Login_Window=new LogInFrame();
208                     JOptionPane.showMessageDialog(frame,Lang.gs("config file saved"),Lang.gs("config title"),JOptionPane.INFORMATION_MESSAGE);
209                     WhisperIM.Login_Window.show();
210                 }
211                 else{
212                     JOptionPane.showMessageDialog(frame,Lang.gs("config file saved"),Lang.gs("config title"),
213                     JOptionPane.INFORMATION_MESSAGE);
214                 }
215                 // dispose
216
frame.dispose();
217             }
218         }
219     }
220     
221     // en/disables the other text field depending on option chosen
222
final class RadioBtnClick implements ActionListener{
223         public void actionPerformed(ActionEvent ae){
224             if(otherRB.isSelected()){
225                 other.setEnabled(true);
226                 browseButton.setEnabled(true);
227                 other.requestFocus();
228             }
229             else{
230                 other.setEnabled(false);
231                 browseButton.setEnabled(false);
232             }
233         }
234     }
235 }
Popular Tags