Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f924cf20a84c33a95fddbf83bc31443bb640120b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.dialogs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.dialogs.IMessageProvider;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.te.ui.jface.dialogs.CustomTitleAreaDialog;

/**
 * Rename dialog implementation.
 */
public class RenameDialog extends CustomTitleAreaDialog {

	private String dialogTitle;
	private String title;
	/* default */ String defaultMessage;
	/* default */ String usedErrorMessage;
	/* default */ String formatErrorMessage;
	private String label;
	/* default */ String formatRegex;
	private String oldName;
	private String newName;
	/* default */ List<String> usedNames;

	/* default */ Text name;

	/**
	 * Constructor.
	 *
	 * @param parent The parent shell.
	 * @param title The title for the dialog.
	 * @param defaultMessage The default info message for the dialog.
	 * @param usedErrorMessage The error message if the name is already in use.
	 * @param formatErrorMessage The error message if the format of the name is illegal.
	 * @param label The label for the entry field.
	 * @param oldName The original name.
	 * @param formatRegex The format regular expression.
	 * @param usedNames The list of used or reserved names.
	 * @param contextHelpId The context help id.
	 */
	public RenameDialog(Shell parent, String dialogTitle, String title, String defaultMessage, String usedErrorMessage, String formatErrorMessage,
					String label, String oldName, String formatRegex, String[] usedNames, String contextHelpId) {
		super(parent, contextHelpId);

		this.dialogTitle = dialogTitle != null ? dialogTitle : "Rename"; //$NON-NLS-1$
		this.title = title != null ? title : ""; //$NON-NLS-1$
		this.defaultMessage = defaultMessage != null ? defaultMessage : ""; //$NON-NLS-1$
		this.usedErrorMessage = usedErrorMessage != null ? usedErrorMessage : ""; //$NON-NLS-1$
		this.formatErrorMessage = formatErrorMessage != null ? formatErrorMessage : ""; //$NON-NLS-1$
		this.formatRegex = formatRegex != null ? formatRegex : ".*"; //$NON-NLS-1$
		this.label = label != null ? label : "Name:"; //$NON-NLS-1$
		this.oldName = oldName != null ? oldName : ""; //$NON-NLS-1$
		this.usedNames = usedNames != null ? Arrays.asList(usedNames) : new ArrayList<String>();
		this.newName = this.oldName;
	}

	/*(non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.dialogs.CustomTitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		setDialogTitle(dialogTitle);
		setTitle(title);
		setDefaultMessage(defaultMessage, IMessageProvider.INFORMATION);

		//set margins of dialog and apply dialog font
		Composite container = (Composite) super.createDialogArea(parent);
		//we need two columns
		Composite comp = new Composite(container, SWT.NONE);
		comp.setLayout(new GridLayout(2, false));
		comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		new Label(comp, SWT.NONE).setText(label);
		name = new Text(comp, SWT.BORDER | SWT.SINGLE);
		name.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		name.setText(oldName);
		name.addModifyListener(new ModifyListener(){
			@Override
			public void modifyText(ModifyEvent e) {
				if (usedNames.contains(name.getText().trim().toUpperCase())) {
					setButtonEnabled(OK, false);
					setMessage(usedErrorMessage, IMessageProvider.ERROR);
				}
				else if (!name.getText().matches(formatRegex)) {
					setButtonEnabled(OK, false);
					setMessage(formatErrorMessage, IMessageProvider.ERROR);
				}
				else {
					setButtonEnabled(OK, true);
					setMessage(defaultMessage, IMessageProvider.INFORMATION);
				}
			}
		});

		applyDialogFont(container);

		return container;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.TrayDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createButtonBar(Composite parent) {
		Control control =  super.createButtonBar(parent);
		setButtonEnabled(OK, false);
		return control;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.dialogs.CustomTitleAreaDialog#okPressed()
	 */
	@Override
	protected void okPressed() {
		newName = name.getText();
		super.okPressed();
	}

	/**
	 * Return the new name after OK was pressed.
	 * Unless OK was pressed, the old name is returned.
	 */
	public String getNewName() {
		return newName;
	}
}

Back to the top