KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IResource;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.TraverseEvent;
19 import org.eclipse.swt.events.TraverseListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.viewers.ITreeContentProvider;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.TreeViewer;
30
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32 import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
33 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
34
35 import org.eclipse.jdt.core.IJavaElement;
36 import org.eclipse.jdt.core.JavaModelException;
37
38 import org.eclipse.jdt.internal.corext.refactoring.reorg.ICreateTargetQuery;
39 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestinationValidator;
40 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
41
42 import org.eclipse.jdt.internal.ui.refactoring.QualifiedNameComponent;
43 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
44 import org.eclipse.jdt.internal.ui.util.SWTUtil;
45
46
47 public class ReorgMoveWizard extends RefactoringWizard {
48
49     public ReorgMoveWizard(MoveRefactoring ref) {
50         super(ref, DIALOG_BASED_USER_INTERFACE | computeHasPreviewPage(ref));
51         if (isTextualMove(ref))
52             setDefaultPageTitle(ReorgMessages.ReorgMoveWizard_textual_move);
53         else
54             setDefaultPageTitle(ReorgMessages.ReorgMoveWizard_3);
55     }
56     
57     private static boolean isTextualMove(MoveRefactoring ref) {
58         JavaMoveProcessor moveProcessor= (JavaMoveProcessor) ref.getAdapter(JavaMoveProcessor.class);
59         return moveProcessor.isTextualMove();
60     }
61
62     private static int computeHasPreviewPage(MoveRefactoring refactoring) {
63         JavaMoveProcessor processor= (JavaMoveProcessor)refactoring.getAdapter(JavaMoveProcessor.class);
64         if (processor.canUpdateReferences() || processor.canEnableQualifiedNameUpdating())
65             return NONE;
66         return NO_PREVIEW_PAGE;
67     }
68
69     protected void addUserInputPages() {
70         addPage(new MoveInputPage());
71     }
72     
73     private static class MoveInputPage extends ReorgUserInputPage{
74
75         private static final String JavaDoc PAGE_NAME= "MoveInputPage"; //$NON-NLS-1$
76
private Button fReferenceCheckbox;
77         private Button fQualifiedNameCheckbox;
78         private QualifiedNameComponent fQualifiedNameComponent;
79         private ICreateTargetQuery fCreateTargetQuery;
80         
81         private Object JavaDoc fDestination;
82         
83         public MoveInputPage() {
84             super(PAGE_NAME);
85         }
86
87         private JavaMoveProcessor getJavaMoveProcessor(){
88             return (JavaMoveProcessor)getRefactoring().getAdapter(JavaMoveProcessor.class);
89         }
90
91         protected Object JavaDoc getInitiallySelectedElement() {
92             return getJavaMoveProcessor().getCommonParentForInputElements();
93         }
94         
95         protected IJavaElement[] getJavaElements() {
96             return getJavaMoveProcessor().getJavaElements();
97         }
98
99         protected IResource[] getResources() {
100             return getJavaMoveProcessor().getResources();
101         }
102
103         protected IReorgDestinationValidator getDestinationValidator() {
104             return getJavaMoveProcessor();
105         }
106
107         protected boolean performFinish() {
108             return super.performFinish() || getJavaMoveProcessor().wasCanceled(); //close the dialog if canceled
109
}
110         
111         protected RefactoringStatus verifyDestination(Object JavaDoc selected) throws JavaModelException{
112             JavaMoveProcessor processor= getJavaMoveProcessor();
113             final RefactoringStatus refactoringStatus;
114             if (selected instanceof IJavaElement)
115                 refactoringStatus= processor.setDestination((IJavaElement)selected);
116             else if (selected instanceof IResource)
117                 refactoringStatus= processor.setDestination((IResource)selected);
118             else refactoringStatus= RefactoringStatus.createFatalErrorStatus(ReorgMessages.ReorgMoveWizard_4);
119             
120             updateUIStatus();
121             fDestination= selected;
122             return refactoringStatus;
123         }
124     
125         private void updateUIStatus() {
126             getRefactoringWizard().setForcePreviewReview(false);
127             JavaMoveProcessor processor= getJavaMoveProcessor();
128             if (fReferenceCheckbox != null){
129                 fReferenceCheckbox.setEnabled(canUpdateReferences());
130                 processor.setUpdateReferences(fReferenceCheckbox.getEnabled() && fReferenceCheckbox.getSelection());
131             }
132             if (fQualifiedNameCheckbox != null){
133                 boolean enabled= processor.canEnableQualifiedNameUpdating();
134                 fQualifiedNameCheckbox.setEnabled(enabled);
135                 if (enabled) {
136                     fQualifiedNameComponent.setEnabled(processor.getUpdateQualifiedNames());
137                     if (processor.getUpdateQualifiedNames())
138                         getRefactoringWizard().setForcePreviewReview(true);
139                 } else {
140                     fQualifiedNameComponent.setEnabled(false);
141                 }
142                 processor.setUpdateQualifiedNames(fQualifiedNameCheckbox.getEnabled() && fQualifiedNameCheckbox.getSelection());
143             }
144         }
145
146         private void addUpdateReferenceComponent(Composite result) {
147             final JavaMoveProcessor processor= getJavaMoveProcessor();
148             if (! processor.canUpdateReferences())
149                 return;
150             fReferenceCheckbox= new Button(result, SWT.CHECK);
151             fReferenceCheckbox.setText(ReorgMessages.JdtMoveAction_update_references);
152             fReferenceCheckbox.setSelection(processor.getUpdateReferences());
153             fReferenceCheckbox.setEnabled(canUpdateReferences());
154             
155             fReferenceCheckbox.addSelectionListener(new SelectionAdapter() {
156                 public void widgetSelected(SelectionEvent e) {
157                     processor.setUpdateReferences(((Button)e.widget).getSelection());
158                     updateUIStatus();
159                 }
160             });
161         }
162
163         private void addUpdateQualifiedNameComponent(Composite parent, int marginWidth) {
164             final JavaMoveProcessor processor= getJavaMoveProcessor();
165             if (!processor.canEnableQualifiedNameUpdating() || !processor.canUpdateQualifiedNames())
166                 return;
167             fQualifiedNameCheckbox= new Button(parent, SWT.CHECK);
168             int indent= marginWidth + fQualifiedNameCheckbox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
169             fQualifiedNameCheckbox.setText(RefactoringMessages.RenameInputWizardPage_update_qualified_names);
170             fQualifiedNameCheckbox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
171             fQualifiedNameCheckbox.setSelection(processor.getUpdateQualifiedNames());
172         
173             fQualifiedNameComponent= new QualifiedNameComponent(parent, SWT.NONE, processor, getRefactoringSettings());
174             fQualifiedNameComponent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
175             GridData gd= (GridData)fQualifiedNameComponent.getLayoutData();
176             gd.horizontalAlignment= GridData.FILL;
177             gd.horizontalIndent= indent;
178             updateQualifiedNameUpdating(processor, processor.getUpdateQualifiedNames());
179
180             fQualifiedNameCheckbox.addSelectionListener(new SelectionAdapter() {
181                 public void widgetSelected(SelectionEvent e) {
182                     boolean enabled= ((Button)e.widget).getSelection();
183                     updateQualifiedNameUpdating(processor, enabled);
184                 }
185
186             });
187         }
188         
189         private void updateQualifiedNameUpdating(final JavaMoveProcessor processor, boolean enabled) {
190             fQualifiedNameComponent.setEnabled(enabled);
191             processor.setUpdateQualifiedNames(enabled);
192             updateUIStatus();
193         }
194         
195         public void createControl(Composite parent) {
196             Composite result;
197             
198             boolean showDestinationTree= ! getJavaMoveProcessor().hasDestinationSet();
199             if (showDestinationTree) {
200                 fCreateTargetQuery= getJavaMoveProcessor().getCreateTargetQuery();
201                 super.createControl(parent);
202                 getTreeViewer().getTree().setFocus();
203                 result= (Composite)super.getControl();
204             } else {
205                 initializeDialogUnits(parent);
206                 result= new Composite(parent, SWT.NONE);
207                 setControl(result);
208                 result.setLayout(new GridLayout());
209                 Dialog.applyDialogFont(result);
210             }
211             addUpdateReferenceComponent(result);
212             addUpdateQualifiedNameComponent(result, ((GridLayout)result.getLayout()).marginWidth);
213             setControl(result);
214             Dialog.applyDialogFont(result);
215         }
216
217         protected Control addLabel(Composite parent) {
218             if (fCreateTargetQuery != null) {
219                 Composite firstLine= new Composite(parent, SWT.NONE);
220                 GridLayout layout= new GridLayout(2, false);
221                 layout.marginHeight= layout.marginWidth= 0;
222                 firstLine.setLayout(layout);
223                 firstLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
224                 
225                 Control label= super.addLabel(firstLine);
226                 label.addTraverseListener(new TraverseListener() {
227                     public void keyTraversed(TraverseEvent e) {
228                         if (e.detail == SWT.TRAVERSE_MNEMONIC && e.doit) {
229                             e.detail= SWT.TRAVERSE_NONE;
230                             getTreeViewer().getTree().setFocus();
231                         }
232                     }
233                 });
234                 
235                 Button newButton= new Button(firstLine, SWT.PUSH);
236                 newButton.setText(fCreateTargetQuery.getNewButtonLabel());
237                 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
238                 gd.widthHint = SWTUtil.getButtonWidthHint(newButton);
239                 newButton.setLayoutData(gd);
240                 newButton.addSelectionListener(new SelectionAdapter() {
241                     public void widgetSelected(SelectionEvent e) {
242                         doNewButtonPressed();
243                     }
244                 });
245                 
246                 return firstLine;
247                 
248             } else {
249                 return super.addLabel(parent);
250             }
251         }
252         
253         private boolean canUpdateReferences() {
254             return getJavaMoveProcessor().canUpdateReferences();
255         }
256
257         private void doNewButtonPressed() {
258             Object JavaDoc newElement= fCreateTargetQuery.getCreatedTarget(fDestination);
259             if (newElement != null) {
260                 TreeViewer viewer= getTreeViewer();
261                 ITreeContentProvider contentProvider= (ITreeContentProvider) viewer.getContentProvider();
262                 viewer.refresh(contentProvider.getParent(newElement));
263                 viewer.setSelection(new StructuredSelection(newElement), true);
264                 viewer.getTree().setFocus();
265             }
266         }
267     }
268 }
269
Popular Tags