View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.interaction;
3   
4   import java.util.*;
5   import javax.swing.*;
6   import javax.swing.table.*;
7   
8   import hep.graphics.heprep.HepRep;
9   import hep.graphics.heprep.HepRepAttDef;
10  import hep.graphics.heprep.HepRepAttValue;
11  import hep.graphics.heprep.HepRepInstance;
12  import hep.graphics.heprep.HepRepType;
13  import hep.graphics.heprep.HepRepTypeTree;
14  
15  import hep.wired.util.SortableListTableModel;
16  
17  import hep.wired.util.ScientificTable;
18  import hep.wired.heprep.util.WiredHepRepUtil;
19  
20  /***
21   * TableModel for HepRep Attribute Categories.
22   *
23   * @author Mark Donszelmann
24   * @version $Id: CategoryTableModel.java 1935 2005-06-21 18:05:29Z duns $
25   */
26  public class CategoryTableModel extends SortableListTableModel {
27  
28      private List/*<String>*/ categoryList;
29      private Set/*<String>*/ shownCategorySet = new HashSet();
30  
31      public CategoryTableModel(List /*<String>*/ categories) {
32          super(new String[] { "Category", "Shown" }, 
33                new Class[] { String.class, Boolean.class }, 
34                categories);
35          categoryList = categories;
36          shownCategorySet = new HashSet();
37      }
38                               
39      public boolean isCellEditable(int row, int col) {
40          return (col != Flag.NAME);
41      }
42                          
43      public Object getValueAt(int row, int col) {
44          String category = (String)categoryList.get(row);
45          switch(col) {
46              case Flag.NAME: 
47                  return category;
48              case Flag.FLAG: 
49                  return Boolean.valueOf(shownCategorySet.contains(category));
50              default: 
51                  return "";
52          }
53      }
54              
55      public void setValueAt(Object value, int row, int col) {
56          switch(col) {
57              case Flag.NAME: 
58                  return;
59              case Flag.FLAG: 
60                  String category = (String)categoryList.get(row);
61                  if (((Boolean)value).booleanValue()) {
62                     shownCategorySet.add(category);
63                  } else {
64                     shownCategorySet.remove(category);
65                  }
66                  changed();
67                  return;
68              default: 
69                  return;
70          }
71      }
72  
73      protected int compareKeys(Object o1, Object o2, int col, boolean ascending) {
74          String c1 = (String)o1;
75          String c2 = (String)o2;
76          switch(col) {
77              default:   
78              case Flag.NAME: 
79                  return (ascending ? 1 : -1) * 
80                         (c1.compareTo(c2));
81              case Flag.FLAG: 
82                  return (ascending ? 1 : -1) * 
83                         (shownCategorySet.contains(c1) ? (shownCategorySet.contains(c2) ? 0 : 1) : shownCategorySet.contains(c2) ? -1 : 0);
84          }
85      }            
86      
87      // package method
88  /*    void addCategories(Set/*<String>*//* categories) {
89          for (Iterator i = categories.iterator(); i.hasNext(); ) {
90              String category = (String)i.next();
91              if (!categoryList.contains(category)) {
92                  categoryList.add(category);
93                  shownCategorySet.add(category);
94              }            
95          }
96      }
97  */
98      public Set getShownSet() {
99          return shownCategorySet;
100     }
101     
102     // Called when the user changes the table data (via GUI)
103     protected void changed() {
104     }
105     
106     public void set(HepRep heprep) {
107         // persist categories
108         List oldCategoryList = new ArrayList();
109         oldCategoryList.addAll(categoryList);
110         categoryList.clear();
111         
112         Set oldShownCategorySet = new HashSet();
113         oldShownCategorySet.addAll(shownCategorySet);
114         shownCategorySet.clear();
115         
116         // categories
117         Set categoryNames = (heprep != null) ? WiredHepRepUtil.getCategories(heprep) : new HashSet();
118         for (Iterator i=categoryNames.iterator(); i.hasNext(); ) {
119             String category = (String)i.next();
120             addCategory(category, !oldCategoryList.contains(category) || oldShownCategorySet.contains(category));
121         }
122     }
123     
124     
125     public void addCategory(String category, boolean shown) {
126         if (!categoryList.contains(category)) categoryList.add(category);
127         if (shown) {
128             shownCategorySet.add(category); 
129         } else {
130             shownCategorySet.remove(category);
131         }
132     }
133 }