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