View Javadoc

1   /*
2    * $Id: ComboBoxCellEditorModel.java,v 1.3 2005/05/31 19:37:20 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.editor.visualeditors.restriction;
10  
11  import java.util.ArrayList;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import org.eclipse.jface.viewers.ComboBoxCellEditor;
17  import org.eclipse.jface.viewers.ILabelProvider;
18  
19  /***
20   * This class provides a model for Combo Box Cell editors so that 
21   * generic objects can be represented conveniently.  It uses a label provider to
22   * generate strings for a combo box cell editor.  Usage is as follows: <br /> <br />
23   * Construct an instance of this class with a label provider.  Call {@link #setElements(List) } 
24   * with a list of the objects you wish to represent in the list.  Call {@link #getItems} to 
25   * retrieve an array of strings suitable for 
26   * {@link org.eclipse.jface.viewers.ComboBoxCellEditor#setItems(java.lang.String[])}.
27   * 
28   * Query the combo box cell editor for the selected index, and use either 
29   * {@link #getElement(int) } or {@link #getElement(Integer) }
30   * to return the original model element associated with that selection. 
31   * 
32   * 
33   * @author aperezlo
34   */
35  class ComboBoxCellEditorModel 
36  {
37     private List _elements;
38     private String[] _items;
39     private HashMap _itemsToIndices;
40     private HashMap _indicesToItems;
41     private ILabelProvider _stringifier;
42     
43     /***
44      * Constructs a model w/ the given ILabelProvider.
45      * 
46      * @param stringy the ILabelProvider to use to create the text representations for the combo box cell editor
47      */
48     public ComboBoxCellEditorModel(ILabelProvider stringy)
49     {
50        _stringifier = stringy;
51        _elements = new ArrayList();
52        _itemsToIndices = new HashMap();
53        _indicesToItems = new HashMap();
54     }
55     
56     /***
57      * Constructs a model w/ the given ILabelProvider and elements.
58      * 
59      * @param stringy the ILabelProvider to use to create the text representations for the combo box cell editor
60      * @param elements the elements that constitute this model
61      */
62     public ComboBoxCellEditorModel(ILabelProvider stringy, List elements)
63     {
64        this(stringy);
65        setElements(elements);
66     }
67     
68     /***
69      * The method sets the contents of this model.  After a call to this method (or 
70      * object construction with {@link #ComboBoxCellEditorModel(ILabelProvider, List) }), a
71      * call to the other methods in this class will result in the proper responses.
72      * 
73      * @param elements a List of the objects to be maintained by this model
74      */
75     public void setElements(List elements)
76     {
77        Iterator i = null;
78        int index = 0;
79        _items = new String[0];
80        Object temp = null;
81        if(elements != null)
82        {
83           i = elements.iterator();
84           _itemsToIndices.clear();
85           _indicesToItems.clear();
86           _items = new String[elements.size()];
87           while(i.hasNext())
88           {
89              temp = i.next();
90              _itemsToIndices.put(temp, new Integer(index));
91              _indicesToItems.put(new Integer(index), temp);
92              _items[index] = _stringifier.getText(temp);
93              index++;
94           }
95        }
96     }
97     
98     /***
99      * After a call to {@link #setElements(List) }, this method will return an array 
100     * of strings suitable as input to {@link ComboBoxCellEditor#setItems(java.lang.String[]) }.
101     * 
102     * @return an array of string representations of this model's objects
103     */
104    public String[] getItems()
105    {
106       return _items;
107    }
108    
109    /***
110     * Retrieves the object associated with an index into the combo box.
111     * @param index an index from the array of strings returned by {@link #getItems() }
112     * @return the object associated with that index
113     */
114    public Object getElement(Integer index)
115    {
116       return _indicesToItems.get(index);
117    }
118    
119    /***
120     * Retrieves the object associated with an index into the combo box.
121     * @param index an index from the array of strings returned by {@link #getItems() }
122     * @return the object associated with that index
123     */
124    public Object getElement(int index)
125    {
126       return getElement(new Integer(index));
127    }
128    
129    /***
130     * This method returns, given an object, the index of that object in the array of 
131     * strings returned by {@link #getItems() } or an Integer representing -1 if the 
132     * model object requested was not in the original List processed by {@link #setElements(List) }. 
133     * 
134     * @param element a model object
135     * @return the index of that object in the array of strings
136     */
137    public Integer getIndex(Object element)
138    {
139       Integer toReturn = null;
140       toReturn = (Integer) _itemsToIndices.get(element);
141       if(toReturn == null)
142       {
143          toReturn = new Integer(-1);
144       }
145       return toReturn;
146    }
147    
148    /***
149     * This method returns, given an object, the index of that object in the array of 
150     * strings returned by {@link #getItems() } or an  -1 if the 
151     * model object requested was not in the original List processed by {@link #setElements(List) }. 
152     * 
153     * @param element a model object
154     * @return the index of that object in the array of strings
155     */
156    public int getIntIndex(Object element)
157    {
158       return getIndex(element).intValue();
159    }
160    
161 }
162