Example usage for java.awt Component isVisible

List of usage examples for java.awt Component isVisible

Introduction

In this page you can find the example usage for java.awt Component isVisible.

Prototype

@Transient
public boolean isVisible() 

Source Link

Document

Determines whether this component should be visible when its parent is visible.

Usage

From source file:mulavito.gui.components.GraphPanel.java

/**
 * Auto-resize and auto-rearrange the {@link LayerViewer}s such that all fit
 * best into the window.//from   ww  w . ja  v a  2  s  .co  m
 * <p/>
 * Mathematically spoken: The minimum of width and height of the
 * {@link LayerViewer}s is maximized.
 */
private void arrangeViewers() {
    // Get width from the parental JScrollPane.
    int width = contentPane.getWidth();
    int height = contentPane.getHeight();

    int numLayers = 0;
    for (Component cmp : contentPane.getComponents())
        if (cmp.isVisible())
            numLayers++;

    if (numLayers == 0)
        return;

    int newSize = 0;
    int colLayout = numLayers;
    int rowLayout = 1;

    for (int cols = numLayers; cols > 0; cols--) {
        int rows = (int) Math.ceil((double) numLayers / (double) cols);

        // The divisions are automatically floored, such that
        // cols * size <= width and rows * size <= height hold.
        int size = Math.min(width / cols, height / rows);

        if (size > newSize) {
            newSize = size;
            colLayout = cols;
            rowLayout = rows;
        }
    }

    // assign layout information
    layout.setRows(rowLayout);
    layout.setColumns(colLayout);
}

From source file:ColumnLayout.java

/**
 * The method that actually performs the layout. Called by the Container
 */// www  .  j av  a  2s .c  om
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    Dimension parent_size = parent.getSize();
    Component kid;
    int nkids = parent.getComponentCount();
    int x0 = insets.left + margin_width; // The base X position
    int x;
    int y = insets.top + margin_height; // Start at the top of the column

    for (int i = 0; i < nkids; i++) { // Loop through the kids
        kid = parent.getComponent(i); // Get the kid
        if (!kid.isVisible())
            continue; // Skip hidden ones
        Dimension pref = kid.getPreferredSize(); // How big is it?
        switch (alignment) { // Compute X coordinate
        default:
        case LEFT:
            x = x0;
            break;
        case CENTER:
            x = (parent_size.width - pref.width) / 2;
            break;
        case RIGHT:
            x = parent_size.width - insets.right - margin_width - pref.width;
            break;
        }
        // Set the size and position of this kid
        kid.setBounds(x, y, pref.width, pref.height);
        y += pref.height + spacing; // Get Y position of the next one
    }
}

From source file:edu.ku.brc.specify.datamodel.busrules.AgentBusRules.java

/**
 * Clears the values and hides somAttachmentOwnere UI depending on what type is selected
 * @param cbx the type cbx//from  w  w  w.j  ava  2s  . c om
 */
protected void fixUpTypeCBX(final JComboBox<?> cbx) {
    if (formViewObj != null) {
        Agent agent = (Agent) formViewObj.getDataObj();
        if (agent != null) {
            final Component addrSubView = formViewObj.getCompById("9");

            boolean isVisible = addrSubView.isVisible();

            byte agentType = (byte) cbx.getSelectedIndex();
            if (agentType != Agent.PERSON) {
                agent.setMiddleInitial(null);
                agent.setFirstName(null);
                agent.setTitle(null);

                boolean enable = agentType == Agent.ORG;
                addrSubView.setVisible(enable);

            } else {
                if (!isVisible) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            Component topComp = UIHelper.getWindow(addrSubView);
                            Component topMost = UIRegistry.getTopWindow();
                            if (topComp != topMost && topComp != null) {
                                ((Window) topComp).pack();
                            }
                        }
                    });
                }
                addrSubView.setVisible(true);
            }
            agent.setAgentType(agentType);
            fixUpFormForAgentType(agent, true);
        }
    }
}

From source file:FunLayout.java

