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

Last change on this file since 11279 was 11198, checked in by Don-vip, 8 years ago

fix #13881 - handle cases where JOptionPane.showInputDialog returns an empty string

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