1 7 8 package javax.swing; 9 10 import java.awt.LayoutManager ; 11 import java.awt.Component ; 12 import java.awt.Container ; 13 import java.awt.Rectangle ; 14 import java.awt.Point ; 15 import java.awt.Dimension ; 16 import java.awt.Insets ; 17 import java.io.Serializable ; 18 19 41 public class ViewportLayout implements LayoutManager , Serializable 42 { 43 static ViewportLayout SHARED_INSTANCE = new ViewportLayout (); 45 46 51 public void addLayoutComponent(String name, Component c) { } 52 53 58 public void removeLayoutComponent(Component c) { } 59 60 61 69 public Dimension preferredLayoutSize(Container parent) { 70 Component view = ((JViewport )parent).getView(); 71 if (view == null) { 72 return new Dimension (0, 0); 73 } 74 else if (view instanceof Scrollable ) { 75 return ((Scrollable )view).getPreferredScrollableViewportSize(); 76 } 77 else { 78 return view.getPreferredSize(); 79 } 80 } 81 82 83 92 public Dimension minimumLayoutSize(Container parent) { 93 return new Dimension (4, 4); 94 } 95 96 97 105 public void layoutContainer(Container parent) 106 { 107 JViewport vp = (JViewport )parent; 108 Component view = vp.getView(); 109 Scrollable scrollableView = null; 110 111 if (view == null) { 112 return; 113 } 114 else if (view instanceof Scrollable ) { 115 scrollableView = (Scrollable ) view; 116 } 117 118 121 122 Insets insets = vp.getInsets(); 123 Dimension viewPrefSize = view.getPreferredSize(); 124 Dimension vpSize = vp.getSize(); 125 Dimension extentSize = vp.toViewCoordinates(vpSize); 126 Dimension viewSize = new Dimension (viewPrefSize); 127 128 if (scrollableView != null) { 129 if (scrollableView.getScrollableTracksViewportWidth()) { 130 viewSize.width = vpSize.width; 131 } 132 if (scrollableView.getScrollableTracksViewportHeight()) { 133 viewSize.height = vpSize.height; 134 } 135 } 136 137 Point viewPosition = vp.getViewPosition(); 138 139 144 if (scrollableView == null || 145 vp.getParent() == null || 146 vp.getParent().getComponentOrientation().isLeftToRight()) { 147 if ((viewPosition.x + extentSize.width) > viewSize.width) { 148 viewPosition.x = Math.max(0, viewSize.width - extentSize.width); 149 } 150 } else { 151 if (extentSize.width > viewSize.width) { 152 viewPosition.x = viewSize.width - extentSize.width; 153 } else { 154 viewPosition.x = Math.max(0, Math.min(viewSize.width - extentSize.width, viewPosition.x)); 155 } 156 } 157 158 162 if ((viewPosition.y + extentSize.height) > viewSize.height) { 163 viewPosition.y = Math.max(0, viewSize.height - extentSize.height); 164 } 165 166 174 if (scrollableView == null) { 175 if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) { 176 viewSize.width = vpSize.width; 177 } 178 if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) { 179 viewSize.height = vpSize.height; 180 } 181 } 182 vp.setViewPosition(viewPosition); 183 vp.setViewSize(viewSize); 184 } 185 } 186 187 | Popular Tags |