KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > RefactoringWizardDialog2


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.NullProgressMonitor;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.StackLayout;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.graphics.Cursor;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36
37 import org.eclipse.jface.dialogs.ControlEnableState;
38 import org.eclipse.jface.dialogs.Dialog;
39 import org.eclipse.jface.dialogs.DialogSettings;
40 import org.eclipse.jface.dialogs.IDialogConstants;
41 import org.eclipse.jface.dialogs.IDialogSettings;
42 import org.eclipse.jface.dialogs.IMessageProvider;
43 import org.eclipse.jface.operation.IRunnableWithProgress;
44 import org.eclipse.jface.operation.ModalContext;
45 import org.eclipse.jface.wizard.IWizardContainer;
46 import org.eclipse.jface.wizard.IWizardPage;
47 import org.eclipse.jface.wizard.ProgressMonitorPart;
48
49 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
50
51 public class RefactoringWizardDialog2 extends Dialog implements IWizardContainer {
52
53     private RefactoringWizard fWizard;
54     private IWizardPage fCurrentPage;
55     private IWizardPage fVisiblePage;
56     
57     private boolean fMakeNextButtonDefault;
58     
59     private PageBook fPageContainer;
60     private PageBook fStatusContainer;
61     private MessageBox fMessageBox;
62     private ProgressMonitorPart fProgressMonitorPart;
63     private int fActiveRunningOperations;
64     private Cursor fWaitCursor;
65     private Cursor fArrowCursor;
66
67     private static final int PREVIEW_ID= IDialogConstants.CLIENT_ID + 1;
68
69     private int fPreviewWidth;
70     private int fPreviewHeight;
71     private IDialogSettings fSettings;
72     private boolean fHasAdditionalPages;
73     private Rectangle fInitialSize;
74     
75     private static final String JavaDoc DIALOG_SETTINGS= "RefactoringWizard.preview"; //$NON-NLS-1$
76
private static final String JavaDoc WIDTH= "width"; //$NON-NLS-1$
77
private static final String JavaDoc HEIGHT= "height"; //$NON-NLS-1$
78

79     private static class MessageBox extends Composite {
80         private Label fImage;
81         private Label fText;
82         public MessageBox(Composite parent, int style) {
83             super(parent, style);
84             GridLayout layout= new GridLayout();
85             layout.numColumns= 2;
86             setLayout(layout);
87             fImage= new Label(this, SWT.NONE);
88             fImage.setImage(RefactoringPluginImages.get(RefactoringPluginImages.IMG_OBJS_REFACTORING_INFO));
89             Point size= fImage.computeSize(SWT.DEFAULT, SWT.DEFAULT);
90             GridData gd= new GridData();
91             gd.verticalAlignment= SWT.TOP;
92             gd.widthHint= size.x;
93             gd.heightHint= size.y;
94             fImage.setLayoutData(gd);
95             fImage.setImage(null);
96             fText= new Label(this, SWT.WRAP);
97             fText.setText(" \n "); //$NON-NLS-1$
98
size= fText.computeSize(SWT.DEFAULT, SWT.DEFAULT);
99             gd= new GridData(GridData.FILL_HORIZONTAL);
100             gd.heightHint= size.y;
101             gd.verticalAlignment= SWT.TOP;
102             fText.setLayoutData(gd);
103         }
104         public void setMessage(IWizardPage page) {
105             String JavaDoc msg= page.getErrorMessage();
106             int type= IMessageProvider.ERROR;
107             if (msg == null || msg.length() == 0) {
108                 msg= page.getMessage();
109                 type= IMessageProvider.NONE;
110             if (msg != null && page instanceof IMessageProvider)
111                 type = ((IMessageProvider)page).getMessageType();
112             }
113             Image image= null;
114             switch (type) {
115                 case IMessageProvider.INFORMATION:
116                     image= RefactoringPluginImages.get(RefactoringPluginImages.IMG_OBJS_REFACTORING_INFO);
117                     break;
118                 case IMessageProvider.WARNING:
119                     image= RefactoringPluginImages.get(RefactoringPluginImages.IMG_OBJS_REFACTORING_WARNING);
120                     break;
121                 case IMessageProvider.ERROR:
122                     image= RefactoringPluginImages.get(RefactoringPluginImages.IMG_OBJS_REFACTORING_ERROR);
123                     break;
124             }
125             if (msg == null)
126                 msg= ""; //$NON-NLS-1$
127
fText.setText(escapeAmpersands(msg));
128             if (image == null && msg.length() > 0)
129                 image= RefactoringPluginImages.get(RefactoringPluginImages.IMG_OBJS_REFACTORING_INFO);
130             fImage.setImage(image);
131         }
132         private String JavaDoc escapeAmpersands(String JavaDoc message) {
133             StringBuffer JavaDoc result= new StringBuffer JavaDoc();
134             for (int i= 0; i < message.length(); i++) {
135                 char ch= message.charAt(i);
136                 if (ch == '&') {
137                     result.append('&');
138                 }
139                 result.append(ch);
140             }
141             return result.toString();
142         }
143     }
144     
145     private static class PageBook extends Composite {
146         private StackLayout fLayout;
147         public PageBook(Composite parent, int style) {
148             super(parent, style);
149             fLayout= new StackLayout();
150             setLayout(fLayout);
151             fLayout.marginWidth= 5; fLayout.marginHeight= 5;
152         }
153         public void showPage(Control page) {
154             fLayout.topControl= page;
155             layout();
156         }
157         public Control getTopPage() {
158             return fLayout.topControl;
159         }
160     }
161     
162     public RefactoringWizardDialog2(Shell shell, RefactoringWizard wizard) {
163         super(shell);
164         Assert.isNotNull(wizard);
165         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
166         IDialogSettings settings= wizard.getDialogSettings();
167         if (settings == null) {
168             settings= RefactoringUIPlugin.getDefault().getDialogSettings();
169             wizard.setDialogSettings(settings);
170         }
171         fWizard= wizard;
172         fWizard.setContainer(this);
173         fWizard.addPages();
174         initSize(settings);
175         fHasAdditionalPages= wizard.getPageCount() > 3;
176     }
177     
178     private void initSize(IDialogSettings settings) {
179         fSettings= settings.getSection(DIALOG_SETTINGS);
180         if (fSettings == null) {
181             fSettings= new DialogSettings(DIALOG_SETTINGS);
182             settings.addSection(fSettings);
183             fSettings.put(WIDTH, 600);
184             fSettings.put(HEIGHT, 400);
185         }
186         fPreviewWidth= 600;
187         fPreviewHeight= 400;
188         try {
189             fPreviewWidth= fSettings.getInt(WIDTH);
190             fPreviewHeight= fSettings.getInt(HEIGHT);
191         } catch (NumberFormatException JavaDoc e) {
192         }
193     }
194     
195     private void saveSize() {
196         if (fCurrentPage instanceof PreviewWizardPage) {
197             Control control= fCurrentPage.getControl().getParent();
198             Point size = control.getSize();
199             fSettings.put(WIDTH, size.x);
200             fSettings.put(HEIGHT, size.y);
201         }
202     }
203     
204     public Button getCancelButton() {
205         return getButton(IDialogConstants.CANCEL_ID);
206     }
207
208     //---- IRefactoringWizardDialog ------------------------------------
209

210     public void makeNextButtonDefault() {
211         fMakeNextButtonDefault= true;
212     }
213     
214     //---- IWizardContainer --------------------------------------------
215

216     /* (non-Javadoc)
217      * Method declared on IWizardContainer.
218      */

219     public void showPage(IWizardPage page) {
220         fCurrentPage= page;
221     }
222
223     /* (non-Javadoc)
224      * Method declared on IWizardContainer.
225      */

226     public void updateButtons() {
227         boolean previewPage= isPreviewPageActive();
228         boolean ok= fWizard.canFinish();
229         boolean canFlip= fCurrentPage.canFlipToNextPage();
230         Button previewButton= getButton(PREVIEW_ID);
231         Button defaultButton= null;
232         if (previewButton != null && !previewButton.isDisposed()) {
233             previewButton.setEnabled(!previewPage);
234             if (!previewPage)
235                 previewButton.setEnabled(canFlip);
236             if (previewButton.isEnabled())
237                 defaultButton= previewButton;
238         }
239         Button nextButton= getButton(IDialogConstants.NEXT_ID);
240         if (nextButton != null && !nextButton.isDisposed()) {
241             nextButton.setEnabled(!previewPage);
242             if (!previewPage)
243                 nextButton.setEnabled(canFlip);
244             if (nextButton.isEnabled())
245                 defaultButton= nextButton;
246         }
247         Button backButton= getButton(IDialogConstants.BACK_ID);
248         if (backButton != null && !backButton.isDisposed())
249             backButton.setEnabled(!isFirstPage());
250         Button okButton= getButton(IDialogConstants.OK_ID);
251         if (okButton != null && !okButton.isDisposed()) {
252             okButton.setEnabled(ok);
253             if (ok)
254                 defaultButton= okButton;
255         }
256         if (defaultButton != null) {
257             defaultButton.getShell().setDefaultButton(defaultButton);
258         }
259     }
260
261     /* (non-Javadoc)
262      * Method declared on IWizardContainer.
263      */

264     public void updateMessage() {
265         if (fStatusContainer == null || fStatusContainer.isDisposed())
266             return;
267         fStatusContainer.showPage(fMessageBox);
268         fMessageBox.setMessage(fCurrentPage);
269     }
270
271     /* (non-Javadoc)
272      * Method declared on IWizardContainer.
273      */

274     public void updateTitleBar() {
275         // we don't have a title bar.
276
}
277
278     /* (non-Javadoc)
279      * Method declared on IWizardContainer.
280      */

281     public void updateWindowTitle() {
282         String JavaDoc title= fWizard.getWindowTitle();
283         if (title == null)
284             title= ""; //$NON-NLS-1$
285
getShell().setText(title);
286     }
287
288     /* (non-Javadoc)
289      * Method declared on IWizardContainer.
290      */

291     public IWizardPage getCurrentPage() {
292         return fCurrentPage;
293     }
294     
295     //---- IRunnableContext --------------------------------------------
296

297     /* (non-Javadoc)
298      * Method declared on IRunnableContext
299      */

300     public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
301         if (fProgressMonitorPart == null) {
302             ModalContext.run(runnable, false, new NullProgressMonitor(), getShell().getDisplay());
303         } else {
304             Object JavaDoc state = null;
305             if(fActiveRunningOperations == 0)
306                 state = aboutToStart(fork && cancelable);
307         
308             fActiveRunningOperations++;
309             try {
310                 ModalContext.run(runnable, fork, fProgressMonitorPart, getShell().getDisplay());
311             } finally {
312                 fActiveRunningOperations--;
313                 //Stop if this is the last one
314
if(state!= null)
315                     stopped(state);
316             }
317         }
318     }
319     
320     private Object JavaDoc aboutToStart(boolean cancelable) {
321         Map JavaDoc savedState = null;
322         Shell shell= getShell();
323         if (shell != null) {
324             // Save focus control
325
Control focusControl = getShell().getDisplay().getFocusControl();
326             if (focusControl != null && focusControl.getShell() != getShell())
327                 focusControl = null;
328                 
329             Button cancelButton= getButton(IDialogConstants.CANCEL_ID);
330             // Set the busy cursor to all shells.
331
Display d = getShell().getDisplay();
332             fWaitCursor = new Cursor(d, SWT.CURSOR_WAIT);
333             setDisplayCursor(d, fWaitCursor);
334                     
335             // Set the arrow cursor to the cancel component.
336
fArrowCursor= new Cursor(d, SWT.CURSOR_ARROW);
337             cancelButton.setCursor(fArrowCursor);
338     
339             boolean hasProgressMonitor= fProgressMonitorPart != null;
340     
341             // Deactivate shell
342
savedState= saveUIState(hasProgressMonitor && cancelable);
343             if (focusControl != null)
344                 savedState.put("focus", focusControl); //$NON-NLS-1$
345

346             if (hasProgressMonitor) {
347                 fProgressMonitorPart.attachToCancelComponent(cancelButton);
348                 fStatusContainer.showPage(fProgressMonitorPart);
349             }
350             // Update the status container since we are blocking the event loop right now.
351
fStatusContainer.update();
352         }
353         return savedState;
354     }
355     
356     private Map JavaDoc saveUIState(boolean keepCancelEnabled) {
357         Map JavaDoc savedState= new HashMap JavaDoc(10);
358         saveEnableStateAndSet(getButton(PREVIEW_ID), savedState, "preview", false); //$NON-NLS-1$
359
saveEnableStateAndSet(getButton(IDialogConstants.OK_ID), savedState, "ok", false); //$NON-NLS-1$
360
saveEnableStateAndSet(getButton(IDialogConstants.BACK_ID), savedState, "back", false); //$NON-NLS-1$
361
saveEnableStateAndSet(getButton(IDialogConstants.NEXT_ID), savedState, "next", false); //$NON-NLS-1$
362
saveEnableStateAndSet(getButton(IDialogConstants.CANCEL_ID), savedState, "cancel", keepCancelEnabled); //$NON-NLS-1$
363
savedState.put("page", ControlEnableState.disable(fVisiblePage.getControl())); //$NON-NLS-1$
364
return savedState;
365     }
366     
367     private void saveEnableStateAndSet(Control w, Map JavaDoc h, String JavaDoc key, boolean enabled) {
368         if (w != null) {
369             h.put(key, Boolean.valueOf(w.getEnabled()));
370             w.setEnabled(enabled);
371         }
372     }
373     
374     private void setDisplayCursor(Display d, Cursor c) {
375         Shell[] shells= d.getShells();
376         for (int i= 0; i < shells.length; i++)
377             shells[i].setCursor(c);
378     }
379
380     private void stopped(Object JavaDoc savedState) {
381         Shell shell= getShell();
382         if (shell != null) {
383             Button cancelButton= getButton(IDialogConstants.CANCEL_ID);
384             
385             if (fProgressMonitorPart != null)
386                 fProgressMonitorPart.removeFromCancelComponent(cancelButton);
387                 
388             fStatusContainer.showPage(fMessageBox);
389             Map JavaDoc state = (Map JavaDoc)savedState;
390             restoreUIState(state);
391     
392             setDisplayCursor(shell.getDisplay(), null);
393             cancelButton.setCursor(null);
394             fWaitCursor.dispose();
395             fWaitCursor = null;
396             fArrowCursor.dispose();
397             fArrowCursor = null;
398             Control focusControl = (Control)state.get("focus"); //$NON-NLS-1$
399
if (focusControl != null)
400                 focusControl.setFocus();
401         }
402     }
403     
404     private void restoreUIState(Map JavaDoc state) {
405         restoreEnableState(getButton(PREVIEW_ID), state, "preview");//$NON-NLS-1$
406
restoreEnableState(getButton(IDialogConstants.OK_ID), state, "ok");//$NON-NLS-1$
407
restoreEnableState(getButton(IDialogConstants.BACK_ID), state, "back"); //$NON-NLS-1$
408
restoreEnableState(getButton(IDialogConstants.NEXT_ID), state, "next"); //$NON-NLS-1$
409
restoreEnableState(getButton(IDialogConstants.CANCEL_ID), state, "cancel");//$NON-NLS-1$
410
ControlEnableState pageState = (ControlEnableState) state.get("page");//$NON-NLS-1$
411
pageState.restore();
412     }
413     
414     private void restoreEnableState(Control w, Map JavaDoc h, String JavaDoc key) {
415         if (w != null) {
416             Boolean JavaDoc b = (Boolean JavaDoc) h.get(key);
417             if (b != null)
418                 w.setEnabled(b.booleanValue());
419         }
420     }
421     
422     //---- Dialog -----------------------------------------------------------
423

