1 42 43 package swingwtx.swing; 44 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.custom.SashForm; 47 48 import swingwt.awt.Dimension; 49 50 61 public class JSplitPane extends JComponent { 62 63 public final static int VERTICAL_SPLIT = 0; 64 public final static int HORIZONTAL_SPLIT = 1; 65 66 protected int pOrientation = HORIZONTAL_SPLIT; 67 protected double pResizeWeight = 0; 68 69 protected swingwt.awt.Component leftComponent = null; 70 protected swingwt.awt.Component rightComponent = null; 71 72 private SashForm ppeer = null; 73 74 public JSplitPane() { 75 } 76 public JSplitPane(int newOrientation) { 77 this(newOrientation, false); 78 } 79 public JSplitPane(int newOrientation, boolean newContinuousLayout) { 80 this(newOrientation, newContinuousLayout, null, null); 81 } 82 public JSplitPane( 83 int newOrientation, 84 swingwt.awt.Component newLeftComponent, 85 swingwt.awt.Component newRightComponent) { 86 this(newOrientation, false, newLeftComponent, newRightComponent); 87 } 88 89 public JSplitPane( 90 int newOrientation, 91 boolean newContinuousLayout, 92 swingwt.awt.Component newLeftComponent, 93 swingwt.awt.Component newRightComponent) { 94 95 pOrientation = newOrientation; 96 if (pOrientation != HORIZONTAL_SPLIT && pOrientation != VERTICAL_SPLIT) 97 throw new IllegalArgumentException ("Invalid orientation."); 98 if (newLeftComponent != null) 99 setLeftComponent(newLeftComponent); 100 if (newRightComponent != null) 101 setRightComponent(newRightComponent); 102 103 } 104 105 public void setLeftComponent(swingwt.awt.Component comp) { 106 leftComponent = comp; 107 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 108 addLeft(); 109 } 110 } 111 112 public void setRightComponent(swingwt.awt.Component comp) { 113 rightComponent = comp; 114 if (SwingWTUtils.isSWTControlAvailable(ppeer)) { 115 addRight(); 116 } 117 } 118 119 void addLeft() { 120 final JSplitPane me = this; 121 SwingUtilities.invokeSync(new Runnable () { 122 public void run() { 123 try { 124 leftComponent.setSwingWTParent(me); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 } 129 }); 130 } 131 132 void addRight() { 133 final JSplitPane me = this; 134 SwingUtilities.invokeSync(new Runnable () { 135 public void run() { 136 try { 137 rightComponent.setSwingWTParent(me); 138 } catch (Exception e) { 139 e.printStackTrace(); 140 } 141 } 142 }); 143 } 144 145 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 146 descendantHasPeer = true; 147 ppeer = new SashForm(parent.getComposite(), SWT.BORDER); 148 peer = ppeer; 149 composite = ppeer; 150 this.parent = parent; 151 ppeer.setOrientation( this.pOrientation == VERTICAL_SPLIT ? SWT.VERTICAL : SWT.HORIZONTAL ); 152 if (leftComponent != null) { 153 addLeft(); 154 if (rightComponent != null) 155 addRight(); 156 } 159 if (pResizeWeight != 0) 160 setResizeWeight(pResizeWeight); 161 } 162 163 public swingwt.awt.Component getLeftComponent() { 164 return leftComponent; 165 } 166 public void setTopComponent(swingwt.awt.Component comp) { 167 setLeftComponent(comp); 168 } 169 public swingwt.awt.Component getTopComponent() { 170 return leftComponent; 171 } 172 public swingwt.awt.Component getRightComponent() { 173 return rightComponent; 174 } 175 public void setBottomComponent(swingwt.awt.Component comp) { 176 setRightComponent(comp); 177 } 178 public swingwt.awt.Component getBottomComponent() { 179 return rightComponent; 180 } 181 public int getOrientation() { 182 return pOrientation; 183 } 184 public void setContinuousLayout(boolean b) { 185 } 186 public int getDividerSize() { 187 return 2; 188 } 189 public void setDividerSize(int newSize) { 190 } 191 public void setOrientation(final int newOrientation) { 192 if (pOrientation != HORIZONTAL_SPLIT && pOrientation != VERTICAL_SPLIT) 193 throw new IllegalArgumentException ("Invalid orientation."); 194 pOrientation = newOrientation; 195 SwingUtilities.invokeSync(new Runnable () { 196 public void run() { 197 if (SwingWTUtils.isSWTControlAvailable(ppeer)) 198 ppeer.setOrientation(newOrientation == VERTICAL_SPLIT ? SWT.VERTICAL : SWT.HORIZONTAL); 199 } 200 }); 201 } 202 public void setResizeWeight(final double value) { 203 pResizeWeight = value; 204 SwingUtilities.invokeSync(new Runnable () { 205 public void run() { 206 if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null && rightComponent != null) { 207 int leftWeight = (int) (value * 10); 208 int rightWeight = 10 - leftWeight; 209 ppeer.setWeights(new int[] { leftWeight, rightWeight }); 210 } 211 } 212 }); 213 } 214 public double getResizeWeight() { 215 return pResizeWeight; 216 } 217 218 221 public void setOneTouchExpandable(boolean newValue) { 222 } 223 224 227 public void resetToPreferredSizes() { 228 if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null && rightComponent != null) { 229 setPreferredSize( 230 new Dimension( 231 leftComponent.getPreferredSize().width 232 + rightComponent.getPreferredSize().width, 233 leftComponent.getPreferredSize().height 234 + rightComponent.getPreferredSize().height)); 235 } 236 } 237 238 242 public void setDividerLocation(double proportionalLocation) { 243 if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null) { 244 setResizeWeight(proportionalLocation); 245 } 246 } 247 248 public void setDividerLocation(int location) { 249 if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null) { 250 setResizeWeight( ((double)location) / ((double)getWidth()) ); 251 } 252 } 253 254 } 255 | Popular Tags |