001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.ui; 005 006 import graphlab.platform.attribute.AttributeSet; 007 import graphlab.platform.attribute.AttributeSetImpl; 008 import graphlab.ui.components.gpropertyeditor.GBasicCellEditor; 009 import graphlab.ui.components.gpropertyeditor.GBasicCellRenderer; 010 011 import java.util.Arrays; 012 import java.util.HashMap; 013 import java.util.Set; 014 015 /** 016 * the eXtended attribute which is a kind of view for notifiableAttributeSet, it then connects to the notifiableAttributeSet 017 * which is a kind of model for it. 018 * 019 * @author Azin Azadi 020 */ 021 public class AttributeSetView { 022 HashMap<String, Boolean> editable = new HashMap<String, Boolean>(); 023 HashMap<String, GBasicCellEditor> editors = new HashMap<String, GBasicCellEditor>(); 024 final int dlen = 8; 025 HashMap[] descriptions = new HashMap[dlen]; 026 private AttributeSet a = new AttributeSetImpl(); 027 028 static int dname = 2, desc = 3, cat = 4, index = 5, visible = 6, valid = 7, EDITOR = 8; 029 private HashMap<String, GBasicCellRenderer> renderers = new HashMap<String, GBasicCellRenderer>(); 030 031 public AttributeSetView() { 032 for (int i = 0; i < dlen; i++) { 033 descriptions[i] = new HashMap(); 034 } 035 } 036 037 public boolean isEditable(String name) { 038 if (editable.get(name) != null) 039 return editable.get(name); 040 else 041 return true; 042 } 043 044 public void setEditable(String name, boolean isEditable) { 045 // if (contains(name)) 046 editable.put(name, isEditable); 047 } 048 049 private void set(int i, String name, String value) { 050 descriptions[i].put(name, value); 051 } 052 053 private String get(int i, String name) { 054 if (!descriptions[i].containsKey(name)) 055 return ""; 056 return (String) descriptions[i].get(name); 057 } 058 059 060 /** 061 * display names not implemented yet. 062 */ 063 public void setDisplayName(String name, String displayName) { 064 //todo: impl. 065 set(dname, name, displayName); 066 } 067 068 public String getDisplayName(String name) { 069 String s = get(dname, name); 070 if (s.equals("")) 071 return name; 072 return s; 073 } 074 075 public void setDescription(String name, String description) { 076 set(desc, name, description); 077 } 078 079 public String getDescription(String name) { 080 return get(desc, name); 081 } 082 083 //todo: implement category @ gpropertyeditor 084 public void setCategory(String name, String category) { 085 set(cat, name, category); 086 } 087 088 public String getCategory(String name) { 089 return get(cat, name); 090 } 091 092 public void setEditor(String name, GBasicCellEditor editor) { 093 editors.put(name, editor); 094 } 095 096 public GBasicCellEditor getEditor(String name) { 097 return editors.get(name); 098 } 099 100 public void setrenderer(String name, GBasicCellRenderer renderer) { 101 renderers.put(name, renderer); 102 } 103 104 public GBasicCellRenderer getrenderer(String name) { 105 return renderers.get(name); 106 } 107 108 public void setVisible(String name, Boolean isVisible) { 109 set(visible, name, isVisible.toString()); 110 } 111 112 /** 113 * returns true if the name didn't setted to be invisible. 114 * visibility option is most usable in property editor 115 */ 116 public boolean isVisible(String name) { 117 return !get(visible, name).equalsIgnoreCase("false"); 118 } 119 120 public void setValid(String name, Boolean isvalid) { 121 set(valid, name, isvalid.toString()); 122 } 123 124 /** 125 * returns true if the name didn't setted to be invalid. 126 * valid option is most usable in property editor 127 * e.g. it is used when the property editor reffers to more than one item 128 * that all of them have the name property but with DIFFERENT values, at 129 * this time the name value should be invalid 130 */ 131 public boolean isvalid(String name) { 132 return !get(valid, name).equalsIgnoreCase("false"); 133 } 134 135 136 HashMap<String, Integer> indices = new HashMap<String, Integer>(); 137 138 /** 139 * set the index of the attribute, the lower index attributes are put before, in the getNames() 140 */ 141 public void setIndex(String name, int atrIndex) { 142 //store indices in a seperate Map from string to int. 143 indices.put(name, atrIndex); 144 // set(index, name, index + ""); 145 } 146 147 /** 148 * returns the index of the given name, if the index didn't set before, it returns Integer.MAX_VALUE 149 */ 150 public int getIndex(String name) { 151 if (!indices.containsKey(name)) { 152 return Integer.MAX_VALUE; 153 } 154 return indices.get(name); 155 } 156 157 public String getNameAt(int i) { 158 //todo: cache the names array for speed & performance 159 return getNames()[i]; 160 } 161 162 /** 163 * gets all visible names sorted by indices and then alphabetically! 164 */ 165 public String[] getNames() { 166 Set<String> s = a.getAttrs().keySet(); 167 // int n = s.size()- descriptions[visible].size(); 168 String[] _ss = new String[s.size()]; 169 int i = 0; 170 for (String _ : s) { 171 if (_ != null && isVisible(_)) 172 _ss[i++] = _; 173 } 174 int n = i; 175 String[] ss = new String[n]; 176 for (i = 0; i < n; i++) { 177 ss[i] = _ss[i]; 178 } 179 String t; 180 Arrays.sort(ss); 181 //sort ss by indices and then alphabetically 182 for (i = 0; i < n; i++) 183 for (int j = 0; j < i; j++) 184 // if index of j is larger, or if indices are equals but they are not in dictionary order 185 if (getIndex(ss[i]) < getIndex(ss[j])) { 186 // if (ss[i].compareTo(ss[j])!=1) { 187 t = ss[i]; 188 ss[i] = ss[j]; 189 ss[j] = t; 190 } 191 return ss; 192 } 193 194 /** 195 * connects this to a 196 * 197 * @param a 198 */ 199 public void setAttribute(AttributeSet a) { 200 this.a = a; 201 } 202 203 public AttributeSet getAttribute() { 204 return a; 205 } 206 }