alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.ui.wizards;

import java.util.ArrayList;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.dialogs.SelectionStatusDialog;

public class RenameDialog extends SelectionStatusDialog {
	private ArrayList oldNames;
	private String oldName;
	private String newName;
	private Text text;
	private IStatus status;
	private boolean isCaseSensitive;
	private IInputValidator fValidator;

	/**
	 * Create a new rename dialog instance for the given window.
	 * @param shell The parent of the dialog
	 * @param oldName Current name of the item being renamed
	 */
	public RenameDialog(Shell shell, String oldName) {
		super(shell);
		this.isCaseSensitive = false;
		initialize();
		setOldName(oldName);
	}

	/**
	 * Create a new rename dialog instance for the given window.
	 * @param shell The parent of the dialog
	 * @param isCaseSensitive Flags whether dialog will perform case sensitive checks against old names
	 * @param names Set of names which the user should not be allowed to rename to
	 * @param oldName Current name of the item being renamed
	 */
	public RenameDialog(Shell shell, boolean isCaseSensitive, String[] names, String oldName) {
		super(shell);
		this.isCaseSensitive = isCaseSensitive;
		initialize();
		if (names != null) {
			for (int i = 0; i < names.length; i++)
				addOldName(names[i]);
		}
		setOldName(oldName);
	}

	public void initialize() {
		oldNames = new ArrayList();
		setStatusLineAboveButtons(true);
	}

	public void addOldName(String oldName) {
		if (!oldNames.contains(oldName))
			oldNames.add(oldName);

	}

	public void setOldName(String oldName) {
		this.oldName = oldName;
		addOldName(oldName);
		if (text != null)
			text.setText(oldName);
		this.newName = oldName;
	}

	protected Control createDialogArea(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = layout.marginWidth = 9;
		container.setLayout(layout);

		GridData gd = new GridData(GridData.FILL_BOTH);
		container.setLayoutData(gd);

		Label label = new Label(container, SWT.NULL);
		label.setText(PDEUIMessages.RenameDialog_label);

		text = new Text(container, SWT.SINGLE | SWT.BORDER);
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				textChanged(text.getText());
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		applyDialogFont(container);
		return container;
	}

	public int open() {
		text.setText(oldName);
		text.selectAll();
		Button okButton = getButton(IDialogConstants.OK_ID);

		status = new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", //$NON-NLS-1$
				null);
		updateStatus(status);
		okButton.setEnabled(false);
		return super.open();
	}

	private void textChanged(String text) {
		Button okButton = getButton(IDialogConstants.OK_ID);
		if (fValidator != null) {
			String message = fValidator.isValid(text);
			if (message != null) {
				status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), IStatus.ERROR, message, null);
				updateStatus(status);
				okButton.setEnabled(false);
				return;
			}
		}
		for (int i = 0; i < oldNames.size(); i++) {
			if ((isCaseSensitive && text.equals(oldNames.get(i))) || (!isCaseSensitive && text.equalsIgnoreCase(oldNames.get(i).toString()))) {
				status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), IStatus.ERROR, PDEUIMessages.RenameDialog_validationError, null);
				updateStatus(status);
				okButton.setEnabled(false);
				break;
			}
			okButton.setEnabled(true);
			status = new Status(IStatus.OK, PDEPlugin.getPluginId(), IStatus.OK, "", //$NON-NLS-1$
					null);
			updateStatus(status);
		}
	}

	public String getNewName() {
		return newName;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	protected void okPressed() {
		newName = text.getText().trim();
		super.okPressed();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
	 */
	protected void computeResult() {
	}

	public void setTitle(String title) {
		getShell().setText(title);
	}

	public void setInputValidator(IInputValidator validator) {
		fValidator = validator;
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.