Clover coverage report - brownies library - 1.0-beta-1 Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00 FRAMES NO FRAMES |
file stats: | LOC: | 197 |  | Methods: | 19
| NCLOC: | 107 |  | Classes: | 6
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
TableUtils.java | 72.2% | 80.6% | 78.9% | 78.1% |

1 |  |
     /*
2 |  |
      * brownies and its relative products are published under the terms
3 |  |
      * of the Apache Software License.
4 |  |
      *
5 |  |
      * Created on 2004/05/27 3:20:55
6 |  |
      */
7 |  |
     package org.asyrinx.brownie.swing.table;
8 |  |
9 |  |
     import java.util.ArrayList;
10 |  |
     import java.util.Collections;
11 |  |
     import java.util.List;
12 |  |
13 |  |
     import javax.swing.table.TableModel;
14 |  |
15 |  |
     import org.apache.commons.lang.ObjectUtils;
16 |  |
     import org.asyrinx.brownie.swing.table.wrapper.TableModelWrapper;
17 |  |
18 |  |
     /**
19 |  |
      * @author akima
20 |  |
      */
21 |  |
     public class TableUtils {
22 |  |
23 | 1 |
         public static TableModel toUneditableModel(TableModel model) {
24 | 1 |
             return new UneditableTableModel(model);
25 |  |
         }
26 |  |
27 | 2 |
         public static InvisibleColumnTableModel toInvisibleColumnModel(
28 |  |
                 TableModel model, int invisibleColumnIndex, int invisibleColumnCount) {
29 | 2 |
             if (invisibleColumnCount < 1)
30 | 0 |
                 return getNullModel(model);
31 | 2 |
             if (invisibleColumnIndex < 0)
32 | 0 |
                 return getNullModel(model);
33 | 2 |
             return new InvisibleColumnTableModelImpl(model, invisibleColumnIndex,
34 |  |
                     invisibleColumnCount);
35 |  |
         }
36 |  |
37 | 0 |
         public static InvisibleColumnTableModel getNullModel(TableModel model) {
38 | 0 |
             return new NullInvisibleColumnTableModel(model);
39 |  |
         }
40 |  |
41 | 1 |
         public static SortableTableModel toSortableModel(TableModel model) {
42 | 1 |
             return new SortableTableModelImpl(model);
43 |  |
         }
44 |  |
     }
45 |  |
46 |  |
     class UneditableTableModel extends TableModelWrapper {
47 |  |
         /**
48 |  |
          * @param model
49 |  |
          */
50 | 1 |
         public UneditableTableModel(TableModel model) {
51 | 1 |
             super(model);
52 |  |
         }
53 |  |
54 | 25 |
         public boolean isCellEditable(int rowIndex, int columnIndex) {
55 | 25 |
             return false;
56 |  |
         }
57 |  |
     }
58 |  |
59 |  |
     class InvisibleColumnTableModelImpl extends TableModelWrapper implements
60 |  |
             InvisibleColumnTableModel {
61 |  |
         /**
62 |  |
          * @param model
63 |  |
          */
64 | 2 |
         public InvisibleColumnTableModelImpl(TableModel model,
65 |  |
                 int invisibleColumnIndex, int invisibleColumnCount) {
66 | 2 |
             super(model);
67 | 2 |
             this.invisibleColumnIndex = invisibleColumnIndex;
68 | 2 |
             this.invisibleColumnCount = invisibleColumnCount;
69 |  |
         }
70 |  |
71 |  |
         private final int invisibleColumnIndex;
72 |  |
73 |  |
         private final int invisibleColumnCount;
74 |  |
75 |  |
         /*
76 |  |
          * (non-Javadoc)
77 |  |
          *
78 |  |
          * @see org.asyrinx.brownie.swing.table.wrapper.TableModelWrapper#getColumnCount()
79 |  |
          */
80 | 2 |
         public int getColumnCount() {
81 | 2 |
             return model.getColumnCount() - invisibleColumnCount;
82 |  |
         }
83 |  |
84 | 20 |
         public int getOriginalColumnIndex(int columnIndex) {
85 | 20 |
             return (columnIndex < invisibleColumnIndex) ? columnIndex : columnIndex
86 |  |
                     + invisibleColumnCount;
87 |  |
         }
88 |  |
89 |  |
         /*
90 |  |
          * (non-Javadoc)
91 |  |
          *
92 |  |
          * @see org.asyrinx.brownie.swing.table.wrapper.TableModelWrapper#getColumnName(int)
93 |  |
          */
94 | 0 |
         public String getColumnName(int columnIndex) {
95 | 0 |
             return model.getColumnName(getOriginalColumnIndex(columnIndex));
96 |  |
         }
97 |  |
98 |  |
         /*
99 |  |
          * (non-Javadoc)
100 |  |
          *
101 |  |
          * @see org.asyrinx.brownie.swing.table.wrapper.TableModelWrapper#getValueAt(int,
102 |  |
          *        int)
103 |  |
          */
104 | 20 |
         public Object getValueAt(int rowIndex, int columnIndex) {
105 | 20 |
             return model.getValueAt(rowIndex, getOriginalColumnIndex(columnIndex));
106 |  |
         }
107 |  |
108 |  |
     }
