001: /**
002: *
003: */package gui.wizards;
004:
005: import gui.RefactoringPlugin;
006:
007: import org.eclipse.core.runtime.IStatus;
008: import org.eclipse.jdt.core.JavaConventions;
009: import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
010: import org.eclipse.jface.dialogs.Dialog;
011: import org.eclipse.jface.dialogs.IDialogSettings;
012: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.events.KeyAdapter;
015: import org.eclipse.swt.events.KeyEvent;
016: import org.eclipse.swt.events.SelectionAdapter;
017: import org.eclipse.swt.events.SelectionEvent;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Combo;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Label;
024: import org.eclipse.swt.widgets.Text;
025:
026: import core.RefactorInfo;
027:
028: /** <p>the input page for the Rename Type refactoring, where users can
029: * control the effects of the refactoring; to be shown in the wizard.</p>
030: *
031: * <p>We let the user enter the new name for the property, and we let her
032: * decide whether other property files in the bundle should be affected, and
033: * whether the operation is supposed to span the entire workspace or only
034: * the current project.</p>
035: *
036: * @author sh
037: */
038: public class RenameTypeInputPage extends UserInputWizardPage {
039:
040: private static final String DS_KEY = RenameTypeInputPage.class
041: .getName();
042: private static final String DS_UPDATE_BUNDLE = "UPDATE_BUNDLE";
043: private static final String DS_ALL_PROJECTS = "ALL_PROJECTS";
044:
045: private final RefactorInfo info;
046:
047: private IDialogSettings dialogSettings;
048: private Text txtVarName;
049: private Combo comboNewType;
050:
051: public static final String[] TYPES = new String[] { "boolean",
052: "byte", "char", "String", "short", "int", "long", "float",
053: "double" };
054:
055: public RenameTypeInputPage(final RefactorInfo info) {
056: super (RenameTypeInputPage.class.getName());
057: this .info = info;
058: //initDialogSettings();
059: }
060:
061: /**
062: * interface methods of UserInputWizardPage
063: */
064: public void createControl(final Composite parent) {
065: Composite composite = createRootComposite(parent);
066: setControl(composite);
067:
068: createLblNewType(composite);
069: createComboNewType(composite);
070: createLblVarName(composite);
071: createTxtVarName(composite);
072:
073: validateType();
074: }
075:
076: /**
077: * UI creation method
078: */
079: private Composite createRootComposite(final Composite parent) {
080: Composite result = new Composite(parent, SWT.NONE);
081: GridLayout gridLayout = new GridLayout(2, false);
082: gridLayout.marginWidth = 10;
083: gridLayout.marginHeight = 10;
084: result.setLayout(gridLayout);
085: initializeDialogUnits(result);
086: Dialog.applyDialogFont(result);
087: return result;
088: }
089:
090: /**
091: * UI creation method
092: */
093: private void createLblNewType(final Composite composite) {
094: Label lblNewName = new Label(composite, SWT.NONE);
095: lblNewName.setText("New type");
096: }
097:
098: /**
099: * UI creation method
100: */
101: private void createComboNewType(Composite composite) {
102: comboNewType = new Combo(composite, SWT.SIMPLE);
103: comboNewType.setItems(TYPES);
104:
105: String oldType = info.getOldName();
106: for (int i = 0; i < TYPES.length; i++) {
107: String element = TYPES[i];
108: if (element.contains(oldType)) {
109: comboNewType.select(i);
110: }
111: }
112:
113: comboNewType.setLayoutData(new GridData(
114: GridData.FILL_HORIZONTAL));
115: comboNewType.addKeyListener(new KeyAdapter() {
116: public void keyReleased(final KeyEvent e) {
117: info.setNewName(comboNewType.getText());
118: validateType();
119: }
120: });
121: comboNewType.addSelectionListener(new SelectionAdapter() {
122: public void widgetSelected(SelectionEvent e) {
123: info.setNewName(comboNewType.getText());
124: validateType();
125: }
126: });
127: }
128:
129: /**
130: * UI creation method
131: */
132: private void createLblVarName(final Composite composite) {
133: Label lblNewName = new Label(composite, SWT.NONE);
134: lblNewName.setText("name");
135: }
136:
137: /**
138: * UI creation method
139: */
140: private void createTxtVarName(Composite composite) {
141: txtVarName = new Text(composite, SWT.BORDER);
142: txtVarName.setText(info.getOldVarName());
143: txtVarName
144: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
145: txtVarName.selectAll();
146: txtVarName.addKeyListener(new KeyAdapter() {
147: public void keyReleased(final KeyEvent e) {
148: info.setNewVarName(txtVarName.getText());
149: validateFieldName();
150: }
151: });
152: }
153:
154: /**
155: * helper method
156: */
157: private void initDialogSettings() {
158: IDialogSettings ds = RefactoringPlugin.getDefault()
159: .getDialogSettings();
160: dialogSettings = ds.getSection(DS_KEY);
161: if (dialogSettings == null) {
162: dialogSettings = ds.addNewSection(DS_KEY);
163: // init default values
164: dialogSettings.put(DS_UPDATE_BUNDLE, true);
165: dialogSettings.put(DS_ALL_PROJECTS, false);
166: }
167: }
168:
169: /**
170: * helper method
171: */
172: private void validateType() {
173: String typeTxt = comboNewType.getText();
174:
175: // check if the selected value exists in the default type collection
176: boolean contained = false;
177: for (int j = 0; j < TYPES.length; j++) {
178: String type = TYPES[j];
179: if (type.equals(typeTxt))
180: contained = true;
181: }
182:
183: // if not, check if the current value is valid
184: if (!contained) {
185: IStatus status = JavaConventions.validateJavaTypeName(
186: typeTxt, CompilerOptions.VERSION_1_3,
187: CompilerOptions.VERSION_1_3);
188: if (status.isOK()) {
189: setPageComplete(true);
190: setMessage(null);
191: } else if (status.ERROR == IStatus.ERROR) {
192: setPageComplete(false);
193: setMessage(status.getMessage());
194: }
195: }
196: }
197:
198: /**
199: * helper method
200: */
201: private void validateFieldName() {
202: String nameTxt = txtVarName.getText();
203: IStatus fieldStatus = JavaConventions.validateFieldName(
204: nameTxt, CompilerOptions.VERSION_1_3,
205: CompilerOptions.VERSION_1_3);
206:
207: if (fieldStatus.isOK() && nameTxt.length() > 0) {
208: setPageComplete(true);
209: setMessage(null);
210: } else if (fieldStatus.ERROR == IStatus.ERROR) {
211: setPageComplete(false);
212: setMessage(fieldStatus.getMessage());
213: }
214: }
215: }
|