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.*;
32 import org.altusmetrum.AltosLib.*;
34 class AltosEditFreqUI extends AltosDialog implements ActionListener {
37 JTextField description;
38 JButton ok_button, cancel_button;
41 public void actionPerformed(ActionEvent e) {
42 String cmd = e.getActionCommand();
44 if ("ok".equals(cmd)) {
48 if ("cancel".equals(cmd)) {
54 public AltosFrequency get() {
58 String f_s = frequency.getText();
59 String d_s = description.getText();
62 double f_d = Double.parseDouble(f_s);
64 return new AltosFrequency(f_d, d_s);
65 } catch (NumberFormatException ne) {
70 public AltosEditFreqUI(Frame in_frame, AltosFrequency existing) {
71 super(in_frame, true);
76 Container pane = getContentPane();
77 pane.setLayout(new GridBagLayout());
79 GridBagConstraints c = new GridBagConstraints();
80 c.insets = new Insets (4,4,4,4);
82 c.fill = GridBagConstraints.NONE;
83 c.anchor = GridBagConstraints.WEST;
90 pane.add(new JLabel("Frequency"), c);
92 frequency = new JTextField(12);
93 c.fill = GridBagConstraints.NONE;
94 c.anchor = GridBagConstraints.WEST;
101 pane.add(frequency, c);
103 c.fill = GridBagConstraints.NONE;
104 c.anchor = GridBagConstraints.WEST;
111 pane.add(new JLabel("Description"), c);
113 description = new JTextField(12);
114 c.fill = GridBagConstraints.NONE;
115 c.anchor = GridBagConstraints.WEST;
122 pane.add(description, c);
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;
135 pane.add(ok_button, c);
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;
148 pane.add(cancel_button, c);
150 if (existing == null)
151 setTitle("Add New Frequency");
153 setTitle("Edit Existing Frequency");
154 frequency.setText(String.format("%7.3f", existing.frequency));
155 description.setText(existing.description);
157 getRootPane().setDefaultButton(ok_button);
160 setLocationRelativeTo(frame);
164 public AltosEditFreqUI(Frame in_frame) {
165 this(in_frame, (AltosFrequency) null);
169 public class AltosConfigFreqUI extends AltosDialog implements ActionListener {
172 LinkedList<ActionListener> listeners;
174 class FrequencyList extends JList {
175 DefaultListModel list_model;
177 public void add(AltosFrequency frequency) {
179 for (i = 0; i < list_model.size(); i++) {
180 AltosFrequency f = (AltosFrequency) list_model.get(i);
181 if (frequency.frequency == f.frequency)
183 if (frequency.frequency < f.frequency)
186 list_model.insertElementAt(frequency, i);
189 public void remove(AltosFrequency frequency) {
190 list_model.removeElement(frequency);
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,
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();
208 int prevIndex = locationToIndex(loc);
209 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
211 if (prevR == null || prevR.y >= r.y) {
217 return super.getScrollableUnitIncrement(
218 visibleRect, orientation, direction);
221 public AltosFrequency selected() {
222 AltosFrequency f = (AltosFrequency) getSelectedValue();
226 public AltosFrequency[] frequencies() {
227 AltosFrequency[] ret;
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);
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]);
243 setVisibleRowCount(in_frequencies.length);
247 FrequencyList frequencies;
249 void save_frequencies() {
250 AltosUIPreferences.set_common_frequencies(frequencies.frequencies());
253 JButton add, edit, remove;
257 public void actionPerformed(ActionEvent e) {
258 String cmd = e.getActionCommand();
260 if ("ok".equals(cmd)) {
263 } else if ("cancel".equals(cmd)) {
265 } else if ("add".equals(cmd)) {
266 AltosEditFreqUI ui = new AltosEditFreqUI(frame);
268 AltosFrequency f = ui.get();
271 } else if ("edit".equals(cmd)) {
272 AltosFrequency old_f = frequencies.selected();
275 AltosEditFreqUI ui = new AltosEditFreqUI(frame, old_f);
277 AltosFrequency new_f = ui.get();
280 frequencies.remove(old_f);
281 frequencies.add(new_f);
283 } else if ("remove".equals(cmd)) {
284 AltosFrequency old_f = frequencies.selected();
287 int ret = JOptionPane.showConfirmDialog(this,
288 String.format("Remove frequency \"%s\"?",
289 old_f.toShortString()),
291 JOptionPane.YES_NO_OPTION);
292 if (ret == JOptionPane.YES_OPTION)
293 frequencies.remove(old_f);
297 public AltosFrequency[] frequencies() {
298 return frequencies.frequencies();
301 public AltosConfigFreqUI(Frame in_frame,
302 AltosFrequency[] in_frequencies) {
303 super(in_frame, "Manage Frequencies", true);
307 listeners = new LinkedList<ActionListener>();
309 frequencies = new FrequencyList(in_frequencies);
311 Container pane = getContentPane();
312 pane.setLayout(new GridBagLayout());
314 GridBagConstraints c = new GridBagConstraints();
315 c.insets = new Insets(4,4,4,4);
318 * Frequencies label and list
320 c.fill = GridBagConstraints.NONE;
321 c.anchor = GridBagConstraints.WEST;
328 pane.add(new JLabel("Frequencies"), c);
330 JScrollPane list_scroller = new JScrollPane(frequencies);
331 list_scroller.setAlignmentX(LEFT_ALIGNMENT);
332 c.fill = GridBagConstraints.BOTH;
333 c.anchor = GridBagConstraints.WEST;
340 pane.add(list_scroller, c);
342 add = new JButton("Add");
343 add.setActionCommand("add");
344 add.addActionListener(this);
345 c.fill = GridBagConstraints.NONE;
346 c.anchor = GridBagConstraints.CENTER;
355 edit = new JButton("Edit");
356 edit.setActionCommand("edit");
357 edit.addActionListener(this);
358 c.fill = GridBagConstraints.NONE;
359 c.anchor = GridBagConstraints.CENTER;
368 remove = new JButton("Remove");
369 remove.setActionCommand("remove");
370 remove.addActionListener(this);
371 c.fill = GridBagConstraints.NONE;
372 c.anchor = GridBagConstraints.CENTER;
381 ok = new JButton("OK");
382 ok.setActionCommand("ok");
383 ok.addActionListener(this);
384 c.fill = GridBagConstraints.NONE;
385 c.anchor = GridBagConstraints.CENTER;
394 cancel = new JButton("Cancel");
395 cancel.setActionCommand("cancel");
396 cancel.addActionListener(this);
397 c.fill = GridBagConstraints.NONE;
398 c.anchor = GridBagConstraints.CENTER;
408 setLocationRelativeTo(frame);
411 public static void show(Component frameComp) {
412 Frame frame = JOptionPane.getFrameForComponent(frameComp);
413 AltosConfigFreqUI dialog;
415 dialog = new AltosConfigFreqUI(frame, AltosUIPreferences.common_frequencies());
416 dialog.setVisible(true);