1 11 package org.eclipse.jface.wizard; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IProgressMonitorWithBlocking; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.jface.dialogs.ProgressIndicator; 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.graphics.Font; 21 import org.eclipse.swt.graphics.FontMetrics; 22 import org.eclipse.swt.graphics.GC; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Event; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Layout; 30 import org.eclipse.swt.widgets.Listener; 31 32 39 public class ProgressMonitorPart extends Composite implements 40 IProgressMonitorWithBlocking { 41 42 43 protected Label fLabel; 44 45 46 protected String fTaskName; 47 48 49 protected String fSubTaskName; 50 51 52 protected ProgressIndicator fProgressIndicator; 53 54 55 protected Control fCancelComponent; 56 57 58 protected boolean fIsCanceled; 59 60 61 protected IStatus blockedStatus; 62 63 64 protected Listener fCancelListener = new Listener() { 65 public void handleEvent(Event e) { 66 setCanceled(true); 67 if (fCancelComponent != null) { 68 fCancelComponent.setEnabled(false); 69 } 70 } 71 }; 72 73 80 public ProgressMonitorPart(Composite parent, Layout layout) { 81 this(parent, layout, SWT.DEFAULT); 82 } 83 84 92 public ProgressMonitorPart(Composite parent, Layout layout, 93 int progressIndicatorHeight) { 94 super(parent, SWT.NONE); 95 initialize(layout, progressIndicatorHeight); 96 } 97 98 104 public void attachToCancelComponent(Control cancelComponent) { 105 Assert.isNotNull(cancelComponent); 106 fCancelComponent = cancelComponent; 107 fCancelComponent.addListener(SWT.Selection, fCancelListener); 108 } 109 110 114 public void beginTask(String name, int totalWork) { 115 fTaskName = name; 116 updateLabel(); 117 if (totalWork == IProgressMonitor.UNKNOWN || totalWork == 0) { 118 fProgressIndicator.beginAnimatedTask(); 119 } else { 120 fProgressIndicator.beginTask(totalWork); 121 } 122 } 123 124 128 public void done() { 129 fLabel.setText(""); fProgressIndicator.sendRemainingWork(); 131 fProgressIndicator.done(); 132 } 133 134 141 protected static String escapeMetaCharacters(String in) { 142 if (in == null || in.indexOf('&') < 0) { 143 return in; 144 } 145 int length = in.length(); 146 StringBuffer out = new StringBuffer (length + 1); 147 for (int i = 0; i < length; i++) { 148 char c = in.charAt(i); 149 if (c == '&') { 150 out.append("&&"); } else { 152 out.append(c); 153 } 154 } 155 return out.toString(); 156 } 157 158 165 protected void initialize(Layout layout, int progressIndicatorHeight) { 166 if (layout == null) { 167 GridLayout l = new GridLayout(); 168 l.marginWidth = 0; 169 l.marginHeight = 0; 170 l.numColumns = 1; 171 layout = l; 172 } 173 setLayout(layout); 174 175 fLabel = new Label(this, SWT.LEFT); 176 fLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 177 178 if (progressIndicatorHeight == SWT.DEFAULT) { 179 GC gc = new GC(fLabel); 180 FontMetrics fm = gc.getFontMetrics(); 181 gc.dispose(); 182 progressIndicatorHeight = fm.getHeight(); 183 } 184 185 fProgressIndicator = new ProgressIndicator(this); 186 GridData gd = new GridData(); 187 gd.horizontalAlignment = GridData.FILL; 188 gd.grabExcessHorizontalSpace = true; 189 gd.verticalAlignment = GridData.CENTER; 190 gd.heightHint = progressIndicatorHeight; 191 fProgressIndicator.setLayoutData(gd); 192 } 193 194 198 public void internalWorked(double work) { 199 fProgressIndicator.worked(work); 200 } 201 202 206 public boolean isCanceled() { 207 return fIsCanceled; 208 } 209 210 215 public void removeFromCancelComponent(Control cc) { 216 Assert.isTrue(fCancelComponent == cc && fCancelComponent != null); 217 fCancelComponent.removeListener(SWT.Selection, fCancelListener); 218 fCancelComponent = null; 219 } 220 221 225 public void setCanceled(boolean b) { 226 fIsCanceled = b; 227 } 228 229 232 public void setFont(Font font) { 233 super.setFont(font); 234 fLabel.setFont(font); 235 fProgressIndicator.setFont(font); 236 } 237 238 242 public void setTaskName(String name) { 243 fTaskName = name; 244 updateLabel(); 245 } 246 247 251 public void subTask(String name) { 252 fSubTaskName = name; 253 updateLabel(); 254 } 255 256 259 protected void updateLabel() { 260 if (blockedStatus == null) { 261 String text = taskLabel(); 262 fLabel.setText(text); 263 } else { 264 fLabel.setText(blockedStatus.getMessage()); 265 } 266 267 fLabel.update(); 269 } 270 271 275 private String taskLabel() { 276 String text = fSubTaskName == null ? "" : fSubTaskName; if (fTaskName != null && fTaskName.length() > 0) { 278 text = JFaceResources.format( 279 "Set_SubTask", new Object [] { fTaskName, text }); } 281 return escapeMetaCharacters(text); 282 } 283 284 288 public void worked(int work) { 289 internalWorked(work); 290 } 291 292 295 public void clearBlocked() { 296 blockedStatus = null; 297 updateLabel(); 298 299 } 300 301 304 public void setBlocked(IStatus reason) { 305 blockedStatus = reason; 306 updateLabel(); 307 308 } 309 } 310 | Popular Tags |