Merge xdebug 1.3.x
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ltk / ui / wizards / RenameIdentifierPage.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.ltk.core.RenameIdentifierInfo;
6 import net.sourceforge.phpdt.ltk.ui.UITexts;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8
9 import org.eclipse.jface.dialogs.Dialog;
10 import org.eclipse.jface.dialogs.IDialogSettings;
11 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.KeyAdapter;
14 import org.eclipse.swt.events.KeyEvent;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Text;
23
24 /**
25  * <p>
26  * the input page for the Rename Property refactoring, where users can control
27  * the effects of the refactoring; to be shown in the wizard.
28  * </p>
29  * 
30  * <p>
31  * We let the user enter the new name for the property, and we let her decide
32  * whether other property files in the bundle should be affected, and whether
33  * the operation is supposed to span the entire workspace or only the current
34  * project.
35  * </p>
36  * 
37  */
38 public class RenameIdentifierPage extends UserInputWizardPage {
39
40         private static final String DS_KEY = RenameIdentifierPage.class.getName();
41
42         private static final String DS_UPDATE_BUNDLE = "UPDATE_BUNDLE"; //$NON-NLS-1$
43
44         private static final String DS_ALL_PROJECTS = "ALL_PROJECTS"; //$NON-NLS-1$
45
46         private final RenameIdentifierInfo info;
47
48         private IDialogSettings dialogSettings;
49
50         private Text txtNewName;
51
52         private Button cbUpdateBundle;
53
54         private Button cbAllProjects;
55
56         public RenameIdentifierPage(final RenameIdentifierInfo info) {
57                 super(RenameIdentifierPage.class.getName());
58                 this.info = info;
59                 initDialogSettings();
60         }
61
62         // interface methods of UserInputWizardPage
63         // /////////////////////////////////////////
64
65         public void createControl(final Composite parent) {
66                 Composite composite = createRootComposite(parent);
67                 setControl(composite);
68
69                 createLblNewName(composite);
70                 createTxtNewName(composite);
71                 createCbUpdateBundle(composite);
72                 createCbAllProjects(composite);
73
74                 validate();
75         }
76
77         // UI creation methods
78         // ////////////////////
79
80         private Composite createRootComposite(final Composite parent) {
81                 Composite result = new Composite(parent, SWT.NONE);
82                 GridLayout gridLayout = new GridLayout(2, false);
83                 gridLayout.marginWidth = 10;
84                 gridLayout.marginHeight = 10;
85                 result.setLayout(gridLayout);
86                 initializeDialogUnits(result);
87                 Dialog.applyDialogFont(result);
88                 return result;
89         }
90
91         private void createLblNewName(final Composite composite) {
92                 Label lblNewName = new Label(composite, SWT.NONE);
93                 lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
94         }
95
96         private void createTxtNewName(Composite composite) {
97                 txtNewName = new Text(composite, SWT.BORDER);
98                 txtNewName.setText(info.getOldName());
99                 txtNewName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
100                 txtNewName.selectAll();
101                 txtNewName.addKeyListener(new KeyAdapter() {
102                         public void keyReleased(final KeyEvent e) {
103                                 info.setNewName(txtNewName.getText());
104                                 validate();
105                         }
106                 });
107         }
108
109         private void createCbUpdateBundle(final Composite composite) {
110                 String texts = UITexts.renamePropertyInputPage_cbUpdateBundle;
111                 cbUpdateBundle = createCheckbox(composite, texts);
112                 cbUpdateBundle.addSelectionListener(new SelectionAdapter() {
113                         public void widgetSelected(final SelectionEvent event) {
114                                 boolean selected = cbUpdateBundle.getSelection();
115                                 dialogSettings.put(DS_UPDATE_BUNDLE, selected);
116                                 info.setUpdateProject(selected);
117                         }
118                 });
119                 initUpdateBundleOption();
120         }
121
122         private void createCbAllProjects(final Composite composite) {
123                 String text = UITexts.renamePropertyInputPage_cbAllProjects;
124                 cbAllProjects = createCheckbox(composite, text);
125                 cbAllProjects.addSelectionListener(new SelectionAdapter() {
126                         public void widgetSelected(final SelectionEvent event) {
127                                 boolean selected = cbAllProjects.getSelection();
128                                 dialogSettings.put(DS_ALL_PROJECTS, selected);
129                                 info.setAllProjects(selected);
130                                 // for demonstration purposes, we enforce the preview for
131                                 // refactorings
132                                 // that span the entire workspace
133                                 getRefactoringWizard().setForcePreviewReview(selected);
134                         }
135                 });
136                 initAllProjectsOption();
137         }
138
139         private Button createCheckbox(final Composite composite, final String text) {
140                 Button result = new Button(composite, SWT.CHECK);
141                 result.setText(text);
142
143                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
144                 gridData.horizontalSpan = 2;
145                 result.setLayoutData(gridData);
146
147                 return result;
148         }
149
150         // helping methods
151         // ////////////////
152
153         private void initDialogSettings() {
154                 IDialogSettings ds = PHPeclipsePlugin.getDefault().getDialogSettings();
155                 dialogSettings = ds.getSection(DS_KEY);
156                 if (dialogSettings == null) {
157                         dialogSettings = ds.addNewSection(DS_KEY);
158                         // init default values
159                         dialogSettings.put(DS_UPDATE_BUNDLE, true);
160                         dialogSettings.put(DS_ALL_PROJECTS, false);
161                 }
162         }
163
164         private void validate() {
165                 String txt = txtNewName.getText();
166                 setPageComplete(txt.length() > 0 && !txt.equals(info.getOldName()));
167         }
168
169         private void initUpdateBundleOption() {
170                 boolean updateRefs = dialogSettings.getBoolean(DS_UPDATE_BUNDLE);
171                 cbUpdateBundle.setSelection(updateRefs);
172                 info.setUpdateProject(updateRefs);
173         }
174
175         private void initAllProjectsOption() {
176                 boolean allProjects = dialogSettings.getBoolean(DS_ALL_PROJECTS);
177                 cbAllProjects.setSelection(allProjects);
178                 info.setAllProjects(allProjects);
179         }
180 }