1 package JSci.swing; 2 3 import java.awt.*; 4 5 15 public final class JGraphLayout implements LayoutManager2 { 16 public final static String TITLE = "Title"; 17 public final static String GRAPH = "Graph"; 18 public final static String X_AXIS = "X-axis"; 19 public final static String Y_AXIS = "Y-axis"; 20 private Component title,graph,xaxis,yaxis; 21 22 public JGraphLayout() {} 23 public void addLayoutComponent(String name,Component comp) { 24 if(TITLE.equals(name)) 25 title=comp; 26 else if(GRAPH.equals(name) || name == null) 27 graph=comp; 28 else if(X_AXIS.equals(name)) 29 xaxis=comp; 30 else if(Y_AXIS.equals(name)) 31 yaxis=comp; 32 } 33 public void addLayoutComponent(Component comp,Object constraint) { 34 String name = (constraint != null ? constraint.toString() : null); 35 addLayoutComponent(name, comp); 36 } 37 public void removeLayoutComponent(Component comp) { 38 if(comp == title) 39 title=null; 40 if(comp == graph) 41 graph=null; 42 if(comp == xaxis) 43 xaxis=null; 44 if(comp == yaxis) 45 yaxis=null; 46 } 47 public void layoutContainer(Container parent) { 48 synchronized(parent.getTreeLock()) { 49 Dimension size=parent.getSize(); 50 Insets insets=parent.getInsets(); 51 final int width=size.width-insets.left-insets.right; 52 final int height=size.height-insets.top-insets.bottom; 53 int graphLeftPad=0,graphAxisPad=0; 54 if(graph instanceof JGraph2D) { 55 graphLeftPad=((JGraph2D)graph).leftAxisPad; 56 graphAxisPad=((JGraph2D)graph).axisPad; 57 } else if(graph instanceof JCategoryGraph2D) { 58 graphLeftPad=((JCategoryGraph2D)graph).leftAxisPad; 59 graphAxisPad=((JCategoryGraph2D)graph).axisPad; 60 } 61 int yaxisWidth=getMinimumSize(yaxis).width; 62 int graphWidth=width-yaxisWidth; 63 int titleWidth=graphWidth-graphLeftPad-graphAxisPad; 64 int xaxisWidth=titleWidth; 65 int titleHeight=getMinimumSize(title).height; 66 int xaxisHeight=getMinimumSize(xaxis).height; 67 int graphHeight=height-titleHeight-xaxisHeight; 68 int yaxisHeight=graphHeight-2*graphAxisPad; 69 if(title!=null) 70 title.setBounds(insets.left+yaxisWidth+graphLeftPad,insets.top,titleWidth,titleHeight); 71 if(graph!=null) 72 graph.setBounds(insets.left+yaxisWidth,insets.top+titleHeight,graphWidth,graphHeight); 73 if(yaxis!=null) 74 yaxis.setBounds(insets.left,insets.top+titleHeight+graphAxisPad,yaxisWidth,yaxisHeight); 75 if(xaxis!=null) 76 xaxis.setBounds(insets.left+yaxisWidth+graphLeftPad,height-xaxisHeight,xaxisWidth,xaxisHeight); 77 } 78 } 79 public void invalidateLayout(Container parent) {} 80 public float getLayoutAlignmentX(Container parent) { 81 return 0.5f; 82 } 83 public float getLayoutAlignmentY(Container parent) { 84 return 0.5f; 85 } 86 public Dimension minimumLayoutSize(Container parent) { 87 return calculateLayoutSize(parent.getInsets(), 88 getMinimumSize(title), 89 getMinimumSize(graph), 90 getMinimumSize(xaxis), 91 getMinimumSize(yaxis)); 92 } 93 private static Dimension getMinimumSize(Component comp) { 94 if(comp==null) 95 return new Dimension(); 96 else 97 return comp.getMinimumSize(); 98 } 99 public Dimension preferredLayoutSize(Container parent) { 100 return calculateLayoutSize(parent.getInsets(), 101 getMinimumSize(title), 102 getPreferredSize(graph), 103 getMinimumSize(xaxis), 104 getMinimumSize(yaxis)); 105 } 106 private static Dimension getPreferredSize(Component comp) { 107 if(comp==null) 108 return new Dimension(); 109 else 110 return comp.getPreferredSize(); 111 } 112 public Dimension maximumLayoutSize(Container parent) { 113 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); 114 } 115 private static Dimension calculateLayoutSize(Insets insets,Dimension title,Dimension graph,Dimension xaxis,Dimension yaxis) { 116 int width = insets.left+insets.right; 117 width += Math.max(title.width, yaxis.width+graph.width); 118 int height = insets.top+insets.bottom; 119 height += title.height+xaxis.height; 120 height += Math.max(yaxis.height, graph.height); 121 return new Dimension(width,height); 122 } 123 } 124 125 | Popular Tags |