1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
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.FlowLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.WindowAdapter;
|
---|
12 | import java.awt.event.WindowEvent;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.swing.AbstractAction;
|
---|
18 | import javax.swing.BorderFactory;
|
---|
19 | import javax.swing.DefaultListModel;
|
---|
20 | import javax.swing.JComponent;
|
---|
21 | import javax.swing.JDialog;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JList;
|
---|
24 | import javax.swing.JOptionPane;
|
---|
25 | import javax.swing.JPanel;
|
---|
26 | import javax.swing.JScrollPane;
|
---|
27 | import javax.swing.KeyStroke;
|
---|
28 | import javax.swing.event.ListSelectionEvent;
|
---|
29 | import javax.swing.event.ListSelectionListener;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
33 | import org.openstreetmap.josm.gui.SideButton;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
36 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * This dialog lets the user select changesets from a list of changesets.
|
---|
40 | * @since 2115
|
---|
41 | */
|
---|
42 | public class CloseChangesetDialog extends JDialog {
|
---|
43 |
|
---|
44 | /** the list */
|
---|
45 | private JList<Changeset> lstOpenChangesets;
|
---|
46 | /** true if the user canceled the dialog */
|
---|
47 | private boolean canceled;
|
---|
48 | /** the list model */
|
---|
49 | private DefaultListModel<Changeset> model;
|
---|
50 |
|
---|
51 | private SideButton btnCloseChangesets;
|
---|
52 |
|
---|
53 | protected JPanel buildTopPanel() {
|
---|
54 | JPanel pnl = new JPanel();
|
---|
55 | pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
56 | pnl.setLayout(new BorderLayout());
|
---|
57 | pnl.add(new JLabel(tr("<html>Please select the changesets you want to close</html>")), BorderLayout.CENTER);
|
---|
58 | return pnl;
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected JPanel buildCenterPanel() {
|
---|
62 | JPanel pnl = new JPanel();
|
---|
63 | pnl.setLayout(new BorderLayout());
|
---|
64 | model = new DefaultListModel<>();
|
---|
65 | pnl.add(new JScrollPane(lstOpenChangesets = new JList<>(model)), BorderLayout.CENTER);
|
---|
66 | lstOpenChangesets.setCellRenderer(new ChangesetCellRenderer());
|
---|
67 | return pnl;
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected JPanel buildSouthPanel() {
|
---|
71 | JPanel pnl = new JPanel();
|
---|
72 | pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
|
---|
73 |
|
---|
74 | // -- close action
|
---|
75 | CloseAction closeAction = new CloseAction();
|
---|
76 | lstOpenChangesets.addListSelectionListener(closeAction);
|
---|
77 | pnl.add(btnCloseChangesets = new SideButton(closeAction));
|
---|
78 | InputMapUtils.enableEnter(btnCloseChangesets);
|
---|
79 |
|
---|
80 | // -- cancel action
|
---|
81 | SideButton btn;
|
---|
82 | pnl.add(btn = new SideButton(new CancelAction()));
|
---|
83 | btn.setFocusable(true);
|
---|
84 | return pnl;
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected void build() {
|
---|
88 | setTitle(tr("Open changesets"));
|
---|
89 | getContentPane().setLayout(new BorderLayout());
|
---|
90 | getContentPane().add(buildTopPanel(), BorderLayout.NORTH);
|
---|
91 | getContentPane().add(buildCenterPanel(), BorderLayout.CENTER);
|
---|
92 | getContentPane().add(buildSouthPanel(), BorderLayout.SOUTH);
|
---|
93 |
|
---|
94 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
|
---|
95 | getRootPane().getActionMap().put("escape", new CancelAction());
|
---|
96 | addWindowListener(new WindowEventHandler());
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void setVisible(boolean visible) {
|
---|
101 | if (visible) {
|
---|
102 | new WindowGeometry(
|
---|
103 | getClass().getName() + ".geometry",
|
---|
104 | WindowGeometry.centerInWindow(Main.parent, new Dimension(300, 300))
|
---|
105 | ).applySafe(this);
|
---|
106 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
---|
107 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
108 | }
|
---|
109 | super.setVisible(visible);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Constructs a new {@code CloseChangesetDialog}.
|
---|
114 | */
|
---|
115 | public CloseChangesetDialog() {
|
---|
116 | super(JOptionPane.getFrameForComponent(Main.parent), ModalityType.DOCUMENT_MODAL);
|
---|
117 | build();
|
---|
118 | }
|
---|
119 |
|
---|
120 | class CloseAction extends AbstractAction implements ListSelectionListener {
|
---|
121 | public CloseAction() {
|
---|
122 | putValue(NAME, tr("Close changesets"));
|
---|
123 | putValue(SMALL_ICON, ImageProvider.get("closechangeset"));
|
---|
124 | putValue(SHORT_DESCRIPTION, tr("Close the selected open changesets"));
|
---|
125 | refreshEnabledState();
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void actionPerformed(ActionEvent e) {
|
---|
130 | setCanceled(false);
|
---|
131 | setVisible(false);
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected void refreshEnabledState() {
|
---|
135 | List<Changeset> list = lstOpenChangesets.getSelectedValuesList();
|
---|
136 | setEnabled(list != null && !list.isEmpty());
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public void valueChanged(ListSelectionEvent e) {
|
---|
141 | refreshEnabledState();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | class CancelAction extends AbstractAction {
|
---|
146 |
|
---|
147 | public CancelAction() {
|
---|
148 | putValue(NAME, tr("Cancel"));
|
---|
149 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
150 | putValue(SHORT_DESCRIPTION, tr("Cancel closing of changesets"));
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void cancel() {
|
---|
154 | setCanceled(true);
|
---|
155 | setVisible(false);
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void actionPerformed(ActionEvent e) {
|
---|
160 | cancel();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | class WindowEventHandler extends WindowAdapter {
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public void windowActivated(WindowEvent arg0) {
|
---|
168 | btnCloseChangesets.requestFocusInWindow();
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public void windowClosing(WindowEvent arg0) {
|
---|
173 | new CancelAction().cancel();
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Replies true if this dialog was canceled
|
---|
180 | * @return true if this dialog was canceled
|
---|
181 | */
|
---|
182 | public boolean isCanceled() {
|
---|
183 | return canceled;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Sets whether this dialog is canceled
|
---|
188 | *
|
---|
189 | * @param canceled true, if this dialog is canceld
|
---|
190 | */
|
---|
191 | protected void setCanceled(boolean canceled) {
|
---|
192 | this.canceled = canceled;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Sets the collection of changesets to be displayed
|
---|
197 | *
|
---|
198 | * @param changesets the collection of changesets. Assumes an empty collection if null
|
---|
199 | */
|
---|
200 | public void setChangesets(Collection<Changeset> changesets) {
|
---|
201 | if (changesets == null) {
|
---|
202 | changesets = new ArrayList<>();
|
---|
203 | }
|
---|
204 | model.removeAllElements();
|
---|
205 | for (Changeset cs: changesets) {
|
---|
206 | model.addElement(cs);
|
---|
207 | }
|
---|
208 | if (!changesets.isEmpty()) {
|
---|
209 | lstOpenChangesets.getSelectionModel().setSelectionInterval(0, changesets.size()-1);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Replies a collection with the changesets the user selected.
|
---|
215 | * Never null, but may be empty.
|
---|
216 | *
|
---|
217 | * @return a collection with the changesets the user selected.
|
---|
218 | */
|
---|
219 | public Collection<Changeset> getSelectedChangesets() {
|
---|
220 | return lstOpenChangesets.getSelectedValuesList();
|
---|
221 | }
|
---|
222 | }
|
---|