View Javadoc

1   /*
2    *  File: SimpleColumn.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui.table.model.simple;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import de.jaret.util.ui.table.model.AbstractColumn;
17  import de.jaret.util.ui.table.model.IRow;
18  
19  /***
20   * Simple implementation of a column for use in the SimpleJaretTableModel.
21   * 
22   * @author kliem
23   * @version $Id: SimpleColumn.java 180 2007-01-07 18:44:01Z olk $
24   */
25  public class SimpleColumn extends AbstractColumn {
26      /*** Map holding the column values. */
27      private Map<Integer, Object> _values = new HashMap<Integer, Object>();
28      /*** header label. */
29      private String _headerLabel = "";
30      /*** index of the column. */
31      private int _index;
32      /***
33       * Tablemodel the column is part of.
34       */
35      private SimpleJaretTableModel _model;
36  
37      /***
38       * Construct a column.
39       * 
40       * @param index index
41       * @param model table model the column is part of
42       */
43      public SimpleColumn(int index, SimpleJaretTableModel model) {
44          _index = index;
45          _model = model;
46      }
47  
48      /***
49       * Allow setting of the header label.
50       * 
51       * @param label the label
52       */
53      public void setHeaderLabel(String label) {
54          _headerLabel = label;
55      }
56  
57      /***
58       * {@inheritDoc} Always assume different classes and rely on the object tht is the value.
59       */
60      public Class<?> getContentClass() {
61          return null;
62      }
63  
64      /***
65       * {@inheritDoc}
66       */
67      public String getHeaderLabel() {
68          return _headerLabel;
69      }
70  
71      /***
72       * {@inheritDoc}
73       */
74      public String getId() {
75          return Integer.toString(_index);
76      }
77  
78      /***
79       * {@inheritDoc}
80       */
81      public Object getValue(IRow row) {
82          return _values.get(((SimpleRow) row).getIndex());
83      }
84  
85      /***
86       * {@inheritDoc}
87       */
88      public void setValue(IRow row, Object value) {
89          Object oldVal = getValue(row);
90          _values.put(((SimpleRow) row).getIndex(), value);
91          fireValueChanged(row, this, oldVal, value);
92          _model.cellChanged(row, this, value);
93      }
94  
95      /***
96       * {@inheritDoc}
97       */
98      public boolean supportsSorting() {
99          return true;
100     }
101 
102     /***
103      * {@inheritDoc} Sorting set null &lt; non-null, non comparables are equal.
104      */
105     @SuppressWarnings("unchecked")
106     public int compare(IRow o1, IRow o2) {
107         Object v1 = getValue(o1);
108         Object v2 = getValue(o2);
109         if (v1 == null && v2 != null) {
110             return -1;
111         }
112         if (v2 == null && v1 != null) {
113             return 1;
114         }
115         if (v1 instanceof Comparable) {
116             Comparable c1 = (Comparable) v1;
117             try {
118                 return c1.compareTo(v2);
119             } catch (Exception e) {
120                 // ignore
121             }
122         }
123 
124         return 0;
125     }
126 
127 }