public void layoutContainer(Container con) {
    int i, count, deltax, deltay, move;
    Dimension conSize;//from w  ww . j a v a 2 s  .co m
    Rectangle rect;
    Component comp;

    conSize = con.getSize();
    if (_prevContainerSize == null) {
        _prevContainerSize = conSize;
        return;
    }
    deltax = conSize.width - _prevContainerSize.width;
    deltay = conSize.height - _prevContainerSize.height;
    _prevContainerSize = conSize;
    count = con.countComponents();
    for (i = 0; i < count; i++) {
        comp = con.getComponent(i);
        if (!comp.isVisible())
            continue;
        move = _getMove(comp);
        if (move == 0)
            continue;
        rect = comp.getBounds();
        if (_negSized.containsKey(comp)) {
            // the component is really at a negative size
            rect = (Rectangle) _negSized.get(comp);
            _negSized.remove(comp);
        }
        if ((move & MOVES_RIGHT) > 0)
            rect.x += deltax;
        else if ((move & WIDTH_CHANGES) > 0)
            rect.width += deltax;
        if ((move & MOVES_DOWN) > 0)
            rect.y += deltay;
        else if ((move & HEIGHT_CHANGES) > 0)
            rect.height += deltay;
        // if a components size becomes negative, we track it since the AWT
        // does not allow components to have a size < (0, 0)
        if (rect.width < 0 || rect.height < 0)
            _negSized.put(comp, rect);
        comp.setBounds(rect.x, rect.y, rect.width, rect.height);
    }
}

From source file:org.opendatakit.briefcase.ui.MainBriefcaseWindow.java

/**
 * Create the application.//from   www  .j a  va2 s. c  o m
 */
