Search in sources :

Example 1 with TunableFieldPanel

use of org.cytoscape.work.swing.AbstractGUITunableHandler.TunableFieldPanel in project cytoscape-impl by cytoscape.

the class GUIDefaults method updateFieldPanel.

private static void updateFieldPanel(final JPanel p, final Component label, final Component control, final boolean horizontalForm) {
    if (p instanceof TunableFieldPanel) {
        ((TunableFieldPanel) p).setControl(control);
        if (label instanceof JLabel)
            ((TunableFieldPanel) p).setLabel((JLabel) label);
        else if (label instanceof JTextArea)
            ((TunableFieldPanel) p).setMultiLineLabel((JTextArea) label);
        return;
    }
    p.removeAll();
    final GroupLayout layout = new GroupLayout(p);
    p.setLayout(layout);
    layout.setAutoCreateContainerGaps(false);
    layout.setAutoCreateGaps(true);
    final Alignment vAlign = control instanceof JPanel || control instanceof JScrollPane ? Alignment.LEADING : Alignment.CENTER;
    if (horizontalForm) {
        p.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
        layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(label, PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE).addComponent(control, PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE));
        layout.setVerticalGroup(layout.createParallelGroup(vAlign, false).addComponent(label).addComponent(control));
    } else {
        p.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
        int w = Math.max(label.getPreferredSize().width, control.getPreferredSize().width);
        // So the label and control are centered
        int gap = w - control.getPreferredSize().width;
        layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(Alignment.TRAILING, true).addComponent(label, w, w, Short.MAX_VALUE)).addGroup(layout.createParallelGroup(Alignment.LEADING, true).addGroup(layout.createSequentialGroup().addComponent(control, PREFERRED_SIZE, DEFAULT_SIZE, PREFERRED_SIZE).addGap(gap, gap, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(vAlign, false).addComponent(label).addComponent(control));
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) Alignment(javax.swing.GroupLayout.Alignment) JTextArea(javax.swing.JTextArea) TunableFieldPanel(org.cytoscape.work.swing.AbstractGUITunableHandler.TunableFieldPanel) GroupLayout(javax.swing.GroupLayout) JLabel(javax.swing.JLabel)

Example 2 with TunableFieldPanel

use of org.cytoscape.work.swing.AbstractGUITunableHandler.TunableFieldPanel in project cytoscape-impl by cytoscape.

the class JPanelTunableMutator method updateTunableFieldPanelMargins.

private void updateTunableFieldPanelMargins() {
    synchronized (lock) {
        if (updatingMargins || handlers == null)
            return;
        updatingMargins = true;
        int maxLeftWidth = 0, maxRightWidth = 0;
        boolean updateMargins = false;
        try {
            // 1st Pass: Get max left/right margin values if vertical form
            for (GUITunableHandler gh : handlers) {
                if (gh instanceof AbstractGUITunableHandler && !((AbstractGUITunableHandler) gh).isHorizontal()) {
                    final JPanel p = gh.getJPanel();
                    if (p instanceof TunableFieldPanel) {
                        updateMargins = true;
                        final TunableFieldPanel tfp = (TunableFieldPanel) p;
                        final JComponent label = tfp.getLabel() != null ? tfp.getLabel() : tfp.getMultiLineLabel();
                        final Component control = tfp.getControl();
                        if (label != null)
                            maxLeftWidth = Math.max(maxLeftWidth, label.getPreferredSize().width);
                        if (control != null)
                            maxRightWidth = Math.max(maxRightWidth, control.getPreferredSize().width);
                    }
                }
            }
            if (updateMargins) {
                // 2nd Pass: Use empty borders to properly align all fields and labels
                for (GUITunableHandler gh : handlers) {
                    final JPanel p = gh.getJPanel();
                    if (p instanceof TunableFieldPanel) {
                        // Update panel's left/right margin by setting an empty border
                        final TunableFieldPanel tfp = (TunableFieldPanel) p;
                        final JComponent label = tfp.getLabel() != null ? tfp.getLabel() : tfp.getMultiLineLabel();
                        final Component control = tfp.getControl();
                        if (control != null)
                            control.removeComponentListener(controlComponentListener);
                        int left = label == null ? 0 : maxLeftWidth - label.getPreferredSize().width;
                        int right = control == null ? 0 : maxRightWidth - control.getPreferredSize().width;
                        // TODO Do not set top/bottom
                        tfp.setBorder(BorderFactory.createEmptyBorder(2, left, 2, right));
                        // Also notify all dependents
                        gh.notifyDependents();
                        if (control != null)
                            control.addComponentListener(controlComponentListener);
                    }
                }
                repackEnclosingDialog(panelMap.get(handlers));
            }
        } finally {
            updatingMargins = false;
        }
    }
}
Also used : JPanel(javax.swing.JPanel) TunableFieldPanel(org.cytoscape.work.swing.AbstractGUITunableHandler.TunableFieldPanel) AbstractGUITunableHandler(org.cytoscape.work.swing.AbstractGUITunableHandler) GUITunableHandler(org.cytoscape.work.swing.GUITunableHandler) JComponent(javax.swing.JComponent) JComponent(javax.swing.JComponent) Component(java.awt.Component) AbstractGUITunableHandler(org.cytoscape.work.swing.AbstractGUITunableHandler)

Aggregations

JPanel (javax.swing.JPanel)2 TunableFieldPanel (org.cytoscape.work.swing.AbstractGUITunableHandler.TunableFieldPanel)2 Component (java.awt.Component)1 GroupLayout (javax.swing.GroupLayout)1 Alignment (javax.swing.GroupLayout.Alignment)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1 JScrollPane (javax.swing.JScrollPane)1 JTextArea (javax.swing.JTextArea)1 AbstractGUITunableHandler (org.cytoscape.work.swing.AbstractGUITunableHandler)1 GUITunableHandler (org.cytoscape.work.swing.GUITunableHandler)1