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.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.LinkedList;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.DefaultListModel;
|
---|
16 | import javax.swing.JButton;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JList;
|
---|
19 | import javax.swing.JTextField;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JScrollPane;
|
---|
23 | import javax.swing.Scrollable;
|
---|
24 | import javax.swing.event.DocumentEvent;
|
---|
25 | import javax.swing.event.DocumentListener;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.plugins.PluginDownloader;
|
---|
29 | import org.openstreetmap.josm.plugins.PluginSelection;
|
---|
30 | import org.openstreetmap.josm.tools.GBC;
|
---|
31 |
|
---|
32 | public class PluginPreference implements PreferenceSetting {
|
---|
33 |
|
---|
34 | public static class Factory implements PreferenceSettingFactory {
|
---|
35 | public PreferenceSetting createPreferenceSetting() {
|
---|
36 | return new PluginPreference();
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | private JPanel plugin;
|
---|
41 | private JPanel pluginPanel = new NoHorizontalScrollPanel(new GridBagLayout());
|
---|
42 | private PreferenceDialog gui;
|
---|
43 | private JScrollPane pluginPane;
|
---|
44 | private PluginSelection selection = new PluginSelection();
|
---|
45 | private JTextField txtFilter;
|
---|
46 |
|
---|
47 | public void addGui(final PreferenceDialog gui) {
|
---|
48 | this.gui = gui;
|
---|
49 | plugin = gui.createPreferenceTab("plugin", tr("Plugins"), tr("Configure available plugins."), false);
|
---|
50 |
|
---|
51 | txtFilter = new JTextField();
|
---|
52 | JLabel lbFilter = new JLabel(tr("Search: "));
|
---|
53 | lbFilter.setLabelFor(txtFilter);
|
---|
54 | plugin.add(lbFilter);
|
---|
55 | plugin.add(txtFilter, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
56 | txtFilter.getDocument().addDocumentListener(new DocumentListener(){
|
---|
57 | public void changedUpdate(DocumentEvent e) {
|
---|
58 | action();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void insertUpdate(DocumentEvent e) {
|
---|
62 | action();
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void removeUpdate(DocumentEvent e) {
|
---|
66 | action();
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void action() {
|
---|
70 | selection.drawPanel(pluginPanel);
|
---|
71 | }
|
---|
72 | });
|
---|
73 | plugin.add(GBC.glue(0,10), GBC.eol());
|
---|
74 |
|
---|
75 | /* main plugin area */
|
---|
76 | pluginPane = new JScrollPane(pluginPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
---|
77 | pluginPane.setBorder(null);
|
---|
78 | plugin.add(pluginPane, GBC.eol().fill(GBC.BOTH));
|
---|
79 | plugin.add(GBC.glue(0,10), GBC.eol());
|
---|
80 |
|
---|
81 | /* buttons at the bottom */
|
---|
82 | JButton morePlugins = new JButton(tr("Download List"));
|
---|
83 | morePlugins.addActionListener(new ActionListener(){
|
---|
84 | public void actionPerformed(ActionEvent e) {
|
---|
85 | selection.updateDescription(pluginPanel);
|
---|
86 | }
|
---|
87 | });
|
---|
88 | plugin.add(morePlugins, GBC.std().insets(0,0,10,0));
|
---|
89 |
|
---|
90 | JButton update = new JButton(tr("Update"));
|
---|
91 | update.addActionListener(new ActionListener(){
|
---|
92 | public void actionPerformed(ActionEvent e) {
|
---|
93 | selection.update(pluginPanel);
|
---|
94 | }
|
---|
95 | });
|
---|
96 | plugin.add(update, GBC.std().insets(0,0,10,0));
|
---|
97 |
|
---|
98 | JButton configureSites = new JButton(tr("Configure Sites..."));
|
---|
99 | configureSites.addActionListener(new ActionListener(){
|
---|
100 | public void actionPerformed(ActionEvent e) {
|
---|
101 | configureSites();
|
---|
102 | }
|
---|
103 | });
|
---|
104 | plugin.add(configureSites, GBC.std());
|
---|
105 |
|
---|
106 | selection.passTxtFilter(txtFilter);
|
---|
107 | selection.loadPlugins();
|
---|
108 | selection.drawPanel(pluginPanel);
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void configureSites() {
|
---|
112 | JPanel p = new JPanel(new GridBagLayout());
|
---|
113 | p.add(new JLabel(tr("Add JOSM Plugin description URL.")), GBC.eol());
|
---|
114 | final DefaultListModel model = new DefaultListModel();
|
---|
115 | for (String s : PluginDownloader.getSites()) {
|
---|
116 | model.addElement(s);
|
---|
117 | }
|
---|
118 | final JList list = new JList(model);
|
---|
119 | p.add(new JScrollPane(list), GBC.std().fill());
|
---|
120 | JPanel buttons = new JPanel(new GridBagLayout());
|
---|
121 | buttons.add(new JButton(new AbstractAction(tr("Add")){
|
---|
122 | public void actionPerformed(ActionEvent e) {
|
---|
123 | String s = JOptionPane.showInputDialog(
|
---|
124 | gui,
|
---|
125 | tr("Add JOSM Plugin description URL."),
|
---|
126 | tr("Enter URL"),
|
---|
127 | JOptionPane.QUESTION_MESSAGE
|
---|
128 | );
|
---|
129 | if (s != null) {
|
---|
130 | model.addElement(s);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
134 | buttons.add(new JButton(new AbstractAction(tr("Edit")){
|
---|
135 | public void actionPerformed(ActionEvent e) {
|
---|
136 | if (list.getSelectedValue() == null) {
|
---|
137 | JOptionPane.showMessageDialog(
|
---|
138 | gui,
|
---|
139 | tr("Please select an entry."),
|
---|
140 | tr("Warning"),
|
---|
141 | JOptionPane.WARNING_MESSAGE
|
---|
142 | );
|
---|
143 | return;
|
---|
144 | }
|
---|
145 | String s = (String)JOptionPane.showInputDialog(
|
---|
146 | Main.parent,
|
---|
147 | tr("Edit JOSM Plugin description URL."),
|
---|
148 | tr("JOSM Plugin description URL"),
|
---|
149 | JOptionPane.QUESTION_MESSAGE,
|
---|
150 | null,
|
---|
151 | null,
|
---|
152 | list.getSelectedValue()
|
---|
153 | );
|
---|
154 | model.setElementAt(s, list.getSelectedIndex());
|
---|
155 | }
|
---|
156 | }), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
157 | buttons.add(new JButton(new AbstractAction(tr("Delete")){
|
---|
158 | public void actionPerformed(ActionEvent event) {
|
---|
159 | if (list.getSelectedValue() == null) {
|
---|
160 | JOptionPane.showMessageDialog(
|
---|
161 | gui,
|
---|
162 | tr("Please select an entry."),
|
---|
163 | tr("Warning"),
|
---|
164 | JOptionPane.WARNING_MESSAGE
|
---|
165 | );
|
---|
166 | return;
|
---|
167 | }
|
---|
168 | model.removeElement(list.getSelectedValue());
|
---|
169 | }
|
---|
170 | }), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
171 | p.add(buttons, GBC.eol());
|
---|
172 | int answer = JOptionPane.showConfirmDialog(
|
---|
173 | gui,
|
---|
174 | p,
|
---|
175 | tr("Configure Plugin Sites"), JOptionPane.OK_CANCEL_OPTION,
|
---|
176 | JOptionPane.PLAIN_MESSAGE);
|
---|
177 | if (answer != JOptionPane.OK_OPTION)
|
---|
178 | return;
|
---|
179 | Collection<String> sites = new LinkedList<String>();
|
---|
180 | for (int i = 0; i < model.getSize(); ++i) {
|
---|
181 | sites.add((String)model.getElementAt(i));
|
---|
182 | }
|
---|
183 | PluginDownloader.setSites(sites);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public boolean ok() {
|
---|
187 | return selection.finish();
|
---|
188 | }
|
---|
189 |
|
---|
190 | private static class NoHorizontalScrollPanel extends JPanel implements Scrollable {
|
---|
191 | public NoHorizontalScrollPanel(GridBagLayout gridBagLayout) {
|
---|
192 | super(gridBagLayout);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public Dimension getPreferredScrollableViewportSize() {
|
---|
196 | return super.getPreferredSize();
|
---|
197 | }
|
---|
198 |
|
---|
199 | public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
|
---|
200 | return 30;
|
---|
201 | }
|
---|
202 |
|
---|
203 | public boolean getScrollableTracksViewportHeight() {
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public boolean getScrollableTracksViewportWidth() {
|
---|
208 | return true;
|
---|
209 | }
|
---|
210 |
|
---|
211 | public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
|
---|
212 | return 10;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|