public MainBriefcaseWindow() {
    frame = new JFrame();
    frame.setBounds(100, 100, 680, 595);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.addWindowListener(new WindowListener() {
        @Override
        public void windowOpened(WindowEvent e) {
        }

        @Override
        public void windowClosing(WindowEvent e) {
        }

        @Override
        public void windowClosed(WindowEvent e) {
        }

        @Override
        public void windowIconified(WindowEvent e) {
        }

        @Override
        public void windowDeiconified(WindowEvent e) {
        }

        @Override
        public void windowActivated(WindowEvent e) {
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
        }
    });

    JLabel lblBriefcaseDirectory = new JLabel(MessageStrings.BRIEFCASE_STORAGE_LOCATION);

    txtBriefcaseDir = new JTextField();
    txtBriefcaseDir.setFocusable(false);
    txtBriefcaseDir.setEditable(false);
    txtBriefcaseDir.setColumns(10);

    btnChoose = new JButton("Change...");
    btnChoose.addActionListener(new FolderActionListener());

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
    groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
            .createSequentialGroup().addContainerGap()
            .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(tabbedPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 628,
                            Short.MAX_VALUE)
                    .addGroup(groupLayout.createSequentialGroup().addComponent(lblBriefcaseDirectory).addGap(18)
                            .addComponent(txtBriefcaseDir, GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                            .addGap(18).addComponent(btnChoose)))
            .addContainerGap()));
    groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup().addContainerGap()
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                            .addComponent(txtBriefcaseDir, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                    GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnChoose).addComponent(lblBriefcaseDirectory))
                    .addGap(33).addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                    .addContainerGap()));

    gatherPanel = new PullTransferPanel(transferTerminationFuture);
    tabbedPane.addTab(PullTransferPanel.TAB_NAME, null, gatherPanel, null);
    PullTransferPanel.TAB_POSITION = 0;

    uploadPanel = new PushTransferPanel(transferTerminationFuture);
    tabbedPane.addTab(PushTransferPanel.TAB_NAME, null, uploadPanel, null);
    PushTransferPanel.TAB_POSITION = 1;

    exportPanel = new ExportPanel(exportTerminationFuture);
    tabbedPane.addTab(ExportPanel.TAB_NAME, null, exportPanel, null);
    frame.getContentPane().setLayout(groupLayout);
    ExportPanel.TAB_POSITION = 2;

    frame.addWindowListener(this);
    setFullUIEnabled(false);

    frame.setFocusTraversalPolicy(new FocusTraversalPolicy() {

        @Override
        public Component getComponentAfter(Container arg0, Component arg1) {
            ArrayList<Component> componentOrdering = new ArrayList<Component>();
            for (;;) {
                int nextPanel = PullTransferPanel.TAB_POSITION;
                componentOrdering.clear();
                componentOrdering.add(txtBriefcaseDir);
                componentOrdering.add(btnChoose);
                componentOrdering.add(tabbedPane);
                int idx = tabbedPane.getSelectedIndex();
                if (idx == PullTransferPanel.TAB_POSITION) {
                    componentOrdering.addAll(gatherPanel.getTraversalOrdering());
                    nextPanel = PushTransferPanel.TAB_POSITION;
                } else if (idx == PushTransferPanel.TAB_POSITION) {
                    componentOrdering.addAll(uploadPanel.getTraversalOrdering());
                    nextPanel = ExportPanel.TAB_POSITION;
                } else if (idx == ExportPanel.TAB_POSITION) {
                    componentOrdering.addAll(exportPanel.getTraversalOrdering());
                    nextPanel = PullTransferPanel.TAB_POSITION;
                }
                componentOrdering.add(btnChoose);
                boolean found = false;
                for (int i = 0; i < componentOrdering.size() - 1; ++i) {
                    if (found || arg1 == componentOrdering.get(i)) {
                        found = true;
                        Component comp = componentOrdering.get(i + 1);
                        if (comp == tabbedPane) {
                            return comp;
                        }
                        if (comp.isVisible() && comp.isEnabled()
                                && (!(comp instanceof JTextField) || ((JTextField) comp).isEditable())) {
                            return comp;
                        }
                    }
                }
                if (!found) {
                    return componentOrdering.get(0);
                }
                tabbedPane.setSelectedIndex(nextPanel);
            }
        }

        @Override
        public Component getComponentBefore(Container arg0, Component arg1) {
            ArrayList<Component> componentOrdering = new ArrayList<Component>();
            for (;;) {
                int nextPanel = PullTransferPanel.TAB_POSITION;
                componentOrdering.clear();
                componentOrdering.add(txtBriefcaseDir);
                componentOrdering.add(btnChoose);
                componentOrdering.add(tabbedPane);
                int idx = tabbedPane.getSelectedIndex();
                if (idx == PullTransferPanel.TAB_POSITION) {
                    componentOrdering.addAll(gatherPanel.getTraversalOrdering());
                    nextPanel = ExportPanel.TAB_POSITION;
                } else if (idx == PushTransferPanel.TAB_POSITION) {
                    componentOrdering.addAll(uploadPanel.getTraversalOrdering());
                    nextPanel = PullTransferPanel.TAB_POSITION;
                } else if (idx == ExportPanel.TAB_POSITION) {
                    componentOrdering.addAll(exportPanel.getTraversalOrdering());
                    nextPanel = PushTransferPanel.TAB_POSITION;
                }
                componentOrdering.add(btnChoose);
                boolean found = false;
                for (int i = componentOrdering.size() - 1; i > 0; --i) {
                    if (found || arg1 == componentOrdering.get(i)) {
                        found = true;
                        Component comp = componentOrdering.get(i - 1);
                        if (comp == tabbedPane) {
                            return comp;
                        }
                        if (comp.isVisible() && comp.isEnabled()
                                && (!(comp instanceof JTextField) || ((JTextField) comp).isEditable())) {
                            return comp;
                        }
                    }
                }
                if (!found) {
                    return componentOrdering.get(componentOrdering.size() - 1);
                }
                tabbedPane.setSelectedIndex(nextPanel);
            }
        }

        @Override
        public Component getDefaultComponent(Container arg0) {
            return btnChoose;
        }

        @Override
        public Component getFirstComponent(Container arg0) {
            return btnChoose;
        }

        @Override
        public Component getLastComponent(Container arg0) {
            return tabbedPane;
        }
    });
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ????????// ww  w  .  j  a va2s.co m
 */
public void toggle() {
    for (Component child : list) {
        child.setVisible(!child.isVisible());
    }
}

From source file:org.kineticsystem.commons.layout.TetrisLayout.java

/**
 * Calculate the preferred size dimensions for the specified panel given
 * the components in the specified parent container. Calculates incremental
 * column width rates and row heights, dimensions used during component
 * laying out. /*from  w w w  .j  a v a 2  s.  c  o m*/
 * @param parent The component to be laid out.
 */
