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