📄 toolbarlayout.java
字号:
/* * ToolBarLayout.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.underworldlabs.swing.toolbar;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Insets;import java.awt.LayoutManager2;import java.awt.Rectangle;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @version $Revision: 1.5 $ * @date $Date: 2006/07/15 12:50:56 $ */public class ToolBarLayout implements LayoutManager2 { /** The default tool bar row height */ private static final int ROW_HEIGHT = 30; /** The bottom border offset */ public static final int BORDER_OFFSET = 2; /** the applied row height */ private int rowHeight = -1; /** The number of rows currently displayed */ private int toolBarRows; /** The comparator for tool bar ordering */ private ToolsPositionComparator comparator; /** The constraints/component pairs */ private Hashtable componentsMap; /** The tool bar constraints */ private ArrayList constraintsList; /** <p>Constructs a new layout instance initialised * to the specified number of rows. * * @param the initial number of rows */ public ToolBarLayout(int toolBarRows) { constraintsList = new ArrayList(); componentsMap = new Hashtable(); comparator = new ToolsPositionComparator(); this.toolBarRows = toolBarRows; } /** <p>Lays out the container in the specified container. * * @param the component which needs to be laid out */ public void layoutContainer(Container parent) { Dimension parentDim = parent.getSize(); Collections.sort(constraintsList, comparator); // the x and y position int locX = 0, locY = 0; // the resize offset x position int resizeOffsetX = 0; // the last and current row number int previousRow = 0, currentRow = 0; // the widths to be set int width = 0, preferredWidth = 0, minWidth = 0; // component count int compCount = constraintsList.size(); // whether the row count changed boolean rowAdded = false; ToolBarConstraints constraints = null; Component component = null; for (int i = 0; i < compCount; i++) { constraints = (ToolBarConstraints)constraintsList.get(i); component = (Component)componentsMap.get(constraints); if (!component.isVisible()) { continue; } if (rowHeight <= 0) { rowHeight = component.getPreferredSize().height; } currentRow = constraints.getRow(); if (currentRow > toolBarRows) { toolBarRows = currentRow; rowAdded = true; } minWidth = constraints.getMinimumWidth(); preferredWidth = constraints.getPreferredWidth(); // reset the x location if new row if (currentRow != previousRow) { locX = 0; locY = currentRow * getRowHeight(); } // check if we stretch this tool bar to fill the width if (i != compCount - 1) { // check the next component ToolBarConstraints _constraints = (ToolBarConstraints) constraintsList.get(i + 1); resizeOffsetX = _constraints.getResizeOffsetX(); // if the next component is on a new row, // fill the current row if (_constraints.getRow() == currentRow + 1) { width = parentDim.width - locX; } // if the component is resizing else if (resizeOffsetX != -1) { ToolBarConstraints[] tbca = getToolBars(currentRow); int maxWidth = parentDim.width - locX; int _width = resizeOffsetX - locX; int _minWidth = 0; int start = 0; // determine the components position in the row for (int k = 0; k < tbca.length; k++) { if (tbca[k] == constraints) { start = k + 1; break; } } for (int j = start; j < tbca.length; j++) { _minWidth = tbca[j].getMinimumWidth(); if (_minWidth == -1) { _minWidth = ((Component)componentsMap.get(constraints)). getMinimumSize().width; } maxWidth -= _minWidth; } if (_width < minWidth) { width = minWidth; } else if (_width > maxWidth) { width = maxWidth; } else { width = _width; } } // if the component has a preferred width else if (preferredWidth != -1) { width = preferredWidth; } // otherwise use the minimum else { width = minWidth; } } // otherwise the component should fill its row else { minWidth = constraints.getMinimumWidth(); if (minWidth == -1) { minWidth = component.getMinimumSize().width; } width = parentDim.width - locX; if (width < minWidth) { width = minWidth; } locX = parentDim.width - width; } component.setBounds(locX, locY, width, getRowHeight()); // reset the component's position constraints.setLocX(locX); // reset values previousRow = currentRow; locX += width; minWidth = 0; resizeOffsetX = -1; } comparator.setFirstLayout(false); } /** <p>Returns all the toolbars on the specified row. * * @parm the tool bar row */ private ToolBarConstraints[] getToolBars(int row) { int _row = -1; ToolBarConstraints tbc = null; Vector _toolBars = new Vector(); for (int i = 0, k = constraintsList.size(); i < k; i++) { tbc = (ToolBarConstraints)constraintsList.get(i); _row = tbc.getRow(); if (_row > row) { break; } if (_row == row) { _toolBars.add(tbc); } } return (ToolBarConstraints[])_toolBars.toArray(new ToolBarConstraints[]{}); } /** <p>Returns the currently displayed tool bar row count. * * @return the current row count */ public int getRowCount() { return toolBarRows; } public void setRows(int rows) { toolBarRows = rows; } /** <p>Retrieves the specified components constraints. * * @param the component * @return the component's constraints */ public ToolBarConstraints getConstraint(Component comp) { ToolBarConstraints constraint = null; Enumeration consEnum = componentsMap.keys(); while (consEnum.hasMoreElements()) { Object element = consEnum.nextElement(); if (componentsMap.get(element) == comp) { constraint = (ToolBarConstraints)element; break; } } return constraint; } /** <p>Determines the component count for the * specified row. * * @param the row * @return the component count for the row */ private int getComponentCount(int row) { int count = 0; int size = constraintsList.size(); ToolBarConstraints constraint = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -