1 19 24 25 package org.netbeans.modules.retouche.navigation.base; 26 27 import javax.swing.*; 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.lang.ref.*; 31 32 42 public final class TapPanel extends javax.swing.JPanel { 43 public static final int UP = 0; 44 public static final int DOWN = 2; 45 46 public static final String PROP_ORIENTATION = "orientation"; private int orientation = UP; 48 private boolean armed = false; 49 private boolean expanded = true; 50 private int minimumHeight = 8; 51 52 55 public TapPanel () { 56 setLayout ( new TrivialLayout () ); 57 } 58 59 private static WeakReference<Adap> adapRef = null; 60 61 static class Adap extends MouseAdapter implements MouseMotionListener { 62 MouseListener other = null; 63 64 public void mouseEntered (MouseEvent e) { 65 ( (TapPanel) e.getSource () ).setArmed ( true ); 66 } 67 68 public void mouseExited (MouseEvent e) { 69 ( (TapPanel) e.getSource () ).setArmed ( false ); 70 } 71 72 public void mouseMoved (MouseEvent e) { 73 ( (TapPanel) e.getSource () ).setArmed ( ( (TapPanel) e.getSource () ).isArmPoint ( e.getPoint () ) ); 74 } 75 76 public void mousePressed (MouseEvent e) { 77 if ( ( (TapPanel) e.getSource () ).isArmPoint ( e.getPoint () ) ) { 78 ( (TapPanel) e.getSource () ).setExpanded ( !( (TapPanel) e.getSource () ).isExpanded () ); 79 e.consume (); 80 } else if ( other != null ) { 81 other.mousePressed ( e ); 82 } 83 } 84 85 public void mouseDragged (MouseEvent e) { 86 } 88 } 89 90 private static Adap getAdapter () { 91 Adap result = null; 92 if ( adapRef != null ) { 93 result = (Adap) adapRef.get (); 94 } 95 if ( result == null ) { 96 result = new Adap (); 97 adapRef = new WeakReference<Adap>( result ); 98 } 99 return result; 100 } 101 102 106 void setSecondaryMouseHandler (MouseListener lis) { 107 getAdapter ().other = lis; 108 } 109 110 public void addNotify () { 111 addMouseMotionListener ( getAdapter () ); 112 addMouseListener ( getAdapter () ); 113 super.addNotify (); 114 } 115 116 public void removeNotify () { 117 super.removeNotify (); 118 removeMouseMotionListener ( getAdapter () ); 119 removeMouseListener ( getAdapter () ); 120 } 121 122 public int getOrientation () { 123 return orientation; 124 } 125 126 public void setOrientation (int i) { 127 if ( i != orientation ) { 128 int oldOr = i; 129 orientation = i; 130 firePropertyChange ( PROP_ORIENTATION, oldOr, i ); 131 } 132 } 133 134 private void setArmed (boolean val) { 135 if ( val != armed ) { 136 armed = val; 137 repaint (); 138 } 139 } 140 141 public boolean isExpanded () { 142 return expanded; 143 } 144 145 public Dimension getPreferredSize () { 146 return getLayout ().preferredLayoutSize ( this ); 147 } 148 149 public Dimension getMinimumSize () { 150 Dimension d = getPreferredSize (); 151 d.width = 20; 152 return d; 153 } 154 155 public Dimension getMaximumSize () { 156 return getPreferredSize (); 157 } 158 159 public void setExpanded (boolean b) { 160 if ( expanded != b ) { 161 Dimension d = getPreferredSize (); 162 expanded = b; 163 Dimension d1 = getPreferredSize (); 164 if ( isDisplayable () ) { 165 revalidate(); 166 } 167 } 168 } 169 170 private boolean isArmPoint (Point p) { 171 if ( !expanded ) { 172 return p.y > 0 && p.y < getHeight (); 173 } else { 174 if ( orientation == UP ) { 175 return p.y > getHeight () - minimumHeight; 176 } else { 177 return p.y < minimumHeight; 178 } 179 } 180 } 181 182 public void updateBorder () { 183 if ( orientation == UP ) { 184 super.setBorder ( BorderFactory.createEmptyBorder ( 0, 0, minimumHeight, 0 ) ); 185 } else { 186 super.setBorder ( BorderFactory.createEmptyBorder ( minimumHeight, 0, 0, 0 ) ); 187 } 188 } 189 190 public int getMinimumHeight () { 191 return minimumHeight; 192 } 193 194 public void setBorder () { 195 } 197 198 public void paintBorder (Graphics g) { 199 Color c = armed ? UIManager.getColor ( "List.selectionBackground" ) : getBackground (); int x = 0; 201 int y = orientation == UP ? 1 + ( getHeight () - minimumHeight ) : 0; 202 int w = getWidth (); 203 int h = minimumHeight - 1; 204 g.setColor ( c ); 205 g.fillRect ( x, y, w, h ); 206 207 int pos = orientation == UP ? getHeight () - 1 : 0; 208 int dir = orientation == UP ? -1 : 1; 209 g.setColor ( armed ? c.darker () : UIManager.getColor ( "controlShadow" ) ); g.drawLine ( 0, pos, w, pos ); 211 pos += dir; 212 213 if ( ( orientation == UP ) == expanded ) { 214 up.paintIcon ( this, g, ( getWidth () / 2 ) - ( up.getIconWidth () / 2 ), 215 getHeight () - ( minimumHeight + ( expanded ? 0 : -1 ) ) ); 216 } else { 217 down.paintIcon ( this, g, ( getWidth () / 2 ) - ( up.getIconWidth () / 2 ), expanded ? 2 : 1 ); 218 } 219 } 220 221 public void paintChildren (Graphics g) { 222 if ( !expanded ) return; 223 super.paintChildren(g); 224 } 225 226 private Icon up = new UpIcon (); 227 private Icon down = new DownIcon (); 228 229 private int ICON_SIZE = 8; 230 231 private class UpIcon implements Icon { 232 public int getIconHeight () { 233 return ICON_SIZE - 2; 234 } 235 236 public int getIconWidth () { 237 return ICON_SIZE + 2; 238 } 239 240 public void paintIcon (java.awt.Component c, Graphics g, int x, int y) { 241 242 g.setColor ( armed ? 243 UIManager.getColor ( "List.selectionForeground" ) : UIManager.getColor ( "controlShadow" ) ); 248 int[] xPoints = new int[]{x, x + 8, x + 4}; 249 int[] yPoints = new int[]{y + 5, y + 5, y}; 250 251 g.fillPolygon ( xPoints, yPoints, 3 ); 252 } 253 } 254 255 private class DownIcon implements Icon { 256 257 public int getIconHeight () { 258 return ICON_SIZE - 3; 259 } 260 261 public int getIconWidth () { 262 return ICON_SIZE + 2; 263 } 264 265 public void paintIcon (java.awt.Component c, Graphics g, int x, int y) { 266 x++; 267 g.setColor ( armed ? 268 UIManager.getColor ( "List.selectionForeground" ) : UIManager.getColor ( "controlShadow" ) ); 274 int[] xPoints = new int[]{x, x + 8, x + 4}; 275 int[] yPoints = new int[]{y, y, y + 4}; 276 277 g.fillPolygon ( xPoints, yPoints, 3 ); 278 } 279 280 } 281 282 } 283 | Popular Tags |