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.cells;
21  
22  /**
23   * LongEnumCell holds enumeration valies. Each long value from min to max
24   * represents one enumeration state, which can have own name and value.
25   *
26   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
27   */
28  public class LongEnumCell extends LongCell
29  {
30  	/** State description property tag. */
31  	public static final String STATE_DESCRIPTION = "stateDescription";
32  
33  	/** State value property tag */
34  	public static final String STATE_VALUE = "stateValue";
35  
36  	/**
37  	 * Constructs a new LongEnumCell.
38  	 *
39  	 */
40  	public LongEnumCell()
41  	{
42  		super();
43  
44  	}
45  
46  	/**
47  	 * Constructs a new LongEnumCell.
48  	 *
49  	 * @param identifier
50  	 * @param dataSource
51  	 * @param value
52  	 * @param commands
53  	 */
54  	public LongEnumCell(String identifier, Object dataSource, long value,
55  	    Command[] commands)
56  	{
57  		super(identifier, dataSource, value, commands);
58  	}
59  
60  	/**
61  	 * Creates a new LongEnumCell object.
62  	 *
63  	 * @param identifier
64  	 * @param dataSource 
65  	 * @param value 
66  	 * @param commands 
67  	 * @param descriptions 
68  	 * @param values 
69  	 */
70  	public LongEnumCell(String identifier, Object dataSource, long value,
71  	    Command[] commands, String[] descriptions, Object[] values)
72  	{
73  		super(identifier, dataSource, value, commands);
74  
75  		for (int i = 0; i < values.length; i++) {
76  			putCharacteristic(STATE_VALUE + i, values[i]);
77  		}
78  
79  		for (int i = 0; i < descriptions.length; i++) {
80  			putCharacteristic(STATE_DESCRIPTION + i, descriptions[i]);
81  		}
82  	}
83  
84  	/**
85  	 * Returns the state description under the index <code>i</code>.
86  	 *
87  	 * @param i index of the requested description
88  	 *
89  	 * @return the description
90  	 */
91  	public String getStateDescription(long i)
92  	{
93  		return (String)getCharacteristic(STATE_DESCRIPTION + i);
94  	}
95  
96  	/**
97  	 * Returns the value under the index <code>i</code>.
98  	 *
99  	 * @param i index of the requested value
100 	 *
101 	 * @return the value
102 	 */
103 	public Object getStateValue(long i)
104 	{
105 		return getCharacteristic(STATE_DESCRIPTION + i);
106 	}
107 
108 	/**
109 	 * Returns the label name of the state under the index <code>i</code>.
110 	 *
111 	 * @param l index of the requested label
112 	 *
113 	 * @return the label
114 	 */
115 	public String getStateLabel(long l)
116 	{
117 		String s = getStateDescription(l);
118 
119 		if (s == null) {
120 			StringBuffer sb = new StringBuffer(8);
121 			sb.append('<');
122 			sb.append(l);
123 			sb.append('>');
124 			s = sb.toString();
125 		}
126 
127 		return s;
128 	}
129 
130 	/**
131 	 * Trys to lookup state table and find enum index with this label. If value
132 	 * is not found, -1 is returned.
133 	 *
134 	 * @param label enum state description label
135 	 *
136 	 * @return state index or -1
137 	 */
138 	public long toLongValue(String label)
139 	{
140 		label = label.trim();
141 
142 		if (label == null || label.length() < 3) {
143 			return -1;
144 		}
145 
146 		if (label.charAt(0) == '<' && label.charAt(label.length() - 1) == '>') {
147 			// this is generated label
148 			return Long.parseLong(label.substring(1, label.length() - 1));
149 		}
150 
151 		// this is label from descriptions
152 		int min = getMinimum(new Long(0)).intValue();
153 		int max = getMaximum(new Long(15)).intValue();
154 
155 		for (int i = min; i <= max; i++) {
156 			if (label.equals(getCharacteristic(STATE_DESCRIPTION + i))) {
157 				return i;
158 			}
159 		}
160 
161 		return -1;
162 	}
163 }
164 
165 /* __oOo__ */