Added empty, foreach and isset keyword.
[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
42                         .getName();
43
44         private static final String DS_RENAME_DQ_STRINGS = "RENAME_DQ_STRINGS"; //$NON-NLS-1$
45
46         private static final String DS_RENAME_PHPDOC = "RENAME_PHPDOC"; //$NON-NLS-1$
47
48         private static final String DS_RENAME_OTHER_COMMENTS = "RENAME_OTHER_COMMENTS"; //$NON-NLS-1$
49
50         private final RenameIdentifierInfo info;
51
52         private IDialogSettings dialogSettings;
53
54         private Text txtNewName;
55
56         private Button cbRenameDQStrings;
57
58         private Button cbRenamePHPdoc;
59
60         private Button cbRenameOtherComments;
61
62         public RenameLocalVariablePage(final RenameIdentifierInfo info) {
63                 super(RenameLocalVariablePage.class.getName());
64                 this.info = info;
65                 initDialogSettings();
66         }
67
68         public void createControl(final Composite parent) {
69                 Composite composite = createRootComposite(parent);
70                 setControl(composite);
71
72                 createLblNewName(composite);
73                 createTxtNewName(composite);
74                 createCbDQStrings(composite);
75                 createCbPHPdoc(composite);
76                 createCbOtherComments(composite);
77
78                 validate();
79
80                 // TODO check if we can leave this step out in the future
81                 getRefactoringWizard().setForcePreviewReview(true);
82         }
83
84         private Composite createRootComposite(final Composite parent) {
85                 Composite result = new Composite(parent, SWT.NONE);
86                 GridLayout gridLayout = new GridLayout(2, false);
87                 gridLayout.marginWidth = 10;
88                 gridLayout.marginHeight = 10;
89                 result.setLayout(gridLayout);
90                 initializeDialogUnits(result);
91                 Dialog.applyDialogFont(result);
92                 return result;
93         }
94
95         private void createLblNewName(final Composite composite) {
96                 Label lblNewName = new Label(composite, SWT.NONE);
97                 lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
98         }
99
100         private void createTxtNewName(Composite composite) {
101                 txtNewName = new Text(composite, SWT.BORDER);
102                 txtNewName.setText(info.getOldName());
103                 txtNewName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
104                 txtNewName.selectAll();
105                 txtNewName.addKeyListener(new KeyAdapter() {
106                         public void keyReleased(final KeyEvent e) {
107                                 info.setNewName(txtNewName.getText());
108                                 validate();
109                         }
110                 });
111         }
112
113         private void createCbDQStrings(final Composite composite) {
114                 String texts = UITexts.renameLocalVariable_cbDQStrings;
115                 cbRenameDQStrings = createCheckbox(composite, texts);
116                 cbRenameDQStrings.addSelectionListener(new SelectionAdapter() {
117                         public void widgetSelected(final SelectionEvent event) {
118                                 boolean selected = cbRenameDQStrings.getSelection();
119                                 dialogSettings.put(DS_RENAME_DQ_STRINGS, selected);
120                                 info.setRenameDQString(selected);
121                         }
122                 });
123                 initDQStringsOption();
124         }
125
126         private void createCbPHPdoc(final Composite composite) {
127                 String texts = UITexts.renameLocalVariable_cbPHPdoc;
128                 cbRenamePHPdoc = createCheckbox(composite, texts);
129                 cbRenamePHPdoc.addSelectionListener(new SelectionAdapter() {
130                         public void widgetSelected(final SelectionEvent event) {
131                                 boolean selected = cbRenamePHPdoc.getSelection();
132                                 dialogSettings.put(DS_RENAME_PHPDOC, selected);
133                                 info.setRenamePHPdoc(selected);
134                         }
135                 });
136                 initPHPdocOption();
137         }
138
139         private void createCbOtherComments(final Composite composite) {
140                 String texts = UITexts.renameLocalVariable_cbOtherDoc;
141                 cbRenameOtherComments = createCheckbox(composite, texts);
142                 cbRenameOtherComments.addSelectionListener(new SelectionAdapter() {
143                         public void widgetSelected(final SelectionEvent event) {
144                                 boolean selected = cbRenameOtherComments.getSelection();
145                                 dialogSettings.put(DS_RENAME_OTHER_COMMENTS, selected);
146                                 info.setRenameOtherComments(selected);
147                         }
148                 });
149                 initOtherCommentsOption();
150         }
151
152         private Button createCheckbox(final Composite composite, final String text) {
153                 Button result = new Button(composite, SWT.CHECK);
154                 result.setText(text);
155
156                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
157                 gridData.horizontalSpan = 2;
158                 result.setLayoutData(gridData);
159
160                 return result;
161         }
162
163         private void initDialogSettings() {
164                 IDialogSettings ds = PHPeclipsePlugin.getDefault().getDialogSettings();
165                 dialogSettings = ds.getSection(DS_KEY);
166                 if (dialogSettings == null) {
167                         dialogSettings = ds.addNewSection(DS_KEY);
168                         // init default values
169                         dialogSettings.put(DS_RENAME_DQ_STRINGS, true);
170                         dialogSettings.put(DS_RENAME_PHPDOC, true);
171                         dialogSettings.put(DS_RENAME_OTHER_COMMENTS, true);
172                 }
173         }
174
175         private static boolean isVariable(String txt) {
176                 if (txt.length() <= 1) {
177                         return false;
178                 }
179                 if (txt.charAt(0) != '$') {
180                         return false;
181                 }
182                 for (int i = 1; i < txt.length(); i++) {
183                         if (!Scanner.isPHPIdentifierPart(txt.charAt(i))) {
184                                 return false;
185                         }
186                 }
187                 return true;
188         }
189
190         private void validate() {
191                 String txt = txtNewName.getText();
192                 Scanner s;
193                 setPageComplete(isVariable(txt) && !txt.equals(info.getOldName()));
194         }
195
196         private void initDQStringsOption() {
197                 boolean refs = dialogSettings.getBoolean(DS_RENAME_DQ_STRINGS);
198                 cbRenameDQStrings.setSelection(refs);
199                 info.setRenameDQString(refs);
200         }
201
202         private void initPHPdocOption() {
203                 boolean refs = dialogSettings.getBoolean(DS_RENAME_PHPDOC);
204                 cbRenamePHPdoc.setSelection(refs);
205                 info.setRenamePHPdoc(refs);
206         }
207
208         private void initOtherCommentsOption() {
209                 boolean refs = dialogSettings.getBoolean(DS_RENAME_OTHER_COMMENTS);
210                 cbRenameOtherComments.setSelection(refs);
211                 info.setRenameOtherComments(refs);
212         }
213
214 }