1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.gui;
26
27
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.util.*;
31
32 import javax.swing.*;
33
34 import mk.fsgrep.util.ExternalEditor;
35 import mk.fsgrep.util.PersistentData;
36
37
38
39 /***
40 * A dialog that allows the user to modify (persistent) application data
41 * settings.
42 *
43 * @author Murali Krishnan
44 *
45 */
46 public class Preferences extends JDialog {
47
48
49
50
51
52 public static final String KEY_RESCAN = "auto_rescan";
53
54
55 /***
56 * A reference to the persistent data object (singleton).
57 */
58 private static final PersistentData pdata = PersistentData.getInstance();
59
60
61
62
63
64
65
66
67 private App _app = null;
68 private JButton _buttonOk = null;
69 private JButton _buttonCancel = null;
70 private JTextField _fieldEditor = new JTextField();
71 private JButton _buttonInfoEditor = null;
72 private JButton _buttonInfoRescan = null;
73 private BooleanChoice _selectAutoRescan = new BooleanChoice();
74
75
76
77
78
79 Preferences(App pApp) {
80 super(pApp.getFrame(), "Preferences", true);
81
82 _app = pApp;
83
84 initialize();
85 }
86
87
88
89
90
91 protected App getApp() {return _app;}
92 protected JButton getButtonOk() {return _buttonOk;}
93 protected JButton getButtonCancel() {return _buttonCancel;}
94 protected JTextField getFieldEditor() {return _fieldEditor;}
95 protected JButton getButtonInfoEditor() {return _buttonInfoEditor;}
96 protected JButton getButtonInfoRescan() {return _buttonInfoRescan;}
97 public BooleanChoice getSelectAutoRescan() {return _selectAutoRescan;}
98
99
100
101
102
103
104
105
106
107
108 protected void initialize() {
109 createComponents();
110 defineActions();
111 makeFrame();
112 }
113
114
115 protected void createComponents() {
116 _buttonOk = new JButton("OK");
117 _buttonCancel = new JButton("Cancel");
118
119 getFieldEditor().setText(pdata.getProperty(ExternalEditor.PERSISTENT_MAP_KEY));
120
121 _buttonInfoEditor = new InfoButton(this, "editor.txt");
122 _buttonInfoRescan = new InfoButton(this, "rescan.txt");
123
124 getSelectAutoRescan().setSelected(pdata.getBooleanProperty(KEY_RESCAN));
125 }
126
127
128 protected void defineActions() {
129 getButtonOk().addActionListener(new ActionListener() {
130 public void actionPerformed (ActionEvent e) {
131 handleSave();
132 }
133 });
134
135 getButtonCancel().addActionListener(new ActionListener() {
136 public void actionPerformed (ActionEvent e) {
137 dispose();
138 }
139 });
140 }
141
142
143 protected void makeFrame() {
144 getContentPane().setLayout(new BorderLayout());
145
146 getContentPane().add(makeMainPanel(), BorderLayout.CENTER);
147 getContentPane().add(makeButtonPanel(), BorderLayout.SOUTH);
148
149 pack();
150 setLocation(30, 30);
151 setVisible(true);
152 }
153
154
155 protected JPanel makeMainPanel() {
156 JPanel result = new JPanel(new GridBagLayout());
157
158 result.setBorder(BorderFactory.createTitledBorder("Fsgrep Application Settings"));
159
160 GridBagConstraints gbc = new GridBagConstraints();
161 gbc.anchor = GridBagConstraints.NORTH;
162 gbc.ipadx = 4;
163 gbc.ipady = 4;
164 gbc.insets = new Insets(2, 4, 2, 4);
165 gbc.fill = GridBagConstraints.HORIZONTAL;
166 gbc.gridx = 0;
167 gbc.gridy = 0;
168 gbc.weightx = 0;
169 gbc.weighty = 0;
170
171 result.add(new JLabel("External Editor:"), gbc);
172
173 gbc.gridx = 1;
174 gbc.weightx = 1;
175 gbc.fill = GridBagConstraints.HORIZONTAL;
176 result.add(getFieldEditor(), gbc);
177
178 gbc.gridx = 2;
179 gbc.weightx = 0;
180 gbc.fill = GridBagConstraints.NONE;
181 result.add(getButtonInfoEditor(), gbc);
182
183 gbc.gridy = 1;
184 gbc.gridx = 0;
185 result.add(new JLabel("Auto-Rescan:"), gbc);
186
187 gbc.gridx = 1;
188 gbc.weightx = 1;
189 gbc.fill = GridBagConstraints.HORIZONTAL;
190 result.add(getSelectAutoRescan(), gbc);
191
192 gbc.gridx = 2;
193 gbc.weightx = 0;
194 gbc.fill = GridBagConstraints.NONE;
195 result.add(getButtonInfoRescan(), gbc);
196
197 return result;
198 }
199
200
201 protected JPanel makeButtonPanel() {
202 JPanel result = new JPanel(new BorderLayout());
203
204 ArrayList<JButton> buttons = new ArrayList<JButton>();
205 {
206 buttons.add(getButtonOk());
207 buttons.add(getButtonCancel());
208 }
209
210 result.add(App.wrapButtons(buttons), BorderLayout.EAST);
211
212 return result;
213 }
214
215
216 protected void handleSave() {
217 pdata.setProperty(ExternalEditor.PERSISTENT_MAP_KEY, getFieldEditor().getText());
218 ExternalEditor.reset();
219
220 pdata.setProperty(KEY_RESCAN, getSelectAutoRescan().getSelectedString());
221
222 pdata.store();
223 dispose();
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 }