Changeset 4718 in josm for trunk/src/org/openstreetmap/josm/gui
- Timestamp:
- 2011-12-27T12:56:27+01:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 3 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MapStatus.java
r4604 r4718 32 32 import javax.swing.JLabel; 33 33 import javax.swing.JPanel; 34 import javax.swing.JProgressBar; 34 35 import javax.swing.JScrollPane; 35 36 import javax.swing.JTextField; 36 37 import javax.swing.Popup; 37 38 import javax.swing.PopupFactory; 39 import javax.swing.UIManager; 38 40 39 41 import org.openstreetmap.josm.Main; … … 44 46 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 45 47 import org.openstreetmap.josm.gui.help.Helpful; 48 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 49 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor.ProgressMonitorDialog; 46 50 import org.openstreetmap.josm.tools.GBC; 47 51 import org.openstreetmap.josm.tools.ImageProvider; … … 95 99 } 96 100 101 public class BackgroundProgressMonitor implements ProgressMonitorDialog { 102 103 private String title; 104 private String customText; 105 106 private void updateText() { 107 if (customText != null && !customText.isEmpty()) { 108 progressBar.setToolTipText(tr("{0} ({1})", title, customText)); 109 } else { 110 progressBar.setToolTipText(title); 111 } 112 } 113 114 public void setVisible(boolean visible) { 115 progressBar.setVisible(visible); 116 } 117 118 public void updateProgress(int progress) { 119 progressBar.setValue(progress); 120 MapStatus.this.doLayout(); 121 } 122 123 public void setCustomText(String text) { 124 this.customText = text; 125 updateText(); 126 } 127 128 public void setTitle(String text) { 129 this.title = text; 130 updateText(); 131 } 132 133 public void setIndeterminate(boolean newValue) { 134 UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100); 135 progressBar.setIndeterminate(newValue); 136 } 137 138 @Override 139 public void appendLogMessage(String message) { 140 if (message != null && !message.isEmpty()) { 141 System.out.println("appendLogMessage not implemented for background tasks. Message was: " + message); 142 } 143 } 144 145 } 146 97 147 final ImageLabel lonText = new ImageLabel("lon", tr("The geographic longitude at the mouse pointer."), 11); 98 148 final ImageLabel nameText = new ImageLabel("name", tr("The name of the object at the mouse pointer."), 20); … … 102 152 final ImageLabel headingText = new ImageLabel("heading", tr("The (compass) heading of the line segment being drawn."), 6); 103 153 final ImageLabel distText = new ImageLabel("dist", tr("The length of the new way segment being drawn."), 10); 154 final JProgressBar progressBar = new JProgressBar(); 155 public final BackgroundProgressMonitor progressMonitor = new BackgroundProgressMonitor(); 104 156 105 157 /** … … 658 710 helpText.setEditable(false); 659 711 add(nameText, GBC.std().insets(3,0,0,0)); 660 add(helpText, GBC.eol().insets(3,0,0,0).fill(GBC.HORIZONTAL)); 712 add(helpText, GBC.std().insets(3,0,0,0).fill(GBC.HORIZONTAL)); 713 714 progressBar.setMaximum(PleaseWaitProgressMonitor.PROGRESS_BAR_MAX); 715 { 716 GBC gbc = GBC.eol(); 717 gbc.ipadx = 100; 718 add(progressBar,gbc); 719 } 720 progressBar.addMouseListener(new MouseAdapter() { 721 @Override 722 public void mouseClicked(MouseEvent e) { 723 PleaseWaitProgressMonitor monitor = Main.currentProgressMonitor; 724 if (monitor != null) { 725 monitor.showForegroundDialog(); 726 } 727 } 728 }); 661 729 662 730 // The background thread -
trunk/src/org/openstreetmap/josm/gui/PleaseWaitDialog.java
r3501 r4718 24 24 25 25 import org.openstreetmap.josm.Main; 26 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor.ProgressMonitorDialog; 26 27 import org.openstreetmap.josm.tools.GBC; 27 28 import org.openstreetmap.josm.tools.ImageProvider; 28 29 29 public class PleaseWaitDialog extends JDialog {30 public class PleaseWaitDialog extends JDialog implements ProgressMonitorDialog { 30 31 31 32 private final JProgressBar progressBar = new JProgressBar(); … … 34 35 private final JLabel customText = new JLabel(""); 35 36 public final BoundedRangeModel progress = progressBar.getModel(); 36 private JButton btnCancel; 37 private JButton btnCancel; 38 private JButton btnInBackground; 37 39 /** the text area and the scroll pane for the log */ 38 40 private JTextArea taLog = new JTextArea(5,50); … … 46 48 pane.add(customText, GBC.eol().fill(GBC.HORIZONTAL)); 47 49 pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL)); 50 JPanel buttons = new JPanel(); 51 buttons.setLayout(new GridBagLayout()); 48 52 btnCancel = new JButton(tr("Cancel")); 49 53 btnCancel.setIcon(ImageProvider.get("cancel")); 50 54 btnCancel.setToolTipText(tr("Click to cancel the current operation")); 51 pane.add(btnCancel, GBC.eol().anchor(GBC.CENTER)); 55 buttons.add(btnCancel); 56 btnInBackground = new JButton(tr("In background")); 57 btnInBackground.setToolTipText(tr("Click to run job in background")); 58 buttons.add(btnInBackground, GBC.std().fill(GBC.VERTICAL).insets(5, 0, 0, 0)); 59 pane.add(buttons, GBC.eol().anchor(GBC.CENTER)); 52 60 GridBagConstraints gc = GBC.eol().fill(GBC.BOTH); 53 61 gc.weighty = 1.0; … … 132 140 } 133 141 142 public void setInBackgroundPossible(boolean value) { 143 btnInBackground.setVisible(value); 144 } 145 134 146 /** 135 147 * Installs a callback for the cancel button. If callback is null, all action listeners … … 148 160 } 149 161 } 162 163 /** 164 * Installs a callback for the "In background" button. If callback is null, all action listeners 165 * are removed from the cancel button. 166 * 167 * @param callback the cancel callback 168 */ 169 public void setInBackgroundCallback(ActionListener callback) { 170 if (callback == null) { 171 ActionListener[] listeners = btnInBackground.getActionListeners(); 172 for (ActionListener l: listeners) { 173 btnInBackground.removeActionListener(l); 174 } 175 } else { 176 btnInBackground.addActionListener(callback); 177 } 178 } 179 180 @Override 181 public void updateProgress(int progress) { 182 this.progress.setValue(progress); 183 } 184 150 185 } -
trunk/src/org/openstreetmap/josm/gui/PleaseWaitRunnable.java
r4310 r4718 11 11 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 12 12 import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener; 13 import org.openstreetmap.josm.gui.progress.ProgressTaskId; 13 14 import org.openstreetmap.josm.io.OsmTransferException; 14 15 import org.openstreetmap.josm.tools.BugReportExceptionHandler; … … 35 36 this(title, false); 36 37 } 37 38 38 /** 39 39 * Create the runnable object with a given message for the user. … … 73 73 private void doRealRun() { 74 74 try { 75 ProgressTaskId oldTaskId = null; 75 76 try { 76 77 progressMonitor.addCancelListener(this); 77 78 progressMonitor.beginTask(title); 79 oldTaskId = progressMonitor.getProgressTaskId(); 80 progressMonitor.setProgressTaskId(canRunInBackground()); 78 81 try { 79 82 realRun(); … … 92 95 progressMonitor.finishTask(); 93 96 progressMonitor.removeCancelListener(this); 97 progressMonitor.setProgressTaskId(oldTaskId); 94 98 if (progressMonitor instanceof PleaseWaitProgressMonitor) { 95 99 ((PleaseWaitProgressMonitor)progressMonitor).close(); 96 100 } 101 afterFinish(); 97 102 } 98 103 } catch (final Exception e) { … … 110 115 } 111 116 } 117 } 118 119 /** 120 * Can be overriden if something needs to run after progress monitor is closed. 121 */ 122 protected void afterFinish() { 123 112 124 } 113 125 … … 152 164 return progressMonitor; 153 165 } 166 167 /** 168 * Task can run in background if returned value <> null. Note that it's tasks responsibility 169 * to ensure proper synchronization, PleaseWaitRunnable doesn't with it. 170 * @return If returned value is <> null then task can run in background. TaskId could be used in future for "Always run in background" checkbox 171 */ 172 public ProgressTaskId canRunInBackground() { 173 return null; 174 } 154 175 } -
trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java
r4701 r4718 1021 1021 getClass().getName() + ".geometry", 1022 1022 WindowGeometry.centerInWindow(Main.parent, new Dimension(1000, 500)) 1023 );1023 ); 1024 1024 ed.showDialog(); 1025 1025 int v = ed.getValue(); … … 1177 1177 @Override 1178 1178 protected void finish() { 1179 } 1180 1181 @Override 1182 protected void afterFinish() { 1179 1183 if(cancel) 1180 1184 return; … … 1274 1278 1275 1279 if (toDownload.size() > 1) { 1276 // hide progress dialog before displaying another pop up. Really closing the1277 // dialog will be handled by PleaseWaitRunnable.1278 if (progressMonitor instanceof PleaseWaitProgressMonitor) {1279 ((PleaseWaitProgressMonitor) progressMonitor).getDialog().setVisible(false);1280 }1281 1282 1280 int ret = JOptionPane.showConfirmDialog( 1283 1281 Main.parent, -
trunk/src/org/openstreetmap/josm/gui/progress/AbstractProgressMonitor.java
r4310 r4718 6 6 import java.util.LinkedList; 7 7 import java.util.Queue; 8 9 import javax.swing.SwingUtilities; 8 10 9 11 public abstract class AbstractProgressMonitor implements ProgressMonitor { … … 61 63 } 62 64 65 protected void doInEDT(Runnable runnable) { 66 if (SwingUtilities.isEventDispatchThread()) { 67 runnable.run(); 68 } else { 69 SwingUtilities.invokeLater(runnable); 70 } 71 } 72 63 73 /*======= 64 74 * Tasks … … 69 79 } 70 80 71 public synchronized void beginTask( finalString title, int ticks) {81 public synchronized void beginTask(String title, int ticks) { 72 82 this.taskTitle = title; 73 83 checkState(State.INIT); -
trunk/src/org/openstreetmap/josm/gui/progress/ChildProgress.java
r4642 r4718 55 55 parent.childFinished(this); 56 56 } 57 58 @Override 59 public void setProgressTaskId(ProgressTaskId taskId) { 60 parent.setProgressTaskId(taskId); 61 } 62 63 @Override 64 public ProgressTaskId getProgressTaskId() { 65 return parent.getProgressTaskId(); 66 } 57 67 } -
trunk/src/org/openstreetmap/josm/gui/progress/NullProgressMonitor.java
r4310 r4718 80 80 return 0; 81 81 } 82 83 @Override 84 public void setProgressTaskId(ProgressTaskId taskId) { 85 } 86 87 @Override 88 public ProgressTaskId getProgressTaskId() { 89 return null; 90 } 82 91 } -
trunk/src/org/openstreetmap/josm/gui/progress/PleaseWaitProgressMonitor.java
r4684 r4718 4 4 import java.awt.Component; 5 5 import java.awt.Dialog; 6 import java.awt.EventQueue;7 6 import java.awt.Frame; 8 7 import java.awt.Window; … … 16 15 17 16 import org.openstreetmap.josm.Main; 17 import org.openstreetmap.josm.gui.MapFrame; 18 import org.openstreetmap.josm.gui.MapStatus.BackgroundProgressMonitor; 18 19 import org.openstreetmap.josm.gui.PleaseWaitDialog; 19 20 20 21 public class PleaseWaitProgressMonitor extends AbstractProgressMonitor { 21 22 22 private static final int PROGRESS_BAR_MAX = 100; 23 /** 24 * Implemented by both foreground dialog and background progress dialog (in status bar) 25 */ 26 public interface ProgressMonitorDialog { 27 void setVisible(boolean visible); 28 void updateProgress(int progress); 29 void setCustomText(String text); 30 void setTitle(String text); 31 void setIndeterminate(boolean newValue); 32 void appendLogMessage(String message); 33 } 34 35 public static final int PROGRESS_BAR_MAX = 100; 23 36 private final Window dialogParent; 24 37 private int currentProgressValue = 0; 25 38 39 private boolean isInBackground; 26 40 private PleaseWaitDialog dialog; 27 41 private String windowTitle; 28 42 protected ProgressTaskId taskId; 43 29 44 private boolean cancelable; 45 46 private ProgressMonitorDialog getDialog() { 47 48 BackgroundProgressMonitor backgroundMonitor = null; 49 MapFrame map = Main.map; 50 if (map != null) { 51 backgroundMonitor = map.statusLine.progressMonitor; 52 } 53 54 if (backgroundMonitor != null) { 55 backgroundMonitor.setVisible(isInBackground); 56 } 57 if (dialog != null) { 58 dialog.setVisible(!isInBackground || backgroundMonitor == null); 59 } 60 61 if (isInBackground && backgroundMonitor != null) { 62 backgroundMonitor.setVisible(true); 63 if (dialog != null) { 64 dialog.setVisible(false); 65 } 66 return backgroundMonitor; 67 } else if (backgroundMonitor != null) { 68 backgroundMonitor.setVisible(false); 69 if (dialog != null) { 70 dialog.setVisible(true); 71 } 72 return dialog; 73 } else if (dialog != null) { 74 dialog.setVisible(true); 75 return dialog; 76 } else 77 return null; 78 } 30 79 31 80 public PleaseWaitProgressMonitor() { … … 55 104 }; 56 105 106 private ActionListener inBackgroundListener = new ActionListener() { 107 @Override 108 public void actionPerformed(ActionEvent e) { 109 isInBackground = true; 110 ProgressMonitorDialog dialog = getDialog(); 111 if (dialog != null) { 112 dialog.setVisible(true); 113 } 114 } 115 }; 116 57 117 private WindowListener windowListener = new WindowAdapter(){ 58 118 @Override public void windowClosing(WindowEvent e) { … … 69 129 } 70 130 71 private void doInEDT(Runnable runnable) {72 EventQueue.invokeLater(runnable);73 }74 75 131 @Override 76 132 public void doBeginTask() { 77 133 doInEDT(new Runnable() { 78 134 public void run() { 135 Main.currentProgressMonitor = PleaseWaitProgressMonitor.this; 79 136 if (dialogParent instanceof Frame && dialog == null) { 80 137 dialog = new PleaseWaitDialog(dialogParent); … … 89 146 dialog.setCancelEnabled(cancelable); 90 147 dialog.setCancelCallback(cancelListener); 148 dialog.setInBackgroundCallback(inBackgroundListener); 91 149 dialog.setCustomText(""); 92 150 dialog.addWindowListener(windowListener); … … 109 167 doInEDT(new Runnable() { 110 168 public void run() { 111 dialog.progress.setValue(currentProgressValue); 169 ProgressMonitorDialog dialog = getDialog(); 170 if (dialog != null) { 171 dialog.updateProgress(currentProgressValue); 172 } 112 173 } 113 174 }); … … 120 181 doInEDT(new Runnable() { 121 182 public void run() { 122 dialog.setCustomText(title); 183 ProgressMonitorDialog dialog = getDialog(); 184 if (dialog != null) { 185 dialog.setCustomText(title); 186 } 123 187 } 124 188 }); … … 130 194 doInEDT(new Runnable() { 131 195 public void run() { 132 dialog.currentAction.setText(title); 196 ProgressMonitorDialog dialog = getDialog(); 197 if (dialog != null) { 198 dialog.setTitle(title); 199 } 133 200 } 134 201 }); … … 139 206 doInEDT(new Runnable() { 140 207 public void run() { 141 if (value && dialog.progress.getValue() == 0) { 142 // Enable only if progress is at the beginning. Doing intermediate progress in the middle 143 // will hide already reached progress 144 dialog.setIndeterminate(true); 145 } else { 146 dialog.setIndeterminate(false); 208 // Enable only if progress is at the beginning. Doing intermediate progress in the middle 209 // will hide already reached progress 210 ProgressMonitorDialog dialog = getDialog(); 211 if (dialog != null) { 212 dialog.setIndeterminate(value && PleaseWaitProgressMonitor.this.dialog.progress.getValue() == 0); 147 213 } 148 214 } … … 154 220 doInEDT(new Runnable() { 155 221 public void run() { 156 dialog.appendLogMessage(message); 222 ProgressMonitorDialog dialog = getDialog(); 223 if (dialog != null) { 224 dialog.appendLogMessage(message); 225 } 157 226 } 158 227 }); … … 160 229 161 230 public void close() { 162 dialog.setVisible(false); 163 dialog.setCancelCallback(null); 164 dialog.removeWindowListener(windowListener); 165 dialog.dispose(); 166 dialog = null; 167 } 168 169 public Component getDialog() { 170 return dialog; 171 } 231 doInEDT(new Runnable() { 232 @Override 233 public void run() { 234 dialog.setVisible(false); 235 dialog.setCancelCallback(null); 236 dialog.setInBackgroundCallback(null); 237 dialog.removeWindowListener(windowListener); 238 dialog.dispose(); 239 dialog = null; 240 MapFrame map = Main.map; 241 if (map != null) { 242 map.statusLine.progressMonitor.setVisible(false); 243 } 244 Main.currentProgressMonitor = null; 245 } 246 }); 247 } 248 249 public void showForegroundDialog() { 250 isInBackground = false; 251 doInEDT(new Runnable() { 252 @Override 253 public void run() { 254 dialog.setInBackgroundPossible(PleaseWaitProgressMonitor.this.taskId != null && Main.isDisplayingMapView()); 255 getDialog(); 256 } 257 }); 258 259 } 260 261 @Override 262 public void setProgressTaskId(ProgressTaskId taskId) { 263 this.taskId = taskId; 264 doInEDT(new Runnable() { 265 @Override 266 public void run() { 267 dialog.setInBackgroundPossible(PleaseWaitProgressMonitor.this.taskId != null && Main.isDisplayingMapView()); 268 } 269 }); 270 } 271 272 @Override 273 public ProgressTaskId getProgressTaskId() { 274 return taskId; 275 } 276 172 277 } -
trunk/src/org/openstreetmap/josm/gui/progress/ProgressMonitor.java
r4310 r4718 50 50 */ 51 51 void beginTask(String title, int ticks); 52 52 53 /** 53 54 * Finish this progress monitor, close the dialog or inform the parent progress monitor … … 55 56 * then further calls are ignored) 56 57 */ 58 57 59 void finishTask(); 58 60 /** … … 126 128 */ 127 129 void appendLogMessage(String message); 130 131 /** 132 * Should be used only by PleaseWaitRunnable. If taskId <> null then "In background" button will be shown 133 * @param taskId 134 */ 135 void setProgressTaskId(ProgressTaskId taskId); 136 137 /** 138 * Should be used only by PleaseWaitRunnable 139 * @param taskId 140 */ 141 ProgressTaskId getProgressTaskId(); 128 142 } -
trunk/src/org/openstreetmap/josm/gui/progress/SwingRenderingProgressMonitor.java
r3083 r4718 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.progress; 3 4 import javax.swing.SwingUtilities;5 3 6 4 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 27 25 CheckParameterUtil.ensureParameterNotNull(delegate, "delegate"); 28 26 this.delegate = delegate; 29 }30 31 private void doInEDT(Runnable runnable) {32 if (SwingUtilities.isEventDispatchThread()) {33 runnable.run();34 } else {35 SwingUtilities.invokeLater(runnable);36 }37 27 } 38 28 … … 93 83 }); 94 84 } 85 86 @Override 87 public void setProgressTaskId(ProgressTaskId taskId) { 88 } 89 90 @Override 91 public ProgressTaskId getProgressTaskId() { 92 return null; 93 } 95 94 }
Note:
See TracChangeset
for help on using the changeset viewer.