upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / newui / RenameConfigurationDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2005, 2009 Intel 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  * Intel Corporation - Initial API and implementation
10  * IBM Corporation
11  *******************************************************************************/
12 package org.eclipse.cdt.ui.newui;
13
14 import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
15
16 import org.eclipse.cdt.internal.ui.newui.Messages;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.preference.JFacePreferences;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Group;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Text;
35
36 /**
37  * @noextend This class is not intended to be subclassed by clients.
38  */
39 public class RenameConfigurationDialog extends Dialog {
40         // Widgets
41         
42         private Text configName;
43         private Text configDescription;
44                 
45         private ICConfigurationDescription[] cfgds;
46         private ICConfigurationDescription renameConfig;
47         private String newName;
48         private String newDescription;
49         private Label statusLabel;
50         
51         private String originalName;
52         final private String title;
53         /**
54          */
55         protected RenameConfigurationDialog(Shell parentShell, 
56                         ICConfigurationDescription _renameConfig,
57                         ICConfigurationDescription[] _cfgds,
58                         String _title) {
59                 super(parentShell);
60                 title = _title;
61                 renameConfig = _renameConfig;
62                 cfgds = _cfgds;
63                 
64                 setShellStyle(getShellStyle()|SWT.RESIZE);
65                 newName = renameConfig.getName();
66                 newDescription = renameConfig.getDescription();
67                 if(newDescription == null) newDescription = new String();
68                 originalName = renameConfig.getName();
69         }
70         
71         /* (non-Javadoc)
72          * Method declared on Dialog. Cache the name and base config selections.
73          * We don't have to worry that the index or name is wrong because we 
74          * enable the OK button IFF those conditions are met.
75          */
76         @Override
77         protected void buttonPressed(int buttonId) {
78                 if (buttonId == IDialogConstants.OK_ID) {
79                         newName = configName.getText().trim();
80                         newDescription = configDescription.getText().trim();
81                 } 
82                 super.buttonPressed(buttonId);
83         }
84
85         /* (non-Javadoc)
86          * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
87          */
88         @Override
89         protected void configureShell(Shell shell) {
90                 super.configureShell(shell);
91                 if (title != null) shell.setText(title);
92         }
93
94         /* (non-Javadoc)
95          * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
96          */
97         @Override
98         protected void createButtonsForButtonBar(Composite parent) {
99                 super.createButtonsForButtonBar(parent);
100                 configName.setFocus();
101                 if (configName != null) {
102                         configName.setText(newName);
103                 }
104                 validateState();
105         }
106
107         @Override
108         protected Control createDialogArea(Composite parent) {
109                 
110                 Composite composite = new Composite(parent, SWT.NULL);
111                 composite.setFont(parent.getFont());
112                 composite.setLayout(new GridLayout(3, false));
113                 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
114                 
115                 // Create a group for the name & description
116
117                 final Group group1 = new Group(composite, SWT.NONE);
118                 group1.setFont(composite.getFont());
119                 GridLayout layout1 = new GridLayout(3, false);
120                 group1.setLayout(layout1);
121                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
122                 gd.horizontalSpan = 3;
123                 group1.setLayoutData(gd);
124
125                 // bug 187634: Add a label to warn user that configuration name will be used directly
126                 // as a directory name in the filesystem.
127                 Label warningLabel = new Label(group1, SWT.BEGINNING | SWT.WRAP);
128                 warningLabel.setFont(parent.getFont());
129                 warningLabel.setText(Messages.RenameConfiguration_label_warning); 
130                 gd = new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1);
131                 gd.widthHint = 300;
132                 warningLabel.setLayoutData(gd);
133
134                 // Add a label and a text widget for Configuration's name
135                 final Label nameLabel = new Label(group1, SWT.LEFT);
136                 nameLabel.setFont(parent.getFont());
137                 nameLabel.setText(Messages.RenameConfiguration_label_name);
138                                 
139                 gd = new GridData(GridData.FILL_HORIZONTAL);
140                 gd.horizontalSpan = 1;
141                 gd.grabExcessHorizontalSpace = false;
142                 nameLabel.setLayoutData(gd);
143
144                 configName = new Text(group1, SWT.SINGLE | SWT.BORDER);
145                 configName.setFont(group1.getFont());
146                 configName.setText(getNewName());
147                 configName.setFocus();
148                 gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
149                 gd.horizontalSpan = 2;
150                 gd.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
151                 configName.setLayoutData(gd);
152                 configName.addModifyListener(new ModifyListener() {
153                         public void modifyText(ModifyEvent e) {
154                                 validateState();
155                         }
156                 });
157                 
158 //               Add a label and a text widget for Configuration's description
159         final Label descriptionLabel = new Label(group1, SWT.LEFT);
160         descriptionLabel.setFont(parent.getFont());
161         descriptionLabel.setText(Messages.RenameConfiguration_label_description);
162
163         gd = new GridData(GridData.FILL_HORIZONTAL);
164         gd.horizontalSpan = 1;
165                 gd.grabExcessHorizontalSpace = false;
166         descriptionLabel.setLayoutData(gd);
167         configDescription = new Text(group1, SWT.SINGLE | SWT.BORDER);
168         configDescription.setFont(group1.getFont());
169                 configDescription.setText(getNewDescription());
170                 configDescription.setFocus();
171                 
172         gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
173         gd.horizontalSpan = 2;
174         gd.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
175         configDescription.setLayoutData(gd);
176         
177         statusLabel = new Label(parent, SWT.CENTER);
178                 gd = new GridData(GridData.FILL_HORIZONTAL);
179                 gd.horizontalSpan = 3;
180                 statusLabel.setLayoutData(gd);
181                 statusLabel.setFont(composite.getFont());
182                 statusLabel.setForeground(JFaceResources.getColorRegistry().get(JFacePreferences.ERROR_COLOR));
183                 return composite;
184         }
185
186         protected boolean isDuplicateName(String newName) {
187                 if(newName.equals(originalName)) return false;
188                 // Return true if there is already a config of that name defined
189                 for (int i = 0; i < cfgds.length; i++) {
190                         if (cfgds[i].getName().equals(newName)) return true;
191                 }
192                 return false;
193         }
194         
195         protected boolean isSimilarName(String newName) {
196                 if(newName.equalsIgnoreCase(originalName))      return false;
197                 // Return true if there is already a config of that name defined on the target
198                 for (int i = 0; i < cfgds.length; i++) {
199                         if (cfgds[i].getName().equalsIgnoreCase(newName)) return true;
200                 }
201                 return false;
202         }
203
204         /* (non-Javadoc)
205          * Checks the argument for leading whitespaces and invalid directory name characters. 
206          * @param name
207          * @return <I>true</i> is the name is a valid directory name with no whitespaces
208          */
209         private boolean validateName(String name) {
210                 // Iterate over the name checking for bad characters
211                 char[] chars = name.toCharArray();
212                 // No whitespaces at the start of a name
213                 if (Character.isWhitespace(chars[0])) {
214                         return false;
215                 }
216                 for (int index = 0; index < chars.length; ++index) {
217                         // Config name must be a valid dir name too, so we ban "\ / : * ? " < >" in the names
218                         if (!Character.isLetterOrDigit(chars[index])) {
219                                 switch (chars[index]) {
220                                 case '/':
221                                 case '\\':
222                                 case ':':
223                                 case '*':
224                                 case '?':
225                                 case '\"':
226                                 case '<':
227                                 case '>':
228                                         return false;
229                                 default:
230                                         break;
231                                 }
232                         }
233                 }
234                 return true;
235         }
236         /* (non-Javadoc)
237          * Update the status message and button state based on the input selected
238          * by the user
239          * 
240          */
241         private void validateState() {
242                 String s = null;
243                 String currentName = configName.getText().trim();
244                 // Make sure that the name is at least one character in length
245                 if (currentName.length() == 0) {
246                         s = ""; //$NON-NLS-1$
247                         // Make sure the name is not a duplicate
248                 } else if (isDuplicateName(currentName)) {
249                         s = NLS.bind(Messages.RenameConfiguration_error_duplicateName, currentName);
250                 } else if (isSimilarName(currentName)) {
251                         s = NLS.bind(Messages.RenameConfiguration_error_caseName, currentName);
252                 } else if (!validateName(currentName)) {
253                         s = NLS.bind(Messages.RenameConfiguration_error_invalidName, currentName);      
254                 }
255                 Button b = getButton(IDialogConstants.OK_ID);
256                 if (s != null) {
257                         statusLabel.setText(s);
258                         statusLabel.setVisible(true);
259                         if (b != null) b.setEnabled(false);
260                 } else {
261                         statusLabel.setVisible(false);
262                         if (b != null) b.setEnabled(true);
263                 }
264                 return;
265         }
266         
267         public String getNewName() { return newName; }
268         public String getNewDescription() { return newDescription; }
269 }