source: josm/trunk/src/org/openstreetmap/josm/gui/PleaseWaitRunnable.java@ 1670

Last change on this file since 1670 was 1670, checked in by Gubaer, 15 years ago

fixed: bug in OsmApi.getOsmApi()
cleanup: exception handling in interfacing with OSM API
new: new action for updating individual elements with the their current state on the server (including new menu item in the file menu)
new: improved user feedback in case of conflicts
new: handles 410 Gone conflicts when uploading a changeset
new: undoable command for "purging" a primitive from the current dataset (necessary if the primitive is already deleted on the server and the user wants to remove it from its local dataset)
new: undoable command for "undeleting" an already deleted primitive on the server (kind of "cloning")
new: after a full upload, checks whether there are primitives in the local dataset which might be deleted on the server.
new: data structures for history data
new: history download support in io package

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.EventQueue;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import java.io.FileNotFoundException;
12import java.io.IOException;
13import java.lang.reflect.InvocationTargetException;
14
15import javax.swing.JOptionPane;
16import javax.swing.SwingUtilities;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22/**
23 * Instanced of this thread will display a "Please Wait" message in middle of JOSM
24 * to indicate a progress being executed.
25 *
26 * @author Imi
27 */
28public abstract class PleaseWaitRunnable implements Runnable {
29 public boolean silent = false;
30 public String errorMessage;
31
32 private boolean closeDialogCalled = false;
33 private boolean cancelled = false;
34 private boolean ignoreException;
35
36 private final String title;
37
38 /**
39 * Create the runnable object with a given message for the user.
40 */
41 public PleaseWaitRunnable(String title) {
42 this(title, false);
43 }
44
45 /**
46 * Create the runnable object with a given message for the user.
47 * @param title Message for user
48 * @param ignoreException If true, exception will be propaged to calling code. If false then
49 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
50 * then use false unless you read result of task (because exception will get lost if you don't)
51 */
52 public PleaseWaitRunnable(String title, boolean ignoreException) {
53 this.title = title;
54 this.ignoreException = ignoreException;
55 Main.pleaseWaitDlg.cancel.addActionListener(new ActionListener(){
56 public void actionPerformed(ActionEvent e) {
57 if (!cancelled) {
58 cancelled = true;
59 cancel();
60 }
61 }
62 });
63 Main.pleaseWaitDlg.addWindowListener(new WindowAdapter(){
64 @Override public void windowClosing(WindowEvent e) {
65 if (!closeDialogCalled) {
66 if (!cancelled) {
67 cancelled = true;
68 cancel();
69 }
70 closeDialog();
71 }
72 }
73 });
74 }
75
76 public final void run() {
77 try {
78 try {
79 if (cancelled)
80 return; // since realRun isn't executed, do not call to finish
81
82 // reset dialog state
83 Main.pleaseWaitDlg.setTitle(title);
84 Main.pleaseWaitDlg.cancel.setEnabled(true);
85 Main.pleaseWaitDlg.setCustomText("");
86 errorMessage = null;
87 closeDialogCalled = false;
88
89 // show the dialog
90 synchronized (this) {
91 EventQueue.invokeLater(new Runnable() {
92 public void run() {
93 synchronized (PleaseWaitRunnable.this) {
94 PleaseWaitRunnable.this.notifyAll();
95 }
96 Main.pleaseWaitDlg.setVisible(true);
97 }
98 });
99 try {wait();} catch (InterruptedException e) {}
100 }
101
102 realRun();
103 } catch (SAXException x) {
104 x.printStackTrace();
105 errorMessage = tr("Error while parsing")+": "+x.getMessage();
106 } catch (FileNotFoundException x) {
107 x.printStackTrace();
108 errorMessage = tr("File not found")+": "+x.getMessage();
109 } catch (IOException x) {
110 x.printStackTrace();
111 errorMessage = x.getMessage();
112 } catch(OsmTransferException x) {
113 x.printStackTrace();
114 if (x.getCause() != null) {
115 errorMessage = x.getCause().getMessage();
116 } else {
117 errorMessage = x.getMessage();
118 }
119 } finally {
120 closeDialog();
121 }
122 } catch (final Throwable e) {
123 if (!ignoreException) {
124 // Exception has to thrown in EDT to be shown to user
125 SwingUtilities.invokeLater(new Runnable() {
126 public void run() {
127 throw new RuntimeException(e);
128 }
129 });
130 }
131 }
132 }
133
134 /**
135 * User pressed cancel button.
136 */
137 protected abstract void cancel();
138
139 /**
140 * Called in the worker thread to do the actual work. When any of the
141 * exception is thrown, a message box will be displayed and closeDialog
142 * is called. finish() is called in any case.
143 */
144 protected abstract void realRun() throws SAXException, IOException, OsmTransferException;
145
146 /**
147 * Finish up the data work. Is guaranteed to be called if realRun is called.
148 * Finish is called in the gui thread just after the dialog disappeared.
149 */
150 protected abstract void finish();
151
152 /**
153 * Close the dialog. Usually called from worker thread.
154 */
155 public void closeDialog() {
156 if (closeDialogCalled)
157 return;
158 closeDialogCalled = true;
159 try {
160 Runnable runnable = new Runnable(){
161 public void run() {
162 try {
163 finish();
164 } finally {
165 Main.pleaseWaitDlg.setVisible(false);
166 Main.pleaseWaitDlg.dispose();
167 }
168 if (errorMessage != null && !silent) {
169 JOptionPane.showMessageDialog(Main.parent, errorMessage);
170 }
171 }
172 };
173
174 // make sure, this is called in the dispatcher thread ASAP
175 if (EventQueue.isDispatchThread()) {
176 runnable.run();
177 } else {
178 EventQueue.invokeAndWait(runnable);
179 }
180
181 } catch (InterruptedException e) {
182 } catch (InvocationTargetException e) {
183 throw new RuntimeException(e);
184 }
185 }
186}
Note: See TracBrowser for help on using the repository browser.