001 /** 002 * 003 */ 004 package nl.cwi.sen1.util; 005 006 import java.awt.Component; 007 import java.awt.Container; 008 import java.awt.Dimension; 009 import java.awt.Insets; 010 import java.awt.LayoutManager2; 011 import java.util.HashMap; 012 import java.util.Map; 013 014 public class StudioStatusBarLayout implements LayoutManager2 { 015 private Map<Component, StudioStatusBarConstraints> constraints = new HashMap<Component, StudioStatusBarConstraints>(); 016 017 public void addLayoutComponent(String name, Component comp) { 018 addLayoutComponent(comp, null); 019 } 020 021 public void addLayoutComponent(Component comp, Object constraint) { 022 constraints.put(comp, (StudioStatusBarConstraints) constraint); 023 } 024 025 public void removeLayoutComponent(Component comp) { 026 constraints.remove(comp); 027 } 028 029 public Dimension preferredLayoutSize(Container parent) { 030 Dimension dim = new Dimension(); 031 for (Component comp : constraints.keySet()) { 032 Dimension d = comp.getPreferredSize(); 033 StudioStatusBarConstraints c = constraints.get(comp); 034 if (c != null) { 035 Insets i = c.getInsets(); 036 d.width += i.left + i.right; 037 d.height += i.top + i.bottom; 038 } 039 dim.height = Math.max(dim.height, d.height); 040 dim.width += d.width; 041 } 042 043 Insets insets = parent.getInsets(); 044 dim.width += insets.left + insets.right; 045 dim.height += insets.top + insets.bottom; 046 047 return dim; 048 } 049 050 public Dimension minimumLayoutSize(Container parent) { 051 return preferredLayoutSize(parent); 052 } 053 054 public Dimension maximumLayoutSize(Container target) { 055 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); 056 } 057 058 public float getLayoutAlignmentX(Container target) { 059 return 0.5f; 060 } 061 062 public float getLayoutAlignmentY(Container target) { 063 return 0.5f; 064 } 065 066 public void invalidateLayout(Container target) { 067 } 068 069 public void layoutContainer(Container parent) { 070 double maxWeight = 0.0; 071 for (Component comp : parent.getComponents()) { 072 if (comp.isVisible()) { 073 StudioStatusBarConstraints c = constraints.get(comp); 074 if (c != null) { 075 maxWeight += c.getWeight(); 076 } 077 } 078 } 079 if (maxWeight == 0) { 080 maxWeight = 1.0; 081 } 082 083 Insets parentInsets = parent.getInsets(); 084 int availableWidth = parent.getWidth() 085 - preferredLayoutSize(parent).width; 086 int nextX = parentInsets.left; 087 int height = parent.getHeight() - parentInsets.top 088 - parentInsets.bottom; 089 090 for (Component comp : parent.getComponents()) { 091 if (comp.isVisible()) { 092 StudioStatusBarConstraints c = constraints.get(comp); 093 double weight = 0.0; 094 Insets insets = new Insets(0, 0, 0, 0); 095 if (c != null) { 096 weight = c.getWeight(); 097 insets = c.getInsets(); 098 } 099 100 int weightedWidth = 0; 101 if (availableWidth > 0) { 102 weightedWidth = (int) ((weight/maxWeight) * availableWidth); 103 } 104 availableWidth -= weightedWidth; 105 106 int width = comp.getPreferredSize().width + weightedWidth; 107 108 int x = nextX + insets.left; 109 int y = parentInsets.top + insets.top; 110 comp.setSize(width, height); 111 comp.setLocation(x, y); 112 nextX = x + width + insets.right; 113 } 114 } 115 } 116 }