blob: aa0c49e02e6f8cdfb1a23dcb35a11f0eeb3f9d2c [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2006 IBM 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 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.dialogs;
kchong38cbf172006-03-29 03:38:21 +000012
13import java.util.Iterator;
14import java.util.List;
15import org.eclipse.jface.dialogs.Dialog;
16import org.eclipse.jface.dialogs.IDialogConstants;
17import org.eclipse.swt.SWT;
18import org.eclipse.swt.events.ModifyEvent;
19import org.eclipse.swt.events.ModifyListener;
20import org.eclipse.swt.layout.GridData;
21import org.eclipse.swt.layout.GridLayout;
22import org.eclipse.swt.widgets.Button;
23import org.eclipse.swt.widgets.Composite;
24import org.eclipse.swt.widgets.Control;
25import org.eclipse.swt.widgets.Label;
26import org.eclipse.swt.widgets.Shell;
27import org.eclipse.swt.widgets.Text;
kchong44c98732006-08-31 02:01:14 +000028import org.eclipse.ui.PlatformUI;
kchonge4fcacc2006-05-01 21:25:59 +000029import org.eclipse.wst.xsd.ui.internal.editor.Messages;
kchong44c98732006-08-31 02:01:14 +000030import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorCSHelpIds;
kchong38cbf172006-03-29 03:38:21 +000031
32public class NewComponentDialog extends Dialog implements ModifyListener
33{
34 protected Text nameField;
35 protected Button okButton;
36 protected String name;
37 protected String title;
38 protected Label errorMessageLabel;
39 protected List usedNames;
40
41 public NewComponentDialog(Shell parentShell, String title, String defaultName)
42 {
43 super(parentShell);
44 setShellStyle(getShellStyle() | SWT.RESIZE);
45 name = defaultName;
46 this.title = title;
47 }
48
49 public NewComponentDialog(Shell parentShell, String title, String defaultName, List usedNames)
50 {
51 super(parentShell);
52 setShellStyle(getShellStyle() | SWT.RESIZE);
53 name = defaultName;
54 this.title = title;
55 this.usedNames = usedNames;
56 }
57
58 public int createAndOpen()
59 {
60 create();
61 getShell().setText(title);
62 setBlockOnOpen(true);
63 return open();
64 }
65
66 protected Control createContents(Composite parent)
67 {
68 Control control = super.createContents(parent);
69 nameField.forceFocus();
70 nameField.selectAll();
71 updateErrorMessage();
72 return control;
73 }
74
75
76 protected void createButtonsForButtonBar(Composite parent)
77 {
78 okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
79 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
80 }
81
82 protected void createHeaderContent(Composite parent)
83 {
84 }
85
86 protected void createExtendedContent(Composite parent)
87 {
88 }
89
90 protected Control createDialogArea(Composite parent)
91 {
92 Composite dialogArea = (Composite)super.createDialogArea(parent);
93
94 createHeaderContent(dialogArea);
95
96 Composite composite = new Composite(dialogArea, SWT.NONE);
97 GridLayout layout = new GridLayout();
98 layout.numColumns = 2;
99 layout.marginWidth = 0;
100 composite.setLayout(layout);
101
102 GridData gdFill= new GridData();
103 gdFill.horizontalAlignment= GridData.FILL;
104 gdFill.grabExcessHorizontalSpace= true;
105 gdFill.verticalAlignment= GridData.FILL;
106 gdFill.grabExcessVerticalSpace= true;
107 composite.setLayoutData(gdFill);
108
109 Label nameLabel = new Label(composite, SWT.NONE);
kchonge4fcacc2006-05-01 21:25:59 +0000110 nameLabel.setText(Messages.UI_LABEL_NAME);
kchong38cbf172006-03-29 03:38:21 +0000111
112 nameField = new Text(composite, SWT.SINGLE | SWT.BORDER);
113 GridData gd= new GridData();
114 gd.horizontalAlignment= GridData.FILL;
115 gd.grabExcessHorizontalSpace= true;
116 gd.widthHint = 200;
117 nameField.setLayoutData(gd);
118 nameField.setText(name);
119 nameField.addModifyListener(this);
kchong44c98732006-08-31 02:01:14 +0000120 PlatformUI.getWorkbench().getHelpSystem().setHelp(nameField, XSDEditorCSHelpIds.NEWTYPE_NAME);
kchong38cbf172006-03-29 03:38:21 +0000121
122 createExtendedContent(dialogArea);
123
124 // error message
125 errorMessageLabel = new Label(dialogArea, SWT.NONE);
kchong618a1942006-04-13 07:14:23 +0000126 errorMessageLabel.setText("error message goes here");
kchong38cbf172006-03-29 03:38:21 +0000127 GridData gd2 = new GridData();
128 gd2.horizontalAlignment= GridData.FILL;
129 gd2.grabExcessHorizontalSpace= true;
130 gd2.widthHint = 200;
131 errorMessageLabel.setLayoutData(gd2);
132// Color color = new Color(errorMessageLabel.getDisplay(), 200, 0, 0);
133// errorMessageLabel.setForeground(color);
134
135 return dialogArea;
136 }
137
138 public void modifyText(ModifyEvent e)
139 {
140 updateErrorMessage();
141 }
142
143 protected String computeErrorMessage(String name)
144 {
145 if (usedNames == null)
146 return null;
147
148 Iterator iterator = usedNames.iterator();
149 while (iterator.hasNext()) {
150 if (name.equalsIgnoreCase((String) iterator.next())) {
rmah837baf82006-06-12 21:32:46 +0000151 return org.eclipse.wst.xsd.ui.internal.common.util.Messages._UI_ERROR_NAME_ALREADY_USED; //$NON-NLS-1$
kchong38cbf172006-03-29 03:38:21 +0000152 }
153 }
154
155 return null;
156 }
157
158 protected void updateErrorMessage()
159 {
160 String errorMessage = null;
161 String name = nameField.getText().trim();
162 if (name.length() > 0)
163 {
164 errorMessage = computeErrorMessage(name);
165 }
166 else
167 {
kchong618a1942006-04-13 07:14:23 +0000168 errorMessage = ""; //$NON-NLS-1$
kchong38cbf172006-03-29 03:38:21 +0000169 }
kchong618a1942006-04-13 07:14:23 +0000170 errorMessageLabel.setText(errorMessage != null ? errorMessage : ""); //$NON-NLS-1$
rmah837baf82006-06-12 21:32:46 +0000171 okButton.setEnabled(errorMessage == null);
kchong38cbf172006-03-29 03:38:21 +0000172 }
173
174 protected void buttonPressed(int buttonId)
175 {
176 if (buttonId == IDialogConstants.OK_ID)
177 {
178 name = nameField.getText();
179 }
180 super.buttonPressed(buttonId);
181 }
182
183 public String getName()
184 {
185 return name;
186 }
rmah837baf82006-06-12 21:32:46 +0000187
188 public void setUsedNames(List usedNames) {
189 this.usedNames = usedNames;
190 }
191
192 public void setDefaultName(String name) {
193 this.name = name;
194 }
kchong38cbf172006-03-29 03:38:21 +0000195}
196