001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.jface.dialogs.IDialogConstants;
017: import org.eclipse.jface.dialogs.IInputValidator;
018: import org.eclipse.pde.internal.ui.PDEPlugin;
019: import org.eclipse.pde.internal.ui.PDEUIMessages;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.ModifyEvent;
022: import org.eclipse.swt.events.ModifyListener;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Button;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.swt.widgets.Text;
031: import org.eclipse.ui.dialogs.SelectionStatusDialog;
032:
033: public class RenameDialog extends SelectionStatusDialog {
034: private ArrayList oldNames;
035: private String oldName;
036: private String newName;
037: private Text text;
038: private IStatus status;
039: private boolean isCaseSensitive;
040: private IInputValidator fValidator;
041:
042: /**
043: * Create a new rename dialog instance for the given window.
044: * @param shell The parent of the dialog
045: * @param oldName Current name of the item being renamed
046: */
047: public RenameDialog(Shell shell, String oldName) {
048: super (shell);
049: this .isCaseSensitive = false;
050: initialize();
051: setOldName(oldName);
052: }
053:
054: /**
055: * Create a new rename dialog instance for the given window.
056: * @param shell The parent of the dialog
057: * @param isCaseSensitive Flags whether dialog will perform case sensitive checks against old names
058: * @param names Set of names which the user should not be allowed to rename to
059: * @param oldName Current name of the item being renamed
060: */
061: public RenameDialog(Shell shell, boolean isCaseSensitive,
062: String[] names, String oldName) {
063: super (shell);
064: this .isCaseSensitive = isCaseSensitive;
065: initialize();
066: if (names != null) {
067: for (int i = 0; i < names.length; i++)
068: addOldName(names[i]);
069: }
070: setOldName(oldName);
071: }
072:
073: public void initialize() {
074: oldNames = new ArrayList();
075: setStatusLineAboveButtons(true);
076: }
077:
078: public void addOldName(String oldName) {
079: if (!oldNames.contains(oldName))
080: oldNames.add(oldName);
081:
082: }
083:
084: public void setOldName(String oldName) {
085: this .oldName = oldName;
086: addOldName(oldName);
087: if (text != null)
088: text.setText(oldName);
089: this .newName = oldName;
090: }
091:
092: protected Control createDialogArea(Composite parent) {
093: Composite container = new Composite(parent, SWT.NULL);
094: GridLayout layout = new GridLayout();
095: layout.numColumns = 2;
096: layout.marginHeight = layout.marginWidth = 9;
097: container.setLayout(layout);
098:
099: GridData gd = new GridData(GridData.FILL_BOTH);
100: container.setLayoutData(gd);
101:
102: Label label = new Label(container, SWT.NULL);
103: label.setText(PDEUIMessages.RenameDialog_label);
104:
105: text = new Text(container, SWT.SINGLE | SWT.BORDER);
106: text.addModifyListener(new ModifyListener() {
107: public void modifyText(ModifyEvent e) {
108: textChanged(text.getText());
109: }
110: });
111: gd = new GridData(GridData.FILL_HORIZONTAL);
112: text.setLayoutData(gd);
113: applyDialogFont(container);
114: return container;
115: }
116:
117: public int open() {
118: text.setText(oldName);
119: text.selectAll();
120: Button okButton = getButton(IDialogConstants.OK_ID);
121:
122: status = new Status(IStatus.OK, PDEPlugin.getPluginId(),
123: IStatus.OK, "", //$NON-NLS-1$
124: null);
125: updateStatus(status);
126: okButton.setEnabled(false);
127: return super .open();
128: }
129:
130: private void textChanged(String text) {
131: Button okButton = getButton(IDialogConstants.OK_ID);
132: if (fValidator != null) {
133: String message = fValidator.isValid(text);
134: if (message != null) {
135: status = new Status(IStatus.ERROR, PDEPlugin
136: .getPluginId(), IStatus.ERROR, message, null);
137: updateStatus(status);
138: okButton.setEnabled(false);
139: return;
140: }
141: }
142: for (int i = 0; i < oldNames.size(); i++) {
143: if ((isCaseSensitive && text.equals(oldNames.get(i)))
144: || (!isCaseSensitive && text
145: .equalsIgnoreCase(oldNames.get(i)
146: .toString()))) {
147: status = new Status(IStatus.ERROR, PDEPlugin
148: .getPluginId(), IStatus.ERROR,
149: PDEUIMessages.RenameDialog_validationError,
150: null);
151: updateStatus(status);
152: okButton.setEnabled(false);
153: break;
154: }
155: okButton.setEnabled(true);
156: status = new Status(IStatus.OK, PDEPlugin.getPluginId(),
157: IStatus.OK, "", //$NON-NLS-1$
158: null);
159: updateStatus(status);
160: }
161: }
162:
163: public String getNewName() {
164: return newName;
165: }
166:
167: /* (non-Javadoc)
168: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
169: */
170: protected void okPressed() {
171: newName = text.getText().trim();
172: super .okPressed();
173: }
174:
175: /* (non-Javadoc)
176: * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
177: */
178: protected void computeResult() {
179: }
180:
181: public void setTitle(String title) {
182: getShell().setText(title);
183: }
184:
185: public void setInputValidator(IInputValidator validator) {
186: fValidator = validator;
187: }
188: }
|