1 19 20 package org.netbeans.core.windows.view.ui.toolbars; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Dimension ; 25 import java.awt.Frame ; 26 import java.awt.Insets ; 27 import java.awt.LayoutManager2 ; 28 import java.awt.Rectangle ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import org.openide.awt.ToolbarPool; 37 import org.openide.windows.WindowManager; 38 39 40 49 public class ToolbarLayout implements LayoutManager2 , java.io.Serializable { 50 51 public static final int HGAP = 1; 52 53 public static final int VGAP = 1; 54 55 static final long serialVersionUID =7489472539255790677L; 56 57 58 ToolbarConfiguration toolbarConfig; 59 60 HashMap <Component ,Object > componentMap; 61 62 65 public ToolbarLayout (ToolbarConfiguration conf) { 66 toolbarConfig = conf; 67 componentMap = new HashMap <Component , Object >(); 68 } 69 70 75 public void addLayoutComponent (String name, Component comp) { 76 throw new IllegalArgumentException (); 77 } 78 79 87 public void addLayoutComponent (Component comp, Object constr) { 88 if (!(constr instanceof ToolbarConstraints)) 89 throw new IllegalArgumentException (ToolbarConfiguration.getBundleString("EXC_wrongConstraints")); 90 91 componentMap.put (comp, constr); 92 ToolbarConstraints tc = (ToolbarConstraints)constr; 93 tc.setPreferredSize (comp.getPreferredSize()); 94 comp.setVisible (tc.isVisible()); 95 } 96 97 101 public void removeLayoutComponent (Component comp) { 102 componentMap.remove (comp); 103 } 104 105 112 public Dimension preferredLayoutSize (Container parent) { 113 Insets insets = parent.getInsets(); 114 Dimension prefSize = new Dimension (insets.left + toolbarConfig.getPrefWidth() + insets.right, 115 insets.top + toolbarConfig.getPrefHeight() + insets.bottom); 116 return prefSize; 117 } 118 119 125 public Dimension minimumLayoutSize (Container parent) { 126 return preferredLayoutSize (parent); 127 } 128 129 135 public Dimension maximumLayoutSize (Container parent) { 136 return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); 137 } 138 139 146 public float getLayoutAlignmentX (Container parent) { 147 return 0; 148 } 149 150 157 public float getLayoutAlignmentY (Container parent) { 158 return 0; 159 } 160 161 165 public void invalidateLayout (Container parent) { 166 } 167 168 172 public void layoutContainer (Container parent) { 173 synchronized (parent.getTreeLock()) { 174 Insets insets = WindowManager.getDefault().getMainWindow().getInsets(); 177 int maxPosition; 178 Frame f = WindowManager.getDefault().getMainWindow(); 179 if ((f.getExtendedState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) { 180 maxPosition = f.getWidth() - HGAP; 181 } else { 182 maxPosition = f.getWidth() - (insets.left + insets.right) - HGAP; 183 } 184 Component comp; 185 ToolbarConstraints constr; 186 187 190 Iterator it = componentMap.keySet().iterator(); 191 while (it.hasNext()) { 192 comp = (Component )it.next(); 193 constr = (ToolbarConstraints)componentMap.get (comp); 194 constr.updatePreferredSize (comp.getPreferredSize()); 195 } 196 197 198 HashSet <ToolbarConstraints> completed = new HashSet <ToolbarConstraints>(componentMap.size()*2); 199 for (int i = 0; i < toolbarConfig.getRowCount(); i++) { 200 ToolbarConstraints overflownTC = processRow(toolbarConfig.getRow(i), completed, maxPosition); 201 for (Iterator <ToolbarConstraints> iter = toolbarConfig.getRow(i).iterator(); it.hasNext(); ) { 203 completed.add(iter.next()); 204 } 205 } 206 parent.repaint(); 207 } 208 } 209 210 private ToolbarConstraints processRow (ToolbarRow row, Collection completed, int maxPosition) { 211 Rectangle bounds; 212 Component comp; 213 ToolbarConstraints constr; 214 List <ToolbarConstraints> moveDownCandidates = new ArrayList <ToolbarConstraints>(5); 215 for (Iterator it = row.iterator(); it.hasNext(); ) { 216 constr = (ToolbarConstraints)it.next(); 217 if (completed.contains(constr)) { 219 continue; 220 } 221 comp = ToolbarPool.getDefault().findToolbar(constr.getName()); 222 223 224 bounds = constr.getBounds(); 225 if ((bounds.x < maxPosition) && (bounds.x + bounds.width > maxPosition)) { bounds.width = maxPosition - bounds.x; comp.setBounds(bounds); 229 } else { 230 if (constr.getPosition() > maxPosition + HGAP) { 231 if (!constr.isAlone() && ((row.toolbarCount() - moveDownCandidates.size()) > 1)) { 233 moveDownCandidates.add(constr); 234 } 235 } else { 236 comp.setBounds(bounds); 237 } 238 } 239 } 240 for (Iterator iter = moveDownCandidates.iterator(); iter.hasNext(); ) { 242 moveToolbarDown((ToolbarConstraints)iter.next()); 243 } 244 245 return null; 246 } 247 248 249 250 private void moveToolbarDown (ToolbarConstraints constr) { 251 int pos = constr.rowIndex() + 1; 252 constr.destroy(); 253 for (int i = pos; i < pos + constr.getRowCount(); i++) { 254 toolbarConfig.getRow(i).addToolbar(constr, 0); 255 } 256 constr.setPosition(0); 257 constr.updatePosition(); 258 } 259 260 261 } 262 263 | Popular Tags |