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