Refactoringaction: net.sourceforge.phpdt.ltk.ui.actions.RenameLocalVariable
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ltk / ui / wizards / RenameLocalVariablePage.java
1 // Copyright (c) 2005 by Leif Frenzel. All rights reserved.
2 // See http://leiffrenzel.de
3 package net.sourceforge.phpdt.ltk.ui.wizards;
4
5 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
6 import net.sourceforge.phpdt.ltk.core.RenameIdentifierInfo;
7 import net.sourceforge.phpdt.ltk.ui.UITexts;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9
10 import org.eclipse.jface.dialogs.Dialog;
11 import org.eclipse.jface.dialogs.IDialogSettings;
12 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.KeyAdapter;
15 import org.eclipse.swt.events.KeyEvent;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
24
25 /**
26  * <p>
27  * the input page for the Rename Property refactoring, where users can control
28  * the effects of the refactoring; to be shown in the wizard.
29  * </p>
30  *
31  * <p>
32  * We let the user enter the new name for the property, and we let her decide
33  * whether other property files in the bundle should be affected, and whether
34  * the operation is supposed to span the entire workspace or only the current
35  * project.
36  * </p>
37  *
38  */
39 public class RenameLocalVariablePage extends UserInputWizardPage {
40
41         private static final String DS_KEY = RenameLocalVariablePage.class.getName();
42
43         private static final String DS_RENAME_DQ_STRINGS = "RENAME_DQ_STRINGS"; //$NON-NLS-1$
44
45         private static final String DS_RENAME_PHPDOC = "RENAME_PHPDOC"; //$NON-NLS-1$
46
47         private static final String DS_RENAME_OTHER_COMMENTS = "RENAME_OTHER_COMMENTS"; //$NON-NLS-1$
48
49         private final RenameIdentifierInfo info;
50
51         private IDialogSettings dialogSettings;
52
53         private Text txtNewName;
54
55         private Button cbRenameDQStrings;
56
57         private Button cbRenamePHPdoc;
58
59         private Button cbRenameOtherComments;
60
61         public RenameLocalVariablePage(final RenameIdentifierInfo info) {
62                 super(RenameLocalVariablePage.class.getName());
63                 this.info = info;
64                 initDialogSettings();
65         }
66
67         public void createControl(final Composite parent) {
68                 Composite composite = createRootComposite(parent);
69                 setControl(composite);
70
71                 createLblNewName(composite);
72                 createTxtNewName(composite);
73                 createCbDQStrings(composite);
74                 createCbPHPdoc(composite);
75                 createCbOtherComments(composite);
76
77                 validate();
78
79                 // TODO check if we can leave this step out in the future
80                 getRefactoringWizard().setForcePreviewReview(true);
81         }
82
83         private Composite createRootComposite(final Composite parent) {
84                 Composite result = new Composite(parent, SWT.NONE);
85                 GridLayout gridLayout = new GridLayout(2, false);
86                 gridLayout.marginWidth = 10;
87                 gridLayout.marginHeight = 10;
88                 result.setLayout(gridLayout);
89                 initializeDialogUnits(result);
90                 Dialog.applyDialogFont(result);
91                 return result;
92         }
93
94         private void createLblNewName(final Composite composite) {
95                 Label lblNewName = new Label(composite, SWT.NONE);
96                 lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
97         }
98
99         private void createTxtNewName(Composite composite) {
100                 txtNewName = new Text(composite, SWT.BORDER);
101                 txtNewName.setText(info.getOldName());
102                 txtNewName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
103                 txtNewName.selectAll();
104                 txtNewName.addKeyListener(new KeyAdapter() {
105                         public void keyReleased(final KeyEvent e) {
106                                 info.setNewName(txtNewName.getText());
107                                 validate();
108                         }
109                 });
110         }
111
112         private void createCbDQStrings(final Composite composite) {
113                 String texts = UITexts.renameLocalVariable_cbDQStrings;
114                 cbRenameDQStrings = createCheckbox(composite, texts);
115                 cbRenameDQStrings.addSelectionListener(new SelectionAdapter() {
116                         public void widgetSelected(final SelectionEvent event) {
117                                 boolean selected = cbRenameDQStrings.getSelection();
118                                 dialogSettings.put(DS_RENAME_DQ_STRINGS, selected);
119                                 info.setRenameDQString(selected);
120                         }
121                 });
122                 initDQStringsOption();
123         }
124
125         private void createCbPHPdoc(final Composite composite) {
126                 String texts = UITexts.renameLocalVariable_cbPHPdoc;
127                 cbRenamePHPdoc = createCheckbox(composite, texts);
128                 cbRenamePHPdoc.addSelectionListener(new SelectionAdapter() {
129                         public void widgetSelected(final SelectionEvent event) {
130                                 boolean selected = cbRenamePHPdoc.getSelection();
131                                 dialogSettings.put(DS_RENAME_PHPDOC, selected);
132                                 info.setRenamePHPdoc(selected);
133                         }
134                 });
135                 initPHPdocOption();
136         }
137
138         private void createCbOtherComments(final Composite composite) {
139                 String texts = UITexts.renameLocalVariable_cbOtherDoc;
140                 cbRenameOtherComments = createCheckbox(composite, texts);
141                 cbRenameOtherComments.addSelectionListener(new SelectionAdapter() {
142                         public void widgetSelected(final SelectionEvent event) {
143                                 boolean selected = cbRenameOtherComments.getSelection();
144                                 dialogSettings.put(DS_RENAME_OTHER_COMMENTS, selected);
145                                 info.setRenameOtherComments(selected);
146                         }
147                 });
148                 initOtherCommentsOption();
149         }
150
151         private Button createCheckbox(final Composite composite, final String text) {
152                 Button result = new Button(composite, SWT.CHECK);
153                 result.setText(text);
154
155                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
156                 gridData.horizontalSpan = 2;
157                 result.setLayoutData(gridData);
158
159                 return result;
160         }
161
162         private void initDialogSettings() {
163                 IDialogSettings ds = PHPeclipsePlugin.getDefault().getDialogSettings();
164                 dialogSettings = ds.getSection(DS_KEY);
165                 if (dialogSettings == null) {
166                         dialogSettings = ds.addNewSection(DS_KEY);
167                         // init default values
168                         dialogSettings.put(DS_RENAME_DQ_STRINGS, true);
169                         dialogSettings.put(DS_RENAME_PHPDOC, true);
170                         dialogSettings.put(DS_RENAME_OTHER_COMMENTS, true);
171                 }
172         }
173
174         private static boolean isVariable(String txt) {
175                 if (txt.length() <= 1) {
176                         return false;
177                 }
178                 if (txt.charAt(0) != '$') {
179                         return false;
180                 }
181                 for (int i = 1; i < txt.length(); i++) {
182                         if (!Scanner.isPHPIdentifierPart(txt.charAt(i))) {
183                                 return false;
184                         }
185                 }
186                 return true;
187         }
188
189         private void validate() {
190                 String txt = txtNewName.getText();
191                 Scanner s;
192                 setPageComplete(isVariable(txt) && !txt.equals(info.getOldName()));
193         }
194
195         private void initDQStringsOption() {
196                 boolean refs = dialogSettings.getBoolean(DS_RENAME_DQ_STRINGS);
197                 cbRenameDQStrings.setSelection(refs);
198                 info.setRenameDQString(refs);
199         }
200
201         private void initPHPdocOption() {
202                 boolean refs = dialogSettings.getBoolean(DS_RENAME_PHPDOC);
203                 cbRenamePHPdoc.setSelection(refs);
204                 info.setRenamePHPdoc(refs);
205         }
206
207         private void initOtherCommentsOption() {
208                 boolean refs = dialogSettings.getBoolean(DS_RENAME_OTHER_COMMENTS);
209                 cbRenameOtherComments.setSelection(refs);
210                 info.setRenameOtherComments(refs);
211         }
212
213 }