KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > RenameDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.help.ui.internal.views;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.help.ui.internal.*;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.*;
20 import org.eclipse.swt.layout.*;
21 import org.eclipse.swt.widgets.*;
22 import org.eclipse.ui.dialogs.SelectionStatusDialog;
23
24 public class RenameDialog extends SelectionStatusDialog {
25     private ArrayList JavaDoc oldNames;
26     private String JavaDoc oldName;
27     private String JavaDoc newName;
28     private Text text;
29     private IStatus status;
30     private boolean isCaseSensitive;
31     
32     /**
33      * Create a new rename dialog instance for the given window.
34      * @param shell The parent of the dialog
35      * @param oldName Current name of the item being renamed
36      */

37     public RenameDialog(Shell shell, String JavaDoc oldName) {
38         super(shell);
39         this.isCaseSensitive = false;
40         initialize();
41         setOldName(oldName);
42     }
43     
44     /**
45      * Create a new rename dialog instance for the given window.
46      * @param shell The parent of the dialog
47      * @param isCaseSensitive Flags whether dialog will perform case sensitive checks against old names
48      * @param names Set of names which the user should not be allowed to rename to
49      * @param oldName Current name of the item being renamed
50      */

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

154     protected void okPressed() {
155         newName = text.getText();
156         super.okPressed();
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
161      */

162     protected void computeResult() {
163     }
164     
165     public void setTitle(String JavaDoc title) {
166         getShell().setText(title);
167     }
168 }
169
Popular Tags