KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > JForcePanel


1 package prefuse.util.ui;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Dimension JavaDoc;
5
6 import javax.swing.BorderFactory JavaDoc;
7 import javax.swing.Box JavaDoc;
8 import javax.swing.BoxLayout JavaDoc;
9 import javax.swing.JFrame JavaDoc;
10 import javax.swing.JPanel JavaDoc;
11 import javax.swing.event.ChangeEvent JavaDoc;
12 import javax.swing.event.ChangeListener JavaDoc;
13
14 import prefuse.util.force.Force;
15 import prefuse.util.force.ForceSimulator;
16
17 /**
18  * Swing component for configuring the parameters of the
19  * Force functions in a given ForceSimulator. Useful for exploring
20  * different parameterizations when crafting a visualization.
21  *
22  * @author <a HREF="http://jheer.org">jeffrey heer</a>
23  */

24 public class JForcePanel extends JPanel JavaDoc {
25     
26     private ForcePanelChangeListener lstnr = new ForcePanelChangeListener();
27     private ForceSimulator fsim;
28     
29     /**
30      * Create a new JForcePanel
31      * @param fsim the ForceSimulator to configure
32      */

33     public JForcePanel(ForceSimulator fsim) {
34         this.fsim = fsim;
35         this.setBackground(Color.WHITE);
36         initUI();
37     }
38     
39     /**
40      * Initialize the UI.
41      */

42     private void initUI() {
43         this.setLayout(new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS));
44         Force[] forces = fsim.getForces();
45         for ( int i=0; i<forces.length; i++ ) {
46             Force f = forces[i];
47             Box JavaDoc v = new Box JavaDoc(BoxLayout.Y_AXIS);
48             for ( int j=0; j<f.getParameterCount(); j++ ) {
49                 JValueSlider field = createField(f,j);
50                 field.addChangeListener(lstnr);
51                 v.add(field);
52             }
53             String JavaDoc name = f.getClass().getName();
54             name = name.substring(name.lastIndexOf(".")+1);
55             v.setBorder(BorderFactory.createTitledBorder(name));
56             this.add(v);
57         }
58     }
59     
60     /**
61      * Create an entry for configuring a single parameter.
62      */

63     private static JValueSlider createField(Force f, int param) {
64         double value = f.getParameter(param);
65         double min = f.getMinValue(param);
66         double max = f.getMaxValue(param);
67         String JavaDoc name = f.getParameterName(param);
68         
69         JValueSlider s = new JValueSlider(name,min,max,value);
70         s.setBackground(Color.WHITE);
71         s.putClientProperty("force", f);
72         s.putClientProperty("param", new Integer JavaDoc(param));
73         s.setPreferredSize(new Dimension JavaDoc(300,30));
74         s.setMaximumSize(new Dimension JavaDoc(300,30));
75         return s;
76     }
77     
78     /**
79      * Change listener that updates paramters in response to interaction.
80      */

81     private static class ForcePanelChangeListener implements ChangeListener JavaDoc {
82         public void stateChanged(ChangeEvent JavaDoc e) {
83             JValueSlider s = (JValueSlider)e.getSource();
84             float val = s.getValue().floatValue();
85             Force f = (Force)s.getClientProperty("force");
86             Integer JavaDoc p = (Integer JavaDoc)s.getClientProperty("param");
87             f.setParameter(p.intValue(), val);
88         }
89     } // end of inner class ForcePanelChangeListener
90

91     /**
92      * Create and displays a new window showing a configuration panel
93      * for the given ForceSimulator.
94      * @param fsim the force simulator
95      * @return a JFrame instance containing a configuration interface
96      * for the force simulator
97      */

98     public static JFrame JavaDoc showForcePanel(ForceSimulator fsim) {
99         JFrame JavaDoc frame = new JFrame JavaDoc("prefuse Force Simulator");
100         frame.setContentPane(new JForcePanel(fsim));
101         frame.pack();
102         frame.setVisible(true);
103         return frame;
104     }
105     
106 } // end of class JForcePanel
107
Popular Tags