KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > ui > celleditor > ExtendedComboBoxCellEditor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ExtendedComboBoxCellEditor.java,v 1.2 2005/06/08 06:24:33 nickb Exp $
16  */

17 package org.eclipse.emf.common.ui.celleditor;
18
19
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.eclipse.jface.viewers.ComboBoxCellEditor;
24 import org.eclipse.jface.viewers.ILabelProvider;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Composite;
27
28
29 /**
30  * This uses a list of objects and a label provider to build a combo box based on model objects rather than on strings.
31  */

32 public class ExtendedComboBoxCellEditor extends ComboBoxCellEditor
33 {
34   public static String JavaDoc [] createItems(List JavaDoc list, ILabelProvider labelProvider, boolean sorted)
35   {
36     String JavaDoc [] result;
37
38     // If there are objects to populate...
39
//
40
if (list != null && list.size() > 0)
41     {
42       // Create an new array..
43
//
44
result = new String JavaDoc [list.size()];
45
46       // Fill in the array with label/value pair items.
47
//
48
int i = 0;
49       for (Iterator JavaDoc objects = list.iterator(); objects.hasNext(); ++i)
50       {
51         Object JavaDoc object = objects.next();
52         result[i] = labelProvider.getText(object);
53       }
54
55       // We could collate the array, but we'd have to keep the list in synch.
56
//
57
// Arrays.sort(result, java.text.Collator.getInstance());
58
}
59     else
60     {
61       result = new String JavaDoc [] { "" };
62     }
63
64     return result;
65   }
66
67   /**
68    * This keeps track of the list of model objects.
69    */

70   protected List JavaDoc list;
71
72   public ExtendedComboBoxCellEditor(Composite composite, List JavaDoc list, ILabelProvider labelProvider)
73   {
74     this(composite, list, labelProvider, false, SWT.READ_ONLY);
75   }
76
77   public ExtendedComboBoxCellEditor(Composite composite, List JavaDoc list, ILabelProvider labelProvider, boolean sorted)
78   {
79     this(composite, list, labelProvider, sorted, SWT.READ_ONLY);
80   }
81
82   public ExtendedComboBoxCellEditor(Composite composite, List JavaDoc list, ILabelProvider labelProvider, int style)
83   {
84     this(composite, list, labelProvider, false, style);
85   }
86
87   public ExtendedComboBoxCellEditor(Composite composite, List JavaDoc list, ILabelProvider labelProvider, boolean sorted, int style)
88   {
89     super(composite, createItems(list, labelProvider, sorted), style);
90
91     this.list = list;
92   }
93      
94   public Object JavaDoc doGetValue()
95   {
96     // Get the index into the list via this call to super.
97
//
98
int index = ((Integer JavaDoc)super.doGetValue()).intValue();
99     return index < list.size() && index >= 0 ? list.get(((Integer JavaDoc)super.doGetValue()).intValue()) : null;
100   }
101
102   public void doSetValue(Object JavaDoc value)
103   {
104     // Set the index of the object value in the list via this call to super.
105
//
106
int index = list.indexOf(value);
107     if (index != -1)
108     {
109       super.doSetValue(new Integer JavaDoc(list.indexOf(value)));
110     }
111   }
112 }
113
Popular Tags