private void setup(Container parent) {

    container = parent;
    Insets insets = parent.getInsets();

    // FIRST PHASE: DEFAULT EVALUATION OF COLUMN AND ROW SIZES.

    // Evaluate column widths.

    for (int c = 0; c < cBars.length; c++) {
        if (cBars[c].getSize() == Bar.COMPONENT_PREFERRED_SIZE) {
            for (int r = 0; r < rBars.length; r++) {
                Component comp = componentAt(r, c);
                Cell cell = (Cell) constraintsMap.get(comp);
                if (comp.isVisible() && ((cell.getCols() + cell.getCol() - 1) == c)) {
                    Size cd = (Size) componentSizes.get(comp);
                    cBars[c].setPreferredSize(Math.max(cd.getPreferredWidth(), cBars[c].getPreferredSize()));
                    cBars[c].setMinimumSize(Math.max(cd.getMinimumWidth(), cBars[c].getMinimumSize()));
                    cBars[c].setMaximumSize(Double.MAX_VALUE);
                }
            }
        } else {
            cBars[c].setPreferredSize(cBars[c].getSize());
        }

        // Check all components containing column c and store their sizes.

        for (Map.Entry<Component, Cell> entry : constraintsMap.entrySet()) {

            Component comp = (Component) entry.getKey();
            Cell cell = (Cell) entry.getValue();
            if (((cell.getCol() + cell.getCols()) >= c) && (cell.getCol() <= c)) {
                Size d = (Size) componentSizes.get(comp);
                d.setPreferredWidth(d.getPreferredWidth() - cBars[c].getPreferredSize());
                d.setMinimumWidth(d.getMinimumWidth() - cBars[c].getMinimumSize());
            }
        }
    }

    // Evaluate row heights.

    for (int r = 0; r < rBars.length; r++) {
        if (rBars[r].getSize() == Bar.COMPONENT_PREFERRED_SIZE) {
            for (int c = 0; c < cBars.length; c++) {
                Component comp = componentAt(r, c);
                Cell cell = (Cell) constraintsMap.get(comp);
                if (comp.isVisible() && ((cell.getRows() + cell.getRow() - 1) == r)) {
                    Size cd = (Size) componentSizes.get(comp);
                    rBars[r].setPreferredSize(Math.max(cd.getPreferredHeight(), rBars[r].getPreferredSize()));
                    rBars[r].setMinimumSize(Math.max(cd.getMinimumHeight(), rBars[r].getMinimumSize()));
                    rBars[r].setMaximumSize(Double.MAX_VALUE);
                }
            }
        } else {
            rBars[r].setWeight(rBars[r].getSize());
        }

        // Check all components containing row r and store their sizes.

        for (Map.Entry<Component, Cell> entry : constraintsMap.entrySet()) {

            Component comp = (Component) entry.getKey();
            Cell cell = (Cell) entry.getValue();
            if (((cell.getRow() + cell.getRows()) >= r) && (cell.getRow() <= r)) {
                Size d = (Size) componentSizes.get(comp);
                d.setPreferredHeight(d.getPreferredHeight() - rBars[r].getPreferredSize());
                d.setMinimumHeight(d.getMinimumHeight() - rBars[r].getMinimumSize());
            }
        }
    }

    // SECOND PHASE: OVERRIDE DEFAULT EVALUATION OF COLUMN AND ROW SIZES.

    /*
     * Override previous evaluation of column and row sizes using
     * connectors.
     */

    initBarConnectors(cBars, cGaps);
    initBarConnectors(rBars, rGaps);

    // Evaluates container widths and heights.

    for (int i = 0; i < cBars.length; i++) {
        minGapFreeWidth += cBars[i].getMinimumSize();
        maxGapFreeWidth += cBars[i].getMaximumSize();
        preferredGapFreeWidth += cBars[i].getPreferredSize();
    }
    for (int i = 0; i < rBars.length; i++) {
        minGapFreeHeight += rBars[i].getMinimumSize();
        maxGapFreeHeight += rBars[i].getMaximumSize();
        preferredGapFreeHeight += rBars[i].getPreferredSize();
    }

    // Adds insets and gaps.

    initGaps(cGaps);
    initGaps(rGaps);

    double preferredWidth = preferredGapFreeWidth + insets.left + insets.right
            + cGaps[cBars.length].getIncSize();
    double preferredHeight = preferredGapFreeHeight + insets.top + insets.bottom
            + rGaps[rBars.length].getIncSize();

    // Sets preferred container size.

    containerSize = new Dimension((int) Math.round(preferredWidth), (int) Math.round(preferredHeight));

    // THIRD PHASE: EVALUATE ROW AND COLUMN SIZE FUNCTIONS.

    initSizes(cSizes, cBars, preferredGapFreeWidth);
    initSizes(rSizes, rBars, preferredGapFreeHeight);
}

From source file:edu.ku.brc.specify.datamodel.busrules.AgentBusRules.java

