source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/EditableList.java@ 6553

Last change on this file since 6553 was 6553, checked in by simon04, 10 years ago

see #9414 - MapCSS-based tagchecker: allow to add custom files in preferences (resumes r6551)

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