1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Container;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.GridLayout;
|
---|
11 | import java.awt.LayoutManager;
|
---|
12 | import java.awt.Rectangle;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.ActionListener;
|
---|
15 | import java.util.HashMap;
|
---|
16 | import java.util.TreeMap;
|
---|
17 | import java.util.Map;
|
---|
18 |
|
---|
19 | import javax.swing.Action;
|
---|
20 | import javax.swing.DefaultListCellRenderer;
|
---|
21 | import javax.swing.DefaultListModel;
|
---|
22 | import javax.swing.Icon;
|
---|
23 | import javax.swing.JButton;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JList;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.JScrollPane;
|
---|
28 | import javax.swing.JToolBar;
|
---|
29 | import javax.swing.ListCellRenderer;
|
---|
30 | import javax.swing.event.ListSelectionEvent;
|
---|
31 | import javax.swing.event.ListSelectionListener;
|
---|
32 |
|
---|
33 | import org.openstreetmap.josm.Main;
|
---|
34 | import org.openstreetmap.josm.tools.GBC;
|
---|
35 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
36 |
|
---|
37 | public class ToolbarPreferences implements PreferenceSetting {
|
---|
38 |
|
---|
39 | private final class Move implements ActionListener {
|
---|
40 | public void actionPerformed(ActionEvent e) {
|
---|
41 | if (e.getActionCommand().equals("<<")) {
|
---|
42 | while (unselected.size() > 1) {
|
---|
43 | selected.addElement(unselected.get(0));
|
---|
44 | unselected.remove(0);
|
---|
45 | }
|
---|
46 | } else if (e.getActionCommand().equals("<") && unselectedList.getSelectedIndex() != -1) {
|
---|
47 | while (unselectedList.getSelectedIndex() != -1 && unselectedList.getSelectedIndex() != unselected.size()-1) {
|
---|
48 | selected.addElement(unselectedList.getSelectedValue());
|
---|
49 | unselected.remove(unselectedList.getSelectedIndex());
|
---|
50 | }
|
---|
51 | if (unselectedList.getSelectedIndex() == unselected.size()-1)
|
---|
52 | selected.addElement(null);
|
---|
53 | } else if (e.getActionCommand().equals(">") && selectedList.getSelectedIndex() != -1) {
|
---|
54 | while (selectedList.getSelectedIndex() != -1) {
|
---|
55 | if (selectedList.getSelectedValue() != null)
|
---|
56 | unselected.add(unselected.size()-1, selectedList.getSelectedValue());
|
---|
57 | selected.remove(selectedList.getSelectedIndex());
|
---|
58 | }
|
---|
59 | } else if (e.getActionCommand().equals(">>")) {
|
---|
60 | while (selected.size() > 0) {
|
---|
61 | if (selected.get(0) != null)
|
---|
62 | unselected.add(unselected.size()-1, selected.get(0));
|
---|
63 | selected.remove(0);
|
---|
64 | }
|
---|
65 | } else if (e.getActionCommand().equals("up")) {
|
---|
66 | int i = selectedList.getSelectedIndex();
|
---|
67 | Object o = selected.get(i);
|
---|
68 | if (i != 0) {
|
---|
69 | selected.remove(i);
|
---|
70 | selected.add(i-1, o);
|
---|
71 | selectedList.setSelectedIndex(i-1);
|
---|
72 | }
|
---|
73 | } else if (e.getActionCommand().equals("down")) {
|
---|
74 | int i = selectedList.getSelectedIndex();
|
---|
75 | Object o = selected.get(i);
|
---|
76 | if (i != selected.size()-1) {
|
---|
77 | selected.remove(i);
|
---|
78 | selected.add(i+1, o);
|
---|
79 | selectedList.setSelectedIndex(i+1);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | private Move moveAction = new Move();
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Key: Registered name (property "toolbar" of action).
|
---|
88 | * Value: The action to execute.
|
---|
89 | */
|
---|
90 | private Map<String, Action> actions = new HashMap<String, Action>();
|
---|
91 |
|
---|
92 | private DefaultListModel selected = new DefaultListModel();
|
---|
93 | private DefaultListModel unselected = new DefaultListModel();
|
---|
94 | private JList selectedList = new JList(selected);
|
---|
95 | private JList unselectedList = new JList(unselected);
|
---|
96 |
|
---|
97 | public JToolBar control = new JToolBar();
|
---|
98 |
|
---|
99 | private JButton upButton;
|
---|
100 | private JButton downButton;
|
---|
101 |
|
---|
102 | public ToolbarPreferences() {
|
---|
103 | control.setFloatable(false);
|
---|
104 |
|
---|
105 | final ListCellRenderer oldRenderer = selectedList.getCellRenderer();
|
---|
106 | ListCellRenderer renderer = new DefaultListCellRenderer(){
|
---|
107 | @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
108 | String s = tr("Separator");
|
---|
109 | Icon i = ImageProvider.get("preferences/separator");
|
---|
110 | if (value != null) {
|
---|
111 | s = (String)((Action)value).getValue(Action.NAME);
|
---|
112 | i = (Icon)((Action)value).getValue(Action.SMALL_ICON);
|
---|
113 | }
|
---|
114 | JLabel l = (JLabel)oldRenderer.getListCellRendererComponent(list, s, index, isSelected, cellHasFocus);
|
---|
115 | l.setIcon(i);
|
---|
116 | return l;
|
---|
117 | }
|
---|
118 | };
|
---|
119 | selectedList.setCellRenderer(renderer);
|
---|
120 | unselectedList.setCellRenderer(renderer);
|
---|
121 |
|
---|
122 | unselectedList.addListSelectionListener(new ListSelectionListener(){
|
---|
123 | public void valueChanged(ListSelectionEvent e) {
|
---|
124 | if ((unselectedList.getSelectedIndex() != -1))
|
---|
125 | selectedList.clearSelection();
|
---|
126 | upButton.setEnabled(selectedList.getSelectedIndex() != -1);
|
---|
127 | downButton.setEnabled(selectedList.getSelectedIndex() != -1);
|
---|
128 | }
|
---|
129 | });
|
---|
130 | selectedList.addListSelectionListener(new ListSelectionListener(){
|
---|
131 | public void valueChanged(ListSelectionEvent e) {
|
---|
132 | boolean sel = selectedList.getSelectedIndex() != -1;
|
---|
133 | if (sel)
|
---|
134 | unselectedList.clearSelection();
|
---|
135 | upButton.setEnabled(sel);
|
---|
136 | downButton.setEnabled(sel);
|
---|
137 | }
|
---|
138 | });
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void addGui(PreferenceDialog gui) {
|
---|
142 | selected.removeAllElements();
|
---|
143 | unselected.removeAllElements();
|
---|
144 | Map<String, Action> us = new TreeMap<String, Action>();
|
---|
145 | for (Action a : actions.values())
|
---|
146 | {
|
---|
147 | String name = a.getValue(a.NAME).toString();
|
---|
148 | if(!name.equals(" "))
|
---|
149 | us.put(a.getValue(a.NAME).toString()+a.toString(), a);
|
---|
150 | }
|
---|
151 | for (String a : us.keySet())
|
---|
152 | unselected.addElement(us.get(a));
|
---|
153 | unselected.addElement(null);
|
---|
154 |
|
---|
155 | final JPanel left = new JPanel(new GridBagLayout());
|
---|
156 | left.add(new JLabel(tr("Toolbar")), GBC.eol());
|
---|
157 | left.add(new JScrollPane(selectedList), GBC.std().fill(GBC.BOTH));
|
---|
158 |
|
---|
159 | final JPanel right = new JPanel(new GridBagLayout());
|
---|
160 | right.add(new JLabel(tr("Available")), GBC.eol());
|
---|
161 | right.add(new JScrollPane(unselectedList), GBC.eol().fill(GBC.BOTH));
|
---|
162 |
|
---|
163 | final JPanel buttons = new JPanel(new GridLayout(6,1));
|
---|
164 | buttons.add(upButton = createButton("up"));
|
---|
165 | buttons.add(createButton("<<"));
|
---|
166 | buttons.add(createButton("<"));
|
---|
167 | buttons.add(createButton(">"));
|
---|
168 | buttons.add(createButton(">>"));
|
---|
169 | buttons.add(downButton = createButton("down"));
|
---|
170 | upButton.setEnabled(false);
|
---|
171 | downButton.setEnabled(false);
|
---|
172 |
|
---|
173 | final JPanel p = new JPanel();
|
---|
174 | p.setLayout(new LayoutManager(){
|
---|
175 | public void addLayoutComponent(String name, Component comp) {}
|
---|
176 | public void removeLayoutComponent(Component comp) {}
|
---|
177 | public Dimension minimumLayoutSize(Container parent) {
|
---|
178 | Dimension l = left.getMinimumSize();
|
---|
179 | Dimension r = right.getMinimumSize();
|
---|
180 | Dimension b = buttons.getMinimumSize();
|
---|
181 | return new Dimension(l.width+b.width+10+r.width,l.height+b.height+10+r.height);
|
---|
182 | }
|
---|
183 | public Dimension preferredLayoutSize(Container parent) {
|
---|
184 | Dimension l = left.getPreferredSize();
|
---|
185 | Dimension r = right.getPreferredSize();
|
---|
186 | return new Dimension(l.width+r.width+10+buttons.getPreferredSize().width,Math.max(l.height, r.height));
|
---|
187 | }
|
---|
188 | public void layoutContainer(Container parent) {
|
---|
189 | Dimension d = p.getSize();
|
---|
190 | Dimension b = buttons.getPreferredSize();
|
---|
191 | int width = d.width/2-10-b.width;
|
---|
192 | left.setBounds(new Rectangle(0,0,width,d.height));
|
---|
193 | right.setBounds(new Rectangle(width+10+b.width,0,width,d.height));
|
---|
194 | buttons.setBounds(new Rectangle(width+5, d.height/2-b.height/2, b.width, b.height));
|
---|
195 | }
|
---|
196 | });
|
---|
197 | p.add(left);
|
---|
198 | p.add(buttons);
|
---|
199 | p.add(right);
|
---|
200 |
|
---|
201 | JPanel panel = gui.createPreferenceTab("toolbar", tr("Toolbar customization"), tr("Customize the elements on the toolbar."));
|
---|
202 | panel.add(p, GBC.eol().fill(GBC.BOTH));
|
---|
203 |
|
---|
204 | for (String s : getToolString()) {
|
---|
205 | if (s.equals("|"))
|
---|
206 | selected.addElement(null);
|
---|
207 | else {
|
---|
208 | Action a = actions.get(s);
|
---|
209 | if (a != null) {
|
---|
210 | selected.addElement(a);
|
---|
211 | unselected.removeElement(a);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | private String[] getToolString() {
|
---|
218 | String s = Main.pref.get("toolbar", "open;save;exportgpx;|;download;upload;|;undo;redo;|;preference");
|
---|
219 | if (s == null || s.equals("null") || s.equals(""))
|
---|
220 | return new String[0];
|
---|
221 | return s.split(";");
|
---|
222 | }
|
---|
223 |
|
---|
224 | private JButton createButton(String name) {
|
---|
225 | JButton b = new JButton();
|
---|
226 | if (name.equals("up"))
|
---|
227 | b.setIcon(ImageProvider.get("dialogs", "up"));
|
---|
228 | else if (name.equals("down"))
|
---|
229 | b.setIcon(ImageProvider.get("dialogs", "down"));
|
---|
230 | else
|
---|
231 | b.setText(name);
|
---|
232 | b.addActionListener(moveAction);
|
---|
233 | b.setActionCommand(name);
|
---|
234 | return b;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public void ok() {
|
---|
238 | StringBuilder b = new StringBuilder();
|
---|
239 | for (int i = 0; i < selected.size(); ++i) {
|
---|
240 | if (selected.get(i) == null)
|
---|
241 | b.append("|");
|
---|
242 | else
|
---|
243 | b.append(((Action)selected.get(i)).getValue("toolbar"));
|
---|
244 | b.append(";");
|
---|
245 | }
|
---|
246 | String s = b.toString();
|
---|
247 | if (s.length() > 0)
|
---|
248 | s = s.substring(0, s.length()-1);
|
---|
249 | else
|
---|
250 | s = "null";
|
---|
251 | Main.pref.put("toolbar", s);
|
---|
252 | refreshToolbarControl();
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * @return The parameter (for better chaining)
|
---|
257 | */
|
---|
258 | public Action register(Action action) {
|
---|
259 | actions.put((String)action.getValue("toolbar"), action);
|
---|
260 | return action;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Parse the toolbar preference setting and construct the toolbar GUI control.
|
---|
265 | *
|
---|
266 | * Call this, if anything has changed in the toolbar settings and you want to refresh
|
---|
267 | * the toolbar content (e.g. after registering actions in a plugin)
|
---|
268 | */
|
---|
269 | public void refreshToolbarControl() {
|
---|
270 | control.removeAll();
|
---|
271 | for (String s : getToolString()) {
|
---|
272 | if (s.equals("|"))
|
---|
273 | control.addSeparator();
|
---|
274 | else
|
---|
275 | control.add(actions.get(s));
|
---|
276 | }
|
---|
277 | control.setVisible(control.getComponentCount() != 0);
|
---|
278 | }
|
---|
279 | }
|
---|