1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Component ; 23 import java.awt.Image ; 24 import java.awt.Toolkit ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.FocusEvent ; 28 import java.awt.event.MouseEvent ; 29 import java.net.URL ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import javax.swing.Action ; 33 import javax.swing.Icon ; 34 import javax.swing.ImageIcon ; 35 import javax.swing.Timer ; 36 import javax.swing.ToolTipManager ; 37 import org.netbeans.swing.tabcontrol.TabData; 38 import org.netbeans.swing.tabcontrol.TabDisplayer; 39 import org.netbeans.swing.tabcontrol.TabListPopupAction; 40 import org.netbeans.swing.tabcontrol.WinsysInfoForTabbed; 41 42 47 class TabControlButtonFactory { 48 49 private static IconLoader iconCache; 50 51 private TabControlButtonFactory() { 52 } 53 54 public static Icon getIcon( String iconPath ) { 55 if( null == iconCache ) 56 iconCache = new IconLoader(); 57 return iconCache.obtainIcon( iconPath ); 58 } 59 60 63 public static TabControlButton createCloseButton( TabDisplayer displayer ) { 64 return new CloseButton( displayer ); 65 } 66 67 71 public static TabControlButton createSlidePinButton( TabDisplayer displayer ) { 72 return new SlidePinButton( displayer ); 73 } 74 75 79 public static TabControlButton createMaximizeRestoreButton( TabDisplayer displayer ) { 80 return new MaximizeRestoreButton( displayer ); 81 } 82 83 public static TabControlButton createScrollLeftButton( TabDisplayer displayer, Action scrollAction ) { 84 TabControlButton button = new TimerButton( TabControlButton.ID_SCROLL_LEFT_BUTTON, displayer, scrollAction ); 85 button.setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Scroll_Documents_Left") ); 86 return button; 87 } 88 89 public static TabControlButton createScrollRightButton( TabDisplayer displayer, Action scrollAction ) { 90 TabControlButton button = new TimerButton( TabControlButton.ID_SCROLL_RIGHT_BUTTON, displayer, scrollAction ); 91 button.setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Scroll_Documents_Right") ); 92 return button; 93 } 94 95 public static TabControlButton createDropDownButton( TabDisplayer displayer ) { 96 return new DropDownButton( displayer ); 97 } 98 99 private static class CloseButton extends TabControlButton { 100 101 public CloseButton( TabDisplayer displayer ) { 102 super( TabControlButton.ID_CLOSE_BUTTON, displayer ); 103 setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Close_Window") ); 104 } 105 106 protected String getTabActionCommand( ActionEvent e ) { 107 return TabDisplayer.COMMAND_CLOSE; 112 } 113 } 114 115 private static class SlidePinButton extends TabControlButton { 116 117 public SlidePinButton( TabDisplayer displayer ) { 118 super( displayer ); 119 ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); 120 toolTipManager.registerComponent( this ); 121 } 122 123 protected String getTabActionCommand( ActionEvent e ) { 124 if( getButtonId() == TabControlButton.ID_PIN_BUTTON ) 125 return TabDisplayer.COMMAND_DISABLE_AUTO_HIDE; 126 return TabDisplayer.COMMAND_ENABLE_AUTO_HIDE; 127 } 128 129 protected int getButtonId() { 130 int retValue = TabControlButton.ID_PIN_BUTTON; 131 Component currentTab = getActiveTab( getTabDisplayer() ); 132 if( null != currentTab ) { 133 WinsysInfoForTabbed winsysInfo = getTabDisplayer().getWinsysInfo(); 134 if( null != winsysInfo ) { 135 Object orientation = winsysInfo.getOrientation( currentTab ); 136 if( TabDisplayer.ORIENTATION_EAST.equals( orientation ) ) 137 retValue = TabControlButton.ID_SLIDE_RIGHT_BUTTON; 138 else if( TabDisplayer.ORIENTATION_WEST.equals( orientation ) ) 139 retValue = TabControlButton.ID_SLIDE_LEFT_BUTTON; 140 else if( TabDisplayer.ORIENTATION_SOUTH.equals( orientation ) ) 141 retValue = TabControlButton.ID_SLIDE_DOWN_BUTTON; 142 } 143 } 144 145 return retValue; 146 } 147 148 public String getToolTipText() { 149 if( getButtonId() == TabControlButton.ID_PIN_BUTTON ) 150 return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Pin"); 151 return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Minimize_Window"); 152 } 153 } 154 155 private static class MaximizeRestoreButton extends TabControlButton { 156 157 public MaximizeRestoreButton( TabDisplayer displayer ) { 158 super( displayer ); 159 ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); 160 toolTipManager.registerComponent( this ); 161 } 162 163 protected String getTabActionCommand( ActionEvent e ) { 164 return TabDisplayer.COMMAND_MAXIMIZE; 165 } 166 167 protected int getButtonId() { 168 int retValue = TabControlButton.ID_MAXIMIZE_BUTTON; 169 Component currentTab = getActiveTab( getTabDisplayer() ); 170 if( null != currentTab ) { 171 WinsysInfoForTabbed winsysInfo = getTabDisplayer().getWinsysInfo(); 172 if( null != winsysInfo ) { 173 if( winsysInfo.inMaximizedMode( currentTab ) ) { 174 retValue = TabControlButton.ID_RESTORE_BUTTON; 175 } 176 } 177 } 178 179 return retValue; 180 } 181 182 public String getToolTipText() { 183 if( getButtonId() == TabControlButton.ID_MAXIMIZE_BUTTON ) 184 return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Maximize_Window"); 185 return java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Restore_Window"); 186 } 187 } 188 189 private static Component getActiveTab( TabDisplayer displayer ) { 190 Component res = null; 191 int selIndex = displayer.getSelectionModel().getSelectedIndex(); 192 if( selIndex >= 0 ) { 193 TabData tab = displayer.getModel().getTab( selIndex ); 194 res = tab.getComponent(); 195 } 196 return res; 197 } 198 199 200 205 private static class TimerButton extends TabControlButton implements ActionListener { 206 Timer timer = null; 207 208 public TimerButton( int buttonId, TabDisplayer displayer, Action a ) { 209 super( buttonId, displayer ); 210 setAction( a ); 211 } 212 213 private Timer getTimer() { 214 if (timer == null) { 215 timer = new Timer (400, this); 216 timer.setRepeats(true); 217 } 218 return timer; 219 } 220 221 int count = 0; 222 223 public void actionPerformed( ActionEvent e ) { 224 count++; 225 if (count > 2) { 226 if (count > 5) { 227 timer.setDelay(75); 228 } else { 229 timer.setDelay(200); 230 } 231 } 232 performAction(); 233 } 234 235 private void performAction() { 236 if (!isEnabled()) { 237 stopTimer(); 238 return; 239 } 240 getAction().actionPerformed(new ActionEvent (this, 241 ActionEvent.ACTION_PERFORMED, 242 getActionCommand())); 243 } 244 245 private void startTimer() { 246 Timer t = getTimer(); 247 if (t.isRunning()) { 248 return; 249 } 250 repaint(); 251 t.setDelay(400); 252 t.start(); 253 } 254 255 private void stopTimer() { 256 if (timer != null) { 257 timer.stop(); 258 } 259 repaint(); 260 count = 0; 261 } 262 263 protected void processMouseEvent(MouseEvent me) { 264 if (isEnabled() && me.getID() == me.MOUSE_PRESSED) { 265 startTimer(); 266 } else if (me.getID() == me.MOUSE_RELEASED) { 267 stopTimer(); 268 } 269 super.processMouseEvent(me); 270 } 271 272 protected void processFocusEvent(FocusEvent fe) { 273 super.processFocusEvent(fe); 274 if (fe.getID() == fe.FOCUS_LOST) { 275 stopTimer(); 276 } 277 } 278 279 protected String getTabActionCommand(ActionEvent e) { 280 return null; 281 } 282 } 283 284 287 private static class DropDownButton extends TabControlButton { 288 289 private boolean forcePressedIcon = false; 290 291 public DropDownButton( TabDisplayer displayer ) { 292 super( TabControlButton.ID_DROP_DOWN_BUTTON, displayer ); 293 setAction( new TabListPopupAction( displayer ) ); 294 setToolTipText( java.util.ResourceBundle.getBundle("org/netbeans/swing/tabcontrol/plaf/Bundle").getString("Tip_Show_Opened_Documents_List") ); 295 } 296 297 protected void processMouseEvent(MouseEvent me) { 298 super.processMouseEvent(me); 299 if (isEnabled() && me.getID() == me.MOUSE_PRESSED) { 300 forcePressedIcon = true; 301 repaint(); 302 getAction().actionPerformed(new ActionEvent (this, 303 ActionEvent.ACTION_PERFORMED, 304 "pressed")); 305 } else if (isEnabled() && me.getID() == me.MOUSE_RELEASED) { 306 forcePressedIcon = false; 307 repaint(); 308 } 309 } 310 311 protected String getTabActionCommand(ActionEvent e) { 312 return null; 313 } 314 315 void performAction( ActionEvent e ) { 316 } 317 318 public Icon getRolloverIcon() { 319 if( forcePressedIcon ) 320 return getPressedIcon(); 321 322 return super.getRolloverIcon(); 323 } 324 325 public Icon getIcon() { 326 if( forcePressedIcon ) 327 return getPressedIcon(); 328 329 return super.getIcon(); 330 } 331 } 332 333 334 337 final private static class IconLoader { 338 339 private Map <String ,Icon > paths2Icons; 340 341 348 public Icon obtainIcon(String iconPath) { 349 if (paths2Icons == null) { 350 paths2Icons = new HashMap <String ,Icon >(6); 351 } 352 Icon icon = paths2Icons.get(iconPath); 353 if (icon == null) { 354 Image image = loadImage(iconPath); 356 if (image == null) { 357 throw new IllegalArgumentException ("Icon with resource path: " 358 + iconPath 359 + " can't be loaded, probably wrong path."); 360 } 361 icon = new ImageIcon (image); 362 paths2Icons.put(iconPath, icon); 363 } 364 return icon; 365 } 366 367 } 369 private static Image loadImage(String path) { 370 try { 371 URL url = TabControlButtonFactory.class.getResource("/"+path); 372 return Toolkit.getDefaultToolkit().createImage(url); 373 } catch (Exception e) { 374 e.printStackTrace(); 375 return null; 376 } 377 } 378 } 379 | Popular Tags |