109 |  |
110 |  |
     class SortableTableModelImpl extends TableModelWrapper implements
111 |  |
             SortableTableModel {
112 |  |
         /**
113 |  |
          * @param model
114 |  |
          */
115 | 1 |
         public SortableTableModelImpl(TableModel model) {
116 | 1 |
             super(model);
117 |  |
         }
118 |  |
119 |  |
         private int[] rowIndexMap = null;
120 |  |
121 |  |
         class SortEntry implements Comparable {
122 |  |
             final String value;
123 |  |
124 |  |
             final int row;
125 |  |
126 | 8 |
             public SortEntry(String value, int row) {
127 | 8 |
                 this.value = (value != null) ? value : "";
128 | 8 |
                 this.row = row;
129 |  |
             }
130 |  |
131 |  |
             /*
132 |  |
              * (non-Javadoc)
133 |  |
              *
134 |  |
              * @see java.lang.Comparable#compareTo(java.lang.Object)
135 |  |
              */
136 | 11 |
             public int compareTo(Object o) {
137 | 11 |
                 return this.value.compareTo(((SortEntry) o).value);
138 |  |
             }
139 |  |
         }
140 |  |
141 |  |
         /*
142 |  |
          * (non-Javadoc)
143 |  |
          *
144 |  |
          * @see org.asyrinx.brownie.swing.table.SortableTableModel#sort(int)
145 |  |
          */
146 | 2 |
         public void sort(int columnIndex, boolean reverse) {
147 | 2 |
             if (model.getRowCount() < 2)
148 | 0 |
                 return;
149 | 2 |
             final List list = new ArrayList();
150 | 2 |
             for (int row = 0; row < this.model.getRowCount(); row++)
151 | 8 |
                 list.add(new SortEntry(ObjectUtils.toString(model.getValueAt(row,
152 |  |
                         columnIndex)), row));
153 | 2 |
             Collections.sort(list);
154 | 2 |
             rowIndexMap = new int[list.size()];
155 | 2 |
             for (int i = 0; i < rowIndexMap.length; i++) {
156 | 8 |
                 final int listIndex = (reverse) ? rowIndexMap.length - 1 - i : i;
157 | 8 |
                 final SortEntry entry = (SortEntry) list.get(listIndex);
158 | 8 |
                 rowIndexMap[i] = entry.row;
159 |  |
             }
160 |  |
         }
161 |  |
162 |  |
         /*
163 |  |
          * (non-Javadoc)
164 |  |
          *
165 |  |
          * @see org.asyrinx.brownie.swing.table.wrapper.TableModelWrapper#getValueAt(int,
166 |  |
          *        int)
167 |  |
          */
168 | 36 |
         public Object getValueAt(int rowIndex, int columnIndex) {
169 | 36 |
             return model.getValueAt(getOriginalRowIndex(rowIndex), columnIndex);
170 |  |
         }
171 |  |
172 |  |
         /*
173 |  |
          * (non-Javadoc)
174 |  |
          *
175 |  |
          * @see org.asyrinx.brownie.swing.table.SortableTableModel#getOriginalRowIndex(int)
176 |  |
          */
177 | 36 |
         public int getOriginalRowIndex(int sortedRowIndex) {
178 | 36 |
             return (rowIndexMap == null) ? sortedRowIndex
179 |  |
                     : rowIndexMap[sortedRowIndex];
180 |  |
         }
181 |  |
182 |  |
     }
183 |  |
184 |  |
     final class NullInvisibleColumnTableModel extends TableModelWrapper implements
185 |  |
             InvisibleColumnTableModel {
186 |  |
         /**
187 |  |
          * @param model
188 |  |
          */
189 | 0 |
         public NullInvisibleColumnTableModel(TableModel model) {
190 | 0 |
             super(model);
191 |  |
192 |  |
         }
193 |  |
194 | 0 |
         public int getOriginalColumnIndex(int columnIndex) {
195 | 0 |
             return columnIndex;
196 |  |
         }
197 |  |
     }