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.JOptionPane;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JScrollPane;
|
---|
22 | import javax.swing.Scrollable;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.plugins.PluginDownloader;
|
---|
25 | import org.openstreetmap.josm.plugins.PluginSelection;
|
---|
26 | import org.openstreetmap.josm.tools.GBC;
|
---|
27 |
|
---|
28 | public class PluginPreference implements PreferenceSetting {
|
---|
29 |
|
---|
30 | private JPanel plugin;
|
---|
31 | private JPanel pluginPanel = new NoHorizontalScrollPanel(new GridBagLayout());
|
---|
32 | private PreferenceDialog gui;
|
---|
33 | private JScrollPane pluginPane;
|
---|
34 | private PluginSelection selection = new PluginSelection();
|
---|
35 |
|
---|
36 | public void addGui(final PreferenceDialog gui) {
|
---|
37 | this.gui = gui;
|
---|
38 | plugin = gui.createPreferenceTab("plugin", tr("Plugins"), tr("Configure available plugins."), false);
|
---|
39 | pluginPane = new JScrollPane(pluginPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
---|
40 | pluginPane.setBorder(null);
|
---|
41 | plugin.add(pluginPane, GBC.eol().fill(GBC.BOTH));
|
---|
42 | plugin.add(GBC.glue(0,10), GBC.eol());
|
---|
43 | JButton morePlugins = new JButton(tr("Download List"));
|
---|
44 | morePlugins.addActionListener(new ActionListener(){
|
---|
45 | public void actionPerformed(ActionEvent e) {
|
---|
46 | selection.updateDescription(pluginPanel);
|
---|
47 | }
|
---|
48 | });
|
---|
49 | plugin.add(morePlugins, GBC.std().insets(0,0,10,0));
|
---|
50 |
|
---|
51 | JButton update = new JButton(tr("Update"));
|
---|
52 | update.addActionListener(new ActionListener(){
|
---|
53 | public void actionPerformed(ActionEvent e) {
|
---|
54 | selection.update(pluginPanel);
|
---|
55 | }
|
---|
56 | });
|
---|
57 | plugin.add(update, GBC.std().insets(0,0,10,0));
|
---|
58 |
|
---|
59 | JButton configureSites = new JButton(tr("Configure Sites..."));
|
---|
60 | configureSites.addActionListener(new ActionListener(){
|
---|
61 | public void actionPerformed(ActionEvent e) {
|
---|
62 | configureSites();
|
---|
63 | }
|
---|
64 | });
|
---|
65 | plugin.add(configureSites, GBC.std());
|
---|
66 |
|
---|
67 | selection.drawPanel(pluginPanel);
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void configureSites() {
|
---|
71 | JPanel p = new JPanel(new GridBagLayout());
|
---|
72 | p.add(new JLabel(tr("Add JOSM Plugin description URL.")), GBC.eol());
|
---|
73 | final DefaultListModel model = new DefaultListModel();
|
---|
74 | for (String s : PluginDownloader.getSites())
|
---|
75 | model.addElement(s);
|
---|
76 | final JList list = new JList(model);
|
---|
77 | p.add(new JScrollPane(list), GBC.std().fill());
|
---|
78 | JPanel buttons = new JPanel(new GridBagLayout());
|
---|
79 | buttons.add(new JButton(new AbstractAction(tr("Add")){
|
---|
80 | public void actionPerformed(ActionEvent e) {
|
---|
81 | String s = JOptionPane.showInputDialog(gui, tr("Add JOSM Plugin description URL."));
|
---|
82 | if (s != null)
|
---|
83 | model.addElement(s);
|
---|
84 | }
|
---|
85 | }), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
86 | buttons.add(new JButton(new AbstractAction(tr("Edit")){
|
---|
87 | public void actionPerformed(ActionEvent e) {
|
---|
88 | if (list.getSelectedValue() == null) {
|
---|
89 | JOptionPane.showMessageDialog(gui, tr("Please select an entry."));
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | String s = JOptionPane.showInputDialog(gui, tr("Edit JOSM Plugin description URL."), list.getSelectedValue());
|
---|
93 | model.setElementAt(s, list.getSelectedIndex());
|
---|
94 | }
|
---|
95 | }), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
96 | buttons.add(new JButton(new AbstractAction(tr("Delete")){
|
---|
97 | public void actionPerformed(ActionEvent event) {
|
---|
98 | if (list.getSelectedValue() == null) {
|
---|
99 | JOptionPane.showMessageDialog(gui, tr("Please select an entry."));
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | model.removeElement(list.getSelectedValue());
|
---|
103 | }
|
---|
104 | }), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
105 | p.add(buttons, GBC.eol());
|
---|
106 | int answer = JOptionPane.showConfirmDialog(gui, p, tr("Configure Plugin Sites"), JOptionPane.OK_CANCEL_OPTION);
|
---|
107 | if (answer != JOptionPane.OK_OPTION)
|
---|
108 | return;
|
---|
109 | Collection<String> sites = new LinkedList<String>();
|
---|
110 | for (int i = 0; i < model.getSize(); ++i)
|
---|
111 | sites.add((String)model.getElementAt(i));
|
---|
112 | PluginDownloader.setSites(sites);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public boolean ok() {
|
---|
116 | return selection.finish();
|
---|
117 | }
|
---|
118 |
|
---|
119 | class NoHorizontalScrollPanel extends JPanel implements Scrollable {
|
---|
120 | public NoHorizontalScrollPanel(GridBagLayout gridBagLayout) {
|
---|
121 | super(gridBagLayout);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public Dimension getPreferredScrollableViewportSize() {
|
---|
125 | return super.getPreferredSize();
|
---|
126 | }
|
---|
127 |
|
---|
128 | public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
|
---|
129 | return 30;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public boolean getScrollableTracksViewportHeight() {
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public boolean getScrollableTracksViewportWidth() {
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
|
---|
141 | return 10;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|