1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.Main;
|
---|
5 | import org.openstreetmap.josm.tools.GBC;
|
---|
6 |
|
---|
7 | import javax.swing.DefaultListModel;
|
---|
8 | import javax.swing.JButton;
|
---|
9 | import javax.swing.JList;
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.JScrollPane;
|
---|
13 | import java.awt.BorderLayout;
|
---|
14 | import java.awt.Dimension;
|
---|
15 | import java.awt.GridBagLayout;
|
---|
16 | import java.awt.event.ActionEvent;
|
---|
17 | import java.awt.event.ActionListener;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A {@link JList} containing items, and {@link JButton}s to add/edit/delete items.
|
---|
25 | */
|
---|
26 | public class EditableList extends JPanel {
|
---|
27 |
|
---|
28 | public final String title;
|
---|
29 | public final JList<String> sourcesList = new JList<String>(new DefaultListModel<String>());
|
---|
30 | public final JButton addSrcButton = new JButton(tr("Add"));
|
---|
31 | public final JButton editSrcButton = new JButton(tr("Edit"));
|
---|
32 | public final JButton deleteSrcButton = new JButton(tr("Delete"));
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code EditableList}.
|
---|
36 | * @param title The title displayed in input dialog
|
---|
37 | */
|
---|
38 | public EditableList(String title) {
|
---|
39 | this.title = title;
|
---|
40 | build();
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected final void build() {
|
---|
44 |
|
---|
45 | setLayout(new BorderLayout());
|
---|
46 |
|
---|
47 | addSrcButton.addActionListener(new ActionListener() {
|
---|
48 | @Override
|
---|
49 | public void actionPerformed(ActionEvent e) {
|
---|
50 | String source = JOptionPane.showInputDialog(
|
---|
51 | Main.parent,
|
---|
52 | title,
|
---|
53 | title,
|
---|
54 | JOptionPane.QUESTION_MESSAGE);
|
---|
55 | if (source != null) {
|
---|
56 | ((DefaultListModel<String>) sourcesList.getModel()).addElement(source);
|
---|
57 | }
|
---|
58 | sourcesList.clearSelection();
|
---|
59 | }
|
---|
60 | });
|
---|
61 |
|
---|
62 | editSrcButton.addActionListener(new ActionListener() {
|
---|
63 | @Override
|
---|
64 | public void actionPerformed(ActionEvent e) {
|
---|
65 | int row = sourcesList.getSelectedIndex();
|
---|
66 | if (row == -1 && sourcesList.getModel().getSize() == 1) {
|
---|
67 | sourcesList.setSelectedIndex(0);
|
---|
68 | row = 0;
|
---|
69 | }
|
---|
70 | if (row == -1) {
|
---|
71 | if (sourcesList.getModel().getSize() == 0) {
|
---|
72 | String source = JOptionPane.showInputDialog(Main.parent, title, title, JOptionPane.QUESTION_MESSAGE);
|
---|
73 | if (source != null) {
|
---|
74 | ((DefaultListModel<String>) sourcesList.getModel()).addElement(source);
|
---|
75 | }
|
---|
76 | } else {
|
---|
77 | JOptionPane.showMessageDialog(
|
---|
78 | Main.parent,
|
---|
79 | tr("Please select the row to edit."),
|
---|
80 | tr("Information"),
|
---|
81 | JOptionPane.INFORMATION_MESSAGE
|
---|
82 | );
|
---|
83 | }
|
---|
84 | } else {
|
---|
85 | String source = (String) JOptionPane.showInputDialog(Main.parent,
|
---|
86 | title,
|
---|
87 | title,
|
---|
88 | JOptionPane.QUESTION_MESSAGE, null, null,
|
---|
89 | sourcesList.getSelectedValue());
|
---|
90 | if (source != null) {
|
---|
91 | ((DefaultListModel<String>) sourcesList.getModel()).setElementAt(source, row);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | sourcesList.clearSelection();
|
---|
95 | }
|
---|
96 | });
|
---|
97 |
|
---|
98 | deleteSrcButton.addActionListener(new ActionListener() {
|
---|
99 | @Override
|
---|
100 | public void actionPerformed(ActionEvent e) {
|
---|
101 | if (sourcesList.getSelectedIndex() == -1) {
|
---|
102 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."), tr("Information"), JOptionPane.QUESTION_MESSAGE);
|
---|
103 | } else {
|
---|
104 | ((DefaultListModel<String>) sourcesList.getModel()).remove(sourcesList.getSelectedIndex());
|
---|
105 | }
|
---|
106 | }
|
---|
107 | });
|
---|
108 | sourcesList.setMinimumSize(new Dimension(300, 50));
|
---|
109 | sourcesList.setVisibleRowCount(3);
|
---|
110 |
|
---|
111 | addSrcButton.setToolTipText(tr("Add a new source to the list."));
|
---|
112 | editSrcButton.setToolTipText(tr("Edit the selected source."));
|
---|
113 | deleteSrcButton.setToolTipText(tr("Delete the selected source from the list."));
|
---|
114 |
|
---|
115 | final JPanel buttonPanel = new JPanel(new GridBagLayout());
|
---|
116 | buttonPanel.add(addSrcButton, GBC.std().insets(0, 5, 0, 0));
|
---|
117 | buttonPanel.add(editSrcButton, GBC.std().insets(5, 5, 5, 0));
|
---|
118 | buttonPanel.add(deleteSrcButton, GBC.std().insets(0, 5, 0, 0));
|
---|
119 |
|
---|
120 | add(new JScrollPane(sourcesList), BorderLayout.CENTER);
|
---|
121 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
122 | setPreferredSize(new Dimension(300, 50 + (int) buttonPanel.getPreferredSize().getHeight()));
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void setItems(final Iterable<String> items) {
|
---|
127 | for (String source : items) {
|
---|
128 | ((DefaultListModel<String>) sourcesList.getModel()).addElement(source);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public List<String> getItems() {
|
---|
133 | final List<String> items = new ArrayList<String>(sourcesList.getModel().getSize());
|
---|
134 | for (int i = 0; i < sourcesList.getModel().getSize(); ++i) {
|
---|
135 | items.add(sourcesList.getModel().getElementAt(i));
|
---|
136 | }
|
---|
137 | return items;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void setEnabled(boolean enabled) {
|
---|
141 | sourcesList.setEnabled(enabled);
|
---|
142 | addSrcButton.setEnabled(enabled);
|
---|
143 | editSrcButton.setEnabled(enabled);
|
---|
144 | deleteSrcButton.setEnabled(enabled);
|
---|
145 | }
|
---|
146 | }
|
---|