[25584] | 1 | /*
|
---|
| 2 | * 12/23/2008
|
---|
| 3 | *
|
---|
| 4 | * SizeGrip.java - A size grip component that sits at the bottom of the window,
|
---|
| 5 | * allowing the user to easily resize that window.
|
---|
| 6 | *
|
---|
| 7 | * This library is distributed under a modified BSD license. See the included
|
---|
| 8 | * RSyntaxTextArea.License.txt file for details.
|
---|
| 9 | */
|
---|
| 10 | package org.fife.ui.rsyntaxtextarea.focusabletip;
|
---|
| 11 |
|
---|
| 12 | import java.awt.Color;
|
---|
| 13 | import java.awt.ComponentOrientation;
|
---|
| 14 | import java.awt.Cursor;
|
---|
| 15 | import java.awt.Dimension;
|
---|
| 16 | import java.awt.Graphics;
|
---|
| 17 | import java.awt.Image;
|
---|
| 18 | import java.awt.Point;
|
---|
| 19 | import java.awt.Window;
|
---|
| 20 | import java.awt.event.MouseEvent;
|
---|
| 21 | import java.io.File;
|
---|
| 22 | import java.io.IOException;
|
---|
| 23 | import java.net.MalformedURLException;
|
---|
| 24 | import java.net.URL;
|
---|
| 25 | import javax.imageio.ImageIO;
|
---|
| 26 | import javax.swing.JPanel;
|
---|
| 27 | import javax.swing.SwingUtilities;
|
---|
| 28 | import javax.swing.UIManager;
|
---|
| 29 | import javax.swing.event.MouseInputAdapter;
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * A component that allows its parent window to be resizable, similar to the
|
---|
| 34 | * size grip seen on status bars.
|
---|
| 35 | *
|
---|
| 36 | * @author Robert Futrell
|
---|
| 37 | * @version 1.0
|
---|
| 38 | */
|
---|
| 39 | class SizeGrip extends JPanel {
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * The size grip to use if we're on OS X.
|
---|
| 43 | */
|
---|
| 44 | private Image osxSizeGrip;
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | public SizeGrip() {
|
---|
| 48 | MouseHandler adapter = new MouseHandler();
|
---|
| 49 | addMouseListener(adapter);
|
---|
| 50 | addMouseMotionListener(adapter);
|
---|
| 51 | setPreferredSize(new Dimension(16, 16));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Overridden to ensure that the cursor for this component is appropriate
|
---|
| 57 | * for the orientation.
|
---|
| 58 | *
|
---|
| 59 | * @param o The new orientation.
|
---|
| 60 | */
|
---|
| 61 | public void applyComponentOrientation(ComponentOrientation o) {
|
---|
| 62 | possiblyFixCursor(o.isLeftToRight());
|
---|
| 63 | super.applyComponentOrientation(o);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Creates and returns the OS X size grip image.
|
---|
| 69 | *
|
---|
| 70 | * @return The OS X size grip.
|
---|
| 71 | */
|
---|
| 72 | private Image createOSXSizeGrip() {
|
---|
| 73 | ClassLoader cl = getClass().getClassLoader();
|
---|
| 74 | URL url = cl.getResource("org/fife/ui/autocomplete/osx_sizegrip.png");
|
---|
| 75 | if (url==null) {
|
---|
| 76 | // We're not running in a jar - we may be debugging in Eclipse,
|
---|
| 77 | // for example
|
---|
| 78 | File f = new File("../AutoComplete/src/org/fife/ui/autocomplete/osx_sizegrip.png");
|
---|
| 79 | if (f.isFile()) {
|
---|
| 80 | try {
|
---|
| 81 | url = f.toURI().toURL();
|
---|
| 82 | } catch (MalformedURLException mue) { // Never happens
|
---|
| 83 | mue.printStackTrace();
|
---|
| 84 | return null;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | else {
|
---|
| 88 | return null; // Can't find resource or image file
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | Image image = null;
|
---|
| 92 | try {
|
---|
| 93 | image = ImageIO.read(url);
|
---|
| 94 | } catch (IOException ioe) { // Never happens
|
---|
| 95 | ioe.printStackTrace();
|
---|
| 96 | }
|
---|
| 97 | return image;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Paints this panel.
|
---|
| 103 | *
|
---|
| 104 | * @param g The graphics context.
|
---|
| 105 | */
|
---|
| 106 | protected void paintComponent(Graphics g) {
|
---|
| 107 |
|
---|
| 108 | super.paintComponent(g);
|
---|
| 109 |
|
---|
| 110 | Dimension dim = getSize();
|
---|
| 111 | Color c1 = UIManager.getColor("Label.disabledShadow");
|
---|
| 112 | Color c2 = UIManager.getColor("Label.disabledForeground");
|
---|
| 113 |
|
---|
| 114 | if (osxSizeGrip!=null) {
|
---|
| 115 | g.drawImage(osxSizeGrip, dim.width-16, dim.height-16, null);
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | ComponentOrientation orientation = getComponentOrientation();
|
---|
| 120 |
|
---|
| 121 | if (orientation.isLeftToRight()) {
|
---|
| 122 | int width = dim.width -= 3;
|
---|
| 123 | int height = dim.height -= 3;
|
---|
| 124 | g.setColor(c1);
|
---|
| 125 | g.fillRect(width-9,height-1, 3,3);
|
---|
| 126 | g.fillRect(width-5,height-1, 3,3);
|
---|
| 127 | g.fillRect(width-1,height-1, 3,3);
|
---|
| 128 | g.fillRect(width-5,height-5, 3,3);
|
---|
| 129 | g.fillRect(width-1,height-5, 3,3);
|
---|
| 130 | g.fillRect(width-1,height-9, 3,3);
|
---|
| 131 | g.setColor(c2);
|
---|
| 132 | g.fillRect(width-9,height-1, 2,2);
|
---|
| 133 | g.fillRect(width-5,height-1, 2,2);
|
---|
| 134 | g.fillRect(width-1,height-1, 2,2);
|
---|
| 135 | g.fillRect(width-5,height-5, 2,2);
|
---|
| 136 | g.fillRect(width-1,height-5, 2,2);
|
---|
| 137 | g.fillRect(width-1,height-9, 2,2);
|
---|
| 138 | }
|
---|
| 139 | else {
|
---|
| 140 | int height = dim.height -= 3;
|
---|
| 141 | g.setColor(c1);
|
---|
| 142 | g.fillRect(10,height-1, 3,3);
|
---|
| 143 | g.fillRect(6,height-1, 3,3);
|
---|
| 144 | g.fillRect(2,height-1, 3,3);
|
---|
| 145 | g.fillRect(6,height-5, 3,3);
|
---|
| 146 | g.fillRect(2,height-5, 3,3);
|
---|
| 147 | g.fillRect(2,height-9, 3,3);
|
---|
| 148 | g.setColor(c2);
|
---|
| 149 | g.fillRect(10,height-1, 2,2);
|
---|
| 150 | g.fillRect(6,height-1, 2,2);
|
---|
| 151 | g.fillRect(2,height-1, 2,2);
|
---|
| 152 | g.fillRect(6,height-5, 2,2);
|
---|
| 153 | g.fillRect(2,height-5, 2,2);
|
---|
| 154 | g.fillRect(2,height-9, 2,2);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Ensures that the cursor for this component is appropriate for the
|
---|
| 162 | * orientation.
|
---|
| 163 | *
|
---|
| 164 | * @param ltr Whether the current component orientation is LTR.
|
---|
| 165 | */
|
---|
| 166 | protected void possiblyFixCursor(boolean ltr) {
|
---|
| 167 | int cursor = Cursor.NE_RESIZE_CURSOR;
|
---|
| 168 | if (ltr) {
|
---|
| 169 | cursor = Cursor.NW_RESIZE_CURSOR;
|
---|
| 170 | }
|
---|
| 171 | if (cursor!=getCursor().getType()) {
|
---|
| 172 | setCursor(Cursor.getPredefinedCursor(cursor));
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | public void updateUI() {
|
---|
| 178 | super.updateUI();
|
---|
| 179 | // TODO: Key off of Aqua LaF, not just OS X, as this size grip looks
|
---|
| 180 | // bad on other LaFs on Mac such as Nimbus.
|
---|
| 181 | if (System.getProperty("os.name").indexOf("OS X")>-1) {
|
---|
| 182 | if (osxSizeGrip==null) {
|
---|
| 183 | osxSizeGrip = createOSXSizeGrip();
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | else { // Clear memory in case of runtime LaF change.
|
---|
| 187 | osxSizeGrip = null;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * Listens for mouse events on this panel and resizes the parent window
|
---|
| 195 | * appropriately.
|
---|
| 196 | *
|
---|
| 197 | * @author Robert Futrell
|
---|
| 198 | * @version 1.0
|
---|
| 199 | */
|
---|
| 200 | /*
|
---|
| 201 | * NOTE: We use SwingUtilities.convertPointToScreen() instead of just using
|
---|
| 202 | * the locations relative to the corner component because the latter proved
|
---|
| 203 | * buggy - stretch the window too wide and some kind of arithmetic error
|
---|
| 204 | * started happening somewhere - our window would grow way too large.
|
---|
| 205 | */
|
---|
| 206 | private class MouseHandler extends MouseInputAdapter {
|
---|
| 207 |
|
---|
| 208 | private Point origPos;
|
---|
| 209 |
|
---|
| 210 | public void mouseDragged(MouseEvent e) {
|
---|
| 211 | Point newPos = e.getPoint();
|
---|
| 212 | SwingUtilities.convertPointToScreen(newPos, SizeGrip.this);
|
---|
| 213 | int xDelta = newPos.x - origPos.x;
|
---|
| 214 | int yDelta = newPos.y - origPos.y;
|
---|
| 215 | Window wind = SwingUtilities.getWindowAncestor(SizeGrip.this);
|
---|
| 216 | if (wind!=null) { // Should always be true
|
---|
| 217 | if (getComponentOrientation().isLeftToRight()) {
|
---|
| 218 | int w = wind.getWidth();
|
---|
| 219 | if (newPos.x>=wind.getX()) {
|
---|
| 220 | w += xDelta;
|
---|
| 221 | }
|
---|
| 222 | int h = wind.getHeight();
|
---|
| 223 | if (newPos.y>=wind.getY()) {
|
---|
| 224 | h += yDelta;
|
---|
| 225 | }
|
---|
| 226 | wind.setSize(w,h);
|
---|
| 227 | }
|
---|
| 228 | else { // RTL
|
---|
| 229 | int newW = Math.max(1, wind.getWidth()-xDelta);
|
---|
| 230 | int newH = Math.max(1, wind.getHeight()+yDelta);
|
---|
| 231 | wind.setBounds(newPos.x, wind.getY(), newW, newH);
|
---|
| 232 | }
|
---|
| 233 | // invalidate()/validate() needed pre-1.6.
|
---|
| 234 | wind.invalidate();
|
---|
| 235 | wind.validate();
|
---|
| 236 | }
|
---|
| 237 | origPos.setLocation(newPos);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | public void mousePressed(MouseEvent e) {
|
---|
| 241 | origPos = e.getPoint();
|
---|
| 242 | SwingUtilities.convertPointToScreen(origPos, SizeGrip.this);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | public void mouseReleased(MouseEvent e) {
|
---|
| 246 | origPos = null;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 | } |
---|