KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > RenameTypeWizardInputPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.ui.refactoring.reorg;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.OperationCanceledException;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Link;
28
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.jface.wizard.IWizardPage;
32
33 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeProcessor;
34 import org.eclipse.jdt.internal.corext.refactoring.tagging.ISimilarDeclarationUpdating;
35
36 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
37 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
38 import org.eclipse.jdt.internal.ui.util.RowLayouter;
39
40 /**
41  * Wizard page for renaming a type (with similarly named elements)
42  *
43  * @since 3.2
44  *
45  */

46 class RenameTypeWizardInputPage extends RenameInputWizardPage {
47
48     private Button fUpdateSimilarElements;
49     private int fSelectedStrategy;
50
51     private Link fUpdateSimilarElementsButton;
52
53     public RenameTypeWizardInputPage(String JavaDoc description, String JavaDoc contextHelpId, boolean isLastUserPage, String JavaDoc initialValue) {
54         super(description, contextHelpId, isLastUserPage, initialValue);
55     }
56
57     protected void addAdditionalOptions(Composite composite, RowLayouter layouter) {
58
59         if (getSimilarElementUpdating() == null || !getSimilarElementUpdating().canEnableSimilarDeclarationUpdating())
60             return;
61
62         try {
63             fSelectedStrategy= getRefactoringSettings().getInt(RenameRefactoringWizard.TYPE_SIMILAR_MATCH_STRATEGY);
64         } catch (NumberFormatException JavaDoc e) {
65             fSelectedStrategy= getSimilarElementUpdating().getMatchStrategy();
66         }
67
68         getSimilarElementUpdating().setMatchStrategy(fSelectedStrategy);
69
70         Composite c= new Composite(composite, SWT.NULL);
71         GridLayout layout= new GridLayout(2, false);
72         layout.marginWidth= 0;
73         layout.marginHeight= 0;
74         c.setLayout(layout);
75
76         fUpdateSimilarElements= new Button(c, SWT.CHECK);
77         fUpdateSimilarElements.setText(RefactoringMessages.RenameTypeWizardInputPage_update_similar_elements);
78
79         final boolean updateSimilarElements= getBooleanSetting(RenameRefactoringWizard.TYPE_UPDATE_SIMILAR_ELEMENTS, getSimilarElementUpdating().getUpdateSimilarDeclarations());
80         fUpdateSimilarElements.setSelection(updateSimilarElements);
81         getSimilarElementUpdating().setUpdateSimilarDeclarations(updateSimilarElements);
82         fUpdateSimilarElements.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
83         fUpdateSimilarElements.addSelectionListener(new SelectionAdapter() {
84
85             public void widgetSelected(SelectionEvent e) {
86                 getSimilarElementUpdating().setUpdateSimilarDeclarations(fUpdateSimilarElements.getSelection());
87                 fUpdateSimilarElementsButton.setEnabled(fUpdateSimilarElements.getSelection());
88             }
89         });
90
91         fUpdateSimilarElementsButton= new Link(c, SWT.NONE);
92         GridData d= new GridData();
93         d.grabExcessHorizontalSpace= true;
94         d.horizontalAlignment= SWT.RIGHT;
95         fUpdateSimilarElementsButton.setText(RefactoringMessages.RenameTypeWizardInputPage_update_similar_elements_configure);
96         fUpdateSimilarElementsButton.setEnabled(updateSimilarElements);
97         fUpdateSimilarElementsButton.addSelectionListener(new SelectionAdapter() {
98
99             public void widgetSelected(SelectionEvent e) {
100                 RenameTypeWizardSimilarElementsOptionsDialog dialog= new RenameTypeWizardSimilarElementsOptionsDialog(getShell(), fSelectedStrategy);
101                 if (dialog.open() == Window.OK) {
102                     fSelectedStrategy= dialog.getSelectedStrategy();
103                     getSimilarElementUpdating().setMatchStrategy(fSelectedStrategy);
104                 }
105             }
106         });
107         fUpdateSimilarElementsButton.setLayoutData(d);
108
109         GridData forC= new GridData();
110         forC.grabExcessHorizontalSpace= true;
111         forC.horizontalAlignment= SWT.FILL;
112         forC.horizontalSpan= 2;
113         c.setLayoutData(forC);
114
115         final Label separator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
116         separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117         layouter.perform(separator);
118     }
119
120     public void dispose() {
121         if (saveSettings())
122             if (fUpdateSimilarElements != null && !fUpdateSimilarElements.isDisposed() && fUpdateSimilarElements.isEnabled()) {
123                 saveBooleanSetting(RenameRefactoringWizard.TYPE_UPDATE_SIMILAR_ELEMENTS, fUpdateSimilarElements);
124                 getRefactoringSettings().put(RenameRefactoringWizard.TYPE_SIMILAR_MATCH_STRATEGY, fSelectedStrategy);
125             }
126
127         super.dispose();
128     }
129
130     /*
131      * Override - we don't want to initialize the next page (may needlessly
132      * trigger change creation if similar elements page is skipped, which is not
133      * indicated by fIsLastUserInputPage in parent).
134      */

135     public boolean canFlipToNextPage() {
136         return isPageComplete();
137     }
138
139     private ISimilarDeclarationUpdating getSimilarElementUpdating() {
140         return (ISimilarDeclarationUpdating) getRefactoring().getAdapter(ISimilarDeclarationUpdating.class);
141     }
142     
143     protected boolean performFinish() {
144         boolean returner= super.performFinish();
145         // check if we got deferred to the error page
146
if (!returner && getContainer().getCurrentPage() != null)
147             getContainer().getCurrentPage().setPreviousPage(this);
148         return returner;
149     }
150
151     /* (non-Javadoc)
152      * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#getNextPage()
153      */

154     public IWizardPage getNextPage() {
155         RenameTypeWizard wizard= (RenameTypeWizard) getWizard();
156         IWizardPage nextPage;
157         
158         if (wizard.isRenameType()) {
159             final RenameTypeProcessor renameTypeProcessor= wizard.getRenameTypeProcessor();
160             try {
161                 getContainer().run(true, true, new IRunnableWithProgress() {
162
163                     public void run(IProgressMonitor pm) throws InterruptedException JavaDoc {
164                         try {
165                             renameTypeProcessor.initializeReferences(pm);
166                         } catch (OperationCanceledException e) {
167                             throw new InterruptedException JavaDoc();
168                         } catch (CoreException e) {
169                             ExceptionHandler.handle(e, RefactoringMessages.RenameTypeWizard_defaultPageTitle,
170                                     RefactoringMessages.RenameTypeWizard_unexpected_exception);
171                         } finally {
172                             pm.done();
173                         }
174                     }
175                 });
176             } catch (InvocationTargetException JavaDoc e) {
177                 ExceptionHandler.handle(e, getShell(), RefactoringMessages.RenameTypeWizard_defaultPageTitle,
178                         RefactoringMessages.RenameTypeWizard_unexpected_exception);
179             } catch (InterruptedException JavaDoc e) {
180                 // user canceled
181
return this;
182             }
183
184             if (renameTypeProcessor.hasSimilarElementsToRename()) {
185                 nextPage= super.getNextPage();
186             } else {
187                 nextPage= computeSuccessorPage();
188             }
189             
190         } else
191             nextPage= computeSuccessorPage();
192         
193         nextPage.setPreviousPage(this);
194         return nextPage;
195     }
196 }
197
Popular Tags