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
9 * Intel Corporation - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.cdt.ui.newui;
14 import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
16 import org.eclipse.cdt.internal.ui.newui.Messages;
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;
37 * @noextend This class is not intended to be subclassed by clients.
39 public class RenameConfigurationDialog extends Dialog {
42 private Text configName;
43 private Text configDescription;
45 private ICConfigurationDescription[] cfgds;
46 private ICConfigurationDescription renameConfig;
47 private String newName;
48 private String newDescription;
49 private Label statusLabel;
51 private String originalName;
52 final private String title;
55 protected RenameConfigurationDialog(Shell parentShell,
56 ICConfigurationDescription _renameConfig,
57 ICConfigurationDescription[] _cfgds,
61 renameConfig = _renameConfig;
64 setShellStyle(getShellStyle()|SWT.RESIZE);
65 newName = renameConfig.getName();
66 newDescription = renameConfig.getDescription();
67 if(newDescription == null) newDescription = new String();
68 originalName = renameConfig.getName();
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.
77 protected void buttonPressed(int buttonId) {
78 if (buttonId == IDialogConstants.OK_ID) {
79 newName = configName.getText().trim();
80 newDescription = configDescription.getText().trim();
82 super.buttonPressed(buttonId);
86 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
89 protected void configureShell(Shell shell) {
90 super.configureShell(shell);
91 if (title != null) shell.setText(title);
95 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
98 protected void createButtonsForButtonBar(Composite parent) {
99 super.createButtonsForButtonBar(parent);
100 configName.setFocus();
101 if (configName != null) {
102 configName.setText(newName);
108 protected Control createDialogArea(Composite parent) {
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));
115 // Create a group for the name & description
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);
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);
132 warningLabel.setLayoutData(gd);
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);
139 gd = new GridData(GridData.FILL_HORIZONTAL);
140 gd.horizontalSpan = 1;
141 gd.grabExcessHorizontalSpace = false;
142 nameLabel.setLayoutData(gd);
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) {
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);
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();
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);
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));
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;
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;
205 * Checks the argument for leading whitespaces and invalid directory name characters.
207 * @return <I>true</i> is the name is a valid directory name with no whitespaces
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])) {
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]) {
237 * Update the status message and button state based on the input selected
241 private void validateState() {
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);
255 Button b = getButton(IDialogConstants.OK_ID);
257 statusLabel.setText(s);
258 statusLabel.setVisible(true);
259 if (b != null) b.setEnabled(false);
261 statusLabel.setVisible(false);
262 if (b != null) b.setEnabled(true);
267 public String getNewName() { return newName; }
268 public String getNewDescription() { return newDescription; }