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.components.gpropertyeditor.utils; 005 006 import graphlab.platform.attribute.NotifiableAttributeSet; 007 import graphlab.platform.attribute.NotifiableAttributeSetImpl; 008 import graphlab.ui.AttributeSetView; 009 import graphlab.ui.PortableNotifiableAttributeSetImpl; 010 import graphlab.ui.components.gpropertyeditor.GPropertyEditor; 011 import graphlab.ui.components.utils.GAttrFrame; 012 013 import javax.swing.*; 014 import javax.swing.event.ListSelectionEvent; 015 import javax.swing.event.ListSelectionListener; 016 import java.lang.reflect.*; 017 import java.util.Arrays; 018 import java.util.Map; 019 import java.util.Set; 020 021 /** 022 * @author Azin Azadi 023 */ 024 public class ObjectViewer implements ListSelectionListener { 025 GPropertyEditor ped; 026 027 public static ObjectViewer showObject(Object o) { 028 ObjectViewer oo = new ObjectViewer(); 029 oo.Connect(o); 030 return oo; 031 } 032 033 public static void main(String[] args) { 034 035 } 036 037 public void Connect(Object obj) { 038 PortableNotifiableAttributeSetImpl a = Object2NotifiableAttributeSet(obj); 039 GAttrFrame f = GAttrFrame.showEditDialog(a, false); 040 ped = f.getPropertyEditor(); 041 JTable t = ped.getTable(); 042 t.getSelectionModel().addListSelectionListener(this); 043 } 044 045 boolean b = true; 046 047 /** 048 * calls when user click on one of the properties 049 */ 050 public void valueChanged(ListSelectionEvent e) { 051 if (b) { 052 ListSelectionModel ta = (ListSelectionModel) e.getSource(); 053 int selRow = ta.getMinSelectionIndex(); 054 showObject(ped.getTable().getValueAt(selRow, 1)); 055 } 056 b = !b; 057 } 058 059 private int index(int m) { 060 if (Modifier.isFinal(m)) 061 return 2000; 062 if (Modifier.isStatic(m)) 063 return 900; 064 if (Modifier.isProtected(m)) 065 return 600; 066 if (Modifier.isPrivate(m)) 067 return 500; 068 if (Modifier.isPublic(m)) 069 return 400; 070 return 2000; 071 } 072 073 private int classP(Member m, Object o) { 074 try { 075 if (m.getDeclaringClass() != o.getClass()) 076 return 50000; 077 } 078 catch (Exception e) { 079 return 500000; 080 } 081 return 0; 082 } 083 084 private PortableNotifiableAttributeSetImpl Object2NotifiableAttributeSet(Object obj) { 085 086 PortableNotifiableAttributeSetImpl a = new PortableNotifiableAttributeSetImpl(); 087 AttributeSetView x = a.getView(); 088 for (Field f : obj.getClass().getFields()) { 089 int mod = f.getModifiers(); 090 try { 091 Object o = f.get(obj); 092 String name = f.getName(); 093 a.put(name, o); 094 x.setIndex(name, index(mod) + classP(f, o)); 095 // if(!Modifier.isFinal(mod)) 096 097 } catch (IllegalAccessException e) { 098 e.printStackTrace(); 099 } 100 } 101 for (Method m : obj.getClass().getMethods()) { 102 if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) { 103 try { 104 Object o = m.invoke(obj, new Object[]{}); 105 int mod = m.getModifiers(); 106 String name = m.getName(); 107 name = name.substring(3); 108 a.put(name, o); 109 x.setIndex(name, index(mod) + classP(m, o) + 1000); 110 } catch (IllegalAccessException e) { 111 e.printStackTrace(); 112 } catch (InvocationTargetException e) { 113 System.err.println(obj.getClass() + "." + m.getName() + " invoke exception"); 114 // e.printStackTrace(); 115 } 116 } 117 } 118 return a; 119 } 120 121 public NotifiableAttributeSetImpl getSortedNotifiableAttributeSet(NotifiableAttributeSet in) { 122 Map<String, Object> atr = in.getAttrs(); 123 Set<String> keys = atr.keySet(); 124 Object[] o = keys.toArray(); 125 String k[] = new String[keys.size()]; 126 NotifiableAttributeSetImpl ret = new NotifiableAttributeSetImpl(); 127 for (int i = 0; i < k.length; i++) { 128 k[i] = (String) o[i]; 129 } 130 Arrays.sort(k); 131 for (String aK : k) { 132 ret.put(aK, atr.get(aK)); 133 } 134 return ret; 135 } 136 }