source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/PluginPreference.java@ 1865

Last change on this file since 1865 was 1865, checked in by Gubaer, 15 years ago

replaced JOptionPane.show* by OptionPaneUtil.show*
ExtendeDialog now always on top, too

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GridBagLayout;
8import java.awt.Rectangle;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.util.Collection;
12import java.util.LinkedList;
13
14import javax.swing.AbstractAction;
15import javax.swing.DefaultListModel;
16import javax.swing.JButton;
17import javax.swing.JLabel;
18import javax.swing.JList;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21import javax.swing.JScrollPane;
22import javax.swing.Scrollable;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.gui.OptionPaneUtil;
26import org.openstreetmap.josm.plugins.PluginDownloader;
27import org.openstreetmap.josm.plugins.PluginSelection;
28import org.openstreetmap.josm.tools.GBC;
29
30public class PluginPreference implements PreferenceSetting {
31
32 public static class Factory implements PreferenceSettingFactory {
33 public PreferenceSetting createPreferenceSetting() {
34 return new PluginPreference();
35 }
36 }
37
38 private JPanel plugin;
39 private JPanel pluginPanel = new NoHorizontalScrollPanel(new GridBagLayout());
40 private PreferenceDialog gui;
41 private JScrollPane pluginPane;
42 private PluginSelection selection = new PluginSelection();
43
44 public void addGui(final PreferenceDialog gui) {
45 this.gui = gui;
46 plugin = gui.createPreferenceTab("plugin", tr("Plugins"), tr("Configure available plugins."), false);
47 pluginPane = new JScrollPane(pluginPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
48 pluginPane.setBorder(null);
49 plugin.add(pluginPane, GBC.eol().fill(GBC.BOTH));
50 plugin.add(GBC.glue(0,10), GBC.eol());
51 JButton morePlugins = new JButton(tr("Download List"));
52 morePlugins.addActionListener(new ActionListener(){
53 public void actionPerformed(ActionEvent e) {
54 selection.updateDescription(pluginPanel);
55 }
56 });
57 plugin.add(morePlugins, GBC.std().insets(0,0,10,0));
58
59 JButton update = new JButton(tr("Update"));
60 update.addActionListener(new ActionListener(){
61 public void actionPerformed(ActionEvent e) {
62 selection.update(pluginPanel);
63 }
64 });
65 plugin.add(update, GBC.std().insets(0,0,10,0));
66
67 JButton configureSites = new JButton(tr("Configure Sites..."));
68 configureSites.addActionListener(new ActionListener(){
69 public void actionPerformed(ActionEvent e) {
70 configureSites();
71 }
72 });
73 plugin.add(configureSites, GBC.std());
74
75 selection.drawPanel(pluginPanel);
76 }
77
78 private void configureSites() {
79 JPanel p = new JPanel(new GridBagLayout());
80 p.add(new JLabel(tr("Add JOSM Plugin description URL.")), GBC.eol());
81 final DefaultListModel model = new DefaultListModel();
82 for (String s : PluginDownloader.getSites()) {
83 model.addElement(s);
84 }
85 final JList list = new JList(model);
86 p.add(new JScrollPane(list), GBC.std().fill());
87 JPanel buttons = new JPanel(new GridBagLayout());
88 buttons.add(new JButton(new AbstractAction(tr("Add")){
89 public void actionPerformed(ActionEvent e) {
90 String s = OptionPaneUtil.showInputDialog(
91 gui,
92 tr("Add JOSM Plugin description URL."),
93 tr("Enter URL"),
94 JOptionPane.QUESTION_MESSAGE
95 );
96 if (s != null) {
97 model.addElement(s);
98 }
99 }
100 }), GBC.eol().fill(GBC.HORIZONTAL));
101 buttons.add(new JButton(new AbstractAction(tr("Edit")){
102 public void actionPerformed(ActionEvent e) {
103 if (list.getSelectedValue() == null) {
104 OptionPaneUtil.showMessageDialog(
105 gui,
106 tr("Please select an entry."),
107 tr("Warning"),
108 JOptionPane.WARNING_MESSAGE
109 );
110 return;
111 }
112 String s = (String)OptionPaneUtil.showInputDialog(
113 Main.parent,
114 tr("Edit JOSM Plugin description URL."),
115 tr("JOSM Plugin description URL"),
116 JOptionPane.QUESTION_MESSAGE,
117 null,
118 null,
119 list.getSelectedValue()
120 );
121 model.setElementAt(s, list.getSelectedIndex());
122 }
123 }), GBC.eol().fill(GBC.HORIZONTAL));
124 buttons.add(new JButton(new AbstractAction(tr("Delete")){
125 public void actionPerformed(ActionEvent event) {
126 if (list.getSelectedValue() == null) {
127 OptionPaneUtil.showMessageDialog(
128 gui,
129 tr("Please select an entry."),
130 tr("Warning"),
131 JOptionPane.WARNING_MESSAGE
132 );
133 return;
134 }
135 model.removeElement(list.getSelectedValue());
136 }
137 }), GBC.eol().fill(GBC.HORIZONTAL));
138 p.add(buttons, GBC.eol());
139 int answer = OptionPaneUtil.showConfirmationDialog(
140 gui,
141 p,
142 tr("Configure Plugin Sites"), JOptionPane.OK_CANCEL_OPTION,
143 JOptionPane.PLAIN_MESSAGE);
144 if (answer != JOptionPane.OK_OPTION)
145 return;
146 Collection<String> sites = new LinkedList<String>();
147 for (int i = 0; i < model.getSize(); ++i) {
148 sites.add((String)model.getElementAt(i));
149 }
150 PluginDownloader.setSites(sites);
151 }
152
153 public boolean ok() {
154 return selection.finish();
155 }
156
157 private static class NoHorizontalScrollPanel extends JPanel implements Scrollable {
158 public NoHorizontalScrollPanel(GridBagLayout gridBagLayout) {
159 super(gridBagLayout);
160 }
161
162 public Dimension getPreferredScrollableViewportSize() {
163 return super.getPreferredSize();
164 }
165
166 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
167 return 30;
168 }
169
170 public boolean getScrollableTracksViewportHeight() {
171 return false;
172 }
173
174 public boolean getScrollableTracksViewportWidth() {
175 return true;
176 }
177
178 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
179 return 10;
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.