View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.table.renderers;
21  
22  import com.cosylab.gui.components.table.cells.LongEnumCell;
23  
24  import javax.swing.AbstractListModel;
25  import javax.swing.ComboBoxModel;
26  
27  
28  class EnumComboBoxModel extends AbstractListModel implements ComboBoxModel
29  {
30  	private static final long serialVersionUID = 1L;
31  	LongEnumCell cell;
32  	int min = -1;
33  	int max = 0;
34  	String selected;
35  
36  	public EnumComboBoxModel()
37  	{
38  		super();
39  	}
40  
41  	/* (non-Javadoc)
42  	 * @see javax.swing.ListModel#getElementAt(int)
43  	 */
44  	public Object getElementAt(int index)
45  	{
46  		if (cell == null) {
47  			return null;
48  		}
49  
50  		return cell.getStateLabel(index - min);
51  	}
52  
53  	/* (non-Javadoc)
54  	 * @see javax.swing.ListModel#getSize()
55  	 */
56  	public int getSize()
57  	{
58  		return max - min + 1;
59  	}
60  
61  	/* (non-Javadoc)
62  	 * @see javax.swing.ComboBoxModel#getSelectedItem()
63  	 */
64  	public Object getSelectedItem()
65  	{
66  		return selected;
67  	}
68  
69  	/* (non-Javadoc)
70  	 * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
71  	 */
72  	public void setSelectedItem(Object anItem)
73  	{
74  		selected = (String)anItem;
75  	}
76  
77  	public void setCell(LongEnumCell c)
78  	{
79  		cell = c;
80  		selected = null;
81  
82  		int size = getSize();
83  		max = 0;
84  		min = -1;
85  
86  		if (size > 0) {
87  			fireIntervalRemoved(this, 0, size);
88  		}
89  
90  		if (cell == null) {
91  			return;
92  		}
93  
94  		min = 15;
95  		max = 0;
96  
97  		for (int i = 0; i < 16; i++) {
98  			Object o = cell.getCharacteristic(LongEnumCell.STATE_DESCRIPTION
99  				    + i);
100 
101 			if (o !=null ) {
102 				if (i < min) {
103 					min = i;
104 				}
105 
106 				if (i > max) {
107 					max = i;
108 				}
109 			}
110 		}
111 
112 		fireIntervalAdded(this, 0, getSize());
113 	}
114 
115 	public Long getLongVale()
116 	{
117 		if (selected == null || cell == null) {
118 			return null;
119 		}
120 
121 		return new Long(cell.toLongValue(selected));
122 	}
123 }
124 
125 /* __oOo__ */