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.*;
24 import org.altusmetrum.altoslib_1.*;
25 import org.altusmetrum.altosuilib_1.*;
27 class AltosEditFreqUI extends AltosUIDialog implements ActionListener {
30 JTextField description;
31 JButton ok_button, cancel_button;
34 public void actionPerformed(ActionEvent e) {
35 String cmd = e.getActionCommand();
37 if ("ok".equals(cmd)) {
41 if ("cancel".equals(cmd)) {
47 public AltosFrequency get() {
51 String f_s = frequency.getText();
52 String d_s = description.getText();
55 double f_d = Double.parseDouble(f_s);
57 return new AltosFrequency(f_d, d_s);
58 } catch (NumberFormatException ne) {
63 public AltosEditFreqUI(Frame in_frame, AltosFrequency existing) {
64 super(in_frame, true);
69 Container pane = getContentPane();
70 pane.setLayout(new GridBagLayout());
72 GridBagConstraints c = new GridBagConstraints();
73 c.insets = new Insets (4,4,4,4);
75 c.fill = GridBagConstraints.NONE;
76 c.anchor = GridBagConstraints.WEST;
83 pane.add(new JLabel("Frequency"), c);
85 frequency = new JTextField(12);
86 c.fill = GridBagConstraints.NONE;
87 c.anchor = GridBagConstraints.WEST;
94 pane.add(frequency, c);
96 c.fill = GridBagConstraints.NONE;
97 c.anchor = GridBagConstraints.WEST;
104 pane.add(new JLabel("Description"), c);
106 description = new JTextField(12);
107 c.fill = GridBagConstraints.NONE;
108 c.anchor = GridBagConstraints.WEST;
115 pane.add(description, c);
117 ok_button = new JButton("OK");
118 ok_button.setActionCommand("ok");
119 ok_button.addActionListener(this);
120 c.fill = GridBagConstraints.NONE;
121 c.anchor = GridBagConstraints.WEST;
128 pane.add(ok_button, c);
130 cancel_button = new JButton("Cancel");
131 cancel_button.setActionCommand("cancel");
132 cancel_button.addActionListener(this);
133 c.fill = GridBagConstraints.NONE;
134 c.anchor = GridBagConstraints.WEST;
141 pane.add(cancel_button, c);
143 if (existing == null)
144 setTitle("Add New Frequency");
146 setTitle("Edit Existing Frequency");
147 frequency.setText(String.format("%7.3f", existing.frequency));
148 description.setText(existing.description);
150 getRootPane().setDefaultButton(ok_button);
153 setLocationRelativeTo(frame);
157 public AltosEditFreqUI(Frame in_frame) {
158 this(in_frame, (AltosFrequency) null);
162 public class AltosConfigFreqUI extends AltosUIDialog implements ActionListener {
165 LinkedList<ActionListener> listeners;
167 class FrequencyList extends JList {
168 DefaultListModel list_model;
170 public void add(AltosFrequency frequency) {
172 for (i = 0; i < list_model.size(); i++) {
173 AltosFrequency f = (AltosFrequency) list_model.get(i);
174 if (frequency.frequency == f.frequency)
176 if (frequency.frequency < f.frequency)
179 list_model.insertElementAt(frequency, i);
182 public void remove(AltosFrequency frequency) {
183 list_model.removeElement(frequency);
186 //Subclass JList to workaround bug 4832765, which can cause the
187 //scroll pane to not let the user easily scroll up to the beginning
188 //of the list. An alternative would be to set the unitIncrement
189 //of the JScrollBar to a fixed value. You wouldn't get the nice
190 //aligned scrolling, but it should work.
191 public int getScrollableUnitIncrement(Rectangle visibleRect,
195 if (orientation == SwingConstants.VERTICAL &&
196 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
197 Rectangle r = getCellBounds(row, row);
198 if ((r.y == visibleRect.y) && (row != 0)) {
199 Point loc = r.getLocation();
201 int prevIndex = locationToIndex(loc);
202 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
204 if (prevR == null || prevR.y >= r.y) {
210 return super.getScrollableUnitIncrement(
211 visibleRect, orientation, direction);
214 public AltosFrequency selected() {
215 AltosFrequency f = (AltosFrequency) getSelectedValue();
219 public AltosFrequency[] frequencies() {
220 AltosFrequency[] ret;
222 ret = new AltosFrequency[list_model.size()];
223 for (int i = 0; i < list_model.size(); i++)
224 ret[i] = (AltosFrequency) list_model.get(i);
228 public FrequencyList(AltosFrequency[] in_frequencies) {
229 list_model = new DefaultListModel();
230 setModel(list_model);
231 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
232 setLayoutOrientation(JList.HORIZONTAL_WRAP);
233 for (int i = 0; i < in_frequencies.length; i++) {
234 add(in_frequencies[i]);
236 setVisibleRowCount(in_frequencies.length);
240 FrequencyList frequencies;
242 void save_frequencies() {
243 AltosUIPreferences.set_common_frequencies(frequencies.frequencies());
246 JButton add, edit, remove;
250 public void actionPerformed(ActionEvent e) {
251 String cmd = e.getActionCommand();
253 if ("ok".equals(cmd)) {
256 } else if ("cancel".equals(cmd)) {
258 } else if ("add".equals(cmd)) {
259 AltosEditFreqUI ui = new AltosEditFreqUI(frame);
261 AltosFrequency f = ui.get();
264 } else if ("edit".equals(cmd)) {
265 AltosFrequency old_f = frequencies.selected();
268 AltosEditFreqUI ui = new AltosEditFreqUI(frame, old_f);
270 AltosFrequency new_f = ui.get();
273 frequencies.remove(old_f);
274 frequencies.add(new_f);
276 } else if ("remove".equals(cmd)) {
277 AltosFrequency old_f = frequencies.selected();
280 int ret = JOptionPane.showConfirmDialog(this,
281 String.format("Remove frequency \"%s\"?",
282 old_f.toShortString()),
284 JOptionPane.YES_NO_OPTION);
285 if (ret == JOptionPane.YES_OPTION)
286 frequencies.remove(old_f);
290 public AltosFrequency[] frequencies() {
291 return frequencies.frequencies();
294 public AltosConfigFreqUI(Frame in_frame,
295 AltosFrequency[] in_frequencies) {
296 super(in_frame, "Manage Frequencies", true);
300 listeners = new LinkedList<ActionListener>();
302 frequencies = new FrequencyList(in_frequencies);
304 Container pane = getContentPane();
305 pane.setLayout(new GridBagLayout());
307 GridBagConstraints c = new GridBagConstraints();
308 c.insets = new Insets(4,4,4,4);
311 * Frequencies label and list
313 c.fill = GridBagConstraints.NONE;
314 c.anchor = GridBagConstraints.WEST;
321 pane.add(new JLabel("Frequencies"), c);
323 JScrollPane list_scroller = new JScrollPane(frequencies);
324 list_scroller.setAlignmentX(LEFT_ALIGNMENT);
325 c.fill = GridBagConstraints.BOTH;
326 c.anchor = GridBagConstraints.WEST;
333 pane.add(list_scroller, c);
335 add = new JButton("Add");
336 add.setActionCommand("add");
337 add.addActionListener(this);
338 c.fill = GridBagConstraints.NONE;
339 c.anchor = GridBagConstraints.CENTER;
348 edit = new JButton("Edit");
349 edit.setActionCommand("edit");
350 edit.addActionListener(this);
351 c.fill = GridBagConstraints.NONE;
352 c.anchor = GridBagConstraints.CENTER;
361 remove = new JButton("Remove");
362 remove.setActionCommand("remove");
363 remove.addActionListener(this);
364 c.fill = GridBagConstraints.NONE;
365 c.anchor = GridBagConstraints.CENTER;
374 ok = new JButton("OK");
375 ok.setActionCommand("ok");
376 ok.addActionListener(this);
377 c.fill = GridBagConstraints.NONE;
378 c.anchor = GridBagConstraints.CENTER;
387 cancel = new JButton("Cancel");
388 cancel.setActionCommand("cancel");
389 cancel.addActionListener(this);
390 c.fill = GridBagConstraints.NONE;
391 c.anchor = GridBagConstraints.CENTER;
401 setLocationRelativeTo(frame);
404 public static void show(Component frameComp) {
405 Frame frame = JOptionPane.getFrameForComponent(frameComp);
406 AltosConfigFreqUI dialog;
408 dialog = new AltosConfigFreqUI(frame, AltosUIPreferences.common_frequencies());
409 dialog.setVisible(true);