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 import com.cosylab.util.NameValueList; 23 import com.cosylab.util.ObjectList; 24 25 import java.util.Iterator; 26 import java.util.List; 27 import java.util.Map; 28 29 30 /** 31 * TableCell is most basic kind of table cells, which has own renderers and 32 * editors in <code>com.cosylab.gui.components.table.renderers</code>. 33 * 34 * <p> 35 * This cell may contain arbitrary set of characteristics. They hint renderers, 36 * how to display particular cell. Consult JavaDoc of particular cell to see 37 * which characteristics are supported and compatible with available 38 * renderers. 39 * </p> 40 * 41 * <p> 42 * User of this cell migth decide to define values for following 43 * characteristics: TIMEOUT, ERROR, SUSPENDED, OUT_OF_BOUNDS. 44 * </p> 45 * 46 * <p> 47 * Note that characteristic map is optimized for number of characteristics up 48 * to 10 because is implemented around linear array. 49 * </p> 50 * 51 * <p> 52 * Note that all characteristics are optional. If they are null, renderers will 53 * try best to use value without them. 54 * </p> 55 * 56 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a> 57 * @version $id$ 58 */ 59 public class TableCell implements Comparable 60 { 61 /** Characteristic name for tooltip. */ 62 public static final String TOOLTIP = "tooltip"; 63 64 /** 65 * Characteristic name for out of bouds value state, when cell value is out 66 * of designated value range, characteristic value must be Boolean.TRUE. 67 */ 68 public static final String OUT_OF_BOUNDS = "outOfBounds"; 69 70 /** 71 * Characteristic name for timeout state, value must be Boolean.TRUE if 72 * data source in timeout. 73 */ 74 public static final String TIMEOUT = "timeout"; 75 76 /** 77 * Characteristic name for error state, value must be Boolean.TRUE if data 78 * source is in error condition. 79 */ 80 public static final String ERROR = "error"; 81 82 /** 83 * Characteristic name for alarm state, value must be Boolean.TRUE if data 84 * source is in alarm condition. 85 */ 86 public static final String ALARM = "alarm"; 87 88 /** 89 * Characteristic name for warning state, value must be Boolean.TRUE if 90 * value is out of bounds or source is in warning condition. 91 */ 92 public static final String WARNING = "warning"; 93 94 /** 95 * Characteristic name for severity of alarm or error state. 96 */ 97 public static final String SEVERITY = "severity"; 98 99 /** 100 * Characteristic name for suspended state, value must be Boolean.TRUE if 101 * data source is in suspended condition (eg. not initialized). 102 */ 103 public static final String SUSPENDED = "suspended"; 104 protected Object value = null; 105 protected Object dataSource; 106 protected Command[] commands = null; 107 protected String identifier = null; 108 private Map<String, Object> characteristics; 109 private List<String> supported = new ObjectList(String.class); 110 protected long timestamp; 111 112 /** 113 * Creates new noninitialized instance of cell proxy. 114 */ 115 protected TableCell() 116 { 117 super(); 118 } 119 120 /** 121 * Creates a new TableCell object. 122 * 123 * @param identifier unique string within context of current table 124 * @param dataSource object which provides values for this cell 125 * @param value a cells value 126 * @param commands commands, which can be executed on this cell 127 */ 128 public TableCell(String identifier, Object dataSource, Object value, 129 Command[] commands) 130 { 131 this.identifier = identifier; 132 this.dataSource = dataSource; 133 this.value = value; 134 this.commands = commands; 135 } 136 137 /** 138 * Returns commands, which can be executed on this cell. 139 * 140 * @return commands, which can be executed on this cell 141 */ 142 public Command[] getCommands() 143 { 144 return commands; 145 } 146 147 /** 148 * Object which deliveres values for this cell. 149 * 150 * @return objects which deliveres values to this cell 151 */ 152 public Object getDataSource() 153 { 154 return dataSource; 155 } 156 157 /** 158 * Generally expected to be unique string within context of current table. 159 * 160 * @return unique string within context of current table 161 */ 162 public String getIdentifier() 163 { 164 return identifier; 165 } 166 167 /** 168 * Return tooltip value or <code>null</code> if none is set. 169 * 170 * @return tooltip 171 */ 172 public String getTooltip() 173 { 174 return (String)characteristics.get(TOOLTIP); 175 } 176 177 /** 178 * Return tooltip value or <code>null</code> if none is set. 179 * 180 * @return tooltip 181 */ 182 public Object getValue() 183 { 184 return value; 185 } 186 187 /** 188 * Sets new value to this cell. 189 * 190 * @param newValue 191 */ 192 public void setValue(Object newValue) 193 { 194 value = newValue; 195 } 196 197 /* (non-Javadoc) 198 * @see java.lang.Object#toString() 199 */ 200 public String toString() 201 { 202 return String.valueOf(value); 203 } 204 205 /** 206 * Return scharacteristic value with specific name. 207 * 208 * @param name a name of requested characteristic 209 * 210 * @return characteristic value, or <code>null</code> if not defined 211 */ 212 public Object getCharacteristic(String name) 213 { 214 return getCharacteristics().get(name); 215 } 216 217 /** 218 * Return scharacteristic value with specific name. 219 * 220 * @param name a name of requested characteristic 221 * @param defaultValue DOCUMENT ME! 222 * 223 * @return characteristic value, or <code>null</code> if not defined 224 */ 225 public Object getCharacteristic(String name, Object defaultValue) 226 { 227 Object o = getCharacteristics().get(name); 228 229 if (o == null) { 230 o = defaultValue; 231 } 232 233 return o; 234 } 235 236 /** 237 * Puts new characteristic value with provided name. 238 * 239 * @param name characteristic name 240 * @param value new characteristic value 241 * 242 * @return old characteristic value, or <code>null</code> if none 243 */ 244 public Object putCharacteristic(String name, Object value) 245 { 246 return getCharacteristics().put(name, value); 247 } 248 249 /** 250 * Puts all characteristics to this cell. 251 * 252 * @param characteristics Map with characteristics 253 */ 254 public void putAllCharacteristics(Map<String, Object> characteristics) 255 { 256 if (characteristics == null) return; 257 Iterator<String> it = characteristics.keySet().iterator(); 258 while (it.hasNext()) { 259 String key = it.next(); 260 Object val= characteristics.get(key); 261 if (val!=null) { 262 getCharacteristics().put(key,val); 263 } 264 } 265 } 266 267 /** 268 * Return map with all characteristics. 269 * 270 * @return map with all characteristics 271 */ 272 public Map<String, Object> getCharacteristics() 273 { 274 if (characteristics == null) { 275 characteristics = new NameValueList(12); 276 } 277 278 return characteristics; 279 } 280 281 /** 282 * Returns time of latest change of value. 283 * 284 * @return time of latest change of value 285 */ 286 public long getTimestamp() 287 { 288 return timestamp; 289 } 290 291 /** 292 * Sets time of latest change of value. 293 * 294 * @param timestamp time of latest change 295 */ 296 public void setTimestamp(long timestamp) 297 { 298 this.timestamp = timestamp; 299 } 300 301 /** 302 * Returns <code>true</code> if ERROR characteristic is true. 303 * 304 * @return <code>true</code> if ERROR characteristic is true 305 */ 306 public boolean isError() 307 { 308 return Boolean.TRUE.equals(getCharacteristic(ERROR)); 309 } 310 311 /** 312 * Returns <code>true</code> if ALARM characteristic is true. 313 * 314 * @return <code>true</code> if ALARM characteristic is true 315 */ 316 public boolean isAlarm() 317 { 318 return Boolean.TRUE.equals(getCharacteristic(ALARM)); 319 } 320 321 /** 322 * Returns <code>true</code> if WARNING characteristic is true. 323 * 324 * @return <code>true</code> if WARNING characteristic is true 325 */ 326 public boolean isWarning() 327 { 328 return Boolean.TRUE.equals(getCharacteristic(WARNING)); 329 } 330 331 /** 332 * Returns <code>true</code> if TIMEOUT characteristic is true. 333 * 334 * @return <code>true</code> if TIMEOUT characteristic is true 335 */ 336 public boolean isTimeout() 337 { 338 return Boolean.TRUE.equals(getCharacteristic(TIMEOUT)); 339 } 340 341 /** 342 * Returns <code>true</code> if SUSPENDED characteristic is true. 343 * 344 * @return <code>true</code> if SUSPENDED characteristic is true 345 */ 346 public boolean isSuspended() 347 { 348 return Boolean.TRUE.equals(getCharacteristic(SUSPENDED)); 349 } 350 351 /** 352 * Returns <code>true</code> if OUT_OF_BOUNDS characteristic is true. 353 * 354 * @return <code>true</code> if OUT_OF_BOUNDS characteristic is true 355 */ 356 public boolean isOutOfBounds() 357 { 358 return Boolean.TRUE.equals(getCharacteristic(OUT_OF_BOUNDS)); 359 } 360 361 /** 362 * DOCUMENT ME! 363 * 364 * @return DOCUMENT ME! 365 */ 366 public String[] getSupportedCharacteristics() 367 { 368 return (String[])supported.toArray(new String[supported.size()]); 369 } 370 371 /** 372 * DOCUMENT ME! 373 * 374 * @param name DOCUMENT ME! 375 */ 376 public void addSupportedCharacteristic(String name) 377 { 378 if (!supported.contains(name)) { 379 supported.add(name); 380 } 381 } 382 383 /** 384 * DOCUMENT ME! 385 * 386 * @param name DOCUMENT ME! 387 */ 388 public void removeSupportedCharacteristic(String name) 389 { 390 supported.remove(name); 391 } 392 393 /* (non-Javadoc) 394 * @see java.lang.Comparable#compareTo(java.lang.Object) 395 */ 396 public int compareTo(Object o) 397 { 398 if (o instanceof TableCell) { 399 TableCell tc = (TableCell)o; 400 401 if (getValue() != null && tc.getValue() != null) { 402 if (getValue().getClass().equals(tc.getValue().getClass()) 403 && (getValue() instanceof Comparable)) { 404 return ((Comparable)value).compareTo(tc.getValue()); 405 } else if ((getValue() instanceof Number) 406 && (tc.getValue() instanceof Number)) { 407 double d = ((Number)getValue()).doubleValue() 408 - ((Number)tc.getValue()).doubleValue(); 409 410 if (d < -0.00001) { 411 return -1; 412 } 413 414 if (d > 0.00001) { 415 return 1; 416 } 417 } 418 } 419 } 420 421 return 0; 422 } 423 } 424 425 /* __oOo__ */