2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import javax.swing.plaf.basic.*;
30 import java.util.prefs.*;
31 import java.util.concurrent.*;
33 class AltosEditFreqUI extends AltosDialog implements ActionListener {
36 JTextField description;
37 JButton ok_button, cancel_button;
40 public void actionPerformed(ActionEvent e) {
41 String cmd = e.getActionCommand();
43 if ("ok".equals(cmd)) {
47 if ("cancel".equals(cmd)) {
53 public AltosFrequency get() {
57 String f_s = frequency.getText();
58 String d_s = description.getText();
61 double f_d = Double.parseDouble(f_s);
63 return new AltosFrequency(f_d, d_s);
64 } catch (NumberFormatException ne) {
69 public AltosEditFreqUI(Frame in_frame, AltosFrequency existing) {
70 super(in_frame, true);
75 Container pane = getContentPane();
76 pane.setLayout(new GridBagLayout());
78 GridBagConstraints c = new GridBagConstraints();
79 c.insets = new Insets (4,4,4,4);
81 c.fill = GridBagConstraints.NONE;
82 c.anchor = GridBagConstraints.WEST;
89 pane.add(new JLabel("Frequency"), c);
91 frequency = new JTextField(12);
92 c.fill = GridBagConstraints.NONE;
93 c.anchor = GridBagConstraints.WEST;
100 pane.add(frequency, c);
102 c.fill = GridBagConstraints.NONE;
103 c.anchor = GridBagConstraints.WEST;
110 pane.add(new JLabel("Description"), c);
112 description = new JTextField(12);
113 c.fill = GridBagConstraints.NONE;
114 c.anchor = GridBagConstraints.WEST;
121 pane.add(description, c);
123 ok_button = new JButton("OK");
124 ok_button.setActionCommand("ok");
125 ok_button.addActionListener(this);
126 c.fill = GridBagConstraints.NONE;
127 c.anchor = GridBagConstraints.WEST;
134 pane.add(ok_button, c);
136 cancel_button = new JButton("Cancel");
137 cancel_button.setActionCommand("cancel");
138 cancel_button.addActionListener(this);
139 c.fill = GridBagConstraints.NONE;
140 c.anchor = GridBagConstraints.WEST;
147 pane.add(cancel_button, c);
149 if (existing == null)
150 setTitle("Add New Frequency");
152 setTitle("Edit Existing Frequency");
153 frequency.setText(String.format("%7.3f", existing.frequency));
154 description.setText(existing.description);
156 getRootPane().setDefaultButton(ok_button);
159 setLocationRelativeTo(frame);
163 public AltosEditFreqUI(Frame in_frame) {
164 this(in_frame, (AltosFrequency) null);
168 public class AltosConfigFreqUI extends AltosDialog implements ActionListener {
171 LinkedList<ActionListener> listeners;
173 class FrequencyList extends JList {
174 DefaultListModel list_model;
176 public void add(AltosFrequency frequency) {
178 for (i = 0; i < list_model.size(); i++) {
179 AltosFrequency f = (AltosFrequency) list_model.get(i);
180 if (frequency.frequency == f.frequency)
182 if (frequency.frequency < f.frequency)
185 list_model.insertElementAt(frequency, i);
188 public void remove(AltosFrequency frequency) {
189 list_model.removeElement(frequency);
192 //Subclass JList to workaround bug 4832765, which can cause the
193 //scroll pane to not let the user easily scroll up to the beginning
194 //of the list. An alternative would be to set the unitIncrement
195 //of the JScrollBar to a fixed value. You wouldn't get the nice
196 //aligned scrolling, but it should work.
197 public int getScrollableUnitIncrement(Rectangle visibleRect,
201 if (orientation == SwingConstants.VERTICAL &&
202 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
203 Rectangle r = getCellBounds(row, row);
204 if ((r.y == visibleRect.y) && (row != 0)) {
205 Point loc = r.getLocation();
207 int prevIndex = locationToIndex(loc);
208 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
210 if (prevR == null || prevR.y >= r.y) {
216 return super.getScrollableUnitIncrement(
217 visibleRect, orientation, direction);
220 public AltosFrequency selected() {
221 AltosFrequency f = (AltosFrequency) getSelectedValue();
225 public AltosFrequency[] frequencies() {
226 AltosFrequency[] ret;
228 ret = new AltosFrequency[list_model.size()];
229 for (int i = 0; i < list_model.size(); i++)
230 ret[i] = (AltosFrequency) list_model.get(i);
234 public FrequencyList(AltosFrequency[] in_frequencies) {
235 list_model = new DefaultListModel();
236 setModel(list_model);
237 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
238 setLayoutOrientation(JList.HORIZONTAL_WRAP);
239 for (int i = 0; i < in_frequencies.length; i++) {
240 add(in_frequencies[i]);
242 setVisibleRowCount(in_frequencies.length);
246 FrequencyList frequencies;
248 void save_frequencies() {
249 AltosPreferences.set_common_frequencies(frequencies.frequencies());
252 JButton add, edit, remove;
256 public void actionPerformed(ActionEvent e) {
257 String cmd = e.getActionCommand();
259 if ("ok".equals(cmd)) {
262 } else if ("cancel".equals(cmd)) {
264 } else if ("add".equals(cmd)) {
265 AltosEditFreqUI ui = new AltosEditFreqUI(frame);
267 AltosFrequency f = ui.get();
270 } else if ("edit".equals(cmd)) {
271 AltosFrequency old_f = frequencies.selected();
274 AltosEditFreqUI ui = new AltosEditFreqUI(frame, old_f);
276 AltosFrequency new_f = ui.get();
279 frequencies.remove(old_f);
280 frequencies.add(new_f);
282 } else if ("remove".equals(cmd)) {
283 AltosFrequency old_f = frequencies.selected();
286 int ret = JOptionPane.showConfirmDialog(this,
287 String.format("Remove frequency \"%s\"?",
288 old_f.toShortString()),
290 JOptionPane.YES_NO_OPTION);
291 if (ret == JOptionPane.YES_OPTION)
292 frequencies.remove(old_f);
296 public AltosFrequency[] frequencies() {
297 return frequencies.frequencies();
300 public AltosConfigFreqUI(Frame in_frame,
301 AltosFrequency[] in_frequencies) {
302 super(in_frame, "Manage Frequencies", true);
306 listeners = new LinkedList<ActionListener>();
308 frequencies = new FrequencyList(in_frequencies);
310 Container pane = getContentPane();
311 pane.setLayout(new GridBagLayout());
313 GridBagConstraints c = new GridBagConstraints();
314 c.insets = new Insets(4,4,4,4);
317 * Frequencies label and list
319 c.fill = GridBagConstraints.NONE;
320 c.anchor = GridBagConstraints.WEST;
327 pane.add(new JLabel("Frequencies"), c);
329 JScrollPane list_scroller = new JScrollPane(frequencies);
330 list_scroller.setAlignmentX(LEFT_ALIGNMENT);
331 c.fill = GridBagConstraints.BOTH;
332 c.anchor = GridBagConstraints.WEST;
339 pane.add(list_scroller, c);
341 add = new JButton("Add");
342 add.setActionCommand("add");
343 add.addActionListener(this);
344 c.fill = GridBagConstraints.NONE;
345 c.anchor = GridBagConstraints.CENTER;
354 edit = new JButton("Edit");
355 edit.setActionCommand("edit");
356 edit.addActionListener(this);
357 c.fill = GridBagConstraints.NONE;
358 c.anchor = GridBagConstraints.CENTER;
367 remove = new JButton("Remove");
368 remove.setActionCommand("remove");
369 remove.addActionListener(this);
370 c.fill = GridBagConstraints.NONE;
371 c.anchor = GridBagConstraints.CENTER;
380 ok = new JButton("OK");
381 ok.setActionCommand("ok");
382 ok.addActionListener(this);
383 c.fill = GridBagConstraints.NONE;
384 c.anchor = GridBagConstraints.CENTER;
393 cancel = new JButton("Cancel");
394 cancel.setActionCommand("cancel");
395 cancel.addActionListener(this);
396 c.fill = GridBagConstraints.NONE;
397 c.anchor = GridBagConstraints.CENTER;
407 setLocationRelativeTo(frame);
410 public static void show(Component frameComp) {
411 Frame frame = JOptionPane.getFrameForComponent(frameComp);
412 AltosConfigFreqUI dialog;
414 dialog = new AltosConfigFreqUI(frame, AltosPreferences.common_frequencies());
415 dialog.setVisible(true);