KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15
16 import org.eclipse.jdt.core.JavaConventions;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28
29 import org.eclipse.jface.dialogs.StatusDialog;
30
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32
33 import org.eclipse.jdt.internal.corext.refactoring.Checks;
34 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
35 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
36 import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext;
37 import org.eclipse.jdt.internal.corext.refactoring.TypeContextChecker;
38 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring;
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
43 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
44 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
45
46 public class ParameterEditDialog extends StatusDialog {
47     
48     private final ParameterInfo fParameter;
49     private final boolean fEditType;
50     private final boolean fEditDefault;
51     private final StubTypeContext fContext;
52     private Text fType;
53     private Text fName;
54     private Text fDefaultValue;
55     
56     /**
57      * @param context the <code>IPackageFragment</code> for type ContentAssist.
58      * Can be <code>null</code> if <code>canEditType</code> is <code>false</code>.
59      */

60     public ParameterEditDialog(Shell parentShell, ParameterInfo parameter, boolean canEditType, boolean canEditDefault, StubTypeContext context) {
61         super(parentShell);
62         fParameter= parameter;
63         fEditType= canEditType;
64         fEditDefault= canEditDefault;
65         fContext= context;
66     }
67     
68     protected void configureShell(Shell newShell) {
69         super.configureShell(newShell);
70         newShell.setText(RefactoringMessages.ParameterEditDialog_title);
71     }
72
73     protected Control createDialogArea(Composite parent) {
74         Composite result= (Composite)super.createDialogArea(parent);
75         GridLayout layout= (GridLayout)result.getLayout();
76         layout.numColumns= 2;
77         Label label;
78         GridData gd;
79         
80         label= new Label(result, SWT.NONE);
81         String JavaDoc newName = fParameter.getNewName();
82         if (newName.length() == 0)
83             label.setText(RefactoringMessages.ParameterEditDialog_message_new);
84         else
85             label.setText(Messages.format(RefactoringMessages.ParameterEditDialog_message, newName));
86         gd= new GridData(GridData.FILL_HORIZONTAL);
87         gd.horizontalSpan= 2;
88         label.setLayoutData(gd);
89         
90         if (fEditType) {
91             label= new Label(result, SWT.NONE);
92             label.setText(RefactoringMessages.ParameterEditDialog_type);
93             fType= new Text(result, SWT.BORDER);
94             gd= new GridData(GridData.FILL_HORIZONTAL);
95             fType.setLayoutData(gd);
96             fType.setText(fParameter.getNewTypeName());
97             fType.addModifyListener(
98                 new ModifyListener() {
99                     public void modifyText(ModifyEvent e) {
100                         validate((Text)e.widget);
101                     }
102                 });
103             TextFieldNavigationHandler.install(fType);
104             JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(true, false);
105             processor.setCompletionContext(fContext.getCuHandle(), fContext.getBeforeString(), fContext.getAfterString());
106             ControlContentAssistHelper.createTextContentAssistant(fType, processor);
107         }
108
109         label= new Label(result, SWT.NONE);
110         fName= new Text(result, SWT.BORDER);
111         initializeDialogUnits(fName);
112         label.setText(RefactoringMessages.ParameterEditDialog_name);
113         gd= new GridData(GridData.FILL_HORIZONTAL);
114         gd.widthHint= convertWidthInCharsToPixels(45);
115         fName.setLayoutData(gd);
116         fName.setText(newName);
117         fName.addModifyListener(
118             new ModifyListener() {
119                 public void modifyText(ModifyEvent e) {
120                     validate((Text)e.widget);
121                 }
122             });
123         TextFieldNavigationHandler.install(fName);
124
125         if (fEditDefault && fParameter.isAdded()) {
126             label= new Label(result, SWT.NONE);
127             label.setText(RefactoringMessages.ParameterEditDialog_defaultValue);
128             fDefaultValue= new Text(result, SWT.BORDER);
129             gd= new GridData(GridData.FILL_HORIZONTAL);
130             fDefaultValue.setLayoutData(gd);
131             fDefaultValue.setText(fParameter.getDefaultValue());
132             fDefaultValue.addModifyListener(
133                 new ModifyListener() {
134                     public void modifyText(ModifyEvent e) {
135                         validate((Text)e.widget);
136                     }
137                 });
138             TextFieldNavigationHandler.install(fDefaultValue);
139         }
140         applyDialogFont(result);
141         return result;
142     }
143     
144     protected void okPressed() {
145         if (fType != null) {
146             fParameter.setNewTypeName(fType.getText());
147         }
148         fParameter.setNewName(fName.getText());
149         if (fDefaultValue != null) {
150             fParameter.setDefaultValue(fDefaultValue.getText());
151         }
152         super.okPressed();
153     }
154     
155     private void validate(Text first) {
156         IStatus[] result= new IStatus[3];
157         if (first == fType) {
158             result[0]= validateType();
159             result[1]= validateName();
160             result[2]= validateDefaultValue();
161         } else if (first == fName) {
162             result[0]= validateName();
163             result[1]= validateType();
164             result[2]= validateDefaultValue();
165         } else {
166             result[0]= validateDefaultValue();
167             result[1]= validateName();
168             result[2]= validateType();
169         }
170         for (int i= 0; i < result.length; i++) {
171             IStatus status= result[i];
172             if (status != null && !status.isOK()) {
173                 updateStatus(status);
174                 return;
175             }
176         }
177         updateStatus(createOkStatus());
178     }
179     
180     private IStatus validateType() {
181         if (fType == null)
182             return null;
183         String JavaDoc type= fType.getText();
184         
185         RefactoringStatus status= TypeContextChecker.checkParameterTypeSyntax(type, fContext.getCuHandle().getJavaProject());
186         if (status == null || status.isOK())
187             return createOkStatus();
188         if (status.hasError())
189             return createErrorStatus(status.getEntryWithHighestSeverity().getMessage());
190         else
191             return createWarningStatus(status.getEntryWithHighestSeverity().getMessage());
192     }
193     
194     private IStatus validateName() {
195         if (fName == null)
196             return null;
197         String JavaDoc text= fName.getText();
198         if (text.length() == 0)
199             return createErrorStatus(RefactoringMessages.ParameterEditDialog_name_error);
200         IStatus status= JavaConventions.validateFieldName(text);
201         if (status.matches(IStatus.ERROR))
202             return status;
203         if (! Checks.startsWithLowerCase(text))
204             return createWarningStatus(RefactoringCoreMessages.ExtractTempRefactoring_convention);
205         return createOkStatus();
206     }
207     
208     private IStatus validateDefaultValue() {
209         if (fDefaultValue == null)
210             return null;
211         String JavaDoc defaultValue= fDefaultValue.getText();
212         if (defaultValue.length() == 0)
213             return createErrorStatus(RefactoringMessages.ParameterEditDialog_defaultValue_error);
214         if (ChangeSignatureRefactoring.isValidExpression(defaultValue))
215             return createOkStatus();
216         String JavaDoc msg= Messages.format(RefactoringMessages.ParameterEditDialog_defaultValue_invalid, new String JavaDoc[]{defaultValue});
217         return createErrorStatus(msg);
218         
219     }
220     
221     private Status createOkStatus() {
222         return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
223
}
224     
225     private Status createWarningStatus(String JavaDoc message) {
226         return new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.WARNING, message, null);
227     }
228     
229     private Status createErrorStatus(String JavaDoc message) {
230         return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR, message, null);
231     }
232 }
233
Popular Tags