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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_11;
23 import java.awt.event.*;
26 import org.altusmetrum.altoslib_11.*;
28 class AltosEditFreqUI extends AltosUIDialog implements ActionListener {
31 JTextField description;
32 JButton ok_button, cancel_button;
35 public void actionPerformed(ActionEvent e) {
36 String cmd = e.getActionCommand();
38 if ("ok".equals(cmd)) {
42 if ("cancel".equals(cmd)) {
48 public AltosFrequency get() {
52 String f_s = frequency.getText();
53 String d_s = description.getText();
56 double f_d = AltosParse.parse_double_locale(f_s);
58 return new AltosFrequency(f_d, d_s);
59 } catch (ParseException ne) {
64 public AltosEditFreqUI(Frame in_frame, AltosFrequency existing) {
65 super(in_frame, true);
70 Container pane = getContentPane();
71 pane.setLayout(new GridBagLayout());
73 GridBagConstraints c = new GridBagConstraints();
74 c.insets = new Insets (4,4,4,4);
76 c.fill = GridBagConstraints.NONE;
77 c.anchor = GridBagConstraints.WEST;
84 pane.add(new JLabel("Frequency"), c);
86 frequency = new JTextField(12);
87 c.fill = GridBagConstraints.NONE;
88 c.anchor = GridBagConstraints.WEST;
95 pane.add(frequency, c);
97 c.fill = GridBagConstraints.NONE;
98 c.anchor = GridBagConstraints.WEST;
105 pane.add(new JLabel("Description"), c);
107 description = new JTextField(12);
108 c.fill = GridBagConstraints.NONE;
109 c.anchor = GridBagConstraints.WEST;
116 pane.add(description, c);
118 ok_button = new JButton("OK");
119 ok_button.setActionCommand("ok");
120 ok_button.addActionListener(this);
121 c.fill = GridBagConstraints.NONE;
122 c.anchor = GridBagConstraints.WEST;
129 pane.add(ok_button, c);
131 cancel_button = new JButton("Cancel");
132 cancel_button.setActionCommand("cancel");
133 cancel_button.addActionListener(this);
134 c.fill = GridBagConstraints.NONE;
135 c.anchor = GridBagConstraints.WEST;
142 pane.add(cancel_button, c);
144 if (existing == null)
145 setTitle("Add New Frequency");
147 setTitle("Edit Existing Frequency");
148 frequency.setText(String.format("%7.3f", existing.frequency));
149 description.setText(existing.description);
151 getRootPane().setDefaultButton(ok_button);
154 setLocationRelativeTo(frame);
158 public AltosEditFreqUI(Frame in_frame) {
159 this(in_frame, (AltosFrequency) null);
163 public class AltosConfigFreqUI extends AltosUIDialog implements ActionListener {
166 LinkedList<ActionListener> listeners;
168 class FrequencyList extends JList<AltosFrequency> {
169 DefaultListModel<AltosFrequency> list_model;
171 public void add(AltosFrequency frequency) {
173 for (i = 0; i < list_model.size(); i++) {
174 AltosFrequency f = (AltosFrequency) list_model.get(i);
175 if (frequency.frequency == f.frequency)
177 if (frequency.frequency < f.frequency)
180 list_model.insertElementAt(frequency, i);
183 public void remove(AltosFrequency frequency) {
184 list_model.removeElement(frequency);
187 //Subclass JList to workaround bug 4832765, which can cause the
188 //scroll pane to not let the user easily scroll up to the beginning
189 //of the list. An alternative would be to set the unitIncrement
190 //of the JScrollBar to a fixed value. You wouldn't get the nice
191 //aligned scrolling, but it should work.
192 public int getScrollableUnitIncrement(Rectangle visibleRect,
196 if (orientation == SwingConstants.VERTICAL &&
197 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
198 Rectangle r = getCellBounds(row, row);
199 if ((r.y == visibleRect.y) && (row != 0)) {
200 Point loc = r.getLocation();
202 int prevIndex = locationToIndex(loc);
203 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
205 if (prevR == null || prevR.y >= r.y) {
211 return super.getScrollableUnitIncrement(
212 visibleRect, orientation, direction);
215 public AltosFrequency selected() {
216 AltosFrequency f = (AltosFrequency) getSelectedValue();
220 public AltosFrequency[] frequencies() {
221 AltosFrequency[] ret;
223 ret = new AltosFrequency[list_model.size()];
224 for (int i = 0; i < list_model.size(); i++)
225 ret[i] = (AltosFrequency) list_model.get(i);
229 public FrequencyList(AltosFrequency[] in_frequencies) {
230 list_model = new DefaultListModel<AltosFrequency>();
231 setModel(list_model);
232 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
233 setLayoutOrientation(JList.HORIZONTAL_WRAP);
234 for (int i = 0; i < in_frequencies.length; i++) {
235 add(in_frequencies[i]);
237 setVisibleRowCount(in_frequencies.length);
241 FrequencyList frequencies;
243 void save_frequencies() {
244 AltosUIPreferences.set_common_frequencies(frequencies.frequencies());
247 JButton add, edit, remove;
251 public void actionPerformed(ActionEvent e) {
252 String cmd = e.getActionCommand();
254 if ("ok".equals(cmd)) {
257 } else if ("cancel".equals(cmd)) {
259 } else if ("add".equals(cmd)) {
260 AltosEditFreqUI ui = new AltosEditFreqUI(frame);
262 AltosFrequency f = ui.get();
265 } else if ("edit".equals(cmd)) {
266 AltosFrequency old_f = frequencies.selected();
269 AltosEditFreqUI ui = new AltosEditFreqUI(frame, old_f);
271 AltosFrequency new_f = ui.get();
274 frequencies.remove(old_f);
275 frequencies.add(new_f);
277 } else if ("remove".equals(cmd)) {
278 AltosFrequency old_f = frequencies.selected();
281 int ret = JOptionPane.showConfirmDialog(this,
282 String.format("Remove frequency \"%s\"?",
283 old_f.toShortString()),
285 JOptionPane.YES_NO_OPTION);
286 if (ret == JOptionPane.YES_OPTION)
287 frequencies.remove(old_f);
291 public AltosFrequency[] frequencies() {
292 return frequencies.frequencies();
295 public AltosConfigFreqUI(Frame in_frame,
296 AltosFrequency[] in_frequencies) {
297 super(in_frame, "Manage Frequencies", true);
301 listeners = new LinkedList<ActionListener>();
303 frequencies = new FrequencyList(in_frequencies);
305 Container pane = getContentPane();
306 pane.setLayout(new GridBagLayout());
308 GridBagConstraints c = new GridBagConstraints();
309 c.insets = new Insets(4,4,4,4);
312 * Frequencies label and list
314 c.fill = GridBagConstraints.NONE;
315 c.anchor = GridBagConstraints.WEST;
322 pane.add(new JLabel("Frequencies"), c);
324 JScrollPane list_scroller = new JScrollPane(frequencies);
325 list_scroller.setAlignmentX(LEFT_ALIGNMENT);
326 c.fill = GridBagConstraints.BOTH;
327 c.anchor = GridBagConstraints.WEST;
334 pane.add(list_scroller, c);
336 add = new JButton("Add");
337 add.setActionCommand("add");
338 add.addActionListener(this);
339 c.fill = GridBagConstraints.NONE;
340 c.anchor = GridBagConstraints.CENTER;
349 edit = new JButton("Edit");
350 edit.setActionCommand("edit");
351 edit.addActionListener(this);
352 c.fill = GridBagConstraints.NONE;
353 c.anchor = GridBagConstraints.CENTER;
362 remove = new JButton("Remove");
363 remove.setActionCommand("remove");
364 remove.addActionListener(this);
365 c.fill = GridBagConstraints.NONE;
366 c.anchor = GridBagConstraints.CENTER;
375 ok = new JButton("OK");
376 ok.setActionCommand("ok");
377 ok.addActionListener(this);
378 c.fill = GridBagConstraints.NONE;
379 c.anchor = GridBagConstraints.CENTER;
388 cancel = new JButton("Cancel");
389 cancel.setActionCommand("cancel");
390 cancel.addActionListener(this);
391 c.fill = GridBagConstraints.NONE;
392 c.anchor = GridBagConstraints.CENTER;
402 setLocationRelativeTo(frame);
405 public static void show(Component frameComp) {
406 Frame frame = JOptionPane.getFrameForComponent(frameComp);
407 AltosConfigFreqUI dialog;
409 dialog = new AltosConfigFreqUI(frame, AltosUIPreferences.common_frequencies());
410 dialog.setVisible(true);