424     public boolean close() {
425         fWizard.dispose();
426         return super.close();
427     }
428
429     protected void cancelPressed() {
430         if (fActiveRunningOperations == 0) {
431             if (fWizard.performCancel())
432                 super.cancelPressed();
433         }
434     }
435
436     protected void okPressed() {
437         IWizardPage current= fCurrentPage;
438         saveInitialSize();
439         if (fWizard.performFinish()) {
440             saveSize();
441             super.okPressed();
442             return;
443         }
444         if (fCurrentPage == current)
445             return;
446         Assert.isTrue(IErrorWizardPage.PAGE_NAME.equals(fCurrentPage.getName()));
447         if (fHasAdditionalPages) {
448             // Show error page as a normal page
449
showCurrentPage();
450         } else if (showErrorDialog((ErrorWizardPage) fCurrentPage)) {
451             // Show error page as a dialog
452
if (fWizard.performFinish()) {
453                 super.okPressed();
454                 return;
455             }
456         }
457     }
458
459     private void showCurrentPage() {
460         if (fCurrentPage.getControl() == null)
461             fCurrentPage.createControl(fPageContainer);
462         resize();
463         makeVisible(fCurrentPage);
464         updateButtons();
465     }
466     
467     protected void handleShellCloseEvent() {
468         if (fActiveRunningOperations == 0) {
469             if (fWizard.performCancel())
470                 super.handleShellCloseEvent();
471         }
472     }
473     
474     private boolean isPreviewPageActive() {
475         return IPreviewWizardPage.PAGE_NAME.equals(fCurrentPage.getName());
476     }
477     
478     private void nextOrPreviewPressed() {
479         IWizardPage current= fCurrentPage;
480         saveInitialSize();
481         fCurrentPage= fCurrentPage.getNextPage();
482         if (current == fCurrentPage)
483             return;
484         if (!fHasAdditionalPages && IErrorWizardPage.PAGE_NAME.equals(fCurrentPage.getName())) {
485             if (showErrorDialog((ErrorWizardPage)fCurrentPage)) {
486                 fCurrentPage= fCurrentPage.getNextPage();
487             } else {
488                 return;
489             }
490         }
491         
492         showCurrentPage();
493     }
494
495     private void saveInitialSize() {
496         if (isFirstPage()) {
497             // Moving away from initial page;
498
// store size (may have changed any time)
499
fInitialSize= getShell().getBounds();
500         }
501     }
502     
503     private boolean isFirstPage() {
504         IWizardPage[] pages= fWizard.getPages();
505         return (fCurrentPage.equals(pages[0]));
506     }
507
508     private void backPressed() {
509         IWizardPage current= fCurrentPage;
510         fCurrentPage= fCurrentPage.getPreviousPage();
511         if (current == fCurrentPage)
512             return;
513
514         showCurrentPage();
515     }
516     
517     private boolean showErrorDialog(ErrorWizardPage page) {
518         RefactoringStatusDialog dialog= new RefactoringStatusDialog(getShell(), page,
519             fWizard.internalShowBackButtonOnStatusDialog(InternalAPI.INSTANCE));
520         switch (dialog.open()) {
521             case IDialogConstants.OK_ID:
522                 return true;
523             case IDialogConstants.BACK_ID:
524                 fCurrentPage= fCurrentPage.getPreviousPage();
525                 break;
526             case IDialogConstants.CANCEL_ID:
527                 cancelPressed();
528         }
529         return false;
530     }
531     
532     private void resize() {
533         
534         if (isFirstPage()) {
535             getShell().setBounds(fInitialSize);
536             return;
537         }
538         
539         Control control= fPageContainer.getTopPage();
540         Point size= control.getSize();
541         int dw= Math.max(0, fPreviewWidth - size.x);
542         int dh= Math.max(0, fPreviewHeight - size.y);
543         int dx = dw / 2;
544         int dy= dh / 2;
545         Shell shell= getShell();
546         Rectangle rect= shell.getBounds();
547         Rectangle clientRect= shell.getMonitor().getClientArea();
548         rect.x= Math.max(clientRect.x, rect.x - dx);
549         rect.y= Math.max(clientRect.y, rect.y - dy);
550         
551         // limit with and height to monitor
552
rect.width= Math.min(rect.width + dw, clientRect.width);
553         rect.height= Math.min(rect.height + dh, clientRect.height);
554
555         // Reposition the dialog so that it will be fully visible.
556
// Normalize x and y relative to the client area.
557
int xe= (rect.x - clientRect.x) + rect.width;
558         if (xe > clientRect.width) {
559             rect.x-= xe - clientRect.width;
560         }
561         int ye= (rect.y - clientRect.y) + rect.height;
562         if (ye > clientRect.height) {
563             rect.y-= ye - clientRect.height;
564         }
565         
566         shell.setBounds(rect);
567     }
568     
569     //---- UI construction ---------------------------------------------------
570

