📄 toolbarlayout.java
字号:
for (int i = 0; i < size; i++) { constraint = (ToolBarConstraints)constraintsList.get(i); if (constraint.getRow() == row) { Component c = (Component)componentsMap.get(constraint); if (c.isVisible()) { count++; } } } return count; } private boolean maybeRemoveRow() { int removeRow = -1; for (int i = 0; i < toolBarRows; i++) { if (getComponentCount(i) == 0) { removeRow = i; break; } } if (removeRow == -1) return false; int currentRow = -1; int size = constraintsList.size(); ToolBarConstraints constraint = null; for (int i = 0; i < size; i++) { constraint = (ToolBarConstraints)constraintsList.get(i); currentRow = constraint.getRow(); if (constraint.getRow() > removeRow) { constraint.setRow(currentRow - 1); } } toolBarRows--; return true; } public int getRowHeight() { if (rowHeight <= 0) { return ROW_HEIGHT; } return rowHeight; } public boolean maybeAddRow(Component comp) { ToolBarConstraints constraint = getConstraint(comp); int currentRow = constraint.getRow(); if (getComponentCount(currentRow) == 1) return false; int newRow = (int)Math.round((double)comp.getY() / (double)getRowHeight()); constraint.setRow(newRow); if (newRow + 1 > toolBarRows) toolBarRows++; return true; } public void componentResized(Component comp, int locX) { ToolBarConstraints constraint = getConstraint(comp); constraint.setResizeOffsetX(locX); constraint.setLocX(locX); } public void componentMoved(Component comp, int locX, int locY) { ToolBarConstraints constraint = getConstraint(comp); // determine the new row (if changed) int currentRow = constraint.getRow(); int newRow = (int)Math.round((double)locY / (double)getRowHeight()); if (newRow != currentRow) constraint.setResizeOffsetX(-1); if (newRow > toolBarRows) newRow = toolBarRows - 1; constraint.setLocX(locX); constraint.setRow(newRow); maybeRemoveRow(); } /** <p>Adds the specified component to the layout, using the specified * constraint object. * * @param the component to be added * @param where/how the component is added to the layout. */ public void addLayoutComponent(Component comp, Object cons) { if (cons instanceof ToolBarConstraints) { ToolBarConstraints _cons = (ToolBarConstraints)((ToolBarConstraints)cons).clone(); if (_cons.getMinimumWidth() == -1) _cons.setMinimumWidth(comp.getMinimumSize().width); componentsMap.put(_cons, comp); constraintsList.add(_cons); } else if (cons != null) { throw new IllegalArgumentException( "cannot add to layout: constraints must be a ToolBarConstraints"); } } /** <p>Returns the maximum size of this component. * * @param the target container */ public Dimension maximumLayoutSize(Container target) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } /** <p>Calculates the minimum size dimensions for the specified * panel given the components in the specified parent container. * * @param parent the component to be laid out * @see #preferredLayoutSize */ public Dimension minimumLayoutSize(Container parent) { return getLayoutSize(parent, false); } private Rectangle getComponentBounds(Component comp, boolean isPreferred) { Rectangle rectangle = comp.getBounds(); if (rectangle.height == 0) { ToolBarConstraints constraints = getConstraint(comp); rectangle.height = getRowHeight(); rectangle.y = getRowHeight() * constraints.getRow(); } if(rectangle.width <= 0 || rectangle.height <= 0) { Dimension dimension = isPreferred ? comp.getPreferredSize() : comp.getMinimumSize(); if(rectangle.width <= 0) rectangle.width = dimension.width; if(rectangle.height <= 0) rectangle.height = dimension.height; } return rectangle; } protected Dimension getLayoutSize(Container parent, boolean isPreferred) { Dimension dimension = new Dimension(0, 0); int width = 0; int height = 0; int i = parent.getComponentCount(); for(int j = 0; j < i; j++) { Component component = parent.getComponent(j); if(component.isVisible()) { Rectangle rectangle = getComponentBounds(component, isPreferred); dimension.width = Math.max(dimension.width, rectangle.x + rectangle.width); } } Insets insets = parent.getInsets(); dimension.width += insets.left + insets.right; dimension.height = (getRowHeight() * toolBarRows) + BORDER_OFFSET + insets.top + insets.bottom; return dimension; } /** <p>Calculates the preferred size dimensions for the specified * panel given the components in the specified parent container. * * @param parent the component to be laid out * @see #minimumLayoutSize */ public Dimension preferredLayoutSize(Container parent) { return getLayoutSize(parent, true); } /** <p>Removes the specified component from the layout. * * @param the component to be removed */ public void removeLayoutComponent(Component comp) {} /** <p>Removes all components from this layout. */ public void removeComponents() { componentsMap.clear(); constraintsList.clear(); comparator.setFirstLayout(true); } /** <p>Invalidates the layout, indicating that if the layout manager * has cached information it should be discarded. * * @param the target container */ public void invalidateLayout(Container target) {} /** <p>Returns the alignment along the x axis. This specifies how * the component would like to be aligned relative to other * components. The value should be a number between 0 and 1 * where 0 represents alignment along the origin, 1 is aligned * the furthest away from the origin, 0.5 is centered, etc. * * @param the target container */ public float getLayoutAlignmentX(Container target) { return 0.5f; } /** <p>Returns the alignment along the y axis. This specifies how * the component would like to be aligned relative to other * components. The value should be a number between 0 and 1 * where 0 represents alignment along the origin, 1 is aligned * the furthest away from the origin, 0.5 is centered, etc. * * @param the target container */ public float getLayoutAlignmentY(Container target) { return 0.5f; } /** <p>Adds the specified component with the specified name to * the layout. This does nothing in ToolBarLayout - constraints * are required. */ public void addLayoutComponent(String name, Component comp) {} // reorders the constraints depending on component placement class ToolsPositionComparator implements Comparator { private boolean firstLayout = true; /** <p>Compares the two objects. */ public int compare(Object obj1, Object obj2) { ToolBarConstraints cons1 = (ToolBarConstraints)obj1; ToolBarConstraints cons2 = (ToolBarConstraints)obj2; int halfWidth = ((Component)componentsMap.get(cons1)).getWidth() / 2; if (firstLayout) { halfWidth = 0; } int firstX = cons1.getLocX() + halfWidth; int secondX = cons2.getLocX(); int firstY = cons1.getRow(); int secondY = cons2.getRow(); if (firstX < secondX) { if (firstY > secondY) { return 1; } else { return -1; } } else if (firstX > secondX) { if (firstY < secondY) { return -1; } else { return 1; } } else { return 0; } } public boolean equals(Object obj) { return this.equals(obj); } public void setFirstLayout(boolean firstLayout) { this.firstLayout = firstLayout; } } // class ToolsPositionComparator }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -