KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.emf.examples.jet.article2.model.Instance;
20
21
22
23 /**
24  * Dialog for editing an <code>Instance</code>.
25  *
26  * @version $Revision: 1.1 $ ($Date: 2004/05/31 21:35:35 $)
27  * @author Remko Popma
28  */

29 public class EditInstanceDialog extends Dialog
30 {
31
32   private Button mCheckboxDefault = null;
33
34   private Text mTextName = null;
35
36   private Text[] mTextAttributeValues = null;
37
38   private Instance mInstance = null;
39
40   private Attribute[] mAttributes = new Attribute [0];
41
42   private String JavaDoc mTitle = "Edit Instance";
43
44   /**
45    * Constructor for EditInstanceDialog.
46    *
47    * @param parentShell
48    */

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

58   protected void okPressed()
59   {
60     if (mCheckboxDefault.getSelection())
61     {
62       getInstance().setDefault();
63     }
64     else
65     {
66       if (getInstance().getType().getDefaultInstance() == getInstance())
67       {
68         getInstance().getType().setDefaultInstance(null);
69       }
70     }
71     getInstance().setName(mTextName.getText());
72     for (int i = 0; i < mAttributes.length; i++)
73     {
74       String JavaDoc name = mAttributes[i].getName();
75       String JavaDoc value = mTextAttributeValues[i].getText();
76       getInstance().getValues().put(name, value);
77     }
78     super.okPressed();
79   }
80
81   /*
82    * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
83    */

84   protected void cancelPressed()
85   {
86     super.cancelPressed();
87   }
88
89   /**
90    * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
91    */

92   protected Control createDialogArea(Composite parent)
93   {
94     Composite result = (Composite)super.createDialogArea(parent);
95
96     Composite panel = new Composite(result, SWT.NONE);
97     panel.setLayout(new GridLayout(2, false));
98     panel.setLayoutData(new GridData(GridData.FILL_BOTH));
99
100     Label name = new Label(panel, SWT.NONE);
101     name.setText(WizardMessages.getString("NewEnumWizPageInst.col.Name"));
102     mTextName = new Text(panel, SWT.SINGLE | SWT.BORDER);
103     mTextName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
104
105     mTextAttributeValues = new Text [mAttributes.length];
106     for (int i = 0; i < mAttributes.length; i++)
107     {
108       Label label = new Label(panel, SWT.NONE);
109       String JavaDoc[] attr = new String JavaDoc []{ mAttributes[i].getType(), mAttributes[i].getName(), };
110       label.setText(WizardMessages.getFormattedString("EditInstanceDialog.attribute.Value", attr));
111       mTextAttributeValues[i] = new Text(panel, SWT.SINGLE | SWT.BORDER);
112       mTextAttributeValues[i].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
113     }
114
115     Label defaultInstance = new Label(panel, SWT.NONE);
116     defaultInstance.setText(WizardMessages.getString("NewEnumWizPageInst.col.Default"));
117     mCheckboxDefault = new Button(panel, SWT.CHECK);
118
119     initWidgetValues();
120     hookListeners();
121
122     mTextName.forceFocus();
123
124     return result;
125   }
126
127   protected Control createContents(Composite parent)
128   {
129     Control result = super.createContents(parent);
130     validateInput();
131     return result;
132   }
133
134   private void initWidgetValues()
135   {
136     if (getInstance() == null)
137     {
138       throw new IllegalStateException JavaDoc("Instance not set; cannot initialize");
139     }
140     mCheckboxDefault.setSelection(getInstance().isDefault());
141     mTextName.setText(getInstance().getName());
142
143     for (int i = 0; i < mTextAttributeValues.length; i++)
144     {
145       String JavaDoc value = getInstance().getAttributeValue(getAttributes()[i]);
146       value = (value == null) ? "" : value;
147       mTextAttributeValues[i].setText(value);
148     }
149   }
150
151   private void hookListeners()
152   {
153
154     mTextName.addModifyListener(new ModifyListener()
155       {
156         public void modifyText(ModifyEvent e)
157         {
158           validateInput();
159         }
160       });
161
162     for (int i = 0; i < mTextAttributeValues.length; i++)
163     {
164       mTextAttributeValues[i].addModifyListener(new ModifyListener()
165         {
166           public void modifyText(ModifyEvent e)
167           {
168             validateInput();
169           }
170         });
171     }
172   }
173
174   private void validateInput()
175   {
176     boolean hasName = mTextName.getText().trim().length() > 0;
177     boolean hasAllValues = true;
178     for (int i = 0; i < mTextAttributeValues.length; i++)
179     {
180       boolean hasValue = mTextAttributeValues[i].getText().trim().length() > 0;
181       hasAllValues &= hasValue;
182     }
183
184     boolean enabled = hasName && hasAllValues;
185     getButton(IDialogConstants.OK_ID).setEnabled(enabled);
186   }
187
188   protected void configureShell(Shell newShell)
189   {
190     super.configureShell(newShell);
191     newShell.setText(getTitle());
192   }
193
194   /**
195    * Returns the dialog title.
196    *
197    * @return the dialog title
198    */

199   public String JavaDoc getTitle()
200   {
201     return mTitle;
202   }
203
204   /**
205    * Sets the dialog title.
206    *
207    * @param title
208    * the dialog title
209    */

210   public void setTitle(String JavaDoc title)
211   {
212     mTitle = title;
213   }
214
215   /**
216    * Returns the <code>Instance</code> edited in this dialog.
217    *
218    * @return the <code>Instance</code>
219    */

220   public Instance getInstance()
221   {
222     return mInstance;
223   }
224
225   /**
226    * Sets the <code>Instance</code> to edit in this dialog.
227    *
228    * @param instance
229    * the <code>Instance</code> to edit
230    */

231   public void setInstance(Instance instance)
232   {
233     mInstance = instance;
234   }
235
236   /**
237    * @return
238    */

239   public Attribute[] getAttributes()
240   {
241     return mAttributes;
242   }
243
244   /**
245    * @param attributes
246    */

247   public void setAttributes(Attribute[] attributes)
248   {
249     if (attributes == null)
250     {
251       attributes = new Attribute [0];
252     }
253     mAttributes = attributes;
254   }
255
256 }
Popular Tags