@Override
public void afterFillForm(final Object dataObjArg) {
    Object dataObj = dataObjArg;//from   w w  w  .ja  v  a 2  s.  c o m
    if (!(viewable instanceof FormViewObj) || !(dataObj instanceof Agent)) {
        if (dataObj == null) {
            Component agentVarSubView = formViewObj.getCompById("10");
            if (agentVarSubView != null) {
                agentVarSubView.setVisible(false);
            }

            Component groupPersonSubForm = formViewObj.getCompById("31");
            if (groupPersonSubForm != null) {
                groupPersonSubForm.setVisible(false);
            }

            Component addrSubView = formViewObj.getCompById("9");
            if (addrSubView != null) {
                addrSubView.setVisible(false);
            }
        }
        return;
    }

    Agent agent = (Agent) dataObj;
    Byte agentType = agent.getAgentType();

    fixUpFormForAgentType(agent, true);

    if (typeComp instanceof ValComboBox) {
        ValComboBox typeCBX = (ValComboBox) typeComp;
        if (typeCBX != null) {
            ignoreSet = true;
            typeCBX.getComboBox().setSelectedIndex(agentType == null ? Agent.PERSON : agentType);
            ignoreSet = false;
            fixUpTypeCBX(typeCBX.getComboBox());
        }

    } else {
        JTextField typeTxt = (JTextField) typeComp;
        if (typeTxt != null) {
            typeTxt.setText(typeTitles[agentType]);
        }
    }

    boolean shouldBeVisible = agentType == Agent.PERSON || agentType == Agent.ORG;
    final Component addrSubView = formViewObj.getCompById("9");
    if (addrSubView != null) {
        boolean isVisible = addrSubView.isVisible();
        if (!isVisible != shouldBeVisible) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Component topComp = UIHelper.getWindow(addrSubView);
                    Component topMost = UIRegistry.getTopWindow();
                    if (topComp != topMost && topComp != null) {
                        ((Window) topComp).pack();
                    }
                }
            });
        }
        addrSubView.setVisible(shouldBeVisible);
    }
}

From source file:fxts.stations.ui.SideLayout.java

