KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > EnumerationRestrictionPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 package org.eclipse.pde.internal.ui.editor.schema;
12
13 import java.util.Vector JavaDoc;
14 import org.eclipse.pde.internal.core.schema.*;
15 import org.eclipse.swt.events.*;
16 import org.eclipse.swt.layout.*;
17 import org.eclipse.swt.widgets.*;
18 import org.eclipse.swt.*;
19 import org.eclipse.pde.internal.core.ischema.*;
20 import org.eclipse.pde.internal.ui.*;
21 import org.eclipse.pde.internal.ui.util.SWTUtil;
22
23 public class EnumerationRestrictionPage implements IRestrictionPage {
24     private List choiceList;
25     private Button addButton;
26     private Button deleteButton;
27     private Text text;
28     private Control control;
29
30     public Control createControl(Composite parent) {
31         Composite container = new Composite(parent, SWT.NULL);
32         container.setLayout(new GridLayout());
33
34         Composite top = new Composite(container, SWT.NULL);
35         GridLayout layout = new GridLayout();
36         layout.marginHeight = 0;
37         layout.marginWidth = 0;
38         layout.numColumns = 2;
39         top.setLayout(layout);
40         top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
41         
42         
43         Label label = new Label(top, SWT.NULL);
44         label.setText(PDEUIMessages.RestrictionDialog_newChoice);
45         GridData gd = new GridData();
46         gd.horizontalSpan = 2;
47         label.setLayoutData(gd);
48         
49         text = new Text(top, SWT.SINGLE | SWT.BORDER);
50         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
51
52         addButton = new Button(top, SWT.PUSH);
53         addButton.setText(PDEUIMessages.RestrictionDialog_add);
54         addButton.setEnabled(false);
55         addButton.addSelectionListener(new SelectionAdapter() {
56             public void widgetSelected(SelectionEvent e) {
57                 handleAdd();
58             }
59         });
60         addButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
61         SWTUtil.setButtonDimensionHint(addButton);
62         
63
64         Composite bottom = new Composite(container, SWT.NULL);
65         bottom.setLayoutData(new GridData(GridData.FILL_BOTH));
66         layout = new GridLayout();
67         layout.marginHeight = 0;
68         layout.marginWidth = 0;
69         layout.numColumns = 2;
70         bottom.setLayout(layout);
71
72         label = new Label(bottom, SWT.NULL);
73         gd = new GridData();
74         gd.horizontalSpan = 2;
75         label.setLayoutData(gd);
76         label.setText(PDEUIMessages.RestrictionDialog_choices);
77         
78         choiceList = new List(bottom, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
79         choiceList.setLayoutData(new GridData(GridData.FILL_BOTH));
80
81         deleteButton = new Button(bottom, SWT.PUSH);
82         deleteButton.setText(PDEUIMessages.RestrictionDialog_remove);
83         deleteButton.setEnabled(false);
84         deleteButton.addSelectionListener(new SelectionAdapter() {
85             public void widgetSelected(SelectionEvent e) {
86                 handleDelete();
87             }
88         });
89         deleteButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
90         SWTUtil.setButtonDimensionHint(deleteButton);
91
92         text.addModifyListener(new ModifyListener() {
93             public void modifyText(ModifyEvent e) {
94                 String JavaDoc item = text.getText();
95                 boolean canAdd = true;
96                 if (item.length() == 0 || choiceList.indexOf(item) != -1)
97                     canAdd = false;
98                 addButton.setEnabled(canAdd);
99             }
100         });
101         text.addListener(SWT.Traverse, new Listener() {
102             public void handleEvent(Event e) {
103                 handleAdd();
104                 e.doit = false;
105             }
106         });
107
108         choiceList.addSelectionListener(new SelectionAdapter() {
109             public void widgetSelected(SelectionEvent e) {
110                 deleteButton.setEnabled(choiceList.getSelectionCount() > 0);
111                 if (choiceList.getSelectionCount() == 1) {
112                     text.setText(choiceList.getSelection()[0]);
113                 }
114             }
115         });
116         this.control = container;
117         return container;
118     }
119     public Class JavaDoc getCompatibleRestrictionClass() {
120         return ChoiceRestriction.class;
121     }
122     public org.eclipse.swt.widgets.Control getControl() {
123         return control;
124     }
125     public ISchemaRestriction getRestriction() {
126         ChoiceRestriction restriction = new ChoiceRestriction((ISchema) null);
127         String JavaDoc[] items = choiceList.getItems();
128         if (items.length > 0) {
129             Vector JavaDoc enums = new Vector JavaDoc();
130             for (int i = 0; i < items.length; i++) {
131                 enums.addElement(new SchemaEnumeration(restriction, items[i]));
132             }
133             restriction.setChildren(enums);
134         }
135         return restriction;
136     }
137     private void handleAdd() {
138         String JavaDoc item = text.getText().trim();
139         if (item.length()==0) return;
140         choiceList.add(item);
141         choiceList.setSelection(new String JavaDoc[] { item });
142         text.setText(""); //$NON-NLS-1$
143
deleteButton.setEnabled(true);
144     }
145
146     private void handleDelete() {
147         String JavaDoc[] selection = choiceList.getSelection();
148         choiceList.setRedraw(false);
149         for (int i = 0; i < selection.length; i++) {
150             choiceList.remove(selection[i]);
151         }
152         choiceList.setRedraw(true);
153         deleteButton.setEnabled(false);
154     }
155     
156     public void initialize(ISchemaRestriction restriction) {
157         if (restriction != null) {
158             Object JavaDoc[] children = restriction.getChildren();
159             for (int i = 0; i < children.length; i++) {
160                 Object JavaDoc child = children[i];
161                 if (child instanceof ISchemaEnumeration) {
162                     choiceList.add(child.toString());
163                 }
164             }
165         }
166     }
167 }
168
Popular Tags