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

Last change on this file since 8509 was 8509, checked in by Don-vip, 9 years ago

fix many checkstyle violations

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