/**
 * Fills in an instance of <code>SideLayoutInfo</code> for the
 * current set of managed children. This requires three passes through the
 * set of children:/*from   w w  w .  j ava2s  .  co m*/
 * <ol>
 * <li>Figure out the dimensions of the layout grid.
 * <li>Determine which cells the components occupy.
 * <li>Distribute the weights and min sizes amoung the rows/columns.
 * </ol>
 * This also caches the minsizes for all the children when they are
 * first encountered (so subsequent loops don't need to ask again).
 *
 * @param aParent   the layout container
 * @param aSizeflag either <code>PREFERREDSIZE</code> or <code>MINSIZE</code>
 *
 * @return the <code>SideLayoutInfo</code> for the set of children
 */
protected SideLayoutInfo getLayoutInfo(Container aParent, int aSizeflag) {
    synchronized (aParent.getTreeLock()) {
        SideLayoutInfo r = new SideLayoutInfo();
        Component comp;
        SideConstraints constraints;
        Dimension d;
        Component[] components = aParent.getComponents();
        int compindex;
        int i;
        int j;
        int k;
        int px;
        int py;
        int pixels_diff;
        int nextSize;
        int curX, curY, curWidth, curHeight, curRow, curCol;
        double weight_diff;
        double weight;
        double start;
        double size;
        int[] xMax;
        int[] yMax;

        /*
        * Pass #1
        *
        * Figure out the dimensions of the layout grid (use a value of 1 for
        * zero or negative widths and heights).
        */

        r.width = r.height = 0;
        curRow = curCol = -1;
        xMax = new int[MAXGRIDSIZE];
        yMax = new int[MAXGRIDSIZE];
        for (compindex = 0; compindex < components.length; compindex++) {
            comp = components[compindex];
            if (!comp.isVisible()) {
                continue;
            }
            constraints = lookupConstraints(comp);
            curX = constraints.gridx;
            curY = constraints.gridy;
            curWidth = constraints.gridwidth;
            if (curWidth <= 0) {
                curWidth = 1;
            }
            curHeight = constraints.gridheight;
            if (curHeight <= 0) {
                curHeight = 1;
            }

            /* If x or y is negative, then use relative positioning: */
            if (curX < 0 && curY < 0) {
                if (curRow >= 0) {
                    curY = curRow;
                } else if (curCol >= 0) {
                    curX = curCol;
                } else {
                    curY = 0;
                }
            }
            if (curX < 0) {
                px = 0;
                for (i = curY; i < curY + curHeight; i++) {
                    px = Math.max(px, xMax[i]);
                }
                curX = px - curX - 1;
                if (curX < 0) {
                    curX = 0;
                }
            } else if (curY < 0) {
                py = 0;
                for (i = curX; i < curX + curWidth; i++) {
                    py = Math.max(py, yMax[i]);
                }
                curY = py - curY - 1;
                if (curY < 0) {
                    curY = 0;
                }
            }

            /* Adjust the grid width and height */
            for (px = curX + curWidth; r.width < px; r.width++) {
            }
            for (py = curY + curHeight; r.height < py; r.height++) {
            }

            /* Adjust the xMax and yMax arrays */
            for (i = curX; i < curX + curWidth; i++) {
                yMax[i] = py;
            }
            for (i = curY; i < curY + curHeight; i++) {
                xMax[i] = px;
            }

            /* Cache the current slave's size. */
            if (aSizeflag == PREFERREDSIZE) {
                d = comp.getPreferredSize();
            } else {
                d = comp.getMinimumSize();
            }
            constraints.minWidth = d.width;
            //constraints.setMinWidth(d.width);
            constraints.minHeight = d.height;

            /* Zero width and height must mean that this is the last item (or
            * else something is wrong). */
            if (constraints.gridheight == 0 && constraints.gridwidth == 0) {
                curRow = curCol = -1;
            }

            /* Zero width starts a new row */
            if (constraints.gridheight == 0 && curRow < 0) {
                curCol = curX + curWidth;
            }

            /* Zero height starts a new column */
            else if (constraints.gridwidth == 0 && curCol < 0) {
                curRow = curY + curHeight;
            }
        }

        /*
        * Apply minimum row/column dimensions
        */
        if (mColumnWidths != null && r.width < mColumnWidths.length) {
            r.width = mColumnWidths.length;
        }
        if (mRowHeights != null && r.height < mRowHeights.length) {
            r.height = mRowHeights.length;
        }

        /*
        * Pass #2
        *
        * Negative values for gridX are filled in with the current x value.
        * Negative values for gridY are filled in with the current y value.
        * Negative or zero values for gridWidth and gridHeight end the current
        *  row or column, respectively.
        */

        curRow = curCol = -1;
        xMax = new int[MAXGRIDSIZE];
        yMax = new int[MAXGRIDSIZE];
        for (compindex = 0; compindex < components.length; compindex++) {
            comp = components[compindex];
            if (!comp.isVisible()) {
                continue;
            }
            constraints = lookupConstraints(comp);
            curX = constraints.gridx;
            curY = constraints.gridy;
            curWidth = constraints.gridwidth;
            curHeight = constraints.gridheight;

            /* If x or y is negative, then use relative positioning: */
            if (curX < 0 && curY < 0) {
                if (curRow >= 0) {
                    curY = curRow;
                } else if (curCol >= 0) {
                    curX = curCol;
                } else {
                    curY = 0;
                }
            }
            if (curX < 0) {
                if (curHeight <= 0) {
                    curHeight += r.height - curY;
                    if (curHeight < 1) {
                        curHeight = 1;
                    }
                }
                px = 0;
                for (i = curY; i < curY + curHeight; i++) {
                    px = Math.max(px, xMax[i]);
                }
                curX = px - curX - 1;
                if (curX < 0) {
                    curX = 0;
                }
            } else if (curY < 0) {
                if (curWidth <= 0) {
                    curWidth += r.width - curX;
                    if (curWidth < 1) {
                        curWidth = 1;
                    }
                }
                py = 0;
                for (i = curX; i < curX + curWidth; i++) {
                    py = Math.max(py, yMax[i]);
                }
                curY = py - curY - 1;
                if (curY < 0) {
                    curY = 0;
                }
            }
            if (curWidth <= 0) {
                curWidth += r.width - curX;
                if (curWidth < 1) {
                    curWidth = 1;
                }
            }
            if (curHeight <= 0) {
                curHeight += r.height - curY;
                if (curHeight < 1) {
                    curHeight = 1;
                }
            }
            px = curX + curWidth;
            py = curY + curHeight;
            for (i = curX; i < curX + curWidth; i++) {
                yMax[i] = py;
            }
            for (i = curY; i < curY + curHeight; i++) {
                xMax[i] = px;
            }

            /* Make negative sizes start a new row/column */
            if (constraints.gridheight == 0 && constraints.gridwidth == 0) {
                curRow = curCol = -1;
            }
            if (constraints.gridheight == 0 && curRow < 0) {
                curCol = curX + curWidth;
            } else if (constraints.gridwidth == 0 && curCol < 0) {
                curRow = curY + curHeight;
            }

            /* Assign the new values to the gridbag slave */
            constraints.tempX = curX;
            constraints.tempY = curY;
            constraints.tempWidth = curWidth;
            constraints.tempHeight = curHeight;
        }

        /*
        * Apply minimum row/column dimensions and weights
        */
        if (mColumnWidths != null) {
            System.arraycopy(mColumnWidths, 0, r.minWidth, 0, mColumnWidths.length);
        }
        if (mRowHeights != null) {
            System.arraycopy(mRowHeights, 0, r.minHeight, 0, mRowHeights.length);
        }
        if (mColumnWeights != null) {
            System.arraycopy(mColumnWeights, 0, r.weightX, 0, mColumnWeights.length);
        }
        if (mRowWeights != null) {
            System.arraycopy(mRowWeights, 0, r.weightY, 0, mRowWeights.length);
        }

        /*
        * Pass #3
        *
        * Distribute the minimun widths and weights:
        */

        nextSize = Integer.MAX_VALUE;
        for (i = 1; i != Integer.MAX_VALUE; i = nextSize, nextSize = Integer.MAX_VALUE) {
            for (compindex = 0; compindex < components.length; compindex++) {
                comp = components[compindex];
                if (!comp.isVisible()) {
                    continue;
                }
                constraints = lookupConstraints(comp);
                if (constraints.tempWidth == i) {
                    px = constraints.tempX + constraints.tempWidth; /* right column */

                    /*
                    * Figure out if we should use this slave\'s weight.  If the weight
                    * is less than the total weight spanned by the width of the cell,
                    * then discard the weight.  Otherwise split the difference
                    * according to the existing weights.
                    */

                    weight_diff = constraints.weightx;
                    for (k = constraints.tempX; k < px; k++) {
                        weight_diff -= r.weightX[k];
                    }
                    if (weight_diff > 0.0) {
                        weight = 0.0;
                        for (k = constraints.tempX; k < px; k++) {
                            weight += r.weightX[k];
                        }
                        for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
                            double wt = r.weightX[k];
                            double dx = (wt * weight_diff) / weight;
                            r.weightX[k] += dx;
                            weight_diff -= dx;
                            weight -= wt;
                        }
                        /* Assign the remainder to the rightmost cell */
                        r.weightX[px - 1] += weight_diff;
                    }

                    /*
                    * Calculate the minWidth array values.
                    * First, figure out how wide the current slave needs to be.
                    * Then, see if it will fit within the current minWidth values.
                    * If it will not fit, add the difference according to the
                    * weightX array.
                    */

                    pixels_diff = constraints.minWidth + constraints.ipadx + constraints.insets.left
                            + constraints.insets.right;
                    for (k = constraints.tempX; k < px; k++) {
                        pixels_diff -= r.minWidth[k];
                    }
                    if (pixels_diff > 0) {
                        weight = 0.0;
                        for (k = constraints.tempX; k < px; k++) {
                            weight += r.weightX[k];
                        }
                        for (k = constraints.tempX; weight > 0.0 && k < px; k++) {
                            double wt = r.weightX[k];
                            int dx = (int) ((wt * (double) pixels_diff) / weight);
                            r.minWidth[k] += dx;
                            pixels_diff -= dx;
                            weight -= wt;
                        }
                        /* Any leftovers go into the rightmost cell */
                        r.minWidth[px - 1] += pixels_diff;
                    }
                } else if (constraints.tempWidth > i && constraints.tempWidth < nextSize) {
                    nextSize = constraints.tempWidth;
                }
                if (constraints.tempHeight == i) {
                    py = constraints.tempY + constraints.tempHeight; /* bottom row */

                    /*
                    * Figure out if we should use this slave's weight.  If the weight
                    * is less than the total weight spanned by the height of the cell,
                    * then discard the weight.  Otherwise split it the difference
                    * according to the existing weights.
                    */

                    weight_diff = constraints.weighty;
                    for (k = constraints.tempY; k < py; k++) {
                        weight_diff -= r.weightY[k];
                    }
                    if (weight_diff > 0.0) {
                        weight = 0.0;
                        for (k = constraints.tempY; k < py; k++) {
                            weight += r.weightY[k];
                        }
                        for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
                            double wt = r.weightY[k];
                            double dy = (wt * weight_diff) / weight;
                            r.weightY[k] += dy;
                            weight_diff -= dy;
                            weight -= wt;
                        }
                        /* Assign the remainder to the bottom cell */
                        r.weightY[py - 1] += weight_diff;
                    }

                    /*
                    * Calculate the minHeight array values.
                    * First, figure out how tall the current slave needs to be.
                    * Then, see if it will fit within the current minHeight values.
                    * If it will not fit, add the difference according to the
                    * weightY array.
                    */

                    pixels_diff = constraints.minHeight + constraints.ipady + constraints.insets.top
                            + constraints.insets.bottom;
                    for (k = constraints.tempY; k < py; k++) {
                        pixels_diff -= r.minHeight[k];
                    }
                    if (pixels_diff > 0) {
                        weight = 0.0;
                        for (k = constraints.tempY; k < py; k++) {
                            weight += r.weightY[k];
                        }
                        for (k = constraints.tempY; weight > 0.0 && k < py; k++) {
                            double wt = r.weightY[k];
                            int dy = (int) ((wt * (double) pixels_diff) / weight);
                            r.minHeight[k] += dy;
                            pixels_diff -= dy;
                            weight -= wt;
                        }
                        /* Any leftovers go into the bottom cell */
                        r.minHeight[py - 1] += pixels_diff;
                    }
                } else if (constraints.tempHeight > i && constraints.tempHeight < nextSize) {
                    nextSize = constraints.tempHeight;
                }
            }
        }
        return r;
    }
}

