kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.dialogs; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
| 13 | import java.util.Iterator; |
| 14 | import java.util.List; |
| 15 | import org.eclipse.jface.dialogs.Dialog; |
| 16 | import org.eclipse.jface.dialogs.IDialogConstants; |
| 17 | import org.eclipse.swt.SWT; |
| 18 | import org.eclipse.swt.events.ModifyEvent; |
| 19 | import org.eclipse.swt.events.ModifyListener; |
| 20 | import org.eclipse.swt.layout.GridData; |
| 21 | import org.eclipse.swt.layout.GridLayout; |
| 22 | import org.eclipse.swt.widgets.Button; |
| 23 | import org.eclipse.swt.widgets.Composite; |
| 24 | import org.eclipse.swt.widgets.Control; |
| 25 | import org.eclipse.swt.widgets.Label; |
| 26 | import org.eclipse.swt.widgets.Shell; |
| 27 | import org.eclipse.swt.widgets.Text; |
kchong | 44c9873 | 2006-08-31 02:01:14 +0000 | [diff] [blame] | 28 | import org.eclipse.ui.PlatformUI; |
kchong | e4fcacc | 2006-05-01 21:25:59 +0000 | [diff] [blame] | 29 | import org.eclipse.wst.xsd.ui.internal.editor.Messages; |
kchong | 44c9873 | 2006-08-31 02:01:14 +0000 | [diff] [blame] | 30 | import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorCSHelpIds; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 31 | |
| 32 | public 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); |
kchong | e4fcacc | 2006-05-01 21:25:59 +0000 | [diff] [blame] | 110 | nameLabel.setText(Messages.UI_LABEL_NAME); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 111 | |
| 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); |
kchong | 44c9873 | 2006-08-31 02:01:14 +0000 | [diff] [blame] | 120 | PlatformUI.getWorkbench().getHelpSystem().setHelp(nameField, XSDEditorCSHelpIds.NEWTYPE_NAME); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 121 | |
| 122 | createExtendedContent(dialogArea); |
| 123 | |
| 124 | // error message |
| 125 | errorMessageLabel = new Label(dialogArea, SWT.NONE); |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 126 | errorMessageLabel.setText("error message goes here"); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 127 | 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())) { |
rmah | 837baf8 | 2006-06-12 21:32:46 +0000 | [diff] [blame] | 151 | return org.eclipse.wst.xsd.ui.internal.common.util.Messages._UI_ERROR_NAME_ALREADY_USED; //$NON-NLS-1$ |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 152 | } |
| 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 | { |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 168 | errorMessage = ""; //$NON-NLS-1$ |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 169 | } |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 170 | errorMessageLabel.setText(errorMessage != null ? errorMessage : ""); //$NON-NLS-1$ |
rmah | 837baf8 | 2006-06-12 21:32:46 +0000 | [diff] [blame] | 171 | okButton.setEnabled(errorMessage == null); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 172 | } |
| 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 | } |
rmah | 837baf8 | 2006-06-12 21:32:46 +0000 | [diff] [blame] | 187 | |
| 188 | public void setUsedNames(List usedNames) { |
| 189 | this.usedNames = usedNames; |
| 190 | } |
| 191 | |
| 192 | public void setDefaultName(String name) { |
| 193 | this.name = name; |
| 194 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |