1 /*
2  *  (C) 2001 by Argonne National Laboratory
3  *      See COPYRIGHT in top-level directory.
4  */
5 
6 /*
7  *  @author  Anthony Chan
8  */
9 
10 package viewer.legends;
11 
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.ArrayList;
15 import java.util.Comparator;
16 import java.util.Collections;
17 
18 import java.awt.Color;
19 import javax.swing.Icon;
20 import javax.swing.table.AbstractTableModel;
21 // import javax.swing.ListSelectionModel;
22 // import javax.swing.event.ListSelectionListener;
23 // import javax.swing.event.ListSelectionEvent;
24 
25 import base.drawable.ColorAlpha;
26 import base.drawable.Category;
27 import logformat.slog2.CategoryMap;
28 
29 public class LegendTableModel extends AbstractTableModel
30 //                              implements ListSelectionListener
31 {
32     private static final long       serialVersionUID       = 2300L;
33 
34     public  static final int        ICON_COLUMN            = 0;
35     public  static final int        NAME_COLUMN            = 1;
36     public  static final int        VISIBILITY_COLUMN      = 2;
37     public  static final int        SEARCHABILITY_COLUMN   = 3;
38     public  static final int        COUNT_COLUMN           = 4;
39     public  static final int        INCL_RATIO_COLUMN      = 5;
40     public  static final int        EXCL_RATIO_COLUMN      = 6;
41 
42     private static final String[]   COLUMN_TITLES
43                                     = { "Topo", "Name", "V", "S",
44                                         "count", "incl", "excl" };
45     private static final String[]   COLUMN_TOOLTIPS
46                                     = { "Topology/Color", "Category Name",
47                                         "Visibility", "Searchability",
48                                         "category Count in the whole logfile",
49                                         "Inclusive Ratio", "Exclusive Ratio" };
50     private static final Class[]    COLUMN_CLASSES
51                                     = { CategoryIcon.class, String.class,
52                                         Boolean.class, Boolean.class,
53                                         Long.class, Float.class, Float.class };
54     private static final Color[]    COLUMN_TITLE_FORE_COLORS
55                                     = { Color.magenta, Color.pink,
56                                         Color.green, Color.yellow,
57                                         Color.white, Color.white, Color.white };
58     private static final Color[]    COLUMN_TITLE_BACK_COLORS
59                                     = { Color.black, Color.gray,
60                                         Color.darkGray.darker(),
61                                         Color.blue.darker(),
62                                         Color.gray, Color.gray, Color.gray };
63     private static final boolean[]  COLUMN_TITLE_RAISED_ICONS
64                                     = { false, false, true, false,
65                                         false, false, false };
66 
67 
68     private List   objdef_list    = null;
69     private List   icon_list      = null;
70 
LegendTableModel( CategoryMap map )71     public LegendTableModel( CategoryMap map )
72     {
73         super();
74 
75         objdef_list  = new ArrayList( map.values() );
76         this.sortNormally( LegendComparators.CASE_SENSITIVE_ORDER );
77     }
78 
79 
80 
81     //  Sorting into various order
initIconListFromCategoryList()82     private void initIconListFromCategoryList()
83     {
84         icon_list    = new ArrayList( objdef_list.size() );
85 
86         Icon      icon;
87         Category  objdef;
88         Iterator  objdefs;
89         objdefs  = objdef_list.iterator();
90         while ( objdefs.hasNext() ) {
91             objdef = (Category) objdefs.next();
92             icon   = new CategoryIcon( objdef );
93             icon_list.add( icon );
94         }
95     }
96 
sortNormally( Comparator comparator )97     private void sortNormally( Comparator comparator )
98     {
99         Collections.sort( objdef_list, comparator );
100         this.initIconListFromCategoryList();
101     }
102 
sortReversely( Comparator comparator )103     private void sortReversely( Comparator comparator )
104     {
105         Collections.sort( objdef_list, comparator );
106         Collections.reverse( objdef_list );
107         this.initIconListFromCategoryList();
108     }
109 
arrangeOrder( Comparator comparator )110     public void arrangeOrder( Comparator comparator )
111     {
112         this.sortNormally( comparator );
113         super.fireTableDataChanged();
114     }
115 
reverseOrder( Comparator comparator )116     public void reverseOrder( Comparator comparator )
117     {
118         this.sortReversely( comparator );
119         super.fireTableDataChanged();
120     }
121 
122 
123 
getRowCount()124     public int getRowCount()
125     {
126         return objdef_list.size();
127     }
128 
getColumnCount()129     public int getColumnCount()
130     {
131         return COLUMN_TITLES.length;
132     }
133 
134     // Overload the AbstractTableModel.getColumnClass()
135     // which always returns Object.class
getColumnClass( int icolumn )136     public Class getColumnClass( int icolumn )
137     {
138         return COLUMN_CLASSES[ icolumn ];
139     }
140 
getColumnName( int icolumn )141     public String getColumnName( int icolumn )
142     {
143         return COLUMN_TITLES[ icolumn ];
144     }
145 
getColumnNameForeground( int icolumn )146     public Color getColumnNameForeground( int icolumn )
147     {
148         return COLUMN_TITLE_FORE_COLORS[ icolumn ];
149     }
150 
getColumnNameBackground( int icolumn )151     public Color getColumnNameBackground( int icolumn )
152     {
153         return COLUMN_TITLE_BACK_COLORS[ icolumn ];
154     }
155 
isRaisedColumnNameIcon( int icolumn )156     public boolean isRaisedColumnNameIcon( int icolumn )
157     {
158         return COLUMN_TITLE_RAISED_ICONS[ icolumn ];
159     }
160 
getColumnToolTip( int icolumn )161     public String getColumnToolTip( int icolumn )
162     {
163         return COLUMN_TOOLTIPS[ icolumn ];
164     }
165 
getColumnMaxValue( int icolumn )166     public Object getColumnMaxValue( int icolumn )
167     {
168         Category objdef;
169         switch ( icolumn ) {
170             case ICON_COLUMN :
171                 return CategoryIcon.BLANK_ICON;
172             case NAME_COLUMN :
173                 objdef = (Category) Collections.max( objdef_list,
174                                     LegendComparators.LONG_NAME_ORDER );
175                 // UpperCase takes up more space.
176                 return objdef.getName().toUpperCase() + ".";
177             case VISIBILITY_COLUMN :
178                 return Boolean.TRUE;
179             case SEARCHABILITY_COLUMN :
180                 return Boolean.TRUE;
181             case COUNT_COLUMN :
182                 objdef = (Category) Collections.max( objdef_list,
183                                     LegendComparators.COUNT_ORDER  );
184                 return new Long( objdef.getSummary().getDrawableCount() * 10 );
185             case INCL_RATIO_COLUMN :
186                 objdef = (Category) Collections.max( objdef_list,
187                                     LegendComparators.INCL_RATIO_ORDER  );
188                 return new Float( objdef.getSummary().getRatio(true) * 10 );
189             case EXCL_RATIO_COLUMN :
190                 objdef = (Category) Collections.max( objdef_list,
191                                     LegendComparators.EXCL_RATIO_ORDER  );
192                 return new Float( objdef.getSummary().getRatio(false) * 10 );
193             default:
194                 System.err.println( "LegendTableModel.getColumnMaxValue("
195                                   + icolumn + ") fails!" );
196                 return null;
197         }
198     }
199 
getValueAt( int irow, int icolumn )200     public Object getValueAt( int irow, int icolumn )
201     {
202         Category  objdef;
203         switch ( icolumn ) {
204             case ICON_COLUMN :
205                 return icon_list.get( irow );
206             case NAME_COLUMN :
207                 objdef = (Category) objdef_list.get( irow );
208                 return objdef.getName();
209             case VISIBILITY_COLUMN :
210                 objdef = (Category) objdef_list.get( irow );
211                 if ( objdef.isVisible() )
212                     return Boolean.TRUE;
213                 else
214                     return Boolean.FALSE;
215             case SEARCHABILITY_COLUMN :
216                 objdef = (Category) objdef_list.get( irow );
217                 if ( objdef.isSearchable() )
218                     return Boolean.TRUE;
219                 else
220                     return Boolean.FALSE;
221             case COUNT_COLUMN :
222                 objdef = (Category) objdef_list.get( irow );
223                 return new Long( objdef.getSummary().getDrawableCount() );
224             case INCL_RATIO_COLUMN :
225                 objdef = (Category) objdef_list.get( irow );
226                 return new Float( objdef.getSummary().getRatio(true) );
227             case EXCL_RATIO_COLUMN :
228                 objdef = (Category) objdef_list.get( irow );
229                 return new Float( objdef.getSummary().getRatio(false) );
230             default:
231                 System.err.println( "LegendTableModel.getValueAt("
232                                   + irow + "," + icolumn + ") fails!" );
233                 return null;
234         }
235     }
236 
isCellEditable( int irow, int icolumn )237     public boolean isCellEditable( int irow, int icolumn )
238     {
239         switch ( icolumn ) {
240             case COUNT_COLUMN :
241             case INCL_RATIO_COLUMN :
242             case EXCL_RATIO_COLUMN :
243                 return false;
244             default:
245                 return true;
246         }
247     }
248 
setValueAt( Object value, int irow, int icolumn )249     public void setValueAt( Object value, int irow, int icolumn )
250     {
251         Category      objdef;
252         CategoryIcon  icon;
253         ColorAlpha    color;
254 
255         objdef = (Category) objdef_list.get( irow );
256         switch ( icolumn ) {
257             case ICON_COLUMN :
258                 color  = (ColorAlpha) value;
259                 objdef.setColor( color );
260                 icon   = (CategoryIcon) icon_list.get( irow );
261                 icon.setDisplayedColor( color );
262                 fireTableCellUpdated( irow, icolumn );
263                 break;
264             case NAME_COLUMN :
265                 objdef.setName( (String) value );
266                 fireTableCellUpdated( irow, icolumn );
267                 break;
268             case VISIBILITY_COLUMN :
269                 objdef.setVisible( ( (Boolean) value ).booleanValue() );
270                 fireTableCellUpdated( irow, icolumn );
271                 break;
272             case SEARCHABILITY_COLUMN :
273                 objdef.setSearchable( ( (Boolean) value ).booleanValue() );
274                 fireTableCellUpdated( irow, icolumn );
275                 break;
276             case COUNT_COLUMN :
277             case INCL_RATIO_COLUMN :
278             case EXCL_RATIO_COLUMN :
279                 // Don't change the CategorySummary property of the Category.
280                 // consistent with isCellEditable();
281                 break;
282             default:
283                 System.err.print( "LegendTableModel.setValueAt("
284                                 + irow + "," + icolumn + ") fails!" );
285         }
286         // System.out.println( "setValueAt("+irow+","+icolumn+"): "+objdef );
287     }
288 
289 /*
290     public void valueChanged( ListSelectionEvent evt )
291     {
292         if ( evt.getValueIsAdjusting() )
293             return;
294 
295         ListSelectionModel  select_model;
296         int                 min_idx, max_idx;
297         select_model = (ListSelectionModel) evt.getSource();
298         min_idx      = select_model.getMinSelectionIndex();
299         max_idx      = select_model.getMaxSelectionIndex();
300         for ( int irow = min_idx; irow <= max_idx; irow++ )
301              if ( select_model.isSelectedIndex( irow ) )
302                  System.out.println( "ListSelectionListener.valueChanged("
303                                    + irow + ")" );
304     }
305 */
306 }
307