1 11 package org.eclipse.search.internal.ui.util; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.Cursor; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Display; 29 import org.eclipse.swt.widgets.Label; 30 import org.eclipse.swt.widgets.Shell; 31 32 import org.eclipse.jface.dialogs.ControlEnableState; 33 import org.eclipse.jface.dialogs.IDialogConstants; 34 import org.eclipse.jface.dialogs.MessageDialog; 35 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 36 import org.eclipse.jface.dialogs.TrayDialog; 37 import org.eclipse.jface.operation.IRunnableContext; 38 import org.eclipse.jface.operation.IRunnableWithProgress; 39 import org.eclipse.jface.operation.ModalContext; 40 import org.eclipse.jface.wizard.ProgressMonitorPart; 41 42 import org.eclipse.search.internal.ui.SearchMessages; 43 44 45 public abstract class ExtendedDialogWindow extends TrayDialog implements IRunnableContext { 46 47 private Control fContents; 48 private Button fCancelButton; 49 private List fActionButtons; 50 private long fActiveRunningOperations; 52 53 private boolean fUseEmbeddedProgressMonitorPart; 55 private ProgressMonitorPart fProgressMonitorPart; 56 private MessageDialog fWindowClosingDialog; 57 private static final String FOCUS_CONTROL= "focusControl"; private Cursor fWaitCursor; 59 private Cursor fArrowCursor; 60 61 62 public ExtendedDialogWindow(Shell shell) { 63 super(shell); 64 fActionButtons= new ArrayList (); 65 66 setShellStyle(getShellStyle() | SWT.RESIZE); 67 } 68 69 71 74 public void setUseEmbeddedProgressMonitorPart(boolean enable) { 75 fUseEmbeddedProgressMonitorPart= enable; 76 } 77 78 86 protected boolean performAction(int buttonId) { 87 return true; 88 } 89 90 97 protected boolean performCancel() { 98 return true; 99 } 100 101 103 108 protected abstract Control createPageArea(Composite parent); 109 110 117 protected void createButtonsForButtonBar(Composite parent) { 118 fCancelButton= createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 119 } 120 121 protected Button createActionButton(Composite parent, int id, String label, 122 boolean defaultButton) { 123 Button actionButton= createButton(parent, id, label, defaultButton); 124 fActionButtons.add(actionButton); 125 return actionButton; 126 } 127 128 133 protected Control createDialogArea(Composite parent) { 134 Composite result= (Composite) super.createDialogArea(parent); 135 136 fContents= createPageArea(result); 137 fContents.setLayoutData(new GridData(GridData.FILL_BOTH)); 138 139 if (fUseEmbeddedProgressMonitorPart) { 140 fProgressMonitorPart= new ProgressMonitorPart(result, new GridLayout(), SWT.DEFAULT); 142 fProgressMonitorPart.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 143 fProgressMonitorPart.setVisible(false); 144 applyDialogFont(fProgressMonitorPart); 145 } 146 147 Label separator= new Label(result, SWT.SEPARATOR | SWT.HORIZONTAL); 148 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 149 150 return result; 151 } 152 153 protected void buttonPressed(int buttonId) { 154 switch (buttonId) { 155 case IDialogConstants.CANCEL_ID: 156 if (fActiveRunningOperations == 0) 157 close(); 158 break; 159 default: 160 if (performAction(buttonId)) 161 close(); 162 } 163 } 164 165 167 171 public void setPerformActionEnabled(boolean state) { 172 for (Iterator buttons = fActionButtons.iterator(); buttons.hasNext(); ) { 173 Button element = (Button) buttons.next(); 174 element.setEnabled(state); 175 } 176 } 177 178 180 181 184 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException , InterruptedException { 185 Object state= null; 188 try { 189 fActiveRunningOperations++; 190 state= aboutToStart(fork && cancelable); 191 if (fUseEmbeddedProgressMonitorPart) { 192 ModalContext.run(runnable, fork, fProgressMonitorPart, getShell().getDisplay()); 193 } else { 194 new ProgressMonitorDialog(getShell()).run(fork, cancelable, runnable); 195 } 196 } finally { 197 if (state != null) 198 stopped(state); 199 fActiveRunningOperations--; 200 } 201 } 202 203 210 protected synchronized Object aboutToStart(boolean enableCancelButton) { 211 HashMap savedState= null; 212 Shell shell= getShell(); 213 if (shell != null) { 214 Display d= shell.getDisplay(); 215 216 Control focusControl= d.getFocusControl(); 218 if (focusControl != null && focusControl.getShell() != shell) 219 focusControl= null; 220 221 fWaitCursor= new Cursor(d, SWT.CURSOR_WAIT); 223 setDisplayCursor(d, fWaitCursor); 224 225 fArrowCursor= new Cursor(d, SWT.CURSOR_ARROW); 227 fCancelButton.setCursor(fArrowCursor); 228 229 savedState= saveUIState(enableCancelButton); 231 if (focusControl != null) 232 savedState.put(FOCUS_CONTROL, focusControl); 233 234 if (fUseEmbeddedProgressMonitorPart) { 235 fProgressMonitorPart.attachToCancelComponent(fCancelButton); 237 fProgressMonitorPart.setVisible(true); 238 } 239 } 240 241 return savedState; 242 } 243 244 250 protected synchronized void stopped(Object savedState) { 251 Assert.isTrue( savedState instanceof HashMap ); 252 Shell shell= getShell(); 253 if (shell != null) { 254 if (fUseEmbeddedProgressMonitorPart) { 255 fProgressMonitorPart.setVisible(false); 256 fProgressMonitorPart.removeFromCancelComponent(fCancelButton); 257 } 258 259 HashMap state= (HashMap )savedState; 260 restoreUIState(state); 261 262 setDisplayCursor(shell.getDisplay(), null); 263 fCancelButton.setCursor(null); 264 fWaitCursor.dispose(); 265 fWaitCursor= null; 266 fArrowCursor.dispose(); 267 fArrowCursor= null; 268 Control focusControl= (Control)state.get(FOCUS_CONTROL); 269 if (focusControl != null && ! focusControl.isDisposed()) 270 focusControl.setFocus(); 271 } 272 } 273 274 private void setDisplayCursor(Display d, Cursor c) { 275 Shell[] shells= d.getShells(); 276 for (int i= 0; i < shells.length; i++) 277 shells[i].setCursor(c); 278 } 279 280 282 private void restoreUIState(HashMap state) { 283 restoreEnableState(fCancelButton, state); 284 for (Iterator actionButtons = fActionButtons.iterator(); actionButtons.hasNext(); ) { 285 Button button = (Button) actionButtons.next(); 286 restoreEnableState(button, state); 287 } 288 ControlEnableState pageState= (ControlEnableState)state.get("tabForm"); pageState.restore(); 290 } 291 292 295 protected void restoreEnableState(Control w, HashMap h) { 296 if (!w.isDisposed()) { 297 Boolean b= (Boolean )h.get(w); 298 if (b != null) 299 w.setEnabled(b.booleanValue()); 300 } 301 } 302 303 private HashMap saveUIState(boolean keepCancelEnabled) { 304 HashMap savedState= new HashMap (10); 305 saveEnableStateAndSet(fCancelButton, savedState, keepCancelEnabled); 306 for (Iterator actionButtons = fActionButtons.iterator(); actionButtons.hasNext(); ) { 307 Button button = (Button) actionButtons.next(); 308 saveEnableStateAndSet(button, savedState, false); 309 } 310 savedState.put("tabForm", ControlEnableState.disable(fContents)); 312 return savedState; 313 } 314 315 private void saveEnableStateAndSet(Control w, HashMap h, boolean enabled) { 316 if (!w.isDisposed()) { 317 h.put(w, new Boolean (w.isEnabled())); 318 w.setEnabled(enabled); 319 } 320 } 321 322 protected void handleShellCloseEvent() { 323 if (okToClose()) 324 super.handleShellCloseEvent(); 325 } 326 327 333 public boolean okToClose() { 334 if (fActiveRunningOperations > 0) { 335 synchronized (this) { 336 fWindowClosingDialog= createClosingDialog(); 337 } 338 fWindowClosingDialog.open(); 339 synchronized (this) { 340 fWindowClosingDialog= null; 341 } 342 return false; 343 } 344 return true; 345 } 346 347 private MessageDialog createClosingDialog() { 348 MessageDialog result= 349 new MessageDialog( 350 getShell(), 351 SearchMessages.SearchDialogClosingDialog_title, 352 null, 353 SearchMessages.SearchDialogClosingDialog_message, 354 MessageDialog.QUESTION, 355 new String [] {IDialogConstants.OK_LABEL}, 356 0); 357 return result; 358 } 359 360 364 protected Control getCancelComponent() { 365 return fCancelButton; 366 } 367 } 368 | Popular Tags |