source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/projection/ListProjectionChoice.java@ 13182

Last change on this file since 13182 was 9990, checked in by Don-vip, 8 years ago

sonar - Inner classes which do not reference their owning classes should be static

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.projection;
3
4import java.awt.GridBagLayout;
5import java.awt.event.ActionListener;
6import java.util.Collection;
7import java.util.Collections;
8
9import javax.swing.JLabel;
10import javax.swing.JPanel;
11
12import org.openstreetmap.josm.gui.widgets.JosmComboBox;
13import org.openstreetmap.josm.tools.GBC;
14import org.openstreetmap.josm.tools.Utils;
15
16/**
17 * A projection choice, that offers a list of projections in a combo-box.
18 */
19public abstract class ListProjectionChoice extends AbstractProjectionChoice {
20
21 protected int index; // 0-based index
22 protected int defaultIndex;
23 protected String[] entries;
24 protected String label;
25
26 /**
27 * Constructs a new {@code ListProjectionChoice}.
28 *
29 * @param name the display name
30 * @param id the unique id for this ProjectionChoice
31 * @param entries the list of display entries for the combo-box
32 * @param label a label shown left to the combo-box
33 * @param defaultIndex the default index for the combo-box
34 */
35 public ListProjectionChoice(String name, String id, String[] entries, String label, int defaultIndex) {
36 super(name, id);
37 this.entries = Utils.copyArray(entries);
38 this.label = label;
39 this.defaultIndex = defaultIndex;
40 }
41
42 /**
43 * Constructs a new {@code ListProjectionChoice}.
44 * @param name the display name
45 * @param id the unique id for this ProjectionChoice
46 * @param entries the list of display entries for the combo-box
47 * @param label a label shown left to the combo-box
48 */
49 public ListProjectionChoice(String name, String id, String[] entries, String label) {
50 this(name, id, entries, label, 0);
51 }
52
53 /**
54 * Convert 0-based index to preference value.
55 * @param idx 0-based index
56 * @return preference value
57 * @see #zoneToIndex
58 */
59 protected abstract String indexToZone(int idx);
60
61 /**
62 * Convert preference value to 0-based index.
63 * @param zone preference value
64 * @return 0-based index
65 * @see #indexToZone
66 */
67 protected abstract int zoneToIndex(String zone);
68
69 @Override
70 public void setPreferences(Collection<String> args) {
71 String zone = null;
72 if (args != null && !args.isEmpty()) {
73 zone = args.iterator().next();
74 }
75 int idx;
76 if (zone == null) {
77 idx = defaultIndex;
78 } else {
79 idx = zoneToIndex(zone);
80 if (idx < 0 || idx >= entries.length) {
81 idx = defaultIndex;
82 }
83 }
84 this.index = idx;
85 }
86
87 protected static class CBPanel extends JPanel {
88 public JosmComboBox<String> prefcb;
89
90 public CBPanel(String[] entries, int initialIndex, String label, final ActionListener listener) {
91 prefcb = new JosmComboBox<>(entries);
92
93 prefcb.setSelectedIndex(initialIndex);
94 this.setLayout(new GridBagLayout());
95 this.add(new JLabel(label), GBC.std().insets(5, 5, 0, 5));
96 this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
97 this.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
98 this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
99
100 if (listener != null) {
101 prefcb.addActionListener(listener);
102 }
103 }
104 }
105
106 @Override
107 public JPanel getPreferencePanel(ActionListener listener) {
108 return new CBPanel(entries, index, label, listener);
109 }
110
111 @Override
112 public Collection<String> getPreferences(JPanel panel) {
113 if (!(panel instanceof CBPanel)) {
114 throw new IllegalArgumentException("Unsupported panel: "+panel);
115 }
116 CBPanel p = (CBPanel) panel;
117 int idx = p.prefcb.getSelectedIndex();
118 return Collections.singleton(indexToZone(idx));
119 }
120}
Note: See TracBrowser for help on using the repository browser.