KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > examples > jet > article2 > ui > EditAttributeDialog


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 /**
23  * Dialog for editing an <code>Attribute</code>.
24  *
25  * @version $Revision: 1.1 $ ($Date: 2004/05/31 21:35:35 $)
26  * @author Remko Popma
27  */

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 JavaDoc mTitle = "Edit Attribute";
40
41   /**
42    * Constructor for EditAttributeDialog.
43    *
44    * @param parentShell
45    */

46   public EditAttributeDialog(Shell parentShell)
47   {
48     super(parentShell);
49     setShellStyle(getShellStyle() | SWT.RESIZE);
50   }
51
52   /*
53    * @see org.eclipse.jface.dialogs.Dialog#okPressed()
54    */

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   /*
64    * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
65    */

66   protected void cancelPressed()
67   {
68     super.cancelPressed();
69   }
70
71   /**
72    * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
73    */

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 JavaDoc("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   /**
157    * Returns the dialog title.
158    *
159    * @return the dialog title
160    */

161   public String JavaDoc getTitle()
162   {
163     return mTitle;
164   }
165
166   /**
167    * Sets the dialog title.
168    *
169    * @param title
170    * the dialog title
171    */

172   public void setTitle(String JavaDoc title)
173   {
174     mTitle = title;
175   }
176
177   /**
178    * Returns the <code>Attribute</code> edited in this dialog.
179    *
180    * @return the <code>Attribute</code>
181    */

182   public Attribute getAttribute()
183   {
184     return mAttribute;
185   }
186
187   /**
188    * Sets the <code>Attribute</code> to edit in this dialog.
189    *
190    * @param attribute
191    * the <code>Attribute</code> to edit
192    */

193   public void setAttribute(Attribute attribute)
194   {
195     mAttribute = attribute;
196   }
197 }
Popular Tags