1 package org.eclipse.emf.examples.jet.article2.ui; 2 3 4 import org.eclipse.jface.dialogs.Dialog; 5 import org.eclipse.jface.dialogs.IDialogConstants; 6 import org.eclipse.swt.SWT; 7 import org.eclipse.swt.events.ModifyEvent; 8 import org.eclipse.swt.events.ModifyListener; 9 import org.eclipse.swt.layout.GridData; 10 import org.eclipse.swt.layout.GridLayout; 11 import org.eclipse.swt.widgets.Button; 12 import org.eclipse.swt.widgets.Composite; 13 import org.eclipse.swt.widgets.Control; 14 import org.eclipse.swt.widgets.Label; 15 import org.eclipse.swt.widgets.Shell; 16 import org.eclipse.swt.widgets.Text; 17 18 import org.eclipse.emf.examples.jet.article2.model.Attribute; 19 20 21 22 28 public class EditAttributeDialog extends Dialog 29 { 30 31 private Button mCheckboxKey = null; 32 33 private Text mTextName = null; 34 35 private Text mTextType = null; 36 37 private Attribute mAttribute = null; 38 39 private String mTitle = "Edit Attribute"; 40 41 46 public EditAttributeDialog(Shell parentShell) 47 { 48 super(parentShell); 49 setShellStyle(getShellStyle() | SWT.RESIZE); 50 } 51 52 55 protected void okPressed() 56 { 57 getAttribute().setKey(mCheckboxKey.getSelection()); 58 getAttribute().setName(mTextName.getText()); 59 getAttribute().setType(mTextType.getText()); 60 super.okPressed(); 61 } 62 63 66 protected void cancelPressed() 67 { 68 super.cancelPressed(); 69 } 70 71 74 protected Control createDialogArea(Composite parent) 75 { 76 Composite result = (Composite)super.createDialogArea(parent); 77 78 Composite panel = new Composite(result, SWT.NONE); 79 panel.setLayout(new GridLayout(2, false)); 80 panel.setLayoutData(new GridData(GridData.FILL_BOTH)); 81 82 Label name = new Label(panel, SWT.NONE); 83 name.setText(WizardMessages.getString("NewEnumWizPageAttr.col.Name")); 84 mTextName = new Text(panel, SWT.SINGLE | SWT.BORDER); 85 mTextName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 86 87 Label type = new Label(panel, SWT.NONE); 88 type.setText(WizardMessages.getString("NewEnumWizPageAttr.col.Type")); 89 mTextType = new Text(panel, SWT.SINGLE | SWT.BORDER); 90 mTextType.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 91 92 Label key = new Label(panel, SWT.NONE); 93 key.setText(WizardMessages.getString("NewEnumWizPageAttr.col.Key")); 94 mCheckboxKey = new Button(panel, SWT.CHECK); 95 96 initWidgetValues(); 97 hookListeners(); 98 99 mTextName.forceFocus(); 100 101 return result; 102 } 103 104 protected Control createContents(Composite parent) 105 { 106 Control result = super.createContents(parent); 107 validateInput(); 108 return result; 109 } 110 111 private void initWidgetValues() 112 { 113 if (getAttribute() == null) 114 { 115 throw new IllegalStateException ("Attribute not set; cannot initialize"); 116 } 117 mCheckboxKey.setSelection(getAttribute().isKey()); 118 mTextName.setText(getAttribute().getName()); 119 mTextType.setText(getAttribute().getType()); 120 } 121 122 private void hookListeners() 123 { 124 125 mTextName.addModifyListener(new ModifyListener() 126 { 127 public void modifyText(ModifyEvent e) 128 { 129 validateInput(); 130 } 131 }); 132 mTextType.addModifyListener(new ModifyListener() 133 { 134 public void modifyText(ModifyEvent e) 135 { 136 validateInput(); 137 } 138 }); 139 } 140 141 private void validateInput() 142 { 143 boolean hasName = mTextName.getText().trim().length() > 0; 144 boolean hasType = mTextType.getText().trim().length() > 0; 145 146 boolean enabled = hasName && hasType; 147 getButton(IDialogConstants.OK_ID).setEnabled(enabled); 148 } 149 150 protected void configureShell(Shell newShell) 151 { 152 super.configureShell(newShell); 153 newShell.setText(getTitle()); 154 } 155 156 161 public String getTitle() 162 { 163 return mTitle; 164 } 165 166 172 public void setTitle(String title) 173 { 174 mTitle = title; 175 } 176 177 182 public Attribute getAttribute() 183 { 184 return mAttribute; 185 } 186 187 193 public void setAttribute(Attribute attribute) 194 { 195 mAttribute = attribute; 196 } 197 } | Popular Tags |