001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.parts;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.custom.CCombo;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.events.SelectionListener;
016: import org.eclipse.swt.widgets.Combo;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.ui.forms.widgets.FormToolkit;
020:
021: public class ComboPart {
022:
023: protected Control combo;
024:
025: public ComboPart() {
026: }
027:
028: public void addSelectionListener(SelectionListener listener) {
029: if (combo instanceof Combo)
030: ((Combo) combo).addSelectionListener(listener);
031: else
032: ((CCombo) combo).addSelectionListener(listener);
033: }
034:
035: public int indexOf(String item) {
036: if (combo instanceof Combo)
037: return ((Combo) combo).indexOf(item);
038:
039: return ((CCombo) combo).indexOf(item);
040: }
041:
042: public void addModifyListener(ModifyListener listener) {
043: if (combo instanceof Combo)
044: ((Combo) combo).addModifyListener(listener);
045: else
046: ((CCombo) combo).addModifyListener(listener);
047: }
048:
049: public void createControl(Composite parent, FormToolkit toolkit,
050: int style) {
051: if (toolkit.getBorderStyle() == SWT.BORDER)
052: combo = new Combo(parent, style | SWT.BORDER);
053: else
054: combo = new CCombo(parent, style | SWT.FLAT);
055: toolkit.adapt(combo, true, false);
056: }
057:
058: public Control getControl() {
059: return combo;
060: }
061:
062: public int getSelectionIndex() {
063: if (combo instanceof Combo)
064: return ((Combo) combo).getSelectionIndex();
065: return ((CCombo) combo).getSelectionIndex();
066: }
067:
068: public void add(String item, int index) {
069: if (combo instanceof Combo)
070: ((Combo) combo).add(item, index);
071: else
072: ((CCombo) combo).add(item, index);
073: }
074:
075: public void add(String item) {
076: if (combo instanceof Combo)
077: ((Combo) combo).add(item);
078: else
079: ((CCombo) combo).add(item);
080: }
081:
082: /**
083: * @param index
084: */
085: public void remove(int index) {
086: // Ensure the index is valid
087: if ((index < 0) || (index >= getItemCount())) {
088: return;
089: }
090: // Remove the item from the specified index
091: if (combo instanceof Combo) {
092: ((Combo) combo).remove(index);
093: } else {
094: ((CCombo) combo).remove(index);
095: }
096: }
097:
098: public void select(int index) {
099: if (combo instanceof Combo)
100: ((Combo) combo).select(index);
101: else
102: ((CCombo) combo).select(index);
103: }
104:
105: public String getSelection() {
106: if (combo instanceof Combo)
107: return ((Combo) combo).getText().trim();
108: return ((CCombo) combo).getText().trim();
109: }
110:
111: public void setText(String text) {
112: if (combo instanceof Combo)
113: ((Combo) combo).setText(text);
114: else
115: ((CCombo) combo).setText(text);
116: }
117:
118: public void setItems(String[] items) {
119: if (combo instanceof Combo)
120: ((Combo) combo).setItems(items);
121: else
122: ((CCombo) combo).setItems(items);
123: }
124:
125: public void setEnabled(boolean enabled) {
126: if (combo instanceof Combo)
127: ((Combo) combo).setEnabled(enabled);
128: else
129: ((CCombo) combo).setEnabled(enabled);
130: }
131:
132: public int getItemCount() {
133: if (combo instanceof Combo)
134: return ((Combo) combo).getItemCount();
135: return ((CCombo) combo).getItemCount();
136: }
137:
138: public String[] getItems() {
139: if (combo instanceof Combo)
140: return ((Combo) combo).getItems();
141: return ((CCombo) combo).getItems();
142: }
143: }
|