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.getName();
43 private static final String DS_RENAME_DQ_STRINGS = "RENAME_DQ_STRINGS"; //$NON-NLS-1$
45 private static final String DS_RENAME_PHPDOC = "RENAME_PHPDOC"; //$NON-NLS-1$
47 private static final String DS_RENAME_OTHER_COMMENTS = "RENAME_OTHER_COMMENTS"; //$NON-NLS-1$
49 private final RenameIdentifierInfo info;
51 private IDialogSettings dialogSettings;
53 private Text txtNewName;
55 private Button cbRenameDQStrings;
57 private Button cbRenamePHPdoc;
59 private Button cbRenameOtherComments;
61 public RenameLocalVariablePage(final RenameIdentifierInfo info) {
62 super(RenameLocalVariablePage.class.getName());
67 public void createControl(final Composite parent) {
68 Composite composite = createRootComposite(parent);
69 setControl(composite);
71 createLblNewName(composite);
72 createTxtNewName(composite);
73 createCbDQStrings(composite);
74 createCbPHPdoc(composite);
75 createCbOtherComments(composite);
79 // TODO check if we can leave this step out in the future
80 getRefactoringWizard().setForcePreviewReview(true);
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);
94 private void createLblNewName(final Composite composite) {
95 Label lblNewName = new Label(composite, SWT.NONE);
96 lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
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());
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);
122 initDQStringsOption();
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);
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);
148 initOtherCommentsOption();
151 private Button createCheckbox(final Composite composite, final String text) {
152 Button result = new Button(composite, SWT.CHECK);
153 result.setText(text);
155 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
156 gridData.horizontalSpan = 2;
157 result.setLayoutData(gridData);
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);
174 private static boolean isVariable(String txt) {
175 if (txt.length() <= 1) {
178 if (txt.charAt(0) != '$') {
181 for (int i = 1; i < txt.length(); i++) {
182 if (!Scanner.isPHPIdentifierPart(txt.charAt(i))) {
189 private void validate() {
190 String txt = txtNewName.getText();
192 setPageComplete(isVariable(txt) && !txt.equals(info.getOldName()));
195 private void initDQStringsOption() {
196 boolean refs = dialogSettings.getBoolean(DS_RENAME_DQ_STRINGS);
197 cbRenameDQStrings.setSelection(refs);
198 info.setRenameDQString(refs);
201 private void initPHPdocOption() {
202 boolean refs = dialogSettings.getBoolean(DS_RENAME_PHPDOC);
203 cbRenamePHPdoc.setSelection(refs);
204 info.setRenamePHPdoc(refs);
207 private void initOtherCommentsOption() {
208 boolean refs = dialogSettings.getBoolean(DS_RENAME_OTHER_COMMENTS);
209 cbRenameOtherComments.setSelection(refs);
210 info.setRenameOtherComments(refs);