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.
18 package org.altusmetrum.altosuilib_6;
21 import java.awt.event.*;
24 import org.altusmetrum.altoslib_6.*;
26 class AltosEditFreqUI extends AltosUIDialog implements ActionListener {
29 JTextField description;
30 JButton ok_button, cancel_button;
33 public void actionPerformed(ActionEvent e) {
34 String cmd = e.getActionCommand();
36 if ("ok".equals(cmd)) {
40 if ("cancel".equals(cmd)) {
46 public AltosFrequency get() {
50 String f_s = frequency.getText();
51 String d_s = description.getText();
54 double f_d = Double.parseDouble(f_s);
56 return new AltosFrequency(f_d, d_s);
57 } catch (NumberFormatException ne) {
62 public AltosEditFreqUI(Frame in_frame, AltosFrequency existing) {
63 super(in_frame, true);
68 Container pane = getContentPane();
69 pane.setLayout(new GridBagLayout());
71 GridBagConstraints c = new GridBagConstraints();
72 c.insets = new Insets (4,4,4,4);
74 c.fill = GridBagConstraints.NONE;
75 c.anchor = GridBagConstraints.WEST;
82 pane.add(new JLabel("Frequency"), c);
84 frequency = new JTextField(12);
85 c.fill = GridBagConstraints.NONE;
86 c.anchor = GridBagConstraints.WEST;
93 pane.add(frequency, c);
95 c.fill = GridBagConstraints.NONE;
96 c.anchor = GridBagConstraints.WEST;
103 pane.add(new JLabel("Description"), c);
105 description = new JTextField(12);
106 c.fill = GridBagConstraints.NONE;
107 c.anchor = GridBagConstraints.WEST;
114 pane.add(description, c);
116 ok_button = new JButton("OK");
117 ok_button.setActionCommand("ok");
118 ok_button.addActionListener(this);
119 c.fill = GridBagConstraints.NONE;
120 c.anchor = GridBagConstraints.WEST;
127 pane.add(ok_button, c);
129 cancel_button = new JButton("Cancel");
130 cancel_button.setActionCommand("cancel");
131 cancel_button.addActionListener(this);
132 c.fill = GridBagConstraints.NONE;
133 c.anchor = GridBagConstraints.WEST;
140 pane.add(cancel_button, c);
142 if (existing == null)
143 setTitle("Add New Frequency");
145 setTitle("Edit Existing Frequency");
146 frequency.setText(String.format("%7.3f", existing.frequency));
147 description.setText(existing.description);
149 getRootPane().setDefaultButton(ok_button);
152 setLocationRelativeTo(frame);
156 public AltosEditFreqUI(Frame in_frame) {
157 this(in_frame, (AltosFrequency) null);
161 public class AltosConfigFreqUI extends AltosUIDialog implements ActionListener {
164 LinkedList<ActionListener> listeners;
166 class FrequencyList extends JList<AltosFrequency> {
167 DefaultListModel<AltosFrequency> list_model;
169 public void add(AltosFrequency frequency) {
171 for (i = 0; i < list_model.size(); i++) {
172 AltosFrequency f = (AltosFrequency) list_model.get(i);
173 if (frequency.frequency == f.frequency)
175 if (frequency.frequency < f.frequency)
178 list_model.insertElementAt(frequency, i);
181 public void remove(AltosFrequency frequency) {
182 list_model.removeElement(frequency);
185 //Subclass JList to workaround bug 4832765, which can cause the
186 //scroll pane to not let the user easily scroll up to the beginning
187 //of the list. An alternative would be to set the unitIncrement
188 //of the JScrollBar to a fixed value. You wouldn't get the nice
189 //aligned scrolling, but it should work.
190 public int getScrollableUnitIncrement(Rectangle visibleRect,
194 if (orientation == SwingConstants.VERTICAL &&
195 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
196 Rectangle r = getCellBounds(row, row);
197 if ((r.y == visibleRect.y) && (row != 0)) {
198 Point loc = r.getLocation();
200 int prevIndex = locationToIndex(loc);
201 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
203 if (prevR == null || prevR.y >= r.y) {
209 return super.getScrollableUnitIncrement(
210 visibleRect, orientation, direction);
213 public AltosFrequency selected() {
214 AltosFrequency f = (AltosFrequency) getSelectedValue();
218 public AltosFrequency[] frequencies() {
219 AltosFrequency[] ret;
221 ret = new AltosFrequency[list_model.size()];
222 for (int i = 0; i < list_model.size(); i++)
223 ret[i] = (AltosFrequency) list_model.get(i);
227 public FrequencyList(AltosFrequency[] in_frequencies) {
228 list_model = new DefaultListModel<AltosFrequency>();
229 setModel(list_model);
230 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
231 setLayoutOrientation(JList.HORIZONTAL_WRAP);
232 for (int i = 0; i < in_frequencies.length; i++) {
233 add(in_frequencies[i]);
235 setVisibleRowCount(in_frequencies.length);
239 FrequencyList frequencies;
241 void save_frequencies() {
242 AltosUIPreferences.set_common_frequencies(frequencies.frequencies());
245 JButton add, edit, remove;
249 public void actionPerformed(ActionEvent e) {
250 String cmd = e.getActionCommand();
252 if ("ok".equals(cmd)) {
255 } else if ("cancel".equals(cmd)) {
257 } else if ("add".equals(cmd)) {
258 AltosEditFreqUI ui = new AltosEditFreqUI(frame);
260 AltosFrequency f = ui.get();
263 } else if ("edit".equals(cmd)) {
264 AltosFrequency old_f = frequencies.selected();
267 AltosEditFreqUI ui = new AltosEditFreqUI(frame, old_f);
269 AltosFrequency new_f = ui.get();
272 frequencies.remove(old_f);
273 frequencies.add(new_f);
275 } else if ("remove".equals(cmd)) {
276 AltosFrequency old_f = frequencies.selected();
279 int ret = JOptionPane.showConfirmDialog(this,
280 String.format("Remove frequency \"%s\"?",
281 old_f.toShortString()),
283 JOptionPane.YES_NO_OPTION);
284 if (ret == JOptionPane.YES_OPTION)
285 frequencies.remove(old_f);
289 public AltosFrequency[] frequencies() {
290 return frequencies.frequencies();
293 public AltosConfigFreqUI(Frame in_frame,
294 AltosFrequency[] in_frequencies) {
295 super(in_frame, "Manage Frequencies", true);
299 listeners = new LinkedList<ActionListener>();
301 frequencies = new FrequencyList(in_frequencies);
303 Container pane = getContentPane();
304 pane.setLayout(new GridBagLayout());
306 GridBagConstraints c = new GridBagConstraints();
307 c.insets = new Insets(4,4,4,4);
310 * Frequencies label and list
312 c.fill = GridBagConstraints.NONE;
313 c.anchor = GridBagConstraints.WEST;
320 pane.add(new JLabel("Frequencies"), c);
322 JScrollPane list_scroller = new JScrollPane(frequencies);
323 list_scroller.setAlignmentX(LEFT_ALIGNMENT);
324 c.fill = GridBagConstraints.BOTH;
325 c.anchor = GridBagConstraints.WEST;
332 pane.add(list_scroller, c);
334 add = new JButton("Add");
335 add.setActionCommand("add");
336 add.addActionListener(this);
337 c.fill = GridBagConstraints.NONE;
338 c.anchor = GridBagConstraints.CENTER;
347 edit = new JButton("Edit");
348 edit.setActionCommand("edit");
349 edit.addActionListener(this);
350 c.fill = GridBagConstraints.NONE;
351 c.anchor = GridBagConstraints.CENTER;
360 remove = new JButton("Remove");
361 remove.setActionCommand("remove");
362 remove.addActionListener(this);
363 c.fill = GridBagConstraints.NONE;
364 c.anchor = GridBagConstraints.CENTER;
373 ok = new JButton("OK");
374 ok.setActionCommand("ok");
375 ok.addActionListener(this);
376 c.fill = GridBagConstraints.NONE;
377 c.anchor = GridBagConstraints.CENTER;
386 cancel = new JButton("Cancel");
387 cancel.setActionCommand("cancel");
388 cancel.addActionListener(this);
389 c.fill = GridBagConstraints.NONE;
390 c.anchor = GridBagConstraints.CENTER;
400 setLocationRelativeTo(frame);
403 public static void show(Component frameComp) {
404 Frame frame = JOptionPane.getFrameForComponent(frameComp);
405 AltosConfigFreqUI dialog;
407 dialog = new AltosConfigFreqUI(frame, AltosUIPreferences.common_frequencies());
408 dialog.setVisible(true);