001: package prefuse.util.ui;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005:
006: import javax.swing.BorderFactory;
007: import javax.swing.Box;
008: import javax.swing.BoxLayout;
009: import javax.swing.JFrame;
010: import javax.swing.JPanel;
011: import javax.swing.event.ChangeEvent;
012: import javax.swing.event.ChangeListener;
013:
014: import prefuse.util.force.Force;
015: import prefuse.util.force.ForceSimulator;
016:
017: /**
018: * Swing component for configuring the parameters of the
019: * Force functions in a given ForceSimulator. Useful for exploring
020: * different parameterizations when crafting a visualization.
021: *
022: * @author <a href="http://jheer.org">jeffrey heer</a>
023: */
024: public class JForcePanel extends JPanel {
025:
026: private ForcePanelChangeListener lstnr = new ForcePanelChangeListener();
027: private ForceSimulator fsim;
028:
029: /**
030: * Create a new JForcePanel
031: * @param fsim the ForceSimulator to configure
032: */
033: public JForcePanel(ForceSimulator fsim) {
034: this .fsim = fsim;
035: this .setBackground(Color.WHITE);
036: initUI();
037: }
038:
039: /**
040: * Initialize the UI.
041: */
042: private void initUI() {
043: this .setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
044: Force[] forces = fsim.getForces();
045: for (int i = 0; i < forces.length; i++) {
046: Force f = forces[i];
047: Box v = new Box(BoxLayout.Y_AXIS);
048: for (int j = 0; j < f.getParameterCount(); j++) {
049: JValueSlider field = createField(f, j);
050: field.addChangeListener(lstnr);
051: v.add(field);
052: }
053: String name = f.getClass().getName();
054: name = name.substring(name.lastIndexOf(".") + 1);
055: v.setBorder(BorderFactory.createTitledBorder(name));
056: this .add(v);
057: }
058: }
059:
060: /**
061: * Create an entry for configuring a single parameter.
062: */
063: private static JValueSlider createField(Force f, int param) {
064: double value = f.getParameter(param);
065: double min = f.getMinValue(param);
066: double max = f.getMaxValue(param);
067: String name = f.getParameterName(param);
068:
069: JValueSlider s = new JValueSlider(name, min, max, value);
070: s.setBackground(Color.WHITE);
071: s.putClientProperty("force", f);
072: s.putClientProperty("param", new Integer(param));
073: s.setPreferredSize(new Dimension(300, 30));
074: s.setMaximumSize(new Dimension(300, 30));
075: return s;
076: }
077:
078: /**
079: * Change listener that updates paramters in response to interaction.
080: */
081: private static class ForcePanelChangeListener implements
082: ChangeListener {
083: public void stateChanged(ChangeEvent e) {
084: JValueSlider s = (JValueSlider) e.getSource();
085: float val = s.getValue().floatValue();
086: Force f = (Force) s.getClientProperty("force");
087: Integer p = (Integer) s.getClientProperty("param");
088: f.setParameter(p.intValue(), val);
089: }
090: } // end of inner class ForcePanelChangeListener
091:
092: /**
093: * Create and displays a new window showing a configuration panel
094: * for the given ForceSimulator.
095: * @param fsim the force simulator
096: * @return a JFrame instance containing a configuration interface
097: * for the force simulator
098: */
099: public static JFrame showForcePanel(ForceSimulator fsim) {
100: JFrame frame = new JFrame("prefuse Force Simulator");
101: frame.setContentPane(new JForcePanel(fsim));
102: frame.pack();
103: frame.setVisible(true);
104: return frame;
105: }
106:
107: } // end of class JForcePanel
|