001 // License: GPL. For details, see LICENSE file. 002 package org.openstreetmap.josm.gui.preferences.projection; 003 004 import java.awt.GridBagLayout; 005 import java.awt.event.ActionListener; 006 import java.util.Collection; 007 import java.util.Collections; 008 009 import javax.swing.JLabel; 010 import javax.swing.JPanel; 011 012 import org.openstreetmap.josm.gui.widgets.JosmComboBox; 013 import org.openstreetmap.josm.tools.GBC; 014 015 /** 016 * A projection choice, that offers a list of projections in a combo-box. 017 */ 018 abstract public class ListProjectionChoice extends AbstractProjectionChoice { 019 020 protected int index; 021 protected int defaultIndex; 022 protected Object[] entries; 023 protected String label; 024 025 public ListProjectionChoice(String id, String name, Object[] entries, String label) { 026 this(id, name, entries, label, 0); 027 } 028 029 /** 030 * Constructor 031 * 032 * @param id the unique id for this ProjectionChoice 033 * @param name the display name 034 * @param entries the list of display entries for the combo-box 035 * @param label a label shown left to the combo-box 036 * @param defaultIndex the default index for the combo-box 037 */ 038 public ListProjectionChoice(String id, String name, Object[] entries, String label, int defaultIndex) { 039 super(id, name); 040 this.entries = entries; 041 this.label = label; 042 this.defaultIndex = defaultIndex; 043 } 044 045 /** 046 * Convert 0-based index to preference value. 047 */ 048 abstract protected String indexToZone(int index); 049 050 /** 051 * Convert preference value to 0-based index. 052 */ 053 abstract protected int zoneToIndex(String zone); 054 055 @Override 056 public void setPreferences(Collection<String> args) { 057 String zone = null; 058 if (args != null && args.size() >= 1) { 059 zone = args.iterator().next(); 060 } 061 int index; 062 if (zone == null) { 063 index = defaultIndex; 064 } else { 065 index = zoneToIndex(zone); 066 if (index < 0 || index >= entries.length) { 067 index = defaultIndex; 068 } 069 } 070 this.index = index; 071 } 072 073 protected class CBPanel extends JPanel { 074 public JosmComboBox prefcb; 075 076 public CBPanel(Object[] entries, int initialIndex, String label, final ActionListener listener) { 077 prefcb = new JosmComboBox(entries); 078 079 prefcb.setSelectedIndex(initialIndex); 080 this.setLayout(new GridBagLayout()); 081 this.add(new JLabel(label), GBC.std().insets(5,5,0,5)); 082 this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL)); 083 this.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL)); 084 this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH)); 085 086 if (listener != null) { 087 prefcb.addActionListener(listener); 088 } 089 } 090 } 091 092 @Override 093 public JPanel getPreferencePanel(ActionListener listener) { 094 return new CBPanel(entries, index, label, listener); 095 } 096 097 @Override 098 public Collection<String> getPreferences(JPanel panel) { 099 CBPanel p = (CBPanel) panel; 100 int index = p.prefcb.getSelectedIndex(); 101 return Collections.singleton(indexToZone(index)); 102 } 103 104 }