litesoft
@ 947
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.aokeyhole.toolkit; import org.litesoft.aokeyhole.swing.*; import java.awt.*; import java.awt.geom.*; public class BoxedText { public static final BoxedText[] EMPTY_ARRAY = new BoxedText[0]; private static int sNextID = 0; private int mID; private int mType; private Color mBackGround; private String mName = ""; private BoxedSet mControllingSet = null; private int mAtX; private int mAtY; private boolean mUnfinished = true; private int mTextHeight; private int mTextWidth; private int mBoxWidth; private int mBoxHeight; private int mFontDecent; private Font mFont; private int mBoxAtX; private int mBoxAtY; public BoxedText( int pType, Color pBackGround, String pName ) { mID = getNextID(); mType = pType; mBackGround = pBackGround; setName( pName ); } private static synchronized int getNextID() { return sNextID++; } public int getID() { return mID; } public int getType() { return mType; } public String getName() { return mName; } public void setName( String pName ) { pName = (pName == null) ? "" : pName.trim(); if ( !mName.equals( pName ) ) { mUnfinished = true; mName = pName; if ( mControllingSet != null ) { mControllingSet.setChanged(); } } } public void setControllingSet( BoxedSet pControllingSet ) { mControllingSet = pControllingSet; } public void dispose() { mControllingSet = null; } public void finish( Graphics pGraphics ) { if ( mUnfinished ) { mUnfinished = false; FontMetrics metrics = pGraphics.getFontMetrics(); Font zFont = metrics.getFont(); mFont = AdjustFont.common( zFont ); if (mFont != zFont) { metrics = pGraphics.getFontMetrics( mFont ); } mFontDecent = metrics.getDescent(); Rectangle2D bounds = metrics.getStringBounds( mName, null ); mBoxHeight = (mTextHeight = (int) (bounds.getHeight() + 0.99)) + 3; mBoxWidth = (mTextWidth = (int) (bounds.getWidth() + 0.99)) + 5; } } public int getBoxWidth() { return mBoxWidth; } public void setBoxWidth( int pBoxWidth ) { mBoxWidth = pBoxWidth; } public int getBoxHeight() { return mBoxHeight; } public int getAtX() { return mAtX; } public int getAtY() { return mAtY; } public void setAtXY( int pAtX, int pAtY ) { mAtX = pAtX; mAtY = pAtY; } public void drawConnection( Graphics pGraphics, BoxedText pToBoxedText ) { if ( pToBoxedText == null ) { drawConnectionToSelf( pGraphics ); } else if ( this.getAtX() < pToBoxedText.getAtX() ) { drawConnectionLeftToRight( pGraphics, pToBoxedText ); } else { pToBoxedText.drawConnectionLeftToRight( pGraphics, this ); } } private void drawConnectionToSelf( Graphics pGraphics ) { int fromX = this.getAtX() + (this.mBoxWidth / 2); int fromY = this.getAtY(); int toX = fromX + 10; int toY = fromY; pGraphics.drawLine( fromX, fromY, toX, toY ); fromX = toX; fromY = toY; toX = fromX; toY = fromY - 5; pGraphics.drawLine( fromX, fromY, toX, toY ); fromX = toX; fromY = toY; toX = fromX - 5; toY = fromY; pGraphics.drawLine( fromX, fromY, toX, toY ); } private void drawConnectionLeftToRight( Graphics pGraphics, BoxedText pToBoxedText ) { pGraphics.drawLine( this.getAtX() + (this.mBoxWidth / 2), this.getAtY(), pToBoxedText.getAtX() - (pToBoxedText.mBoxWidth / 2), pToBoxedText.getAtY() ); } public void drawConnectionCenterToCenter( Graphics pGraphics, BoxedText pToBoxedText ) { pGraphics.drawLine( this.getAtX(), this.getAtY(), pToBoxedText.getAtX(), pToBoxedText.getAtY() ); } public void draw( Graphics pGraphics ) { drawBox( pGraphics ); drawText( pGraphics ); } public void drawBox( Graphics pGraphics ) { mBoxAtX = (mAtX - (mBoxWidth + 1) / 2); mBoxAtY = (mAtY - (mBoxHeight + 1) / 2); Color curColor = pGraphics.getColor(); pGraphics.setColor( mBackGround ); pGraphics.fillRect( mBoxAtX, mBoxAtY, mBoxWidth, mBoxHeight ); pGraphics.setColor( curColor ); pGraphics.drawRect( mBoxAtX, mBoxAtY, mBoxWidth, mBoxHeight ); } public void drawText( Graphics pGraphics ) { int atX = (mAtX - (mTextWidth + 1) / 2) + 1; int atY = (mAtY - (mTextHeight + 1) / 2) + mTextHeight - (mFontDecent + 1); Font curFont = AdjustFont.common( pGraphics.getFont() ); if ( mFont.equals( curFont ) ) { pGraphics.drawString( mName, atX, atY ); } else { pGraphics.setFont( mFont ); pGraphics.drawString( mName, atX, atY ); pGraphics.setFont( curFont ); } } public boolean isClickedOn( Point pAt ) { return isBetween( mBoxAtX, pAt.x, mBoxAtX + mBoxWidth ) && isBetween( mBoxAtY, pAt.y, mBoxAtY + mBoxHeight ); } private boolean isBetween( int pLow, int pToCheck, int pHi ) { return (pLow < pToCheck) && (pToCheck < pHi); } } |