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