1 19 package org.openharmonise.him.window.swing; 20 21 import java.awt.Component ; 22 import java.awt.event.ComponentAdapter ; 23 import java.awt.event.ComponentEvent ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 27 import javax.swing.JSplitPane ; 28 29 34 public class ExtendedSplitPane extends JSplitPane { 35 36 private double m_pos = 0.5; 37 38 private boolean reentrant = false; 39 40 43 public ExtendedSplitPane() { 44 super(); 45 setup(); 46 } 47 48 51 public ExtendedSplitPane(int newOrientation) { 52 super(newOrientation); 53 setup(); 54 } 55 56 60 public ExtendedSplitPane(int newOrientation, boolean newContinuousLayout) { 61 super(newOrientation, newContinuousLayout); 62 setup(); 63 } 64 65 70 public ExtendedSplitPane( 71 int newOrientation, 72 Component newLeftComponent, 73 Component newRightComponent) { 74 super(newOrientation, newLeftComponent, newRightComponent); 75 setup(); 76 } 77 78 84 public ExtendedSplitPane( 85 int newOrientation, 86 boolean newContinuousLayout, 87 Component newLeftComponent, 88 Component newRightComponent) { 89 super( 90 newOrientation, 91 newContinuousLayout, 92 newLeftComponent, 93 newRightComponent); 94 setup(); 95 } 96 97 private void setup() { 98 this.setOneTouchExpandable(true); 99 this.setContinuousLayout(true); 100 DividerListener l = new DividerListener(); 101 this.addPropertyChangeListener(l); 102 this.addComponentListener(l); 103 } 104 105 public void animateTo(double newLoc) { 106 if(newLoc!=this.m_pos) { 107 new MoveThread(this.m_pos, newLoc).run(); 108 } 109 } 110 111 public double getProportionalDividerLocation() { 112 return this.m_pos; 113 } 114 115 public void setDividerLocation(double loc) { 116 this.m_pos = loc; 117 super.setDividerLocation(loc); 118 } 119 120 public void setExtDividerLocation(int loc) { 121 super.setDividerLocation(loc); 122 } 123 124 public class DividerListener 125 extends ComponentAdapter 126 implements PropertyChangeListener { 127 double percentage = 1.0; 128 boolean ignore = false; 129 130 public void propertyChange(PropertyChangeEvent event) { 131 String prop = event.getPropertyName(); 132 JSplitPane split = (JSplitPane ) event.getSource(); 133 if (prop.equals("lastDividerLocation")) { 134 if (ignore) { 135 ignore = false; 136 } else { 137 if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) { 138 int location = getDividerLocation(); 139 double newPercentage = 140 ((double) location) / split.getWidth(); 141 if (percentage - newPercentage > 0.05 142 || newPercentage - percentage > 0.05) { 143 percentage = newPercentage; 144 } 145 } else { 146 int location = getDividerLocation(); 147 double newPercentage = 148 ((double) location) / split.getHeight(); 149 if (percentage - newPercentage > 0.05 150 || newPercentage - percentage > 0.05) { 151 percentage = newPercentage; 152 } 153 } 154 Component [] comps = getComponents(); 155 for(int i=0; i<comps.length;i++) { 156 comps[i].validate(); 157 } 158 } 159 } 160 m_pos = percentage; 161 } 162 163 public void componentResized(ComponentEvent event) { 164 if (percentage > 0 && percentage < 1) { 165 ignore = true; 166 setDividerLocation(percentage); 167 repaint(); 168 } 169 } 170 } 171 172 class MoveThread extends Thread { 173 174 private double m_currentLoc; 175 private double m_newLoc; 176 private int m_nCount = 5; 177 private double m_diff; 178 179 MoveThread(double currentLoc, double newLoc) { 180 this.m_currentLoc = currentLoc; 181 this.m_newLoc = newLoc; 182 if (m_currentLoc > m_newLoc) { 183 m_diff = (m_currentLoc - m_newLoc) / m_nCount; 184 } else if (newLoc > m_currentLoc) { 185 m_diff = (m_newLoc - m_currentLoc) / m_nCount; 186 } else { 187 m_diff = 0; 188 } 189 } 190 191 public void run() { 192 for (int n = 0; n < m_nCount; n++) { 193 if (m_currentLoc > m_newLoc) { 194 m_currentLoc = m_currentLoc + m_diff; 195 } else { 196 m_currentLoc = m_currentLoc - m_diff; 197 } 198 setDividerLocation(m_currentLoc); 199 try { 200 paintAll(getGraphics()); 201 Thread.sleep(20L); 202 } catch (Exception exception) { 203 } 204 } 205 } 206 } 207 208 211 public void setBottomComponent(Component arg0) { 212 super.setBottomComponent(arg0); 213 super.setDividerLocation(this.m_pos); 214 } 215 216 219 public void setLeftComponent(Component arg0) { 220 super.setLeftComponent(arg0); 221 if(this.m_pos>=0.0 && this.m_pos<=1.0) { 222 super.setDividerLocation(this.m_pos); 223 } 224 } 225 226 229 public void setRightComponent(Component arg0) { 230 super.setRightComponent(arg0); 231 super.setDividerLocation(this.m_pos); 232 } 233 234 237 public void setTopComponent(Component arg0) { 238 super.setTopComponent(arg0); 239 super.setDividerLocation(this.m_pos); 240 } 241 242 } 243 | Popular Tags |