1 /* 2 Copyright aswing.org, see the LICENCE.txt. 3 */ 4 5 import org.aswing.ASWingConstants; 6 import org.aswing.Container; 7 import org.aswing.Insets; 8 import org.aswing.plaf.ToolBarUI; 9 import org.aswing.SoftBoxLayout; 10 import org.aswing.UIManager; 11 12 /** 13 * <code>JToolBar</code> provides a component that is useful for 14 * displaying commonly used <code>Action</code>s or controls. 15 * 16 * <p> 17 * JToolBar will change buttons's isOpaque() method, so if your programe's logic is related 18 * to button's opaque, take care to add buttons to JToolBar. 19 * 20 * <p> 21 * With most look and feels, 22 * the user can drag out a tool bar into a separate window 23 * (unless the <code>floatable</code> property is set to <code>false</code>). 24 * For drag-out to work correctly, it is recommended that you add 25 * <code>JToolBar</code> instances to one of the four "sides" of a 26 * container whose layout manager is a <code>BorderLayout</code>, 27 * and do not add children to any of the other four "sides". 28 * <p> 29 * 30 * @author iiley 31 */ 32 class org.aswing.JToolBar extends Container { 33 34 public static var HORIZONTAL:Number = ASWingConstants.HORIZONTAL; 35 public static var VERTICAL:Number = ASWingConstants.VERTICAL; 36 37 private var margin:Insets; 38 private var floatable:Boolean; 39 private var orientation:Number; 40 private var title:String; 41 42 /** 43 * JToolBar(title:String, orientation:Number)<br> 44 * JToolBar(title:String)<br> default orientation to HORIZONTAL 45 * JToolBar()<br>default title to null, orientation to HORIZONTAL 46 * <p> 47 * Creates a new tool bar with a specified <code>title</code> and 48 * <code>orientation</code>. 49 * title is only shown when the tool bar is undocked. 50 * @param title the title of the tool bar 51 * @param orientation orientation the initial orientation -- it must be 52 * either <code>HORIZONTAL</code> or <code>VERTICAL</code> 53 */ 54 public function JToolBar(title:String, orientation:Number) { 55 super(); 56 this.title = (title == undefined ? null : title); 57 this.orientation = (orientation == undefined ? HORIZONTAL : orientation); 58 setLayoutWidthOrientation(); 59 updateUI(); 60 } 61 62 public function updateUI():Void{ 63 setUI(ToolBarUI(UIManager.getUI(this))); 64 } 65 66 public function setUI(newUI:ToolBarUI):Void{ 67 super.setUI(newUI); 68 } 69 70 public function getUIClassID():String{ 71 return "ToolBarUI"; 72 } 73 74 /** 75 * Sets the margin between the tool bar's border and 76 * its buttons. Setting to <code>null</code> causes the tool bar to 77 * use the default margins. The tool bar's default <code>Border</code> 78 * object uses this value to create the proper margin. 79 * However, if a non-default border is set on the tool bar, 80 * it is that <code>Border</code> object's responsibility to create the 81 * appropriate margin space (otherwise this property will 82 * effectively be ignored). 83 * 84 * @param m an <code>Insets</code> object that defines the space 85 * between the border and the buttons 86 * @see Insets 87 */ 88 public function setMargin(m:Insets):Void{ 89 if(m != margin || !m.equals(margin)){ 90 margin = m; 91 revalidate(); 92 repaint(); 93 } 94 } 95 96 /** 97 * Returns the margin between the tool bar's border and 98 * its buttons. 99 * 100 * @return an <code>Insets</code> object containing the margin values 101 * @see Insets 102 */ 103 public function getMargin():Insets{ 104 if(margin == null){ 105 return new Insets(0, 0, 0, 0); 106 }else{ 107 return (new Insets()).addInsets(margin);//return a copy 108 } 109 } 110 111 /** 112 * Gets the <code>floatable</code> property. 113 * @return the value of the <code>floatable</code> property 114 * @see #setFloatable() 115 */ 116 public function isFloatable():Boolean{ 117 return floatable; 118 } 119 120 /** 121 * Sets the <code>floatable</code> property, 122 * which must be <code>true</code> for the user to move the tool bar. 123 * Typically, a floatable tool bar can be 124 * dragged into a different position within the same container 125 * or out into its own window. 126 * The default value of this property is <code>true</code>. 127 * Some look and feels might not implement floatable tool bars; 128 * they will ignore this property. 129 * 130 * @param b if <code>true</code>, the tool bar can be moved; 131 * <code>false</code> otherwise 132 * @see #isFloatable() 133 */ 134 public function setFloatable(b:Boolean):Void{ 135 if(b != floatable){ 136 floatable = b; 137 revalidate(); 138 repaint(); 139 } 140 } 141 142 /** 143 * Returns the current orientation of the tool bar. The value is either 144 * <code>HORIZONTAL</code> or <code>VERTICAL</code>. 145 * 146 * @return an integer representing the current orientation -- either 147 * <code>HORIZONTAL</code> or <code>VERTICAL</code> 148 * @see #setOrientation() 149 */ 150 public function getOrientation():Number{ 151 return orientation; 152 } 153 154 /** 155 * Sets the orientation of the tool bar. The orientation must have 156 * either the value <code>HORIZONTAL</code> or <code>VERTICAL</code>. 157 * If <code>orientation</code> is 158 * an invalid value, default it to HORIZONTAL. 159 * 160 * @param o the new orientation -- either <code>HORIZONTAL</code> or 161 * </code>VERTICAL</code> 162 * @see #getOrientation() 163 */ 164 public function setOrientation(o:Number):Void 165 { 166 if(o == undefined){ 167 o = HORIZONTAL; 168 } 169 170 if (orientation != o){ 171 orientation = o; 172 setLayoutWidthOrientation(); 173 revalidate(); 174 repaint(); 175 } 176 } 177 178 private function setLayoutWidthOrientation():Void{ 179 if(orientation == VERTICAL){ 180 setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS)); 181 }else{ 182 setLayout(new SoftBoxLayout(SoftBoxLayout.X_AXIS)); 183 } 184 } 185 186 } 187