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