From source file:processing.app.Editor.java

protected JMenu buildToolsMenu() throws Exception {
    toolsMenu = new JMenu(_("Tools"));

    addInternalTools(toolsMenu);//  w w w.j a v  a2 s .  co m

    JMenuItem item = newJMenuItemShift(_("Serial Monitor"), 'M');
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            handleSerial();
        }
    });
    toolsMenu.add(item);

    addTools(toolsMenu, BaseNoGui.getToolsFolder());
    File sketchbookTools = new File(BaseNoGui.getSketchbookFolder(), "tools");
    addTools(toolsMenu, sketchbookTools);

    toolsMenu.addSeparator();

    numTools = toolsMenu.getItemCount();

    // XXX: DAM: these should probably be implemented using the Tools plugin
    // API, if possible (i.e. if it supports custom actions, etc.)

    for (JMenu menu : base.getBoardsCustomMenus()) {
        toolsMenu.add(menu);
    }

    if (serialMenu == null)
        serialMenu = new JMenu(_("Port"));
    populatePortMenu();
    toolsMenu.add(serialMenu);
    toolsMenu.addSeparator();

    JMenu programmerMenu = new JMenu(_("Programmer"));
    base.rebuildProgrammerMenu(programmerMenu);
    toolsMenu.add(programmerMenu);

    item = new JMenuItem(_("Burn Bootloader"));
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            handleBurnBootloader();
        }
    });
    toolsMenu.add(item);

    toolsMenu.addMenuListener(new StubMenuListener() {
        public void menuSelected(MenuEvent e) {
            //System.out.println("Tools menu selected.");
            populatePortMenu();
            for (Component c : toolsMenu.getMenuComponents()) {
                if ((c instanceof JMenu) && c.isVisible()) {
                    JMenu menu = (JMenu) c;
                    String name = menu.getText();
                    if (name == null)
                        continue;
                    String basename = name;
                    int index = name.indexOf(':');
                    if (index > 0)
                        basename = name.substring(0, index);
                    String sel = null;
                    int count = menu.getItemCount();
                    for (int i = 0; i < count; i++) {
                        JMenuItem item = menu.getItem(i);
                        if (item != null && item.isSelected()) {
                            sel = item.getText();
                            if (sel != null)
                                break;
                        }
                    }
                    if (sel == null) {
                        if (!name.equals(basename))
                            menu.setText(basename);
                    } else {
                        if (sel.length() > 50)
                            sel = sel.substring(0, 50) + "...";
                        String newname = basename + ": \"" + sel + "\"";
                        if (!name.equals(newname))
                            menu.setText(newname);
                    }
                }
            }
        }
    });

    return toolsMenu;
}