571     protected void configureShell(Shell newShell) {
572         super.configureShell(newShell);
573         String JavaDoc title= fWizard.getDefaultPageTitle();
574         if (title == null)
575             title= ""; //$NON-NLS-1$
576
newShell.setText(title);
577         fWizard.getRefactoring().setValidationContext(newShell);
578     }
579     
580     protected Control createContents(Composite parent) {
581         Composite result= new Composite(parent, SWT.NONE);
582         GridLayout layout= new GridLayout();
583         layout.marginHeight= 0; layout.marginWidth= 0;
584         layout.verticalSpacing= 0; layout.horizontalSpacing= 0;
585         result.setLayout(layout);
586         result.setLayoutData(new GridData(GridData.FILL_BOTH));
587         
588         // initialize the dialog units
589
initializeDialogUnits(result);
590     
591         fPageContainer= new PageBook(result, SWT.NONE);
592         GridData gd= new GridData(GridData.FILL_BOTH);
593         fPageContainer.setLayoutData(gd);
594         fCurrentPage= fWizard.getStartingPage();
595         dialogArea= fPageContainer;
596         if (fCurrentPage instanceof PreviewWizardPage) {
597             gd.widthHint= fPreviewWidth;
598             gd.heightHint= fPreviewHeight;
599         }
600         
601         fStatusContainer= new PageBook(result, SWT.NONE);
602         gd= new GridData(GridData.FILL_HORIZONTAL);
603         gd.widthHint= convertWidthInCharsToPixels(fWizard.getMessageLineWidthInChars());
604         fStatusContainer.setLayoutData(gd);
605         if (fWizard.needsProgressMonitor())
606             createProgressMonitorPart();
607         createMessageBox();
608         fStatusContainer.showPage(fMessageBox);
609         
610         buttonBar= createButtonBar(result);
611         
612         if (fCurrentPage != null) {
613             fCurrentPage.createControl(fPageContainer);
614             makeVisible(fCurrentPage);
615             updateMessage();
616             updateButtons();
617         }
618         
619         applyDialogFont(result);
620         return result;
621     }
622     
623     private void createProgressMonitorPart() {
624         // Insert a progress monitor
625
GridLayout pmlayout= new GridLayout();
626         pmlayout.numColumns= 1;
627         pmlayout.marginHeight= 0;
628         fProgressMonitorPart= new ProgressMonitorPart(fStatusContainer, pmlayout);
629     }
630     
631     private void createMessageBox() {
632         fMessageBox= new MessageBox(fStatusContainer, SWT.NONE);
633     }
634     
635     protected void createButtonsForButtonBar(Composite parent) {
636         
637         if (fHasAdditionalPages)
638             createPreviousAndNextButtons(parent);
639         else
640             createPreviewButton(parent);
641
642         String JavaDoc OK_LABEL= (fHasAdditionalPages) ? IDialogConstants.FINISH_LABEL : IDialogConstants.OK_LABEL;
643         String JavaDoc CANCEL_LABEL= IDialogConstants.CANCEL_LABEL;
644         if (fWizard.internalIsYesNoStyle(InternalAPI.INSTANCE)) {
645             OK_LABEL= IDialogConstants.YES_LABEL;
646             CANCEL_LABEL= IDialogConstants.NO_LABEL;
647         }
648         createButton(
649             parent,
650             IDialogConstants.OK_ID,
651             OK_LABEL,
652             true);
653         createButton(
654             parent,
655             IDialogConstants.CANCEL_ID,
656             CANCEL_LABEL,
657             false);
658         Button okButton= getButton(IDialogConstants.OK_ID);
659         okButton.setFocus();
660     }
661     
662     private void createPreviewButton(Composite parent) {
663         if (! (fCurrentPage instanceof PreviewWizardPage) && fWizard.internalHasPreviewPage(InternalAPI.INSTANCE)) {
664             Button preview= createButton(parent, PREVIEW_ID, RefactoringUIMessages.RefactoringWizardDialog2_buttons_preview_label, false);
665             if (fMakeNextButtonDefault) {
666                 preview.getShell().setDefaultButton(preview);
667             }
668             preview.addSelectionListener(new SelectionAdapter() {
669
670                 public void widgetSelected(SelectionEvent e) {
671                     nextOrPreviewPressed();
672                 }
673             });
674         }
675     }
676
677     private Composite createPreviousAndNextButtons(Composite parent) {
678         // Copied from Wizard Dialog.
679

680         // increment the number of columns in the button bar
681
((GridLayout) parent.getLayout()).numColumns+= 2; // parent is assumed to have a GridLayout (see javadoc of Dialog#createButton(..))
682
Composite composite= new Composite(parent, SWT.NONE);
683         // create a layout with spacing and margins appropriate for the font
684
// size.
685
GridLayout layout= new GridLayout();
686         layout.numColumns= 0; // will be incremented by createButton
687
layout.marginWidth= 0;
688         layout.marginHeight= 0;
689         layout.horizontalSpacing= 0;
690         layout.verticalSpacing= 0;
691         composite.setLayout(layout);
692         composite.setFont(parent.getFont());
693         Button backButton= createButton(composite, IDialogConstants.BACK_ID, IDialogConstants.BACK_LABEL, false);
694         backButton.addSelectionListener(new SelectionAdapter() {
695
696             public void widgetSelected(SelectionEvent e) {
697                 backPressed();
698             }
699         });
700         Button nextButton= createButton(composite, IDialogConstants.NEXT_ID, IDialogConstants.NEXT_LABEL, false);
701         nextButton.addSelectionListener(new SelectionAdapter() {
702
703             public void widgetSelected(SelectionEvent e) {
704                 nextOrPreviewPressed();
705             }
706         });
707
708         GridData data= new GridData();
709         int widthHint= convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
710         Point minSize1= backButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
711         Point minSize2= nextButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
712         data.widthHint= Math.max(widthHint * 2, minSize1.x + minSize2.x);
713         data.horizontalAlignment= SWT.END;
714         data.horizontalSpan= 2;
715         composite.setLayoutData(data);
716
717         return composite;
718     }
719     
720     private void makeVisible(IWizardPage page) {
721         if (fVisiblePage == page)
722             return;
723         if (fVisiblePage != null)
724             fVisiblePage.setVisible(false);
725         fVisiblePage= page;
726         fPageContainer.showPage(page.getControl());
727         fVisiblePage.setVisible(true);
728     }
729 }
730
Popular Tags