1 5 package prefuse.demos; 6 7 import java.awt.Insets ; 8 import java.awt.event.ActionEvent ; 9 import java.awt.event.ActionListener ; 10 import java.awt.event.MouseEvent ; 11 import java.awt.geom.Rectangle2D ; 12 import java.util.Iterator ; 13 import java.util.logging.Level ; 14 import java.util.logging.Logger ; 15 16 import javax.swing.AbstractAction ; 17 import javax.swing.BorderFactory ; 18 import javax.swing.JFrame ; 19 20 import prefuse.Constants; 21 import prefuse.Display; 22 import prefuse.Visualization; 23 import prefuse.action.ActionList; 24 import prefuse.action.RepaintAction; 25 import prefuse.action.assignment.ColorAction; 26 import prefuse.action.distortion.Distortion; 27 import prefuse.action.distortion.FisheyeDistortion; 28 import prefuse.action.layout.Layout; 29 import prefuse.controls.AnchorUpdateControl; 30 import prefuse.controls.ControlAdapter; 31 import prefuse.data.Schema; 32 import prefuse.data.Table; 33 import prefuse.render.DefaultRendererFactory; 34 import prefuse.render.LabelRenderer; 35 import prefuse.util.ColorLib; 36 import prefuse.visual.VisualItem; 37 38 50 public class FisheyeMenu extends Display { 51 52 53 public static final String ITEMS = "items"; 54 55 public static final String LABEL = "label"; 56 57 public static final String ACTION = "action"; 58 59 63 protected static final Schema ITEM_SCHEMA = new Schema(); 64 static { 65 ITEM_SCHEMA.addColumn(LABEL, String .class); 66 ITEM_SCHEMA.addColumn(ACTION, ActionListener .class); 67 } 68 69 private Table m_items = ITEM_SCHEMA.instantiate(); 71 private double m_maxHeight = 500; private double m_scale = 7; 74 78 public FisheyeMenu() { 79 super(new Visualization()); 80 m_vis.addTable(ITEMS, m_items); 81 82 LabelRenderer renderer = new LabelRenderer(LABEL); 84 renderer.setHorizontalPadding(0); 85 renderer.setVerticalPadding(1); 86 renderer.setHorizontalAlignment(Constants.LEFT); 87 m_vis.setRendererFactory(new DefaultRendererFactory(renderer)); 88 89 setSize(100,470); 91 setHighQuality(true); 92 setBorder(BorderFactory.createEmptyBorder(10,10,10,5)); 93 addControlListener(new ControlAdapter() { 94 public void itemClicked(VisualItem item, MouseEvent e) { 96 ActionListener al = (ActionListener )item.get(ACTION); 97 al.actionPerformed(new ActionEvent (item, e.getID(), 98 "click", e.getWhen(), e.getModifiers())); 99 } 100 }); 101 102 ColorAction colors = new ColorAction(ITEMS, VisualItem.TEXTCOLOR); 105 colors.setDefaultColor(ColorLib.gray(0)); 106 colors.add("hover()", ColorLib.rgb(255,0,0)); 107 108 ActionList init = new ActionList(); 110 init.add(new VerticalLineLayout(m_maxHeight)); 111 init.add(colors); 112 init.add(new RepaintAction()); 113 m_vis.putAction("init", init); 114 115 ActionList distort = new ActionList(); 117 Distortion feye = new FisheyeDistortion(0,m_scale); 118 distort.add(feye); 119 distort.add(colors); 120 distort.add(new RepaintAction()); 121 m_vis.putAction("distort", distort); 122 123 addControlListener(new AnchorUpdateControl(feye, "distort")); 126 } 127 128 135 public void addMenuItem(String name, ActionListener listener) { 136 int row = m_items.addRow(); 137 m_items.set(row, LABEL, name); 138 m_items.set(row, ACTION, listener); 139 } 140 141 144 public static final void main(String [] argv) { 145 Logger.getLogger("prefuse").setLevel(Level.WARNING); 147 148 FisheyeMenu fm = demo(); 149 150 JFrame f = new JFrame ("p r e f u s e | f i s h e y e"); 152 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 153 f.getContentPane().add(fm); 154 f.pack(); 155 f.setVisible(true); 156 } 157 158 public static FisheyeMenu demo() { 159 FisheyeMenu fm = new FisheyeMenu(); 161 for ( int i=1; i<=72; ++i ) { 162 fm.addMenuItem(String.valueOf(i), new AbstractAction () { 164 public void actionPerformed(ActionEvent e) { 165 System.out.println("clicked item: "+ 166 ((VisualItem)e.getSource()).get(LABEL)); 167 System.out.flush(); 168 } 169 }); 170 } 171 fm.getVisualization().run("init"); 172 return fm; 173 } 174 175 180 public class VerticalLineLayout extends Layout { 181 private double m_maxHeight = 600; 182 183 public VerticalLineLayout(double maxHeight) { 184 m_maxHeight = maxHeight; 185 } 186 187 public void run(double frac) { 188 double w = 0, h = 0; 190 Iterator iter = m_vis.items(); 191 while ( iter.hasNext() ) { 192 VisualItem item = (VisualItem)iter.next(); 193 item.setSize(1.0); 194 h += item.getBounds().getHeight(); 195 } 196 double scale = h > m_maxHeight ? m_maxHeight/h : 1.0; 197 198 Display d = m_vis.getDisplay(0); 199 Insets ins = d.getInsets(); 200 201 h = ins.top; 203 double ih, y=0, x=ins.left; 204 iter = m_vis.items(); 205 while ( iter.hasNext() ) { 206 VisualItem item = (VisualItem)iter.next(); 207 item.setSize(scale); item.setEndSize(scale); 208 Rectangle2D b = item.getBounds(); 209 210 w = Math.max(w, b.getWidth()); 211 ih = b.getHeight(); 212 y = h+(ih/2); 213 setX(item, null, x); 214 setY(item, null, y); 215 h += ih; 216 } 217 218 d.setSize((int)Math.round(2*m_scale*w + ins.left + ins.right), 220 (int)Math.round(h + ins.bottom)); 221 } 222 } 224 } | Popular Tags |