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;
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;
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.
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
39 public class RenameLocalVariablePage extends UserInputWizardPage {
41 private static final String DS_KEY = RenameLocalVariablePage.class
44 private static final String DS_RENAME_DQ_STRINGS = "RENAME_DQ_STRINGS"; //$NON-NLS-1$
46 private static final String DS_RENAME_PHPDOC = "RENAME_PHPDOC"; //$NON-NLS-1$
48 private static final String DS_RENAME_OTHER_COMMENTS = "RENAME_OTHER_COMMENTS"; //$NON-NLS-1$
50 private final RenameIdentifierInfo info;
52 private IDialogSettings dialogSettings;
54 private Text txtNewName;
56 private Button cbRenameDQStrings;
58 private Button cbRenamePHPdoc;
60 private Button cbRenameOtherComments;
62 public RenameLocalVariablePage(final RenameIdentifierInfo info) {
63 super(RenameLocalVariablePage.class.getName());
68 public void createControl(final Composite parent) {
69 Composite composite = createRootComposite(parent);
70 setControl(composite);
72 createLblNewName(composite);
73 createTxtNewName(composite);
74 createCbDQStrings(composite);
75 createCbPHPdoc(composite);
76 createCbOtherComments(composite);
80 // TODO check if we can leave this step out in the future
81 getRefactoringWizard().setForcePreviewReview(true);
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);
95 private void createLblNewName(final Composite composite) {
96 Label lblNewName = new Label(composite, SWT.NONE);
97 lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
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());
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);
123 initDQStringsOption();
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);
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);
149 initOtherCommentsOption();
152 private Button createCheckbox(final Composite composite, final String text) {
153 Button result = new Button(composite, SWT.CHECK);
154 result.setText(text);
156 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
157 gridData.horizontalSpan = 2;
158 result.setLayoutData(gridData);
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);
175 private static boolean isVariable(String txt) {
176 if (txt.length() <= 1) {
179 if (txt.charAt(0) != '$') {
182 for (int i = 1; i < txt.length(); i++) {
183 if (!Scanner.isPHPIdentifierPart(txt.charAt(i))) {
190 private void validate() {
191 String txt = txtNewName.getText();
193 setPageComplete(isVariable(txt) && !txt.equals(info.getOldName()));
196 private void initDQStringsOption() {
197 boolean refs = dialogSettings.getBoolean(DS_RENAME_DQ_STRINGS);
198 cbRenameDQStrings.setSelection(refs);
199 info.setRenameDQString(refs);
202 private void initPHPdocOption() {
203 boolean refs = dialogSettings.getBoolean(DS_RENAME_PHPDOC);
204 cbRenamePHPdoc.setSelection(refs);
205 info.setRenamePHPdoc(refs);
208 private void initOtherCommentsOption() {
209 boolean refs = dialogSettings.getBoolean(DS_RENAME_OTHER_COMMENTS);
210 cbRenameOtherComments.setSelection(refs);
211 info.setRenameOtherComments(refs);