@Test public void testMixLayout() { CanvasLayoutManager layoutMgr = new CanvasLayoutManager(CanvasLayoutManager.MIX); panel.setLayout(layoutMgr); panel.setSize(100, 100); JPanel childPanel1 = new JPanel(); panel.add(childPanel1, new Rectangle(10, 20, 30, 40)); layoutMgr.invalidateLayout(panel); JPanel childPanel2 = new JPanel(); panel.add(childPanel2, new Rectangle(20, 30, 40, 50)); layoutMgr.invalidateLayout(panel); JPanel childPanel3 = new JPanel(); panel.add(childPanel3, new Rectangle(30, 40, 50, 60)); layoutMgr.invalidateLayout(panel); Component[] childs = panel.getComponents(); Assert.assertEquals(childs.length, 3); Assert.assertEquals(childs[0].getBounds(), new Rectangle(10, 20, 30, 40)); Assert.assertEquals(childs[1].getBounds(), new Rectangle(20, 30, 40, 50)); Assert.assertEquals(childs[2].getBounds(), new Rectangle(30, 40, 50, 60)); panel = new JPanel(); setupParent(panel); panel.setLayout(layoutMgr); panel.setSize(100, 100); childPanel1 = new JPanel(); childPanel2 = new JPanel(); childPanel3 = new JPanel(); panel.add(childPanel1, new Rectangle(0, 0, 30, 40)); panel.add(childPanel2, new Rectangle(20, 30, 40, 50)); panel.add(childPanel3, new Rectangle(30, 40, 50, 60)); layoutMgr.invalidateLayout(panel); childs = panel.getComponents(); Assert.assertEquals(childs.length, 3); Assert.assertEquals(childs[0].getBounds().getLocation(), new Point(0, 0)); Assert.assertEquals(childs[1].getBounds().getLocation(), new Point(30, 0)); Assert.assertEquals(childs[2].getBounds().getLocation(), new Point(0, 50)); }
@Override @SuppressWarnings("fallthrough") public void invalidateLayout(Container target) { try { Set<Component> components = newlyAddedComponents.keySet(); LAYOUT_ARRANGEMENT layout = componentLayout; layout = ((componentLayout == LAYOUT_ARRANGEMENT.mix) && (newlyAddedComponents.size() == 1)) ? LAYOUT_ARRANGEMENT.free : componentLayout; switch (layout) { case mix: if (!newlyAddedComponents.isEmpty()) { Entry<Component, Rectangle> firstEntry = newlyAddedComponents.entrySet() .iterator().next(); Rectangle r = firstEntry.getValue(); if (r != null) { nextLocation = r.getLocation(); } } case tile: Container parent = target.getParent(); Dimension parentDimension = parent.getBounds().getSize(); int largestHeight = 0; for (Component comp : components) { Rectangle r = newlyAddedComponents.get(comp); if (r != null) { if (nextLocation.x + r.width >= parentDimension.width) { nextLocation.x = 0; nextLocation.y += largestHeight; largestHeight = 0; } largestHeight = Math.max(largestHeight, r.height); nextLocation = marshalLocation(nextLocation); comp.setBounds(nextLocation.x, nextLocation.y, r.width, r.height); nextLocation.x += r.width; } else { r = marshalLocation(comp.getBounds()); nextLocation.x = r.x + r.width; } } break; case free: default: for (Component comp : components) { Rectangle r = marshalLocation(newlyAddedComponents.get(comp)); if (r != null) { comp.setBounds(r); } } break; } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { newlyAddedComponents.clear(); } }
CanvasLayoutManager implements LayoutManager2 { @Override @SuppressWarnings("fallthrough") public void invalidateLayout(Container target) { try { Set<Component> components = newlyAddedComponents.keySet(); LAYOUT_ARRANGEMENT layout = componentLayout; layout = ((componentLayout == LAYOUT_ARRANGEMENT.mix) && (newlyAddedComponents.size() == 1)) ? LAYOUT_ARRANGEMENT.free : componentLayout; switch (layout) { case mix: if (!newlyAddedComponents.isEmpty()) { Entry<Component, Rectangle> firstEntry = newlyAddedComponents.entrySet() .iterator().next(); Rectangle r = firstEntry.getValue(); if (r != null) { nextLocation = r.getLocation(); } } case tile: Container parent = target.getParent(); Dimension parentDimension = parent.getBounds().getSize(); int largestHeight = 0; for (Component comp : components) { Rectangle r = newlyAddedComponents.get(comp); if (r != null) { if (nextLocation.x + r.width >= parentDimension.width) { nextLocation.x = 0; nextLocation.y += largestHeight; largestHeight = 0; } largestHeight = Math.max(largestHeight, r.height); nextLocation = marshalLocation(nextLocation); comp.setBounds(nextLocation.x, nextLocation.y, r.width, r.height); nextLocation.x += r.width; } else { r = marshalLocation(comp.getBounds()); nextLocation.x = r.x + r.width; } } break; case free: default: for (Component comp : components) { Rectangle r = marshalLocation(newlyAddedComponents.get(comp)); if (r != null) { comp.setBounds(r); } } break; } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { newlyAddedComponents.clear(); } } }
CanvasLayoutManager implements LayoutManager2 { @Override @SuppressWarnings("fallthrough") public void invalidateLayout(Container target) { try { Set<Component> components = newlyAddedComponents.keySet(); LAYOUT_ARRANGEMENT layout = componentLayout; layout = ((componentLayout == LAYOUT_ARRANGEMENT.mix) && (newlyAddedComponents.size() == 1)) ? LAYOUT_ARRANGEMENT.free : componentLayout; switch (layout) { case mix: if (!newlyAddedComponents.isEmpty()) { Entry<Component, Rectangle> firstEntry = newlyAddedComponents.entrySet() .iterator().next(); Rectangle r = firstEntry.getValue(); if (r != null) { nextLocation = r.getLocation(); } } case tile: Container parent = target.getParent(); Dimension parentDimension = parent.getBounds().getSize(); int largestHeight = 0; for (Component comp : components) { Rectangle r = newlyAddedComponents.get(comp); if (r != null) { if (nextLocation.x + r.width >= parentDimension.width) { nextLocation.x = 0; nextLocation.y += largestHeight; largestHeight = 0; } largestHeight = Math.max(largestHeight, r.height); nextLocation = marshalLocation(nextLocation); comp.setBounds(nextLocation.x, nextLocation.y, r.width, r.height); nextLocation.x += r.width; } else { r = marshalLocation(comp.getBounds()); nextLocation.x = r.x + r.width; } } break; case free: default: for (Component comp : components) { Rectangle r = marshalLocation(newlyAddedComponents.get(comp)); if (r != null) { comp.setBounds(r); } } break; } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { newlyAddedComponents.clear(); } } CanvasLayoutManager();  CanvasLayoutManager(LAYOUT_ARRANGEMENT layout); }
CanvasLayoutManager implements LayoutManager2 { @Override @SuppressWarnings("fallthrough") public void invalidateLayout(Container target) { try { Set<Component> components = newlyAddedComponents.keySet(); LAYOUT_ARRANGEMENT layout = componentLayout; layout = ((componentLayout == LAYOUT_ARRANGEMENT.mix) && (newlyAddedComponents.size() == 1)) ? LAYOUT_ARRANGEMENT.free : componentLayout; switch (layout) { case mix: if (!newlyAddedComponents.isEmpty()) { Entry<Component, Rectangle> firstEntry = newlyAddedComponents.entrySet() .iterator().next(); Rectangle r = firstEntry.getValue(); if (r != null) { nextLocation = r.getLocation(); } } case tile: Container parent = target.getParent(); Dimension parentDimension = parent.getBounds().getSize(); int largestHeight = 0; for (Component comp : components) { Rectangle r = newlyAddedComponents.get(comp); if (r != null) { if (nextLocation.x + r.width >= parentDimension.width) { nextLocation.x = 0; nextLocation.y += largestHeight; largestHeight = 0; } largestHeight = Math.max(largestHeight, r.height); nextLocation = marshalLocation(nextLocation); comp.setBounds(nextLocation.x, nextLocation.y, r.width, r.height); nextLocation.x += r.width; } else { r = marshalLocation(comp.getBounds()); nextLocation.x = r.x + r.width; } } break; case free: default: for (Component comp : components) { Rectangle r = marshalLocation(newlyAddedComponents.get(comp)); if (r != null) { comp.setBounds(r); } } break; } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { newlyAddedComponents.clear(); } } CanvasLayoutManager();  CanvasLayoutManager(LAYOUT_ARRANGEMENT layout); @Override void addLayoutComponent(Component comp, Object constraints); @Override float getLayoutAlignmentX(Container target); @Override float getLayoutAlignmentY(Container target); @Override @SuppressWarnings("fallthrough") void invalidateLayout(Container target); Rectangle marshalLocation(Rectangle origBound); @Override Dimension maximumLayoutSize(Container target); @Override void addLayoutComponent(String name, Component comp); @Override void layoutContainer(Container parent); @Override Dimension minimumLayoutSize(Container parent); @Override Dimension preferredLayoutSize(Container parent); @Override void removeLayoutComponent(Component comp); int getGridSize(); void setGridSize(int gridSize); boolean isSnapEnable(); void enableSnap(boolean snapToGrid); void switchLayout(LAYOUT_ARRANGEMENT layout); }
CanvasLayoutManager implements LayoutManager2 { @Override @SuppressWarnings("fallthrough") public void invalidateLayout(Container target) { try { Set<Component> components = newlyAddedComponents.keySet(); LAYOUT_ARRANGEMENT layout = componentLayout; layout = ((componentLayout == LAYOUT_ARRANGEMENT.mix) && (newlyAddedComponents.size() == 1)) ? LAYOUT_ARRANGEMENT.free : componentLayout; switch (layout) { case mix: if (!newlyAddedComponents.isEmpty()) { Entry<Component, Rectangle> firstEntry = newlyAddedComponents.entrySet() .iterator().next(); Rectangle r = firstEntry.getValue(); if (r != null) { nextLocation = r.getLocation(); } } case tile: Container parent = target.getParent(); Dimension parentDimension = parent.getBounds().getSize(); int largestHeight = 0; for (Component comp : components) { Rectangle r = newlyAddedComponents.get(comp); if (r != null) { if (nextLocation.x + r.width >= parentDimension.width) { nextLocation.x = 0; nextLocation.y += largestHeight; largestHeight = 0; } largestHeight = Math.max(largestHeight, r.height); nextLocation = marshalLocation(nextLocation); comp.setBounds(nextLocation.x, nextLocation.y, r.width, r.height); nextLocation.x += r.width; } else { r = marshalLocation(comp.getBounds()); nextLocation.x = r.x + r.width; } } break; case free: default: for (Component comp : components) { Rectangle r = marshalLocation(newlyAddedComponents.get(comp)); if (r != null) { comp.setBounds(r); } } break; } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { newlyAddedComponents.clear(); } } CanvasLayoutManager();  CanvasLayoutManager(LAYOUT_ARRANGEMENT layout); @Override void addLayoutComponent(Component comp, Object constraints); @Override float getLayoutAlignmentX(Container target); @Override float getLayoutAlignmentY(Container target); @Override @SuppressWarnings("fallthrough") void invalidateLayout(Container target); Rectangle marshalLocation(Rectangle origBound); @Override Dimension maximumLayoutSize(Container target); @Override void addLayoutComponent(String name, Component comp); @Override void layoutContainer(Container parent); @Override Dimension minimumLayoutSize(Container parent); @Override Dimension preferredLayoutSize(Container parent); @Override void removeLayoutComponent(Component comp); int getGridSize(); void setGridSize(int gridSize); boolean isSnapEnable(); void enableSnap(boolean snapToGrid); void switchLayout(LAYOUT_ARRANGEMENT layout); final static LAYOUT_ARRANGEMENT TILE; final static LAYOUT_ARRANGEMENT MIX; }