KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > ComboPart


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 package org.eclipse.pde.internal.ui.parts;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.custom.CCombo;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.widgets.Combo;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.ui.forms.widgets.FormToolkit;
20
21
22 public class ComboPart {
23     
24     protected Control combo;
25
26     public ComboPart() {
27     }
28     
29     public void addSelectionListener(SelectionListener listener) {
30         if (combo instanceof Combo)
31             ((Combo) combo).addSelectionListener(listener);
32         else
33             ((CCombo) combo).addSelectionListener(listener);
34     }
35     
36     public int indexOf(String JavaDoc item) {
37         if (combo instanceof Combo)
38             return ((Combo) combo).indexOf(item);
39         
40         return ((CCombo) combo).indexOf(item);
41     }
42     
43     public void addModifyListener(ModifyListener listener) {
44         if (combo instanceof Combo)
45             ((Combo) combo).addModifyListener(listener);
46         else
47             ((CCombo) combo).addModifyListener(listener);
48     }
49     public void createControl(Composite parent, FormToolkit toolkit, int style) {
50         if (toolkit.getBorderStyle() == SWT.BORDER)
51             combo = new Combo(parent, style | SWT.BORDER);
52         else
53             combo = new CCombo(parent, style | SWT.FLAT);
54         toolkit.adapt(combo, true, false);
55     }
56     public Control getControl() {
57         return combo;
58     }
59     public int getSelectionIndex() {
60         if (combo instanceof Combo)
61             return ((Combo) combo).getSelectionIndex();
62         return ((CCombo) combo).getSelectionIndex();
63     }
64     public void add(String JavaDoc item, int index) {
65         if (combo instanceof Combo)
66             ((Combo) combo).add(item, index);
67         else
68             ((CCombo) combo).add(item, index);
69     }
70     public void add(String JavaDoc item) {
71         if (combo instanceof Combo)
72             ((Combo) combo).add(item);
73         else
74             ((CCombo) combo).add(item);
75     }
76     
77     /**
78      * @param index
79      */

80     public void remove(int index) {
81         // Ensure the index is valid
82
if ((index < 0) ||
83                 (index >= getItemCount())) {
84             return;
85         }
86         // Remove the item from the specified index
87
if (combo instanceof Combo) {
88             ((Combo)combo).remove(index);
89         } else {
90             ((CCombo)combo).remove(index);
91         }
92     }
93     
94     public void select(int index) {
95         if (combo instanceof Combo)
96             ((Combo) combo).select(index);
97         else
98             ((CCombo) combo).select(index);
99     }
100     public String JavaDoc getSelection() {
101         if (combo instanceof Combo)
102             return ((Combo) combo).getText().trim();
103         return ((CCombo) combo).getText().trim();
104     }
105     public void setText(String JavaDoc text) {
106         if (combo instanceof Combo)
107             ((Combo) combo).setText(text);
108         else
109             ((CCombo) combo).setText(text);
110     }
111     public void setItems(String JavaDoc [] items) {
112         if (combo instanceof Combo)
113             ((Combo) combo).setItems(items);
114         else
115             ((CCombo) combo).setItems(items);
116     }
117     public void setEnabled(boolean enabled) {
118         if (combo instanceof Combo)
119             ((Combo) combo).setEnabled(enabled);
120         else
121             ((CCombo) combo).setEnabled(enabled);
122     }
123     public int getItemCount() {
124         if (combo instanceof Combo)
125             return ((Combo) combo).getItemCount();
126         return ((CCombo) combo).getItemCount();
127     }
128 }
129
Popular Tags