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