Calculates the size of the target container given the sizes of the components. If the doLayout is true it will also lay out the container. Used by #minimumLayoutSize and #preferredLayoutSize. @param target the component which needs to be laid out @param componentSizes array o
(Container target,
Dimension[] componentSizes)
| 72 | * @return a {@link Dimension} value. |
| 73 | */ |
| 74 | protected Dimension getCompositeSize(Container target, |
| 75 | Dimension[] componentSizes){ |
| 76 | //find the origin of the popup |
| 77 | Point location = new Point(0, 0); |
| 78 | if(target.isShowing()){ |
| 79 | location = target.getLocationOnScreen(); |
| 80 | }else{ |
| 81 | if(target instanceof JPopupMenu){ |
| 82 | Component invoker = ((JPopupMenu)target).getInvoker(); |
| 83 | if(invoker != null) location = invoker.getLocationOnScreen(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | //find the maximum size |
| 88 | Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 89 | Rectangle contentsBounds = new Rectangle(toolkit.getScreenSize()); |
| 90 | Insets screenInsets = new Insets(0, 0, 0, 0); |
| 91 | GraphicsConfiguration gc = findGraphicsConfiguration(target); |
| 92 | if (gc != null) { |
| 93 | contentsBounds = gc.getBounds(); |
| 94 | screenInsets = toolkit.getScreenInsets(gc); |
| 95 | } |
| 96 | |
| 97 | // take screen insets (e.g. taskbar) into account |
| 98 | contentsBounds.width -= screenInsets.left + screenInsets.right; |
| 99 | contentsBounds.height -= screenInsets.top + screenInsets.bottom; |
| 100 | //take the location into account assuming that the largest side will be used |
| 101 | contentsBounds.height = Math.max(location.y, |
| 102 | contentsBounds.height - location.y); |
| 103 | |
| 104 | // take component insets into account |
| 105 | Insets insets = target.getInsets(); |
| 106 | contentsBounds.width -= insets.left + insets.right; |
| 107 | contentsBounds.height -= insets.top + insets.bottom; |
| 108 | Dimension dim = new Dimension(0, 0); |
| 109 | int previousColumnsWidth = 0; |
| 110 | int previousColumnsHeight = 0; |
| 111 | int columnIndex = 1; |
| 112 | int firstComponentIndexForColumn = 0; |
| 113 | columnForComponentIndex = new int[componentSizes.length]; |
| 114 | preferredWidthForColumn = new ArrayList<Integer>(); |
| 115 | for (int i = 0; i < componentSizes.length; i++) { |
| 116 | if ( (dim.height + |
| 117 | componentSizes[i].height) <= contentsBounds.height) { |
| 118 | //we can fit the current component in the current row |
| 119 | dim.height += componentSizes[i].height; |
| 120 | dim.width = Math.max(dim.width, componentSizes[i].width); |
| 121 | } |
| 122 | else { |
| 123 | //we need to start a new column |
| 124 | Arrays.fill(columnForComponentIndex, firstComponentIndexForColumn, |
| 125 | i, columnIndex); |
| 126 | preferredWidthForColumn.add(dim.width); |
| 127 | firstComponentIndexForColumn = i; |
| 128 | columnIndex++; |
| 129 | previousColumnsWidth += dim.width; |
| 130 | previousColumnsHeight = Math.max(previousColumnsHeight, dim.height); |
| 131 | dim.height = componentSizes[i].height; |
no test coverage detected