source: josm/trunk/src/org/openstreetmap/josm/gui/io/CloseChangesetDialog.java@ 5998

Last change on this file since 5998 was 5998, checked in by Don-vip, 12 years ago

fix #8775 - IllegalComponentStateException when remembering position of dialogs not displayed on screen

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