KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > rows > ChoiceAttributeRow


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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  *******************************************************************************/

11 /*
12  * Created on Jan 30, 2004
13  *
14  * To change the template for this generated file go to
15  * Window - Preferences - Java - Code Generation - Code and Comments
16  */

17 package org.eclipse.pde.internal.ui.editor.plugin.rows;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
21 import org.eclipse.pde.internal.core.ischema.ISchemaEnumeration;
22 import org.eclipse.pde.internal.core.ischema.ISchemaRestriction;
23 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
24 import org.eclipse.pde.internal.ui.PDEPlugin;
25 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
26 import org.eclipse.pde.internal.ui.editor.IContextPart;
27 import org.eclipse.pde.internal.ui.parts.ComboPart;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.ui.forms.widgets.FormToolkit;
34
35
36 public class ChoiceAttributeRow extends ExtensionAttributeRow {
37     protected ComboPart combo;
38     /**
39      * @param att
40      */

41     public ChoiceAttributeRow(IContextPart part, ISchemaAttribute att) {
42         super(part, att);
43     }
44
45     public void createContents(Composite parent, FormToolkit toolkit, int span) {
46         super.createContents(parent, toolkit, span);
47         createLabel(parent, toolkit);
48         combo = new ComboPart();
49         combo.createControl(parent, toolkit, SWT.READ_ONLY);
50         ISchemaSimpleType type = getAttribute().getType();
51         ISchemaRestriction restriction = type.getRestriction();
52         if (restriction!=null) {
53             Object JavaDoc rchildren[] = restriction.getChildren();
54             if (getUse()!=ISchemaAttribute.REQUIRED)
55                 combo.add(""); //$NON-NLS-1$
56
for (int i=0; i<rchildren.length; i++) {
57                 Object JavaDoc rchild = rchildren[i];
58                 if (rchild instanceof ISchemaEnumeration)
59                     combo.add(((ISchemaEnumeration)rchild).getName());
60             }
61         }
62         GridData gd = new GridData(span==2?GridData.FILL_HORIZONTAL:GridData.HORIZONTAL_ALIGN_FILL);
63         gd.widthHint = 20;
64         gd.horizontalSpan = span-1;
65         gd.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
66         combo.getControl().setLayoutData(gd);
67         combo.addSelectionListener(new SelectionAdapter() {
68             public void widgetSelected(SelectionEvent e) {
69                 if (!blockNotification) markDirty();
70             }
71         });
72         combo.getControl().setEnabled(part.isEditable());
73     }
74     /* (non-Javadoc)
75      * @see org.eclipse.pde.internal.ui.neweditor.plugin.ExtensionElementEditor#update(org.eclipse.pde.internal.ui.neweditor.plugin.DummyExtensionElement)
76      */

77     protected void update() {
78         blockNotification=true;
79         String JavaDoc value = getValue();
80         if (value!= null && isValid(value))
81             combo.setText(value);
82         else if (getUse()==ISchemaAttribute.REQUIRED)
83             combo.setText(getValidValue());
84         else
85             combo.setText(""); //$NON-NLS-1$
86
blockNotification = false;
87         dirty=false;
88     }
89     
90     protected String JavaDoc getValidValue(){
91         ISchemaAttribute attInfo = getAttribute();
92         if (attInfo.getType().getRestriction()!= null)
93             return attInfo.getType().getRestriction().getChildren()[0].toString();
94         return ""; //$NON-NLS-1$
95
}
96     
97     protected boolean isValid(String JavaDoc value){
98         if (getAttribute().getUse() != ISchemaAttribute.REQUIRED && value.equals("")) //$NON-NLS-1$
99
return true;
100         
101         ISchemaRestriction restriction = getAttribute().getType().getRestriction();
102         if (restriction == null)
103             return true;
104         Object JavaDoc[] children = restriction.getChildren();
105         for (int i =0; i<children.length; i++){
106             Object JavaDoc rchild = children[i];
107             if (rchild instanceof ISchemaEnumeration && ((ISchemaEnumeration)rchild).getName().equals(value))
108                 return true;
109         }
110         return false;
111     }
112     public void commit() {
113         if (dirty && input != null) {
114             try {
115                 String JavaDoc selection = combo.getSelection();
116                 if (selection.length()==0) selection = null;
117                 input.setAttribute(getName(), selection);
118                 dirty = false;
119             } catch (CoreException e) {
120                 PDEPlugin.logException(e);
121             }
122         }
123     }
124     public void setFocus() {
125         combo.getControl().setFocus();
126     }
127 }
128
Popular Tags