1 // Copyright (c) 2005 by Leif Frenzel. All rights reserved.
2 // See http://leiffrenzel.de
3 package net.sourceforge.phpdt.ltk.ui.wizards;
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;
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;
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.
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
40 public class RenameLocalVariablePage extends UserInputWizardPage {
42 private static final String DS_KEY = RenameLocalVariablePage.class
45 private static final String DS_RENAME_DQ_STRINGS = "RENAME_DQ_STRINGS"; //$NON-NLS-1$
47 private static final String DS_RENAME_PHPDOC = "RENAME_PHPDOC"; //$NON-NLS-1$
49 private static final String DS_RENAME_OTHER_COMMENTS = "RENAME_OTHER_COMMENTS"; //$NON-NLS-1$
51 private final RenameIdentifierInfo info;
53 private IDialogSettings dialogSettings;
55 private Text txtNewName;
57 private Button cbRenameDQStrings;
59 private Button cbRenamePHPdoc;
61 private Button cbRenameOtherComments;
63 public RenameLocalVariablePage(final RenameIdentifierInfo info) {
64 super(RenameLocalVariablePage.class.getName());
69 public void createControl(final Composite parent) {
70 Composite composite = createRootComposite(parent);
71 setControl(composite);
73 createLblNewName(composite);
74 createTxtNewName(composite);
75 createCbDQStrings(composite);
76 createCbPHPdoc(composite);
77 createCbOtherComments(composite);
81 // TODO check if we can leave this step out in the future
82 getRefactoringWizard().setForcePreviewReview(true);
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);
96 private void createLblNewName(final Composite composite) {
97 Label lblNewName = new Label(composite, SWT.NONE);
98 lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
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());
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);
124 initDQStringsOption();
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);
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);
150 initOtherCommentsOption();
153 private Button createCheckbox(final Composite composite, final String text) {
154 Button result = new Button(composite, SWT.CHECK);
155 result.setText(text);
157 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
158 gridData.horizontalSpan = 2;
159 result.setLayoutData(gridData);
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);
176 private static boolean isVariable(String txt) {
177 if (txt.length() <= 1) {
180 if (txt.charAt(0) != '$') {
183 for (int i = 1; i < txt.length(); i++) {
184 if (!Scanner.isPHPIdentifierPart(txt.charAt(i))) {
191 private void validate() {
192 String txt = txtNewName.getText();
194 setPageComplete(isVariable(txt) && !txt.equals(info.getOldName()));
197 private void initDQStringsOption() {
198 boolean refs = dialogSettings.getBoolean(DS_RENAME_DQ_STRINGS);
199 cbRenameDQStrings.setSelection(refs);
200 info.setRenameDQString(refs);
203 private void initPHPdocOption() {
204 boolean refs = dialogSettings.getBoolean(DS_RENAME_PHPDOC);
205 cbRenamePHPdoc.setSelection(refs);
206 info.setRenamePHPdoc(refs);
209 private void initOtherCommentsOption() {
210 boolean refs = dialogSettings.getBoolean(DS_RENAME_OTHER_COMMENTS);
211 cbRenameOtherComments.setSelection(refs);
212 info.setRenameOtherComments(refs);