1 19 20 21 package org.netbeans.core.windows.view; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import org.netbeans.core.windows.Constants; 29 import org.netbeans.core.windows.Debug; 30 import org.netbeans.core.windows.view.ui.MultiSplitPane; 31 32 import javax.swing.*; 33 import java.awt.*; 34 import java.beans.PropertyChangeEvent ; 35 import java.beans.PropertyChangeListener ; 36 37 38 43 public class SplitView extends ViewElement { 44 45 private int orientation; 46 47 private ArrayList <Double > splitWeights; 48 49 private ArrayList <ViewElement> children; 50 51 private MultiSplitPane splitPane; 52 53 private boolean isDirty = false; 54 55 public SplitView(Controller controller, double resizeWeight, 56 int orientation, List <Double > splitWeights, List <ViewElement> children) { 57 super(controller, resizeWeight); 58 59 this.orientation = orientation; 60 this.splitWeights = new ArrayList <Double >( splitWeights ); 61 this.children = new ArrayList <ViewElement>( children ); 62 } 63 64 public void setOrientation( int newOrientation ) { 65 this.orientation = newOrientation; 66 } 67 68 public void setSplitWeights( List <Double > newSplitWeights ) { 69 splitWeights.clear(); 70 splitWeights.addAll( newSplitWeights ); 71 } 72 73 public int getOrientation() { 74 return orientation; 75 } 76 77 public List <ViewElement> getChildren() { 78 return new ArrayList <ViewElement>( children ); 79 } 80 81 public Component getComponent() { 82 return getSplitPane(); 83 } 84 85 public void remove( ViewElement view ) { 86 int index = children.indexOf( view ); 87 if( index >= 0 ) { 88 children.remove( index ); 89 splitWeights.remove( index ); 90 if( null != splitPane ) { 91 splitPane.removeViewElementAt( index ); 92 } 93 isDirty = true; } 95 } 96 97 public void setChildren( List <ViewElement> newChildren ) { 98 children.clear(); 99 children.addAll( newChildren ); 100 101 assert children.size() == splitWeights.size(); 102 103 isDirty = true; 105 if( null != splitPane ) { 106 updateSplitPane(); 107 } 108 } 109 110 public boolean updateAWTHierarchy(Dimension availableSpace) { 111 boolean res = false; 112 113 if( !availableSpace.equals( getSplitPane().getSize() ) || isDirty ) { 114 isDirty = false; 115 getSplitPane().setSize( availableSpace ); 116 getSplitPane().invalidate(); 117 res = true; 118 } 119 for( Iterator i=children.iterator(); i.hasNext(); ) { 120 ViewElement child = (ViewElement)i.next(); 121 res |= child.updateAWTHierarchy( child.getComponent().getSize() ); 122 } 123 124 return res; 125 } 126 127 private MultiSplitPane getSplitPane() { 128 if(splitPane == null) { 129 splitPane = new MultiSplitPane(); 130 updateSplitPane(); 131 132 133 splitPane.setDividerSize(orientation == JSplitPane.VERTICAL_SPLIT 134 ? Constants.DIVIDER_SIZE_VERTICAL 135 : Constants.DIVIDER_SIZE_HORIZONTAL); 136 137 splitPane.setBorder(BorderFactory.createEmptyBorder()); 138 139 splitPane.addPropertyChangeListener("splitPositions", new PropertyChangeListener () { 141 public void propertyChange(PropertyChangeEvent evt) { 142 ArrayList <Double > weights = new ArrayList <Double >( children.size() ); 143 ArrayList <ViewElement> views = new ArrayList <ViewElement>( children.size() ); 144 splitPane.calculateSplitWeights( views, weights ); 145 ViewElement[] arrViews = new ViewElement[views.size()]; 146 double[] arrWeights = new double[views.size()]; 147 for( int i=0; i<views.size(); i++ ) { 148 arrViews[i] = views.get( i ); 149 arrWeights[i] = weights.get( i ).doubleValue(); 150 } 151 getController().userMovedSplit( SplitView.this, arrViews, arrWeights ); 152 } 153 }); 154 } 155 156 return splitPane; 157 } 158 159 public int getDividerSize() { 160 return getSplitPane().getDividerSize(); 161 } 162 163 public String toString() { 164 StringBuffer buffer = new StringBuffer (); 165 buffer.append( super.toString() ); 166 buffer.append( "[" ); for( int i=0; i<children.size(); i++ ) { 168 ViewElement child = (ViewElement)children.get( i ); 169 buffer.append( (i+1) ); 170 buffer.append( '=' ); 171 if( child instanceof SplitView ) { 172 buffer.append( child.getClass() ); 173 buffer.append( '@' ); buffer.append( Integer.toHexString(child.hashCode()) ); 175 } else { 176 buffer.append( child.toString() ); 177 } 178 if( i < children.size()-1 ) 179 buffer.append( ", " ); } 181 buffer.append( "]" ); 183 return buffer.toString(); 184 } 185 186 private void updateSplitPane() { 187 ViewElement[] arrViews = new ViewElement[children.size()]; 188 double[] arrSplitWeights = new double[children.size()]; 189 for( int i=0; i<children.size(); i++ ) { 190 ViewElement view = (ViewElement)children.get( i ); 191 192 arrViews[i] = view; 193 arrSplitWeights[i] = ((Double )splitWeights.get(i)).doubleValue(); 194 } 195 splitPane.setChildren( orientation, arrViews, arrSplitWeights ); 196 } 197 198 private static void debugLog(String message) { 199 Debug.log(SplitView.class, message); 200 } 201 } 202 203 | Popular Tags |