]> git.gag.com Git - fw/altos/blob - altosui/AltosConfigFreqUI.java
altosdroid: remove old commented code
[fw/altos] / altosui / AltosConfigFreqUI.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import javax.swing.plaf.basic.*;
27 import java.io.*;
28 import java.util.*;
29 import java.text.*;
30 import java.util.prefs.*;
31 import java.util.concurrent.*;
32 import org.altusmetrum.AltosLib.*;
33
34 class AltosEditFreqUI extends AltosDialog implements ActionListener {
35         Frame           frame;
36         JTextField      frequency;
37         JTextField      description;
38         JButton         ok_button, cancel_button;
39         boolean         got_ok;
40
41         public void actionPerformed(ActionEvent e) {
42                 String  cmd = e.getActionCommand();
43
44                 if ("ok".equals(cmd)) {
45                         got_ok = true;
46                         setVisible(false);
47                 }
48                 if ("cancel".equals(cmd)) {
49                         got_ok = false;
50                         setVisible(false);
51                 }
52         }
53
54         public AltosFrequency get() {
55                 if (!got_ok)
56                         return null;
57
58                 String  f_s = frequency.getText();
59                 String  d_s = description.getText();
60
61                 try {
62                         double  f_d = Double.parseDouble(f_s);
63
64                         return new AltosFrequency(f_d, d_s);
65                 } catch (NumberFormatException ne) {
66                 }
67                 return null;
68         }
69
70         public AltosEditFreqUI(Frame in_frame, AltosFrequency existing) {
71                 super(in_frame, true);
72
73                 got_ok = false;
74                 frame = in_frame;
75
76                 Container pane = getContentPane();
77                 pane.setLayout(new GridBagLayout());
78
79                 GridBagConstraints c = new GridBagConstraints();
80                 c.insets = new Insets (4,4,4,4);
81                 
82                 c.fill = GridBagConstraints.NONE;
83                 c.anchor = GridBagConstraints.WEST;
84                 c.gridx = 0;
85                 c.gridy = 0;
86                 c.gridwidth = 1;
87                 c.gridheight = 1;
88                 c.weightx = 0;
89                 c.weighty = 0;
90                 pane.add(new JLabel("Frequency"), c);
91
92                 frequency = new JTextField(12);
93                 c.fill = GridBagConstraints.NONE;
94                 c.anchor = GridBagConstraints.WEST;
95                 c.gridx = 1;
96                 c.gridy = 0;
97                 c.gridwidth = 1;
98                 c.gridheight = 1;
99                 c.weightx = 0;
100                 c.weighty = 0;
101                 pane.add(frequency, c);
102
103                 c.fill = GridBagConstraints.NONE;
104                 c.anchor = GridBagConstraints.WEST;
105                 c.gridx = 0;
106                 c.gridy = 1;
107                 c.gridwidth = 1;
108                 c.gridheight = 1;
109                 c.weightx = 0;
110                 c.weighty = 0;
111                 pane.add(new JLabel("Description"), c);
112
113                 description = new JTextField(12);
114                 c.fill = GridBagConstraints.NONE;
115                 c.anchor = GridBagConstraints.WEST;
116                 c.gridx = 1;
117                 c.gridy = 1;
118                 c.gridwidth = 1;
119                 c.gridheight = 1;
120                 c.weightx = 0;
121                 c.weighty = 0;
122                 pane.add(description, c);
123
124                 ok_button = new JButton("OK");
125                 ok_button.setActionCommand("ok");
126                 ok_button.addActionListener(this);
127                 c.fill = GridBagConstraints.NONE;
128                 c.anchor = GridBagConstraints.WEST;
129                 c.gridx = 0;
130                 c.gridy = 2;
131                 c.gridwidth = 1;
132                 c.gridheight = 1;
133                 c.weightx = 0;
134                 c.weighty = 0;
135                 pane.add(ok_button, c);
136                 
137                 cancel_button = new JButton("Cancel");
138                 cancel_button.setActionCommand("cancel");
139                 cancel_button.addActionListener(this);
140                 c.fill = GridBagConstraints.NONE;
141                 c.anchor = GridBagConstraints.WEST;
142                 c.gridx = 1;
143                 c.gridy = 2;
144                 c.gridwidth = 1;
145                 c.gridheight = 1;
146                 c.weightx = 0;
147                 c.weighty = 0;
148                 pane.add(cancel_button, c);
149                 
150                 if (existing == null)
151                         setTitle("Add New Frequency");
152                 else {
153                         setTitle("Edit Existing Frequency");
154                         frequency.setText(String.format("%7.3f", existing.frequency));
155                         description.setText(existing.description);
156                 }
157                 getRootPane().setDefaultButton(ok_button);
158
159                 pack();
160                 setLocationRelativeTo(frame);
161                 
162         }
163
164         public AltosEditFreqUI(Frame in_frame) {
165                 this(in_frame, (AltosFrequency) null);
166         }
167 }
168
169 public class AltosConfigFreqUI extends AltosDialog implements ActionListener {
170
171         Frame frame;
172         LinkedList<ActionListener> listeners;
173
174         class FrequencyList extends JList {
175                 DefaultListModel list_model;
176
177                 public void add(AltosFrequency frequency) {
178                         int i;
179                         for (i = 0; i < list_model.size(); i++) {
180                                 AltosFrequency  f = (AltosFrequency) list_model.get(i);
181                                 if (frequency.frequency == f.frequency)
182                                         return;
183                                 if (frequency.frequency < f.frequency)
184                                         break;
185                         }
186                         list_model.insertElementAt(frequency, i);
187                 }
188
189                 public void remove(AltosFrequency frequency) {
190                         list_model.removeElement(frequency);
191                 }
192
193                 //Subclass JList to workaround bug 4832765, which can cause the
194                 //scroll pane to not let the user easily scroll up to the beginning
195                 //of the list.  An alternative would be to set the unitIncrement
196                 //of the JScrollBar to a fixed value. You wouldn't get the nice
197                 //aligned scrolling, but it should work.
198                 public int getScrollableUnitIncrement(Rectangle visibleRect,
199                                                       int orientation,
200                                                       int direction) {
201                         int row;
202                         if (orientation == SwingConstants.VERTICAL &&
203                             direction < 0 && (row = getFirstVisibleIndex()) != -1) {
204                                 Rectangle r = getCellBounds(row, row);
205                                 if ((r.y == visibleRect.y) && (row != 0))  {
206                                         Point loc = r.getLocation();
207                                         loc.y--;
208                                         int prevIndex = locationToIndex(loc);
209                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
210
211                                         if (prevR == null || prevR.y >= r.y) {
212                                                 return 0;
213                                         }
214                                         return prevR.height;
215                                 }
216                         }
217                         return super.getScrollableUnitIncrement(
218                                 visibleRect, orientation, direction);
219                 }
220
221                 public AltosFrequency selected() {
222                         AltosFrequency  f = (AltosFrequency) getSelectedValue();
223                         return f;
224                 }
225
226                 public AltosFrequency[] frequencies() {
227                         AltosFrequency[]        ret;
228
229                         ret = new AltosFrequency[list_model.size()];
230                         for (int i = 0; i < list_model.size(); i++)
231                                 ret[i] = (AltosFrequency) list_model.get(i);
232                         return ret;
233                 }
234
235                 public FrequencyList(AltosFrequency[] in_frequencies) {
236                         list_model = new DefaultListModel();
237                         setModel(list_model);
238                         setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
239                         setLayoutOrientation(JList.HORIZONTAL_WRAP);
240                         for (int i = 0; i < in_frequencies.length; i++) {
241                                 add(in_frequencies[i]);
242                         }
243                         setVisibleRowCount(in_frequencies.length);
244                 }
245         }
246
247         FrequencyList   frequencies;
248
249         void save_frequencies() {
250                 AltosUIPreferences.set_common_frequencies(frequencies.frequencies());
251         }
252
253         JButton add, edit, remove;
254
255         JButton cancel, ok;
256
257         public void actionPerformed(ActionEvent e) {
258                 String  cmd = e.getActionCommand();
259
260                 if ("ok".equals(cmd)) {
261                         save_frequencies();
262                         setVisible(false);
263                 } else if ("cancel".equals(cmd)) {
264                         setVisible(false);
265                 } else if ("add".equals(cmd)) {
266                         AltosEditFreqUI ui = new AltosEditFreqUI(frame);
267                         ui.setVisible(true);
268                         AltosFrequency  f = ui.get();
269                         if (f != null)
270                                 frequencies.add(f);
271                 } else if ("edit".equals(cmd)) {
272                         AltosFrequency  old_f = frequencies.selected();
273                         if (old_f == null)
274                                 return;
275                         AltosEditFreqUI ui = new AltosEditFreqUI(frame, old_f);
276                         ui.setVisible(true);
277                         AltosFrequency  new_f = ui.get();
278                         if (new_f != null) {
279                                 if (old_f != null)
280                                         frequencies.remove(old_f);
281                                 frequencies.add(new_f);
282                         }
283                 } else if ("remove".equals(cmd)) {
284                         AltosFrequency  old_f = frequencies.selected();
285                         if (old_f == null)
286                                 return;
287                         int ret = JOptionPane.showConfirmDialog(this,
288                                                                 String.format("Remove frequency \"%s\"?",
289                                                                               old_f.toShortString()),
290                                                                 "Remove Frequency",
291                                                                 JOptionPane.YES_NO_OPTION);
292                         if (ret == JOptionPane.YES_OPTION)
293                                 frequencies.remove(old_f);
294                 }
295         }
296
297         public AltosFrequency[] frequencies() {
298                 return frequencies.frequencies();
299         }
300         
301         public AltosConfigFreqUI(Frame in_frame,
302                                  AltosFrequency[] in_frequencies) {
303                 super(in_frame, "Manage Frequencies", true);
304
305                 frame = in_frame;
306
307                 listeners = new LinkedList<ActionListener>();
308
309                 frequencies = new FrequencyList(in_frequencies);
310
311                 Container pane = getContentPane();
312                 pane.setLayout(new GridBagLayout());
313
314                 GridBagConstraints c = new GridBagConstraints();
315                 c.insets = new Insets(4,4,4,4);
316
317                 /*
318                  * Frequencies label and list
319                  */
320                 c.fill = GridBagConstraints.NONE;
321                 c.anchor = GridBagConstraints.WEST;
322                 c.gridx = 0;
323                 c.gridy = 0;
324                 c.gridwidth = 1;
325                 c.gridheight = 1;
326                 c.weightx = 0;
327                 c.weighty = 0;
328                 pane.add(new JLabel("Frequencies"), c);
329
330                 JScrollPane list_scroller = new JScrollPane(frequencies);
331                 list_scroller.setAlignmentX(LEFT_ALIGNMENT);
332                 c.fill = GridBagConstraints.BOTH;
333                 c.anchor = GridBagConstraints.WEST;
334                 c.gridx = 0;
335                 c.gridy = 1;
336                 c.gridwidth = 6;
337                 c.gridheight = 2;
338                 c.weightx = 1;
339                 c.weighty = 1;
340                 pane.add(list_scroller, c);
341
342                 add = new JButton("Add");
343                 add.setActionCommand("add");
344                 add.addActionListener(this);
345                 c.fill = GridBagConstraints.NONE;
346                 c.anchor = GridBagConstraints.CENTER;
347                 c.gridx = 0;
348                 c.gridy = 3;
349                 c.gridwidth = 2;
350                 c.gridheight = 1;
351                 c.weightx = 0;
352                 c.weighty = 0;
353                 pane.add(add, c);
354
355                 edit = new JButton("Edit");
356                 edit.setActionCommand("edit");
357                 edit.addActionListener(this);
358                 c.fill = GridBagConstraints.NONE;
359                 c.anchor = GridBagConstraints.CENTER;
360                 c.gridx = 2;
361                 c.gridy = 3;
362                 c.gridwidth = 2;
363                 c.gridheight = 1;
364                 c.weightx = 0;
365                 c.weighty = 0;
366                 pane.add(edit, c);
367
368                 remove = new JButton("Remove");
369                 remove.setActionCommand("remove");
370                 remove.addActionListener(this);
371                 c.fill = GridBagConstraints.NONE;
372                 c.anchor = GridBagConstraints.CENTER;
373                 c.gridx = 4;
374                 c.gridy = 3;
375                 c.gridwidth = 2;
376                 c.gridheight = 1;
377                 c.weightx = 0;
378                 c.weighty = 0;
379                 pane.add(remove, c);
380
381                 ok = new JButton("OK");
382                 ok.setActionCommand("ok");
383                 ok.addActionListener(this);
384                 c.fill = GridBagConstraints.NONE;
385                 c.anchor = GridBagConstraints.CENTER;
386                 c.gridx = 0;
387                 c.gridy = 4;
388                 c.gridwidth = 3;
389                 c.gridheight = 1;
390                 c.weightx = 0;
391                 c.weighty = 0;
392                 pane.add(ok, c);
393
394                 cancel = new JButton("Cancel");
395                 cancel.setActionCommand("cancel");
396                 cancel.addActionListener(this);
397                 c.fill = GridBagConstraints.NONE;
398                 c.anchor = GridBagConstraints.CENTER;
399                 c.gridx = 3;
400                 c.gridy = 4;
401                 c.gridwidth = 3;
402                 c.gridheight = 1;
403                 c.weightx = 0;
404                 c.weighty = 0;
405                 pane.add(cancel, c);
406
407                 pack();
408                 setLocationRelativeTo(frame);
409         }
410
411         public static void show(Component frameComp) {
412                 Frame   frame = JOptionPane.getFrameForComponent(frameComp);
413                 AltosConfigFreqUI       dialog;
414
415                 dialog = new AltosConfigFreqUI(frame, AltosUIPreferences.common_frequencies());
416                 dialog.setVisible(true);
417         }
418
419 }