1 19 package org.netbeans.swing.tabcontrol; 20 21 import java.awt.Color ; 22 import java.awt.Component ; 23 import java.awt.Insets ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import javax.swing.JComponent ; 27 import javax.swing.Timer ; 28 import javax.swing.JToggleButton ; 29 import javax.swing.SwingConstants ; 30 import javax.swing.UIManager ; 31 import javax.swing.plaf.ButtonUI ; 32 33 41 public final class SlidingButton extends JToggleButton { 42 43 public static final String UI_CLASS_ID = "SlidingButtonUI"; 44 45 50 51 private int orientation; 52 53 private TabData data; 54 55 56 61 public SlidingButton(TabData buttonData, int orientation) { 62 super(buttonData.getText(), buttonData.getIcon(), false); 63 64 Component comp = buttonData.getComponent(); 67 if (comp instanceof JComponent ) { 68 Object slidingName = ((JComponent )comp).getClientProperty("SlidingName"); 69 if (slidingName instanceof String ) { 70 setText((String )slidingName); 71 } 72 } 73 74 this.orientation = orientation; 75 data = buttonData; 76 setFocusable(false); 79 setRolloverEnabled(true); 81 setIconTextGap(3); 82 setVerticalAlignment(SwingConstants.CENTER); 83 setHorizontalAlignment(SwingConstants.CENTER); 84 setMargin(new Insets (0,3,0,3)); 86 setBorderPainted(false); 87 } 91 92 public void addNotify() { 93 super.addNotify(); 94 } 96 97 public void removeNotify() { 98 super.removeNotify(); 99 setBlinking(false); 100 } 102 103 public String getToolTipText() { 104 return data.getTooltip(); 105 } 106 107 108 109 public void updateUI () { 110 ButtonUI ui = (ButtonUI )UIManager.getUI(this); 111 if (ui == null) { 112 ui = (ButtonUI )SlidingButtonUI.createUI(this); 114 } 115 setUI (ui); 116 } 117 118 public String getUIClassID() { 119 return UI_CLASS_ID; 120 } 121 122 123 124 125 public int getOrientation() { 126 return orientation; 127 } 128 129 public boolean isBlinking() { 130 return blinkState; 131 } 132 133 private Timer blinkTimer = null; 134 private boolean blinkState = false; 135 public void setBlinking (boolean val) { 136 if (!val && blinkTimer != null) { 137 blinkTimer.stop(); 138 blinkTimer = null; 139 boolean wasBlinkState = blinkState; 140 blinkState = false; 141 if (wasBlinkState) { 142 repaint(); 143 } 144 } else if (val && blinkTimer == null) { 145 blinkTimer = new Timer (700, new BlinkListener()); 146 blinkState = true; 147 blinkTimer.start(); 148 repaint(); 149 } 150 } 151 152 private class BlinkListener implements ActionListener { 153 public void actionPerformed (ActionEvent ae) { 154 blinkState = !blinkState; 155 repaint(); 156 } 157 } 158 159 163 public final boolean isBlinkState() { 164 return blinkState; 165 } 166 167 public final Color getBackground() { 168 return isBlinkState() ? 169 new Color (252, 250, 244) : super.getBackground(); 170 } 171 172 public TabData getData() { 173 return data; 174 } 175 176 } 177 | Popular Tags |