KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > RenameDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.IInputValidator;
19 import org.eclipse.pde.internal.ui.PDEPlugin;
20 import org.eclipse.pde.internal.ui.PDEUIMessages;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.ui.dialogs.SelectionStatusDialog;
33
34 public class RenameDialog extends SelectionStatusDialog {
35     private ArrayList JavaDoc oldNames;
36     private String JavaDoc oldName;
37     private String JavaDoc newName;
38     private Text text;
39     private IStatus status;
40     private boolean isCaseSensitive;
41     private IInputValidator fValidator;
42     
43     /**
44      * Create a new rename dialog instance for the given window.
45      * @param shell The parent of the dialog
46      * @param oldName Current name of the item being renamed
47      */

48     public RenameDialog(Shell shell, String JavaDoc oldName) {
49         super(shell);
50         this.isCaseSensitive = false;
51         initialize();
52         setOldName(oldName);
53     }
54     
55     /**
56      * Create a new rename dialog instance for the given window.
57      * @param shell The parent of the dialog
58      * @param isCaseSensitive Flags whether dialog will perform case sensitive checks against old names
59      * @param names Set of names which the user should not be allowed to rename to
60      * @param oldName Current name of the item being renamed
61      */

62     public RenameDialog(Shell shell, boolean isCaseSensitive, String JavaDoc[] names, String JavaDoc oldName){
63         super(shell);
64         this.isCaseSensitive = isCaseSensitive;
65         initialize();
66         if (names!=null){
67             for (int i = 0; i<names.length; i++)
68                 addOldName(names[i]);
69         }
70         setOldName(oldName);
71     }
72     
73     public void initialize(){
74         oldNames = new ArrayList JavaDoc();
75         setStatusLineAboveButtons(true);
76     }
77     public void addOldName(String JavaDoc oldName){
78         if (!oldNames.contains(oldName))
79             oldNames.add(oldName);
80         
81     }
82     public void setOldName(String JavaDoc oldName) {
83         this.oldName = oldName;
84         addOldName(oldName);
85         if (text!=null)
86             text.setText(oldName);
87         this.newName = oldName;
88     }
89     
90     protected Control createDialogArea(Composite parent) {
91         Composite container = new Composite(parent, SWT.NULL);
92         GridLayout layout = new GridLayout();
93         layout.numColumns = 2;
94         layout.marginHeight = layout.marginWidth = 9;
95         container.setLayout(layout);
96         
97         GridData gd = new GridData(GridData.FILL_BOTH);
98         container.setLayoutData(gd);
99         
100         Label label = new Label(container, SWT.NULL);
101         label.setText(PDEUIMessages.RenameDialog_label);
102         
103         text = new Text(container, SWT.SINGLE|SWT.BORDER);
104         text.addModifyListener(new ModifyListener() {
105             public void modifyText(ModifyEvent e) {
106                 textChanged(text.getText());
107             }
108         });
109         gd = new GridData(GridData.FILL_HORIZONTAL);
110         text.setLayoutData(gd);
111         applyDialogFont(container);
112         return container;
113     }
114     
115     public int open() {
116         text.setText(oldName);
117         text.selectAll();
118         Button okButton = getButton(IDialogConstants.OK_ID);
119         
120         status = new Status(
121                 IStatus.OK,
122                 PDEPlugin.getPluginId(),
123                 IStatus.OK,
124                 "", //$NON-NLS-1$
125
null);
126         updateStatus(status);
127         okButton.setEnabled(false);
128         return super.open();
129     }
130     
131     private void textChanged(String JavaDoc text) {
132         Button okButton = getButton(IDialogConstants.OK_ID);
133         if (fValidator != null) {
134             String JavaDoc message = fValidator.isValid(text);
135             if (message != null) {
136                 status = new Status(
137                         IStatus.ERROR, PDEPlugin.getPluginId(),
138                         IStatus.ERROR, message, null);
139                 updateStatus(status);
140                 okButton.setEnabled(false);
141                 return;
142             }
143         }
144         for (int i=0; i<oldNames.size(); i++){
145             if((isCaseSensitive && text.equals(oldNames.get(i))) ||
146                     (!isCaseSensitive && text.equalsIgnoreCase(oldNames.get(i).toString()))){
147                 status = new Status(
148                         IStatus.ERROR,
149                         PDEPlugin.getPluginId(),
150                         IStatus.ERROR,
151                         PDEUIMessages.RenameDialog_validationError,
152                         null);
153                 updateStatus(status);
154                 okButton.setEnabled(false);
155                 break;
156             }
157             okButton.setEnabled(true);
158             status = new Status(
159                 IStatus.OK,
160                 PDEPlugin.getPluginId(),
161                 IStatus.OK,
162                 "", //$NON-NLS-1$
163
null);
164             updateStatus(status);
165         }
166     }
167     
168     public String JavaDoc getNewName() {
169         return newName;
170     }
171
172     /* (non-Javadoc)
173      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
174      */

175     protected void okPressed() {
176         newName = text.getText().trim();
177         super.okPressed();
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
182      */

183     protected void computeResult() {
184     }
185     
186     public void setTitle(String JavaDoc title) {
187         getShell().setText(title);
188     }
189     
190     public void setInputValidator(IInputValidator validator) {
191         fValidator = validator;
192     }
193 }
194
Popular Tags