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

Last change on this file since 13661 was 12304, checked in by michael2402, 7 years ago

Javadoc for public methods / classes in gui.util and gui.widgets

  • 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.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 /**
28 * The title displayed in input dialog
29 */
30 public final String title;
31 /**
32 * The list items
33 */
34 public final JList<String> sourcesList = new JList<>(new DefaultListModel<String>());
35 /**
36 * The add button
37 */
38 public final JButton addSrcButton = new JButton(tr("Add"));
39 /**
40 * The edit button displayed nex to the list
41 */
42 public final JButton editSrcButton = new JButton(tr("Edit"));
43 /**
44 * The delete button
45 */
46 public final JButton deleteSrcButton = new JButton(tr("Delete"));
47
48 /**
49 * Constructs a new {@code EditableList}.
50 * @param title The title displayed in input dialog
51 */
52 public EditableList(String title) {
53 this.title = title;
54 build();
55 }
56
57 protected final void build() {
58
59 setLayout(new BorderLayout());
60
61 addSrcButton.addActionListener(e -> {
62 String source = JOptionPane.showInputDialog(
63 Main.parent,
64 title,
65 title,
66 JOptionPane.QUESTION_MESSAGE);
67 if (source != null && !source.isEmpty()) {
68 ((DefaultListModel<String>) sourcesList.getModel()).addElement(source);
69 }
70 sourcesList.clearSelection();
71 });
72
73 editSrcButton.addActionListener(e -> {
74 int row = sourcesList.getSelectedIndex();
75 if (row == -1 && sourcesList.getModel().getSize() == 1) {
76 sourcesList.setSelectedIndex(0);
77 row = 0;
78 }
79 if (row == -1) {
80 if (sourcesList.getModel().getSize() == 0) {
81 String source1 = JOptionPane.showInputDialog(Main.parent, title, title, JOptionPane.QUESTION_MESSAGE);
82 if (source1 != null && !source1.isEmpty()) {
83 ((DefaultListModel<String>) sourcesList.getModel()).addElement(source1);
84 }
85 } else {
86 JOptionPane.showMessageDialog(
87 Main.parent,
88 tr("Please select the row to edit."),
89 tr("Information"),
90 JOptionPane.INFORMATION_MESSAGE
91 );
92 }
93 } else {
94 String source2 = (String) JOptionPane.showInputDialog(Main.parent,
95 title,
96 title,
97 JOptionPane.QUESTION_MESSAGE, null, null,
98 sourcesList.getSelectedValue());
99 if (source2 != null && !source2.isEmpty()) {
100 ((DefaultListModel<String>) sourcesList.getModel()).setElementAt(source2, row);
101 }
102 }
103 sourcesList.clearSelection();
104 });
105
106 deleteSrcButton.addActionListener(e -> {
107 if (sourcesList.getSelectedIndex() == -1) {
108 JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."), tr("Information"),
109 JOptionPane.QUESTION_MESSAGE);
110 } else {
111 ((DefaultListModel<String>) sourcesList.getModel()).remove(sourcesList.getSelectedIndex());
112 }
113 });
114 sourcesList.setMinimumSize(new Dimension(300, 50));
115 sourcesList.setVisibleRowCount(3);
116
117 addSrcButton.setToolTipText(tr("Add a new source to the list."));
118 editSrcButton.setToolTipText(tr("Edit the selected source."));
119 deleteSrcButton.setToolTipText(tr("Delete the selected source from the list."));
120
121 final JPanel buttonPanel = new JPanel(new GridBagLayout());
122 buttonPanel.add(addSrcButton, GBC.std().insets(0, 5, 0, 0));
123 buttonPanel.add(editSrcButton, GBC.std().insets(5, 5, 5, 0));
124 buttonPanel.add(deleteSrcButton, GBC.std().insets(0, 5, 0, 0));
125
126 add(new JScrollPane(sourcesList), BorderLayout.CENTER);
127 add(buttonPanel, BorderLayout.SOUTH);
128 setPreferredSize(new Dimension(300, 50 + (int) buttonPanel.getPreferredSize().getHeight()));
129
130 }
131
132 /**
133 * Sets the list items by a given list of strings
134 * @param items The items that should be set
135 */
136 public void setItems(final Iterable<String> items) {
137 for (String source : items) {
138 ((DefaultListModel<String>) sourcesList.getModel()).addElement(source);
139 }
140 }
141
142 /**
143 * Gets all items that are currently displayed
144 * @return All items as list of strings
145 */
146 public List<String> getItems() {
147 final List<String> items = new ArrayList<>(sourcesList.getModel().getSize());
148 for (int i = 0; i < sourcesList.getModel().getSize(); ++i) {
149 items.add(sourcesList.getModel().getElementAt(i));
150 }
151 return items;
152 }
153
154 @Override
155 public void setEnabled(boolean enabled) {
156 sourcesList.setEnabled(enabled);
157 addSrcButton.setEnabled(enabled);
158 editSrcButton.setEnabled(enabled);
159 deleteSrcButton.setEnabled(enabled);
160 }
161}
Note: See TracBrowser for help on using the repository browser.