1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.FlowLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.DefaultListModel;
|
---|
14 | import javax.swing.JDialog;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JList;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.event.ListSelectionEvent;
|
---|
21 | import javax.swing.event.ListSelectionListener;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
25 | import org.openstreetmap.josm.gui.SideButton;
|
---|
26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
27 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
28 |
|
---|
29 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This dialog lets the user select changesets from a list of changesets.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class CloseChangesetDialog extends JDialog {
|
---|
36 |
|
---|
37 | /** the list */
|
---|
38 | private JList lstOpenChangesets;
|
---|
39 | /** true if the user cancelled the dialog */
|
---|
40 | private boolean canceled;
|
---|
41 | /** the list model */
|
---|
42 | private DefaultListModel model;
|
---|
43 |
|
---|
44 | protected JPanel buildTopPanel() {
|
---|
45 | JPanel pnl = new JPanel();
|
---|
46 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
47 | pnl.setLayout(new BorderLayout());
|
---|
48 | pnl.add(new JLabel(tr("<html>Please select the changesets you want to close</html>")), BorderLayout.CENTER);
|
---|
49 | return pnl;
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected JPanel buildCenterPanel() {
|
---|
53 | JPanel pnl = new JPanel();
|
---|
54 | pnl.setLayout(new BorderLayout());
|
---|
55 | model = new DefaultListModel();
|
---|
56 | pnl.add(new JScrollPane(lstOpenChangesets = new JList(model)), BorderLayout.CENTER);
|
---|
57 | lstOpenChangesets.setCellRenderer(new ChangesetCellRenderer());
|
---|
58 | return pnl;
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected JPanel buildSouthPanel() {
|
---|
62 | JPanel pnl = new JPanel();
|
---|
63 | pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
|
---|
64 |
|
---|
65 | // -- close action
|
---|
66 | CloseAction closeAction = new CloseAction();
|
---|
67 | lstOpenChangesets.addListSelectionListener(closeAction);
|
---|
68 | pnl.add(new SideButton(closeAction));
|
---|
69 | pnl.add(new SideButton(new CancelAction()));
|
---|
70 | return pnl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected void build() {
|
---|
74 | setTitle(tr("Open changesets"));
|
---|
75 | getContentPane().setLayout(new BorderLayout());
|
---|
76 | getContentPane().add(buildTopPanel(), BorderLayout.NORTH);
|
---|
77 | getContentPane().add(buildCenterPanel(), BorderLayout.CENTER);
|
---|
78 | getContentPane().add(buildSouthPanel(), BorderLayout.SOUTH);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public void setVisible(boolean visible) {
|
---|
83 | if (visible) {
|
---|
84 | new WindowGeometry(
|
---|
85 | getClass().getName() + ".geometry",
|
---|
86 | WindowGeometry.centerInWindow(Main.parent, new Dimension(300,300))
|
---|
87 | ).apply(this);
|
---|
88 | } else {
|
---|
89 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
90 | }
|
---|
91 | super.setVisible(visible);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public CloseChangesetDialog() {
|
---|
95 | super(JOptionPane.getFrameForComponent(Main.parent), true /* modal */);
|
---|
96 | build();
|
---|
97 | }
|
---|
98 |
|
---|
99 | class CloseAction extends AbstractAction implements ListSelectionListener {
|
---|
100 | public CloseAction() {
|
---|
101 | putValue(NAME, tr("Close changesets"));
|
---|
102 | //putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
103 | putValue(SHORT_DESCRIPTION, tr("Close the selected open changesets"));
|
---|
104 | refreshEnabledState();
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void actionPerformed(ActionEvent e) {
|
---|
108 | setCanceled(false);
|
---|
109 | setVisible(false);
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected void refreshEnabledState() {
|
---|
113 | setEnabled(lstOpenChangesets.getSelectedValues() != null && lstOpenChangesets.getSelectedValues().length > 0);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void valueChanged(ListSelectionEvent e) {
|
---|
117 | refreshEnabledState();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | class CancelAction extends AbstractAction {
|
---|
122 |
|
---|
123 | public CancelAction() {
|
---|
124 | putValue(NAME, tr("Cancel"));
|
---|
125 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
126 | putValue(SHORT_DESCRIPTION, tr("Cancel closing of changesets"));
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void actionPerformed(ActionEvent e) {
|
---|
130 | setCanceled(true);
|
---|
131 | setVisible(false);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Replies true if this dialog was canceled
|
---|
137 | * @return true if this dialog was canceled
|
---|
138 | */
|
---|
139 | public boolean isCanceled() {
|
---|
140 | return canceled;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Sets whether this dialog is canceled
|
---|
145 | *
|
---|
146 | * @param canceled true, if this dialog is canceld
|
---|
147 | */
|
---|
148 | protected void setCanceled(boolean canceled) {
|
---|
149 | this.canceled = canceled;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Sets the collection of changesets to be displayed
|
---|
154 | *
|
---|
155 | * @param changesets the collection of changesets. Assumes an empty collection if null
|
---|
156 | */
|
---|
157 | public void setChangesets(Collection<Changeset> changesets) {
|
---|
158 | if (changesets == null) {
|
---|
159 | changesets = new ArrayList<Changeset>();
|
---|
160 | }
|
---|
161 | model.removeAllElements();
|
---|
162 | for (Changeset cs: changesets) {
|
---|
163 | model.addElement(cs);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Replies a collection with the changesets the user selected.
|
---|
169 | * Never null, but may be empty.
|
---|
170 | *
|
---|
171 | * @return a collection with the changesets the user selected.
|
---|
172 | */
|
---|
173 | public Collection<Changeset> getSelectedChangesets() {
|
---|
174 | Object [] sel = lstOpenChangesets.getSelectedValues();
|
---|
175 | ArrayList<Changeset> ret = new ArrayList<Changeset>();
|
---|
176 | for (Object o: sel) {
|
---|
177 | ret.add((Changeset)o);
|
---|
178 | }
|
---|
179 | return ret;
|
---|
180 | }
|
---|
181 | }
|
---|