KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > MoveMembersWizard


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.jdt.internal.ui.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ModifyEvent;
22 import org.eclipse.swt.events.ModifyListener;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31
32 import org.eclipse.jface.dialogs.Dialog;
33 import org.eclipse.jface.window.Window;
34 import org.eclipse.jface.wizard.IWizardPage;
35
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
38
39 import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
40 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
41 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
42
43 import org.eclipse.jdt.core.IJavaElement;
44 import org.eclipse.jdt.core.IMember;
45 import org.eclipse.jdt.core.IPackageFragment;
46 import org.eclipse.jdt.core.IType;
47 import org.eclipse.jdt.core.JavaConventions;
48 import org.eclipse.jdt.core.JavaModelException;
49 import org.eclipse.jdt.core.search.IJavaSearchConstants;
50 import org.eclipse.jdt.core.search.IJavaSearchScope;
51 import org.eclipse.jdt.core.search.SearchEngine;
52
53 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor;
54 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
55 import org.eclipse.jdt.internal.corext.util.Messages;
56
57 import org.eclipse.jdt.ui.JavaElementLabels;
58
59 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
60 import org.eclipse.jdt.internal.ui.JavaPlugin;
61 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
62 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
63 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
64 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
65 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
66 import org.eclipse.jdt.internal.ui.util.SWTUtil;
67
68 public class MoveMembersWizard extends RefactoringWizard {
69
70     public MoveMembersWizard(MoveRefactoring ref) {
71         super(ref, DIALOG_BASED_USER_INTERFACE);
72         setDefaultPageTitle(RefactoringMessages.MoveMembersWizard_page_title);
73     }
74
75     /* non java-doc
76      * @see RefactoringWizard#addUserInputPages
77      */

78     protected void addUserInputPages(){
79         addPage(new MoveMembersInputPage());
80     }
81     
82     private static class MoveMembersInputPage extends UserInputWizardPage {
83
84         public static final String JavaDoc PAGE_NAME= "MoveMembersInputPage"; //$NON-NLS-1$
85
private static final long LABEL_FLAGS= JavaElementLabels.ALL_DEFAULT;
86
87         private Combo fDestinationField;
88         private Button fLeaveDelegateCheckBox;
89         private Button fDeprecateDelegateCheckBox;
90         private static final int MRU_COUNT= 10;
91         private static List JavaDoc fgMruDestinations= new ArrayList JavaDoc(MRU_COUNT);
92
93         public MoveMembersInputPage() {
94             super(PAGE_NAME);
95         }
96     
97         public void setVisible(boolean visible){
98             if (visible){
99                 String JavaDoc message= Messages.format(RefactoringMessages.MoveMembersInputPage_descriptionKey,
100                     new String JavaDoc[]{new Integer JavaDoc(getMoveProcessor().getMembersToMove().length).toString(),
101                                  JavaModelUtil.getFullyQualifiedName(getMoveProcessor().getDeclaringType())});
102                 setDescription(message);
103             }
104             super.setVisible(visible);
105         }
106     
107         public void createControl(Composite parent) {
108             Composite composite= new Composite(parent, SWT.NONE);
109             GridLayout layout= new GridLayout();
110             layout.numColumns= 2;
111             composite.setLayout(layout);
112         
113             addLabel(composite);
114             addDestinationControls(composite);
115             fLeaveDelegateCheckBox= DelegateUIHelper.generateLeaveDelegateCheckbox(composite, getRefactoring(), getMoveProcessor().getMembersToMove().length > 1);
116             GridData data= new GridData();
117             data.horizontalSpan= 2;
118             if (fLeaveDelegateCheckBox != null) {
119                 fLeaveDelegateCheckBox.setLayoutData(data);
120                 fDeprecateDelegateCheckBox= new Button(composite, SWT.CHECK);
121                 data= new GridData();
122                 data.horizontalAlignment= GridData.FILL;
123                 data.horizontalIndent= (layout.marginWidth + fDeprecateDelegateCheckBox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
124                 data.horizontalSpan= 2;
125                 fDeprecateDelegateCheckBox.setLayoutData(data);
126                 fDeprecateDelegateCheckBox.setText(DelegateUIHelper.getDeprecateDelegateCheckBoxTitle());
127                 fDeprecateDelegateCheckBox.setSelection(DelegateUIHelper.loadDeprecateDelegateSetting(getMoveProcessor()));
128                 getMoveProcessor().setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
129                 fDeprecateDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
130                     public void widgetSelected(SelectionEvent e) {
131                         getMoveProcessor().setDeprecateDelegates(fDeprecateDelegateCheckBox.getSelection());
132                     }
133                 });
134                 fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
135                 fLeaveDelegateCheckBox.addSelectionListener(new SelectionAdapter() {
136                     public void widgetSelected(SelectionEvent e) {
137                         fDeprecateDelegateCheckBox.setEnabled(fLeaveDelegateCheckBox.getSelection());
138                     }
139                 });
140             }
141             setControl(composite);
142             Dialog.applyDialogFont(composite);
143             PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.MOVE_MEMBERS_WIZARD_PAGE);
144         }
145
146         public void dispose() {
147             DelegateUIHelper.saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
148             DelegateUIHelper.saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
149             super.dispose();
150         }
151         
152         private void addLabel(Composite parent) {
153             Label label= new Label(parent, SWT.NONE);
154             IMember[] members= getMoveProcessor().getMembersToMove();
155             if (members.length == 1) {
156                 label.setText(Messages.format(
157                         RefactoringMessages.MoveMembersInputPage_destination_single,
158                         JavaElementLabels.getElementLabel(members[0], LABEL_FLAGS)));
159             } else {
160                 label.setText(Messages.format(
161                         RefactoringMessages.MoveMembersInputPage_destination_multi,
162                         String.valueOf(members.length)));
163             }
164             GridData gd= new GridData();
165             gd.horizontalSpan= 2;
166             label.setLayoutData(gd);
167         }
168
169         private void addDestinationControls(Composite composite) {
170             fDestinationField= new Combo(composite, SWT.SINGLE | SWT.BORDER);
171             fDestinationField.setFocus();
172             fDestinationField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
173             fDestinationField.setItems((String JavaDoc[]) fgMruDestinations.toArray(new String JavaDoc[fgMruDestinations.size()]));
174             fDestinationField.addModifyListener(new ModifyListener(){
175                 public void modifyText(ModifyEvent e) {
176                     handleDestinationChanged();
177                 }
178                 private void handleDestinationChanged() {
179                     IStatus status= JavaConventions.validateJavaTypeName(fDestinationField.getText());
180                     if (status.getSeverity() == IStatus.ERROR){
181                         error(status.getMessage());
182                     } else {
183                         try {
184                             final IType declaring= getMoveProcessor().getDeclaringType();
185                             IType resolvedType= declaring.getJavaProject().findType(fDestinationField.getText());
186                             if (resolvedType == null)
187                                 resolvedType= declaring.getJavaProject().findType(declaring.getPackageFragment().getElementName(), fDestinationField.getText());
188                             IStatus validationStatus= validateDestinationType(resolvedType, fDestinationField.getText());
189                             if (validationStatus.isOK()){
190                                 setErrorMessage(null);
191                                 setPageComplete(true);
192                             } else {
193                                 error(validationStatus.getMessage());
194                             }
195                         } catch(JavaModelException ex) {
196                             JavaPlugin.log(ex); //no ui here
197
error(RefactoringMessages.MoveMembersInputPage_invalid_name);
198                         }
199                     }
200                 }
201                 private void error(String JavaDoc message){
202                     setErrorMessage(message);
203                     setPageComplete(false);
204                 }
205             });
206             if (fgMruDestinations.size() > 0) {
207                 fDestinationField.select(0);
208             } else {
209                 setPageComplete(false);
210             }
211             JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(false, false, true);
212             IPackageFragment context= (IPackageFragment) getMoveProcessor().getDeclaringType().getAncestor(IJavaElement.PACKAGE_FRAGMENT);
213             processor.setPackageFragment(context);
214             ControlContentAssistHelper.createComboContentAssistant(fDestinationField, processor);
215             TextFieldNavigationHandler.install(fDestinationField);
216             
217             Button button= new Button(composite, SWT.PUSH);
218             button.setText(RefactoringMessages.MoveMembersInputPage_browse);
219             button.setLayoutData(new GridData());
220             SWTUtil.setButtonDimensionHint(button);
221             button.addSelectionListener(new SelectionAdapter(){
222                 public void widgetSelected(SelectionEvent e) {
223                     openTypeSelectionDialog();
224                 }
225             });
226         }
227             
228         protected boolean performFinish() {
229             initializeRefactoring();
230             return super.performFinish();
231         }
232     
233         public IWizardPage getNextPage() {
234             initializeRefactoring();
235             return super.getNextPage();
236         }
237
238         private void initializeRefactoring() {
239             try {
240                 String JavaDoc destination= fDestinationField.getText();
241                 if (!fgMruDestinations.remove(destination) && fgMruDestinations.size() >= MRU_COUNT)
242                     fgMruDestinations.remove(fgMruDestinations.size() - 1);
243                 fgMruDestinations.add(0, destination);
244                 
245                 getMoveProcessor().setDestinationTypeFullyQualifiedName(destination);
246             } catch(JavaModelException e) {
247                 ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveMembersInputPage_move_Member, RefactoringMessages.MoveMembersInputPage_exception);
248             }
249         }
250     
251         private IJavaSearchScope createWorkspaceSourceScope(){
252             IJavaElement[] project= new IJavaElement[] { getMoveProcessor().getDeclaringType().getJavaProject() };
253             return SearchEngine.createJavaSearchScope(project, IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.SOURCES);
254         }
255     
256         private void openTypeSelectionDialog(){
257             int elementKinds= IJavaSearchConstants.TYPE;
258             final IJavaSearchScope scope= createWorkspaceSourceScope();
259             FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false,
260                 getWizard().getContainer(), scope, elementKinds);
261             dialog.setTitle(RefactoringMessages.MoveMembersInputPage_choose_Type);
262             dialog.setMessage(RefactoringMessages.MoveMembersInputPage_dialogMessage);
263             dialog.setValidator(new ISelectionStatusValidator(){
264                 public IStatus validate(Object JavaDoc[] selection) {
265                     Assert.isTrue(selection.length <= 1);
266                     if (selection.length == 0)
267                         return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, RefactoringMessages.MoveMembersInputPage_Invalid_selection, null);
268                     Object JavaDoc element= selection[0];
269                     if (! (element instanceof IType))
270                         return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, RefactoringMessages.MoveMembersInputPage_Invalid_selection, null);
271                     IType type= (IType)element;
272                     return validateDestinationType(type, type.getElementName());
273                 }
274             });
275             dialog.setInitialPattern(createInitialFilter());
276             if (dialog.open() == Window.CANCEL)
277                 return;
278             IType firstResult= (IType)dialog.getFirstResult();
279             fDestinationField.setText(JavaModelUtil.getFullyQualifiedName(firstResult));
280         }
281
282         private String JavaDoc createInitialFilter() {
283             if (! fDestinationField.getText().trim().equals("")) //$NON-NLS-1$
284
return fDestinationField.getText();
285             else
286                 return getMoveProcessor().getDeclaringType().getElementName();
287         }
288     
289         private static IStatus validateDestinationType(IType type, String JavaDoc typeName){
290             if (type == null || ! type.exists())
291                 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format(RefactoringMessages.MoveMembersInputPage_not_found, typeName), null);
292             if (type.isBinary())
293                 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, RefactoringMessages.MoveMembersInputPage_no_binary, null);
294             return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
295
}
296     
297         private MoveStaticMembersProcessor getMoveProcessor() {
298             return (MoveStaticMembersProcessor)getRefactoring().getAdapter(MoveStaticMembersProcessor.class);
299         }
300     }
301 }
302
Popular Tags