Changeset 188
- Timestamp:
- 09/09/11 11:22:59 (13 years ago)
- Location:
- ether_megapoli/trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
ether_megapoli/trunk/applets/src/gov/noaa/pmel/sgt/LabelDrawer2.java
r174 r188 11 11 */ 12 12 13 package gov.noaa.pmel.sgt; 14 13 package gov.noaa.pmel.sgt; 14 15 import gov.noaa.pmel.swing.MRJUtil; 15 16 import gov.noaa.pmel.util.Point2D; 16 17 import gov.noaa.pmel.util.Rectangle2D; 17 import gov.noaa.pmel.util.Debug;18 import gov.noaa.pmel.swing.MRJUtil;19 18 20 19 import java.awt.*; 21 import java.beans.*; 22 import java.awt.image.PixelGrabber; 23 import java.awt.image.MemoryImageSource; 24 import java.awt.image.ColorModel; 25 import java.awt.image.ImageObserver; 26 import java.awt.geom.AffineTransform; 20 import java.awt.font.FontRenderContext; 27 21 import java.awt.font.TextAttribute; 28 22 import java.awt.font.TextLayout; 29 import java.awt. font.FontRenderContext;23 import java.awt.geom.AffineTransform; 30 24 import java.text.AttributedString; 31 25 … … 37 31 * @since 2.0 38 32 */ 39 public class LabelDrawer2 implements LabelDrawer, Cloneable { 40 private String label_; 41 private Color clr_; 42 private Font font_; 43 private transient Layer layer_; 44 private int orient_; 45 private int halign_; 46 private int valign_; 47 private Point dorigin_; 48 private Rectangle dbounds_; 49 private Point2D.Double porigin_; 50 private Rectangle2D.Double pbounds_; 51 private Polygon dpolygon_; 52 private double angle_; 53 private double sinthta_; 54 private double costhta_; 55 private double height_; 56 private boolean visible_; 57 private static boolean fixMetrics_ = MRJUtil.fixFontMetrics(); 58 59 private int xoff_ = 0; 60 private int yoff_ = 0; 61 62 private boolean alwaysAntiAlias_ = true; 63 64 private Rectangle savedBounds_ = null; 65 private Point savedLoc_ = null; 66 67 public LabelDrawer2(String lbl, double hgt, Point2D.Double loc, 68 int valign, int halign) { 69 label_ = lbl; 70 height_ = hgt; 71 porigin_ = loc; 72 valign_ = valign; 73 halign_ = halign; 74 // 75 dbounds_ = new Rectangle(); 76 dorigin_ = new Point(0,0); 77 pbounds_ = new Rectangle2D.Double(); 78 } 79 80 public LabelDrawer copy() { 81 LabelDrawer1 newLabel = null; 33 public class LabelDrawer2 34 implements LabelDrawer, Cloneable 35 { 36 private String label_; 37 private Color clr_; 38 private Font font_; 39 private transient Layer layer_; 40 private int orient_; 41 private int halign_; 42 private int valign_; 43 private Point dorigin_; 44 private Rectangle dbounds_; 45 private Point2D.Double porigin_; 46 private Rectangle2D.Double pbounds_; 47 private Polygon dpolygon_; 48 private double angle_; 49 private double sinthta_; 50 private double costhta_; 51 private double height_; 52 private boolean visible_; 53 private static boolean fixMetrics_ = MRJUtil.fixFontMetrics(); 54 55 private int xoff_ = 0; 56 private int yoff_ = 0; 57 58 private boolean alwaysAntiAlias_ = true; 59 60 private Rectangle savedBounds_ = null; 61 private Point savedLoc_ = null; 62 63 public LabelDrawer2( String lbl, double hgt, Point2D.Double loc, 64 int valign, int halign ) 65 { 66 label_ = lbl; 67 height_ = hgt; 68 porigin_ = loc; 69 valign_ = valign; 70 halign_ = halign; 71 // 72 dbounds_ = new Rectangle(); 73 dorigin_ = new Point( 0, 0 ); 74 pbounds_ = new Rectangle2D.Double(); 75 } 76 77 public LabelDrawer copy() 78 { 79 LabelDrawer1 newLabel = null; 82 80 // try { 83 81 // newLabel = (LabelDrawer1)clone(); … … 93 91 // } 94 92 // } 95 return newLabel; 96 } 97 98 public void draw(Graphics g) throws LayerNotFoundException { 99 int xs, ys; 100 if((label_.length() <= 0) || !visible_ || g == null) return; 101 if(layer_ == (Layer) null) throw new LayerNotFoundException(); 102 // 103 // set label heigth in physical units 104 // 105 computeBoundsD(g); 106 if(clr_ == null) { 107 g.setColor(layer_.getPane().getComponent().getForeground()); 108 } else { 109 g.setColor(clr_); 110 } 111 if(orient_ == SGLabel.HORIZONTAL) { 112 xs = dbounds_.x + xoff_; 113 ys = dbounds_.y + yoff_; 114 drawString(g, xs, ys); 115 } else if(orient_ == SGLabel.VERTICAL) { 116 // 117 // draw text in offscreen image (horizontal) 118 // 119 xs = dbounds_.x + xoff_; 120 ys = dbounds_.y + yoff_; 121 drawString(g, xs, ys); 122 } else { 123 // 124 // Angled text, draw in offscreen image and rotate 125 // 126 xs = layer_.getXPtoD(porigin_.x); 127 ys = layer_.getYPtoD(porigin_.y); 128 drawString(g, xs, ys); 129 } 130 } 131 132 public void setText(String lbl) { 133 label_ = lbl; 134 } 135 public String getText() { 136 return label_; 137 } 138 139 public void setColor(Color clr) { 140 clr_ = clr; 141 } 142 public Color getColor() { 143 return clr_; 144 } 145 146 public void setFont(Font font) { 147 font_ = font; 148 } 149 public Font getFont() { 150 return font_; 151 } 152 153 public void setLayer(Layer layer) { 154 layer_ = layer; 155 if(savedBounds_ != null) { 156 setBounds(savedBounds_.x, savedBounds_.y, 157 savedBounds_.width, savedBounds_.height); 158 savedBounds_ = null; 159 } 160 if(savedLoc_ != null) { 161 setLocation(savedLoc_); 162 savedLoc_ = null; 163 } 164 } 165 public Layer getLayer() { 166 return layer_; 167 } 168 169 public void setOrientation(int orient) { 170 if(orient_ != orient) { 171 if(orient == SGLabel.HORIZONTAL) { 172 costhta_ = 1.0; 173 sinthta_ = 0.0; 174 } else if(orient == SGLabel.VERTICAL) { 175 costhta_ = 0.0; 176 sinthta_ = 1.0; 177 } 178 orient_ = orient; 179 } 180 } 181 public int getOrientation() { 182 return orient_; 183 } 184 185 public void setHAlign(int halign) { 186 halign_ = halign; 187 } 188 public int getHAlign() { 189 return halign_; 190 } 191 192 public void setVAlign(int valign) { 193 valign_ = valign; 194 } 195 public int getVAlign() { 196 return valign_; 197 } 198 199 public void setLocation(Point loc) { 200 if(layer_ == null) { 201 savedLoc_ = new Point(loc); 202 return; 203 } 204 computeBoundsD(layer_.getPane().getComponent().getGraphics()); 205 if(dbounds_.x != loc.x || dbounds_.y != loc.y) { 206 setBounds(loc.x, loc.y, dbounds_.width, dbounds_.height); 207 } 208 } 209 public Point getLocation() { 210 if(savedLoc_ != null) return savedLoc_; 211 return dorigin_; 212 } 213 214 public void setBounds(int x, int y, int width, int height) { 215 int swidth, sascent, xd, yd; 216 if(layer_ == null) { 217 savedBounds_ = new Rectangle(x, y, width, height); 218 return; 219 } 220 Graphics g = layer_.getPane().getComponent().getGraphics(); 221 if(g == null) return; 222 font_ = computeFontSize(g); 223 FontRenderContext frc = getFontRenderContext((Graphics2D)g); 224 TextLayout tlayout; 225 if(label_.length() == 0) { 226 tlayout = new TextLayout(" ", font_, frc); 227 } else { 228 tlayout = new TextLayout(label_, font_, frc); 229 } 230 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 231 int theight = (int)tbounds.getHeight(); 232 int twidth = (int)tbounds.getWidth(); 233 int tx = (int)tbounds.getX(); 234 int ty = (int)tbounds.getY(); 235 if(fixMetrics_) ty -= (int)(0.7*tlayout.getAscent()); // hack for MacOS X java.runtime.version = 1.4.1_01-39 236 sascent = (int)tlayout.getAscent(); 237 238 if(orient_ == SGLabel.HORIZONTAL) { 239 swidth = width; 240 xd = x; 241 yd = y - ty; 242 switch(valign_) { 243 case SGLabel.TOP: 244 yd = yd - sascent; 245 break; 246 case SGLabel.MIDDLE: 247 yd = yd + (ty + theight/2); 248 break; 249 case SGLabel.BOTTOM: 250 } 251 switch(halign_) { 252 case SGLabel.RIGHT: 253 xd = xd + swidth; 254 break; 255 case SGLabel.CENTER: 256 xd = xd + swidth/2; 257 break; 258 case SGLabel.LEFT: 259 } 260 } else { 261 swidth = height; 262 yd = y + height; 263 xd = x - ty; 264 switch(valign_) { 265 case SGLabel.TOP: 266 xd = xd - sascent; 267 break; 268 case SGLabel.MIDDLE: 269 xd = xd - sascent/2; 270 break; 271 case SGLabel.BOTTOM: 272 } 273 switch(halign_) { 274 case SGLabel.RIGHT: 275 yd = yd - swidth; 276 break; 277 case SGLabel.CENTER: 278 yd = yd - swidth/2; 279 break; 280 case SGLabel.LEFT: 281 } 282 } 283 if(dorigin_.x != xd || dorigin_.y != yd) { 284 dorigin_.x = xd; 285 dorigin_.y = yd; 286 porigin_.x = layer_.getXDtoP(xd); 287 porigin_.y = layer_.getYDtoP(yd); 288 } 289 } 290 public Rectangle getBounds() { 291 if(savedBounds_ != null) return savedBounds_; 292 if(layer_ != null) computeBoundsD(layer_.getPane().getComponent().getGraphics()); 293 return dbounds_; 294 } 295 296 public void setLocationP(Point2D.Double loc) { 297 porigin_ = loc; 298 } 299 public Point2D.Double getLocationP() { 300 return porigin_; 301 } 302 303 public Rectangle2D.Double getBoundsP() { 304 computeBoundsD(layer_.getPane().getComponent().getGraphics()); 305 return pbounds_; 306 } 307 308 public void setAngle(double angle) { 309 angle_ = angle; 310 double thta = angle_*Math.PI/180.0; 311 if(Math.abs(thta) < 0.001) { 312 orient_ = SGLabel.HORIZONTAL; 313 costhta_ = 1.0; 314 sinthta_ = 0.0; 315 } else if(Math.abs(thta - 90.0) < 0.001) { 316 orient_ = SGLabel.VERTICAL; 317 costhta_ = 0.0; 318 sinthta_ = 1.0; 319 } else { 320 orient_ = SGLabel.ANGLE; 321 costhta_ = Math.cos(thta); 322 sinthta_ = Math.sin(thta); 323 } 324 } 325 public double getAngle() { 326 return angle_; 327 } 328 329 public void setHeightP(double hgt) { 330 height_ = hgt; 331 } 332 public double getHeightP() { 333 return height_; 334 } 335 336 public void setVisible(boolean vis) { 337 visible_ = vis; 338 } 339 public boolean isVisible() { 340 return visible_; 341 } 342 343 private void computeBoundsD(Graphics g) { 344 int sascent, xd, yd; 345 int xorig, yorig; 346 int[] xt = new int[4]; 347 int[] yt = new int[4]; 348 int[] xn = new int[4]; 349 int[] yn = new int[4]; 350 // 351 // compute size of font and adjust to be height tall! 352 // 353 if(g == null) return; 354 font_ = computeFontSize(g); 355 FontRenderContext frc = getFontRenderContext((Graphics2D)g); 356 TextLayout tlayout; 357 if(label_.length() == 0) { 358 tlayout = new TextLayout(" ", font_, frc); 359 } else { 360 tlayout = new TextLayout(label_, font_, frc); 361 } 362 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 93 return newLabel; 94 } 95 96 public void draw( Graphics g ) 97 throws LayerNotFoundException 98 { 99 int xs, ys; 100 if( ( label_.length() <= 0 ) || !visible_ || g == null ) return; 101 if( layer_ == (Layer) null ) throw new LayerNotFoundException(); 102 // 103 // set label heigth in physical units 104 // 105 computeBoundsD( g ); 106 if( clr_ == null ) 107 { 108 g.setColor( layer_.getPane().getComponent().getForeground() ); 109 } 110 else 111 { 112 g.setColor( clr_ ); 113 } 114 // g.setFont( new Font( "Helvetica", Font.PLAIN, 6 ) ); 115 116 if( orient_ == SGLabel.HORIZONTAL ) 117 { 118 xs = dbounds_.x + xoff_; 119 ys = dbounds_.y + yoff_; 120 drawString( g, xs, ys ); 121 } 122 else if( orient_ == SGLabel.VERTICAL ) 123 { 124 // 125 // draw text in offscreen image (horizontal) 126 // 127 xs = dbounds_.x + xoff_; 128 ys = dbounds_.y + yoff_; 129 drawString( g, xs, ys ); 130 } 131 else 132 { 133 // 134 // Angled text, draw in offscreen image and rotate 135 // 136 xs = layer_.getXPtoD( porigin_.x ); 137 ys = layer_.getYPtoD( porigin_.y ); 138 drawString( g, xs, ys ); 139 } 140 } 141 142 public void setText( String lbl ) 143 { 144 label_ = lbl; 145 } 146 147 public String getText() 148 { 149 return label_; 150 } 151 152 public void setColor( Color clr ) 153 { 154 clr_ = clr; 155 } 156 157 public Color getColor() 158 { 159 return clr_; 160 } 161 162 public void setFont( Font font ) 163 { 164 font_ = font; 165 } 166 167 public Font getFont() 168 { 169 return font_; 170 } 171 172 public void setLayer( Layer layer ) 173 { 174 layer_ = layer; 175 if( savedBounds_ != null ) 176 { 177 setBounds( savedBounds_.x, savedBounds_.y, 178 savedBounds_.width, savedBounds_.height ); 179 savedBounds_ = null; 180 } 181 if( savedLoc_ != null ) 182 { 183 setLocation( savedLoc_ ); 184 savedLoc_ = null; 185 } 186 } 187 188 public Layer getLayer() 189 { 190 return layer_; 191 } 192 193 public void setOrientation( int orient ) 194 { 195 if( orient_ != orient ) 196 { 197 if( orient == SGLabel.HORIZONTAL ) 198 { 199 costhta_ = 1.0; 200 sinthta_ = 0.0; 201 } 202 else if( orient == SGLabel.VERTICAL ) 203 { 204 costhta_ = 0.0; 205 sinthta_ = 1.0; 206 } 207 orient_ = orient; 208 } 209 } 210 211 public int getOrientation() 212 { 213 return orient_; 214 } 215 216 public void setHAlign( int halign ) 217 { 218 halign_ = halign; 219 } 220 221 public int getHAlign() 222 { 223 return halign_; 224 } 225 226 public void setVAlign( int valign ) 227 { 228 valign_ = valign; 229 } 230 231 public int getVAlign() 232 { 233 return valign_; 234 } 235 236 public void setLocation( Point loc ) 237 { 238 if( layer_ == null ) 239 { 240 savedLoc_ = new Point( loc ); 241 return; 242 } 243 computeBoundsD( layer_.getPane().getComponent().getGraphics() ); 244 if( dbounds_.x != loc.x || dbounds_.y != loc.y ) 245 { 246 setBounds( loc.x, loc.y, dbounds_.width, dbounds_.height ); 247 } 248 } 249 250 public Point getLocation() 251 { 252 if( savedLoc_ != null ) return savedLoc_; 253 return dorigin_; 254 } 255 256 public void setBounds( int x, int y, int width, int height ) 257 { 258 int swidth, sascent, xd, yd; 259 if( layer_ == null ) 260 { 261 savedBounds_ = new Rectangle( x, y, width, height ); 262 return; 263 } 264 Graphics g = layer_.getPane().getComponent().getGraphics(); 265 if( g == null ) return; 266 font_ = computeFontSize( g ); 267 FontRenderContext frc = getFontRenderContext( (Graphics2D) g ); 268 TextLayout tlayout; 269 if( label_.length() == 0 ) 270 { 271 tlayout = new TextLayout( " ", font_, frc ); 272 } 273 else 274 { 275 tlayout = new TextLayout( label_, font_, frc ); 276 } 277 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 278 int theight = (int) tbounds.getHeight(); 279 int twidth = (int) tbounds.getWidth(); 280 int tx = (int) tbounds.getX(); 281 int ty = (int) tbounds.getY(); 282 if( fixMetrics_ ) 283 ty -= (int) ( 0.7 * tlayout.getAscent() ); // hack for MacOS X java.runtime.version = 1.4.1_01-39 284 sascent = (int) tlayout.getAscent(); 285 286 if( orient_ == SGLabel.HORIZONTAL ) 287 { 288 swidth = width; 289 xd = x; 290 yd = y - ty; 291 switch( valign_ ) 292 { 293 case SGLabel.TOP: 294 yd = yd - sascent; 295 break; 296 case SGLabel.MIDDLE: 297 yd = yd + ( ty + theight / 2 ); 298 break; 299 case SGLabel.BOTTOM: 300 } 301 switch( halign_ ) 302 { 303 case SGLabel.RIGHT: 304 xd = xd + swidth; 305 break; 306 case SGLabel.CENTER: 307 xd = xd + swidth / 2; 308 break; 309 case SGLabel.LEFT: 310 } 311 } 312 else 313 { 314 swidth = height; 315 yd = y + height; 316 xd = x - ty; 317 switch( valign_ ) 318 { 319 case SGLabel.TOP: 320 xd = xd - sascent; 321 break; 322 case SGLabel.MIDDLE: 323 xd = xd - sascent / 2; 324 break; 325 case SGLabel.BOTTOM: 326 } 327 switch( halign_ ) 328 { 329 case SGLabel.RIGHT: 330 yd = yd - swidth; 331 break; 332 case SGLabel.CENTER: 333 yd = yd - swidth / 2; 334 break; 335 case SGLabel.LEFT: 336 } 337 } 338 if( dorigin_.x != xd || dorigin_.y != yd ) 339 { 340 dorigin_.x = xd; 341 dorigin_.y = yd; 342 porigin_.x = layer_.getXDtoP( xd ); 343 porigin_.y = layer_.getYDtoP( yd ); 344 } 345 } 346 347 public Rectangle getBounds() 348 { 349 if( savedBounds_ != null ) return savedBounds_; 350 if( layer_ != null ) computeBoundsD( layer_.getPane().getComponent().getGraphics() ); 351 return dbounds_; 352 } 353 354 public void setLocationP( Point2D.Double loc ) 355 { 356 porigin_ = loc; 357 } 358 359 public Point2D.Double getLocationP() 360 { 361 return porigin_; 362 } 363 364 public Rectangle2D.Double getBoundsP() 365 { 366 computeBoundsD( layer_.getPane().getComponent().getGraphics() ); 367 return pbounds_; 368 } 369 370 public void setAngle( double angle ) 371 { 372 angle_ = angle; 373 double thta = angle_ * Math.PI / 180.0; 374 if( Math.abs( thta ) < 0.001 ) 375 { 376 orient_ = SGLabel.HORIZONTAL; 377 costhta_ = 1.0; 378 sinthta_ = 0.0; 379 } 380 else if( Math.abs( thta - 90.0 ) < 0.001 ) 381 { 382 orient_ = SGLabel.VERTICAL; 383 costhta_ = 0.0; 384 sinthta_ = 1.0; 385 } 386 else 387 { 388 orient_ = SGLabel.ANGLE; 389 costhta_ = Math.cos( thta ); 390 sinthta_ = Math.sin( thta ); 391 } 392 } 393 394 public double getAngle() 395 { 396 return angle_; 397 } 398 399 public void setHeightP( double hgt ) 400 { 401 height_ = hgt; 402 } 403 404 public double getHeightP() 405 { 406 return height_; 407 } 408 409 public void setVisible( boolean vis ) 410 { 411 visible_ = vis; 412 } 413 414 public boolean isVisible() 415 { 416 return visible_; 417 } 418 419 private void computeBoundsD( Graphics g ) 420 { 421 int sascent, xd, yd; 422 int xorig, yorig; 423 int[] xt = new int[4]; 424 int[] yt = new int[4]; 425 int[] xn = new int[4]; 426 int[] yn = new int[4]; 427 // 428 // compute size of font and adjust to be height tall! 429 // 430 if( g == null ) return; 431 // VMIPSL 432 // font_ = computeFontSize( g ); 433 FontRenderContext frc = getFontRenderContext( (Graphics2D) g ); 434 TextLayout tlayout; 435 if( label_.length() == 0 ) 436 { 437 tlayout = new TextLayout( " ", font_, frc ); 438 } 439 else 440 { 441 tlayout = new TextLayout( label_, font_, frc ); 442 } 443 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 363 444 364 445 // System.out.println("LabelDrawer2('" + label_ + "'): fixMetrics = " + fixMetrics_); … … 368 449 // System.out.println("LabelDrawer2('" + label_ + "'): TextLayout = " + tlayout); 369 450 370 int theight = (int)tbounds.getHeight(); 371 int twidth = (int)tbounds.getWidth(); 372 int tx = (int)tbounds.getX(); 373 int ty = (int)tbounds.getY(); 374 if(fixMetrics_) ty -= (int)(0.7*tlayout.getAscent()); // hack for MacOS X java.runtime.version = 1.4.1_01-39 375 sascent = (int)tlayout.getAscent(); 451 int theight = (int) tbounds.getHeight(); 452 int twidth = (int) tbounds.getWidth(); 453 int tx = (int) tbounds.getX(); 454 int ty = (int) tbounds.getY(); 455 if( fixMetrics_ ) 456 ty -= (int) ( 0.7 * tlayout.getAscent() ); // hack for MacOS X java.runtime.version = 1.4.1_01-39 457 sascent = (int) tlayout.getAscent(); 458 // 459 xd = layer_.getXPtoD( porigin_.x ); 460 yd = layer_.getYPtoD( porigin_.y ); 461 // 462 // set device origin 463 // 464 dorigin_.x = xd; 465 dorigin_.y = yd; 466 xorig = xd; 467 yorig = yd; 468 // 469 switch( valign_ ) 470 { 471 case SGLabel.TOP: 472 yd = yd + sascent; 473 break; 474 case SGLabel.MIDDLE: 475 yd = yd - ( ty + theight / 2 ); 476 break; 477 case SGLabel.BOTTOM: 478 } 479 switch( halign_ ) 480 { 481 case SGLabel.RIGHT: 482 xd = xd - twidth; 483 break; 484 case SGLabel.CENTER: 485 xd = xd - twidth / 2; 486 break; 487 case SGLabel.LEFT: 488 } 489 if( orient_ == SGLabel.HORIZONTAL ) 490 { 491 xoff_ = 0; 492 yoff_ = -ty; 493 } 494 else if( orient_ == SGLabel.VERTICAL ) 495 { 496 xoff_ = -ty; 497 yoff_ = twidth; 498 } 499 xt[0] = xd + tx; 500 xt[1] = xt[0]; 501 xt[2] = xt[0] + twidth; 502 xt[3] = xt[2]; 503 504 yt[0] = yd + ty; 505 yt[1] = yt[0] + theight; 506 yt[2] = yt[1]; 507 yt[3] = yt[0]; 508 // 509 // rotate 510 // 511 for( int i = 0; i < 4; i++ ) 512 { 513 xn[i] = (int) ( ( xt[i] - xorig ) * costhta_ + ( yt[i] - yorig ) * sinthta_ ) + xorig; 514 yn[i] = (int) ( ( yt[i] - yorig ) * costhta_ - ( xt[i] - xorig ) * sinthta_ ) + yorig; 515 } 516 517 dpolygon_ = new Polygon( xn, yn, 4 ); 518 dbounds_ = dpolygon_.getBounds(); 519 // 520 // compute pbounds 521 // 522 pbounds_.x = layer_.getXDtoP( dbounds_.x ); 523 pbounds_.y = layer_.getYDtoP( dbounds_.y ); 524 pbounds_.width = layer_.getXDtoP( dbounds_.x + 525 dbounds_.width ) - pbounds_.x; 526 pbounds_.height = pbounds_.y - 527 layer_.getYDtoP( dbounds_.y + dbounds_.height ); 528 } 529 376 530 // 377 xd = layer_.getXPtoD(porigin_.x); 378 yd = layer_.getYPtoD(porigin_.y); 531 // a bad method to compute the font size! 379 532 // 380 // set device origin 381 // 382 dorigin_.x = xd; 383 dorigin_.y = yd; 384 xorig = xd; 385 yorig = yd; 386 // 387 switch(valign_) { 388 case SGLabel.TOP: 389 yd = yd + sascent; 390 break; 391 case SGLabel.MIDDLE: 392 yd = yd - (ty + theight/2); 393 break; 394 case SGLabel.BOTTOM: 395 } 396 switch(halign_) { 397 case SGLabel.RIGHT: 398 xd = xd - twidth; 399 break; 400 case SGLabel.CENTER: 401 xd = xd - twidth/2; 402 break; 403 case SGLabel.LEFT: 404 } 405 if(orient_ == SGLabel.HORIZONTAL) { 406 xoff_ = 0; 407 yoff_ = -ty; 408 } else if(orient_ == SGLabel.VERTICAL) { 409 xoff_ = -ty; 410 yoff_ = twidth; 411 } 412 xt[0] = xd + tx; 413 xt[1] = xt[0]; 414 xt[2] = xt[0] + twidth; 415 xt[3] = xt[2]; 416 417 yt[0] = yd + ty; 418 yt[1] = yt[0] + theight; 419 yt[2] = yt[1]; 420 yt[3] = yt[0]; 421 // 422 // rotate 423 // 424 for(int i=0; i < 4; i++) { 425 xn[i] = (int)((xt[i]-xorig)*costhta_ + (yt[i]-yorig)*sinthta_) + xorig; 426 yn[i] = (int)((yt[i]-yorig)*costhta_ - (xt[i]-xorig)*sinthta_) + yorig; 427 } 428 429 dpolygon_ = new Polygon(xn, yn, 4); 430 dbounds_ = dpolygon_.getBounds(); 431 // 432 // compute pbounds 433 // 434 pbounds_.x = layer_.getXDtoP(dbounds_.x); 435 pbounds_.y = layer_.getYDtoP(dbounds_.y); 436 pbounds_.width = layer_.getXDtoP(dbounds_.x + 437 dbounds_.width) - pbounds_.x; 438 pbounds_.height = pbounds_.y - 439 layer_.getYDtoP(dbounds_.y + dbounds_.height); 440 } 441 442 // 443 // a bad method to compute the font size! 444 // 445 Font computeFontSize(Graphics g) { 446 Font tfont; 447 int pt_0, pt_1, hgt; 448 int count = 1; 449 double hgt_0, hgt_1, del_0, del_1; 450 double a, b; 451 FontRenderContext frc = getFontRenderContext((Graphics2D)g); 452 TextLayout tlayout; 453 // 454 // first guess 455 // 456 if(g == null) return font_; // return original font! 457 hgt = layer_.getXPtoD(height_) - layer_.getXPtoD(0.0f); 458 pt_0 = hgt - 3; 459 tfont = new Font(font_.getName(), font_.getStyle(), pt_0); 460 if(label_.length() == 0) { 461 tlayout = new TextLayout(" ", tfont, frc); 462 } else { 463 tlayout = new TextLayout(label_, tfont, frc); 464 } 465 hgt = (int)(tlayout.getAscent() + tlayout.getDescent()); 466 hgt_0 = layer_.getXDtoP(hgt) - layer_.getXDtoP(0); 467 pt_0 = tfont.getSize(); 468 pt_1 = (int)((double)pt_0*(height_/hgt_0)); 469 while((pt_0 != pt_1) && (count < 5)) { 470 tfont = new Font(font_.getName(), font_.getStyle(), pt_1); 471 if(label_.length() == 0) { 472 tlayout = new TextLayout(" ", tfont, frc); 473 } else { 474 tlayout = new TextLayout(label_, tfont, frc); 475 } 476 hgt = (int)(tlayout.getAscent() + tlayout.getDescent()); 477 hgt_1 = layer_.getXDtoP(hgt) - layer_.getXDtoP(0); 478 del_0 = Math.abs(height_ - hgt_0); 479 del_1 = Math.abs(height_ - hgt_1); 480 if((Math.abs(pt_0 - pt_1) <= 1) && (del_0 > del_1)) return tfont; 481 pt_0 = pt_1; 482 hgt_0 = hgt_1; 483 pt_1 = (int)((double)pt_0*(height_/hgt_0)); 484 count++; 485 } 486 return tfont; 487 } 488 489 private void drawString(Graphics g, int x, int y) { 490 float angle; 491 if(g == null) return; 492 if(orient_ == SGLabel.HORIZONTAL) { 493 angle = 0.0f; 494 } else if(orient_ == SGLabel.VERTICAL) { 495 angle = -90.0f; 496 } else { 497 angle = -(float)angle_; 498 } 499 RenderingHints oldRenderingHints = null; 500 Graphics2D g2 = (Graphics2D)g; 501 if(angle != 0.0f || alwaysAntiAlias_) { 502 oldRenderingHints = g2.getRenderingHints(); 503 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 504 RenderingHints.VALUE_ANTIALIAS_ON); 505 g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, 506 RenderingHints.VALUE_FRACTIONALMETRICS_ON); 507 } 508 AffineTransform oldTransform = g2.getTransform(); 509 // System.out.println("\n angle = " + angle + ", text = " + label_); 510 // System.out.println("oldTransform = " + oldTransform); 511 AttributedString as = new AttributedString(label_); 512 as.addAttribute(TextAttribute.FONT, font_); 513 g2.translate(x, y); 514 // System.out.println("translated = " + g2.getTransform()); 515 g2.rotate(Math.PI * angle/180.0); 516 // System.out.println("newTransform = " + g2.getTransform()); 517 g2.drawString(as.getIterator(), 0, 0); 518 g2.setTransform(oldTransform); 519 if(angle != 0.0f || alwaysAntiAlias_) { 520 g2.setRenderingHints(oldRenderingHints); 521 } 522 // 533 Font computeFontSize( Graphics g ) 534 { 535 Font tfont; 536 int pt_0, pt_1, hgt; 537 int count = 1; 538 double hgt_0, hgt_1, del_0, del_1; 539 double a, b; 540 FontRenderContext frc = getFontRenderContext( (Graphics2D) g ); 541 TextLayout tlayout; 542 // 543 // first guess 544 // 545 if( g == null ) return font_; // return original font! 546 hgt = layer_.getXPtoD( height_ ) - layer_.getXPtoD( 0.0f ); 547 pt_0 = hgt - 3; 548 tfont = new Font( font_.getName(), font_.getStyle(), pt_0 ); 549 if( label_.length() == 0 ) 550 { 551 tlayout = new TextLayout( " ", tfont, frc ); 552 } 553 else 554 { 555 tlayout = new TextLayout( label_, tfont, frc ); 556 } 557 hgt = (int) ( tlayout.getAscent() + tlayout.getDescent() ); 558 hgt_0 = layer_.getXDtoP( hgt ) - layer_.getXDtoP( 0 ); 559 pt_0 = tfont.getSize(); 560 pt_1 = (int) ( (double) pt_0 * ( height_ / hgt_0 ) ); 561 while( ( pt_0 != pt_1 ) && ( count < 5 ) ) 562 { 563 tfont = new Font( font_.getName(), font_.getStyle(), pt_1 ); 564 if( label_.length() == 0 ) 565 { 566 tlayout = new TextLayout( " ", tfont, frc ); 567 } 568 else 569 { 570 tlayout = new TextLayout( label_, tfont, frc ); 571 } 572 hgt = (int) ( tlayout.getAscent() + tlayout.getDescent() ); 573 hgt_1 = layer_.getXDtoP( hgt ) - layer_.getXDtoP( 0 ); 574 del_0 = Math.abs( height_ - hgt_0 ); 575 del_1 = Math.abs( height_ - hgt_1 ); 576 if( ( Math.abs( pt_0 - pt_1 ) <= 1 ) && ( del_0 > del_1 ) ) return tfont; 577 pt_0 = pt_1; 578 hgt_0 = hgt_1; 579 pt_1 = (int) ( (double) pt_0 * ( height_ / hgt_0 ) ); 580 count++; 581 } 582 return tfont; 583 } 584 585 private void drawString( Graphics g, int x, int y ) 586 { 587 float angle; 588 if( g == null ) return; 589 if( orient_ == SGLabel.HORIZONTAL ) 590 { 591 angle = 0.0f; 592 } 593 else if( orient_ == SGLabel.VERTICAL ) 594 { 595 angle = -90.0f; 596 } 597 else 598 { 599 angle = -(float) angle_; 600 } 601 RenderingHints oldRenderingHints = null; 602 Graphics2D g2 = (Graphics2D) g; 603 if( angle != 0.0f || alwaysAntiAlias_ ) 604 { 605 oldRenderingHints = g2.getRenderingHints(); 606 g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, 607 RenderingHints.VALUE_ANTIALIAS_ON ); 608 g2.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, 609 RenderingHints.VALUE_FRACTIONALMETRICS_ON ); 610 } 611 AffineTransform oldTransform = g2.getTransform(); 612 // System.out.println("\n angle = " + angle + ", text = " + label_); 613 // System.out.println("oldTransform = " + oldTransform); 614 AttributedString as = new AttributedString( label_ ); 615 as.addAttribute( TextAttribute.FONT, font_ ); 616 g2.translate( x, y ); 617 // System.out.println("translated = " + g2.getTransform()); 618 g2.rotate( Math.PI * angle / 180.0 ); 619 // System.out.println("newTransform = " + g2.getTransform()); 620 g2.drawString( as.getIterator(), 0, 0 ); 621 g2.setTransform( oldTransform ); 622 if( angle != 0.0f || alwaysAntiAlias_ ) 623 { 624 g2.setRenderingHints( oldRenderingHints ); 625 } 626 // 523 627 // g2.setStroke(normal); 524 628 // Color oldColor = g.getColor(); … … 532 636 // g.setColor(oldColor); 533 637 534 } 535 public float getStringWidth(Graphics g) { 536 if(g == null) return 0.0f; 537 font_ = computeFontSize(g); 538 FontRenderContext frc = getFontRenderContext((Graphics2D)g); 539 TextLayout tlayout; 540 if(label_.length() == 0) { 541 tlayout = new TextLayout(" ", font_, frc); 542 } else { 543 tlayout = new TextLayout(label_, font_, frc); 544 } 545 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 546 return (float)tbounds.getWidth(); 547 } 548 public float getStringHeight(Graphics g) { 549 if(g == null) return 0.0f; 550 font_ = computeFontSize(g); 551 FontRenderContext frc = getFontRenderContext((Graphics2D)g); 552 TextLayout tlayout; 553 if(label_.length() == 0) { 554 tlayout = new TextLayout(" ", font_, frc); 555 } else { 556 tlayout = new TextLayout(label_, font_, frc); 557 } 558 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 559 return (float)tbounds.getHeight(); 560 } 561 562 FontRenderContext getFontRenderContext(Graphics2D g2) { 563 if(g2 == null) return null; 564 RenderingHints oldRenderingHints = null; 565 if(angle_ != 0.0 || alwaysAntiAlias_) { 566 oldRenderingHints = g2.getRenderingHints(); 567 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 568 RenderingHints.VALUE_ANTIALIAS_ON); 569 } 570 FontRenderContext frc = g2.getFontRenderContext(); 571 if(angle_ != 0.0 || alwaysAntiAlias_) { 572 g2.setRenderingHints(oldRenderingHints); 573 } 574 return frc; 575 } 638 } 639 640 public float getStringWidth( Graphics g ) 641 { 642 if( g == null ) return 0.0f; 643 font_ = computeFontSize( g ); 644 FontRenderContext frc = getFontRenderContext( (Graphics2D) g ); 645 TextLayout tlayout; 646 if( label_.length() == 0 ) 647 { 648 tlayout = new TextLayout( " ", font_, frc ); 649 } 650 else 651 { 652 tlayout = new TextLayout( label_, font_, frc ); 653 } 654 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 655 return (float) tbounds.getWidth(); 656 } 657 658 public float getStringHeight( Graphics g ) 659 { 660 if( g == null ) return 0.0f; 661 font_ = computeFontSize( g ); 662 FontRenderContext frc = getFontRenderContext( (Graphics2D) g ); 663 TextLayout tlayout; 664 if( label_.length() == 0 ) 665 { 666 tlayout = new TextLayout( " ", font_, frc ); 667 } 668 else 669 { 670 tlayout = new TextLayout( label_, font_, frc ); 671 } 672 java.awt.geom.Rectangle2D tbounds = tlayout.getBounds(); 673 return (float) tbounds.getHeight(); 674 } 675 676 FontRenderContext getFontRenderContext( Graphics2D g2 ) 677 { 678 if( g2 == null ) return null; 679 RenderingHints oldRenderingHints = null; 680 if( angle_ != 0.0 || alwaysAntiAlias_ ) 681 { 682 oldRenderingHints = g2.getRenderingHints(); 683 g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, 684 RenderingHints.VALUE_ANTIALIAS_ON ); 685 } 686 FontRenderContext frc = g2.getFontRenderContext(); 687 if( angle_ != 0.0 || alwaysAntiAlias_ ) 688 { 689 g2.setRenderingHints( oldRenderingHints ); 690 } 691 return frc; 692 } 576 693 } -
ether_megapoli/trunk/applets/src/gov/noaa/pmel/sgt/LineKey.java
r174 r188 11 11 */ 12 12 13 package 13 package gov.noaa.pmel.sgt; 14 14 15 15 import gov.noaa.pmel.util.Point2D; 16 16 import gov.noaa.pmel.util.Rectangle2D; 17 import gov.noaa.pmel.util.Debug; 18 17 18 import java.awt.*; 19 import java.beans.PropertyChangeEvent; 20 import java.beans.PropertyChangeListener; 21 import java.beans.PropertyChangeSupport; 22 import java.util.Enumeration; 19 23 import java.util.Vector; 20 import java.util.Enumeration;21 import java.awt.Graphics;22 import java.awt.Rectangle;23 import java.awt.Point;24 25 import java.beans.PropertyChangeListener;26 import java.beans.PropertyChangeEvent;27 import java.beans.PropertyChangeSupport;28 24 29 25 // jdk1.2 … … 38 34 * @version $Revision: 1.16 $, $Date: 2003/08/22 23:02:32 $ 39 35 * @since 1.0 40 **/ 41 public class LineKey implements Cloneable, 42 DataKey, Moveable, PropertyChangeListener { 43 private String ident_; 44 /** @directed */ 45 private Layer layer_; 46 /** @link aggregation 47 * @supplierCardinality * 48 * @label line*/ 49 /*#LineCartesianRenderer lnkLineCartesianRenderer;*/ 50 private Vector line_; 51 /** @link aggregation 52 * @supplierCardinality * 53 * @label label*/ 54 /*#SGLabel lnkSGLabel;*/ 55 private Vector label_; 56 private int columns_; 57 private int style_; 58 private int valign_; 59 private int halign_; 60 private Point2D.Double porigin_; 61 private double lineLengthP_; 62 private int maxLabelLength_; 63 private int maxLabelHeight_; 64 private StrokeDrawer stroke_ = null; 65 private boolean selected_; 66 private boolean selectable_; 67 private boolean visible_; 68 private boolean moveable_; 69 private PropertyChangeSupport changes_ = new PropertyChangeSupport(this); 70 private static final int VERTICAL_BORDER_ = 3; 71 private static final int HORIZONTAL_BORDER_ = 15; 72 private static final int COLUMN_SPACE_ = 10; 73 private static final int ROW_SPACE_ = 3; 74 private static final int LABEL_SPACE_ = 15; 75 /** 76 * Use plain line border. 77 */ 78 public static final int PLAIN_LINE = 0; 79 /** 80 * Use raised border. 81 */ 82 public static final int RAISED = 1; 83 /** 84 * Do not draw a border. 85 */ 86 public static final int NO_BORDER = 2; 87 /** 88 * Align to top of key. 89 */ 90 public static final int TOP = 0; 91 /** 92 * Align to middle of key. 93 */ 94 public static final int MIDDLE = 1; 95 /** 96 * Align to bottom of key. 97 */ 98 public static final int BOTTOM = 2; 99 /** 100 * Align to left of key. 101 */ 102 public static final int LEFT = 0; 103 /** 104 * Align to center of key. 105 */ 106 public static final int CENTER = 1; 107 /** 108 * Align to right of key. 109 */ 110 public static final int RIGHT = 2; 111 112 113 /** 114 * Default constructor. 115 */ 116 public LineKey() { 117 this(new Point2D.Double(0.0, 0.0), BOTTOM, LEFT); 118 } 119 /** 120 * 121 */ 122 public LineKey(Point2D.Double loc,int valign,int halign) { 123 porigin_ = loc; 124 valign_ = valign; 125 halign_ = halign; 126 line_ = new Vector(2,2); 127 label_ = new Vector(2,2); 128 // 129 // set defaults 130 // 131 style_ = PLAIN_LINE; 132 columns_ = 1; 133 ident_ = ""; 134 lineLengthP_ = 0.3f; 135 selected_ = false; 136 selectable_ = true; 137 visible_ = true; 138 moveable_ = true; 139 stroke_ = PaneProxy.strokeDrawer; 140 } 141 /** 142 * Create of copy of LineKey. 143 */ 144 public LayerChild copy() { 145 LineKey newKey; 146 try { 147 newKey = (LineKey)clone(); 148 } catch (CloneNotSupportedException e) { 149 newKey = new LineKey(); 150 } 151 newKey.line_ = new Vector(2,2); 152 newKey.label_ = new Vector(2,2); 153 return newKey; 154 } 155 public void setSelected(boolean sel) { 156 selected_ = sel; 157 } 158 public boolean isSelected() { 159 return selected_; 160 } 161 public void setSelectable(boolean select) { 162 selectable_ = select; 163 } 164 public boolean isSelectable() { 165 return selectable_; 166 } 167 public boolean isMoveable() { 168 return moveable_; 169 } 170 public void setMoveable(boolean moveable) { 171 moveable_ = moveable; 172 } 173 /** 174 * Set parent layer. 175 * 176 * @param l parent layer 177 */ 178 public void setLayer(Layer l) { 179 layer_ = l; 180 } 181 /** 182 * Get layer. 183 * 184 * @return layer 185 */ 186 public Layer getLayer() { 187 return layer_; 188 } 189 190 public AbstractPane getPane() { 191 return layer_.getPane(); 192 } 193 194 public void modified(String mess) { 195 if(layer_ != null) 196 layer_.modified(mess); 197 } 198 /** 199 * Set LineKey identifier. 200 * 201 * @param id key identifier 202 */ 203 public void setId(String id) { 204 ident_ = id; 205 } 206 /** 207 * Get LineKey identifier 208 * 209 * @return identifier 210 */ 211 public String getId() { 212 return ident_; 213 } 214 /** 215 * Set line length. 216 * 217 * @param len line length 218 */ 219 public void setLineLengthP(double len) { 220 if(lineLengthP_ != len) { 221 lineLengthP_ = len; 222 modified("LineKey: setLineLengthP()"); 223 } 224 } 225 /** 226 * Get line length 227 * 228 * @return line length 229 */ 230 public double getLineLengthP() { 231 return lineLengthP_; 232 } 233 /** 234 * Set the number of columns. 235 * 236 * @param col number of columns 237 */ 238 public void setColumns(int col) { 239 if(columns_ != col) { 240 columns_ = col; 241 modified("LineKey: setColumms()"); 242 } 243 } 244 /** 245 * Get the number of columns. 246 * 247 * @return number of columns 248 */ 249 public int getColumns() { 250 return columns_; 251 } 252 /** 253 * Set border style. 254 * 255 * @param style border style 256 * @see #PLAIN_LINE 257 * @see #RAISED 258 * @see #NO_BORDER 259 */ 260 public void setBorderStyle(int style) { 261 if(style_ != style) { 262 style_ = style; 263 modified("LineKey: setBorderStyle()"); 264 } 265 } 266 /** 267 * Get border style. 268 * 269 * @return border style 270 */ 271 public int getBorderStyle() { 272 return style_; 273 } 274 /** 275 * Set alignment. 276 * 277 * @param vert vertical alignment 278 * @param horz horizontal alignment 279 */ 280 public void setAlign(int vert,int horz) { 281 if(valign_ != vert || halign_ != horz) { 282 valign_ = vert; 283 halign_ = horz; 284 modified("LineKey: setAlign()"); 285 } 286 } 287 /** 288 * Set vertical alignment 289 * 290 * @param vert vertical alignment 291 */ 292 public void setVAlign(int vert) { 293 if(valign_ != vert) { 294 valign_ = vert; 295 modified("LineKey: setVAlign()"); 296 } 297 } 298 /** 299 * Set horizontal alignment 300 * 301 * @param horz horizontal alignment 302 */ 303 public void setHAlign(int horz) { 304 if(halign_ != horz) { 305 halign_ = horz; 306 modified("LineKey: setHAlign()"); 307 } 308 } 309 /** 310 * Get vertical alignment 311 * 312 * @return vertical alignment 313 */ 314 public int getVAlign() { 315 return valign_; 316 } 317 /** 318 * Get horizontal alignment 319 * 320 * @return horizontal alignment 321 */ 322 public int getHAlign() { 323 return halign_; 324 } 325 /** 326 * Set location of key 327 * <BR><B>Property Change:</B> <code>location</code>. 328 * 329 * @param loc key location 330 */ 331 public void setLocationP(Point2D.Double loc) { 332 if(porigin_ == null || !porigin_.equals(loc)) { 333 Point2D.Double temp = porigin_; 334 porigin_ = loc; 335 changes_.firePropertyChange("location", 336 temp, 337 porigin_); 338 modified("LineKey: setLocationP()"); 339 } 340 } 341 /** 342 * Set the bounds of the <code>LineKey</code> in physical units. 343 */ 344 public void setBoundsP(Rectangle2D.Double r) { 345 setLocationP(new Point2D.Double(r.x, r.y)); 346 } 347 /** 348 * Get key bounds in physical coordinates. 349 * Not presently implemented. 350 */ 351 public Rectangle2D.Double getBoundsP() { 352 throw new MethodNotImplementedError(); 353 } 354 /** 355 * Get location of key. 356 * 357 * @return Key location 358 */ 359 public Point2D.Double getLocationP() { 360 return porigin_; 361 } 362 /** 363 * Add a LineCartesianRenderer and label to the LineKey. 364 * 365 * @param line LineCartesianGraph object 366 * @param label descriptive label 367 */ 368 public void addLineGraph(LineCartesianRenderer line, SGLabel label) { 369 line_.addElement(line); 370 label.setLayer(layer_); 371 label.setMoveable(false); 372 label.setSelectable(false); 373 label_.addElement(label); 374 ((LineAttribute)line.getAttribute()).addPropertyChangeListener(this); 375 modified("LineKey: addLineGraph()"); 376 } 377 /** 378 * Add a LineCartesianRenderer and label to the LineKey. 379 * 380 * @param rend LineCartesianRenderer object 381 * @param label descriptive label 382 * @since 3.0 383 */ 384 public void addGraph(CartesianRenderer rend, SGLabel label) 385 throws IllegalArgumentException { 386 if(!(rend instanceof LineCartesianRenderer)) 387 throw new IllegalArgumentException("Renderer is not a LineCartesianRenderer"); 388 addLineGraph((LineCartesianRenderer)rend, label); 389 } 390 /** 391 * Remove a line from the LineKey. 392 * 393 */ 394 public void removeLineGraph(SGLabel label) { 395 } 396 /** 397 * Remove a line from the LineKey. 398 * 399 */ 400 public void removeLineRenderer(LineCartesianRenderer line) { 401 } 402 /** 403 * Remove a line from the LineKey. 404 * 405 */ 406 public void removeLineGraph(String ident) { 407 } 408 /** 409 * Remove all lines from the LineKey. 410 */ 411 public void clearAll() { 412 LineAttribute attr; 413 for(Enumeration e = line_.elements(); e.hasMoreElements(); ) { 414 attr = (LineAttribute)((LineCartesianRenderer)e.nextElement()).getAttribute(); 415 attr.removePropertyChangeListener(this); 416 } 417 line_.removeAllElements(); 418 label_.removeAllElements(); 419 modified("LineKey: clearAll()"); 420 } 421 /** 422 * Remove line associated with data id from LineKey. 423 * @since 2.0 424 */ 425 public void clear(String data_id) { 426 LineCartesianRenderer lcr; 427 int indx = -1; 428 for(Enumeration it = line_.elements(); it.hasMoreElements();) { 429 indx++; 430 lcr = (LineCartesianRenderer)it.nextElement(); 431 if(lcr.getLine().getId().equals(data_id)) { 432 lcr.getAttribute().removePropertyChangeListener(this); 433 line_.removeElement(lcr); 434 label_.removeElementAt(indx); 435 modified("LineKey: clear()"); 436 break; 437 } 438 } 439 } 440 /** 441 * Return rowheight of key in pixels. 442 * @since 2.0 443 */ 444 public int getRowHeight() { 445 Rectangle bounds; 446 bounds = getBounds(); 447 return ROW_SPACE_ + maxLabelHeight_; 448 } 449 /** 450 * Draw the Key. 451 */ 452 public void draw(Graphics g) { 453 double maxLabelLength, maxLabelHeight; 454 int numLines, numRows, i, lineLength; 455 int col, row, ytemp; 456 double xloc, labelSpace; 457 double[] xp, yp; 458 int[] xd, yd; 459 int[] xout, yout; 460 Rectangle bounds; 461 LineCartesianRenderer render = null; 462 SGLabel label; 463 LineAttribute attr = null; 464 // 465 numLines = line_.size(); 466 if((numLines <= 0) || !visible_) return; 467 468 numRows = numLines/columns_; 469 if(numLines%columns_ != 0) numRows++; 470 471 xp = new double[columns_]; 472 xd = new int[columns_]; 473 yp = new double[numRows]; 474 yd = new int[numRows]; 475 xout = new int[2]; 476 yout = new int[2]; 477 478 g.setColor(layer_.getPane().getComponent().getForeground()); 479 bounds = getBounds(); 480 // 481 // compute location of rows and columns in device and physical coordinates 482 // 483 lineLength = layer_.getXPtoD(lineLengthP_) - layer_.getXPtoD(0.0f); 484 labelSpace = layer_.getXDtoP(LABEL_SPACE_) - layer_.getXDtoP(0); 485 // 486 yd[0] = bounds.y + VERTICAL_BORDER_ + maxLabelHeight_; 487 yp[0] = layer_.getYDtoP(yd[0]); 488 for(i=1; i < numRows; i++) { 489 yd[i] = yd[i-1] + ROW_SPACE_ + maxLabelHeight_; 490 yp[i] = layer_.getYDtoP(yd[i]); 491 } 492 xd[0] = bounds.x + HORIZONTAL_BORDER_; 493 xp[0] = layer_.getXDtoP(xd[0]); 494 for(i=1; i < columns_; i++) { 495 xd[i] = xd[i-1] + COLUMN_SPACE_ + lineLength + LABEL_SPACE_ + maxLabelLength_; 496 xp[i] = layer_.getXDtoP(xd[i]); 497 } 498 // 499 row = 0; 500 col = 0; 501 Object obj; 502 Enumeration labelIt = label_.elements(); 503 for(Enumeration lineIt = line_.elements(); lineIt.hasMoreElements();) { 504 obj = lineIt.nextElement(); 505 render = (LineCartesianRenderer)obj; 506 attr = (LineAttribute)render.getAttribute(); 507 label = (SGLabel)labelIt.nextElement(); 508 // 509 // draw line 510 // 511 g.setColor(attr.getColor()); 512 xout[0] = xd[col]; 513 xout[1] = xout[0] + lineLength; 514 yout[0] = yd[row] - maxLabelHeight_/2; 515 yout[1] = yout[0]; 516 switch(attr.getStyle()) { 517 case LineAttribute.MARK: 518 render.drawMark(g, xout, yout, 2, attr); 519 break; 520 case LineAttribute.HIGHLIGHT: 521 stroke_.drawHighlight(g, xout, yout, 2, attr); 522 break; 523 case LineAttribute.HEAVY: 524 stroke_.drawHeavy(g, xout, yout, 2, attr); 525 break; 526 case LineAttribute.DASHED: 527 stroke_.drawDashed(g, xout, yout, 2, attr); 528 break; 529 case LineAttribute.STROKE: 530 stroke_.drawStroke(g, xout, yout , 2, attr); 531 break; 532 case LineAttribute.MARK_LINE: 533 render.drawMark(g, xout, yout, 2, attr); 534 default: 535 case LineAttribute.SOLID: 536 g.drawLine(xout[0], yout[0], xout[1], yout[1]); 537 } 538 // 539 xloc = xp[col] + lineLengthP_ + labelSpace; 540 label.setLocationP(new Point2D.Double(xloc, yp[row])); 541 try { 542 label.draw(g); 543 } catch (SGException e) {} 544 // 545 col++; 546 if(col >= columns_) { 36 */ 37 public class LineKey 38 implements Cloneable, 39 DataKey, Moveable, PropertyChangeListener 40 { 41 private String ident_; 42 /** 43 * @directed 44 */ 45 private Layer layer_; 46 /** 47 * @link aggregation 48 * @supplierCardinality * 49 * @label line 50 */ 51 /*#LineCartesianRenderer lnkLineCartesianRenderer;*/ 52 private Vector line_; 53 /** 54 * @link aggregation 55 * @supplierCardinality * 56 * @label label 57 */ 58 /*#SGLabel lnkSGLabel;*/ 59 private Vector label_; 60 private int columns_; 61 private int style_; 62 private int valign_; 63 private int halign_; 64 private Point2D.Double porigin_; 65 private double lineLengthP_; 66 private int maxLabelLength_; 67 private int maxLabelHeight_; 68 private StrokeDrawer stroke_ = null; 69 private boolean selected_; 70 private boolean selectable_; 71 private boolean visible_; 72 private boolean moveable_; 73 private PropertyChangeSupport changes_ = new PropertyChangeSupport( this ); 74 private static final int VERTICAL_BORDER_ = 3; 75 private static final int HORIZONTAL_BORDER_ = 15; 76 // VMIPSL 77 private static final int COLUMN_SPACE_ = 0; 78 private static final int ROW_SPACE_ = 15; 79 private static final int LABEL_SPACE_ = 10; 80 /** 81 * Use plain line border. 82 */ 83 public static final int PLAIN_LINE = 0; 84 /** 85 * Use raised border. 86 */ 87 public static final int RAISED = 1; 88 /** 89 * Do not draw a border. 90 */ 91 public static final int NO_BORDER = 2; 92 /** 93 * Align to top of key. 94 */ 95 public static final int TOP = 0; 96 /** 97 * Align to middle of key. 98 */ 99 public static final int MIDDLE = 1; 100 /** 101 * Align to bottom of key. 102 */ 103 public static final int BOTTOM = 2; 104 /** 105 * Align to left of key. 106 */ 107 public static final int LEFT = 0; 108 /** 109 * Align to center of key. 110 */ 111 public static final int CENTER = 1; 112 /** 113 * Align to right of key. 114 */ 115 public static final int RIGHT = 2; 116 117 118 /** 119 * Default constructor. 120 */ 121 public LineKey() 122 { 123 this( new Point2D.Double( 0.0, 0.0 ), BOTTOM, LEFT ); 124 } 125 126 /** 127 * 128 */ 129 public LineKey( Point2D.Double loc, int valign, int halign ) 130 { 131 porigin_ = loc; 132 valign_ = valign; 133 halign_ = halign; 134 line_ = new Vector( 2, 2 ); 135 label_ = new Vector( 2, 2 ); 136 // 137 // set defaults 138 // 139 style_ = PLAIN_LINE; 140 columns_ = 1; 141 ident_ = ""; 142 lineLengthP_ = 0.3f; 143 selected_ = false; 144 selectable_ = true; 145 visible_ = true; 146 moveable_ = true; 147 stroke_ = PaneProxy.strokeDrawer; 148 } 149 150 /** 151 * Create of copy of LineKey. 152 */ 153 public LayerChild copy() 154 { 155 LineKey newKey; 156 try 157 { 158 newKey = (LineKey) clone(); 159 } 160 catch( CloneNotSupportedException e ) 161 { 162 newKey = new LineKey(); 163 } 164 newKey.line_ = new Vector( 2, 2 ); 165 newKey.label_ = new Vector( 2, 2 ); 166 return newKey; 167 } 168 169 public void setSelected( boolean sel ) 170 { 171 selected_ = sel; 172 } 173 174 public boolean isSelected() 175 { 176 return selected_; 177 } 178 179 public void setSelectable( boolean select ) 180 { 181 selectable_ = select; 182 } 183 184 public boolean isSelectable() 185 { 186 return selectable_; 187 } 188 189 public boolean isMoveable() 190 { 191 return moveable_; 192 } 193 194 public void setMoveable( boolean moveable ) 195 { 196 moveable_ = moveable; 197 } 198 199 /** 200 * Set parent layer. 201 * 202 * @param l parent layer 203 */ 204 public void setLayer( Layer l ) 205 { 206 layer_ = l; 207 } 208 209 /** 210 * Get layer. 211 * 212 * @return layer 213 */ 214 public Layer getLayer() 215 { 216 return layer_; 217 } 218 219 public AbstractPane getPane() 220 { 221 return layer_.getPane(); 222 } 223 224 public void modified( String mess ) 225 { 226 if( layer_ != null ) 227 layer_.modified( mess ); 228 } 229 230 /** 231 * Set LineKey identifier. 232 * 233 * @param id key identifier 234 */ 235 public void setId( String id ) 236 { 237 ident_ = id; 238 } 239 240 /** 241 * Get LineKey identifier 242 * 243 * @return identifier 244 */ 245 public String getId() 246 { 247 return ident_; 248 } 249 250 /** 251 * Set line length. 252 * 253 * @param len line length 254 */ 255 public void setLineLengthP( double len ) 256 { 257 if( lineLengthP_ != len ) 258 { 259 lineLengthP_ = len; 260 modified( "LineKey: setLineLengthP()" ); 261 } 262 } 263 264 /** 265 * Get line length 266 * 267 * @return line length 268 */ 269 public double getLineLengthP() 270 { 271 return lineLengthP_; 272 } 273 274 /** 275 * Set the number of columns. 276 * 277 * @param col number of columns 278 */ 279 public void setColumns( int col ) 280 { 281 if( columns_ != col ) 282 { 283 columns_ = col; 284 modified( "LineKey: setColumms()" ); 285 } 286 } 287 288 /** 289 * Get the number of columns. 290 * 291 * @return number of columns 292 */ 293 public int getColumns() 294 { 295 return columns_; 296 } 297 298 /** 299 * Set border style. 300 * 301 * @param style border style 302 * @see #PLAIN_LINE 303 * @see #RAISED 304 * @see #NO_BORDER 305 */ 306 public void setBorderStyle( int style ) 307 { 308 if( style_ != style ) 309 { 310 style_ = style; 311 modified( "LineKey: setBorderStyle()" ); 312 } 313 } 314 315 /** 316 * Get border style. 317 * 318 * @return border style 319 */ 320 public int getBorderStyle() 321 { 322 return style_; 323 } 324 325 /** 326 * Set alignment. 327 * 328 * @param vert vertical alignment 329 * @param horz horizontal alignment 330 */ 331 public void setAlign( int vert, int horz ) 332 { 333 if( valign_ != vert || halign_ != horz ) 334 { 335 valign_ = vert; 336 halign_ = horz; 337 modified( "LineKey: setAlign()" ); 338 } 339 } 340 341 /** 342 * Set vertical alignment 343 * 344 * @param vert vertical alignment 345 */ 346 public void setVAlign( int vert ) 347 { 348 if( valign_ != vert ) 349 { 350 valign_ = vert; 351 modified( "LineKey: setVAlign()" ); 352 } 353 } 354 355 /** 356 * Set horizontal alignment 357 * 358 * @param horz horizontal alignment 359 */ 360 public void setHAlign( int horz ) 361 { 362 if( halign_ != horz ) 363 { 364 halign_ = horz; 365 modified( "LineKey: setHAlign()" ); 366 } 367 } 368 369 /** 370 * Get vertical alignment 371 * 372 * @return vertical alignment 373 */ 374 public int getVAlign() 375 { 376 return valign_; 377 } 378 379 /** 380 * Get horizontal alignment 381 * 382 * @return horizontal alignment 383 */ 384 public int getHAlign() 385 { 386 return halign_; 387 } 388 389 /** 390 * Set location of key 391 * <BR><B>Property Change:</B> <code>location</code>. 392 * 393 * @param loc key location 394 */ 395 public void setLocationP( Point2D.Double loc ) 396 { 397 if( porigin_ == null || !porigin_.equals( loc ) ) 398 { 399 Point2D.Double temp = porigin_; 400 porigin_ = loc; 401 changes_.firePropertyChange( "location", 402 temp, 403 porigin_ ); 404 modified( "LineKey: setLocationP()" ); 405 } 406 } 407 408 /** 409 * Set the bounds of the <code>LineKey</code> in physical units. 410 */ 411 public void setBoundsP( Rectangle2D.Double r ) 412 { 413 setLocationP( new Point2D.Double( r.x, r.y ) ); 414 } 415 416 /** 417 * Get key bounds in physical coordinates. 418 * Not presently implemented. 419 */ 420 public Rectangle2D.Double getBoundsP() 421 { 422 throw new MethodNotImplementedError(); 423 } 424 425 /** 426 * Get location of key. 427 * 428 * @return Key location 429 */ 430 public Point2D.Double getLocationP() 431 { 432 return porigin_; 433 } 434 435 436 /** 437 * Add a LineCartesianRenderer and label to the LineKey. 438 * 439 * @param line LineCartesianGraph object 440 * @param label descriptive label 441 */ 442 public void addLineGraph( LineCartesianRenderer line, SGLabel label ) 443 { 444 line_.addElement( line ); 445 label.setLayer( layer_ ); 446 label.setMoveable( false ); 447 label.setSelectable( false ); 448 label_.addElement( label ); 449 ( (LineAttribute) line.getAttribute() ).addPropertyChangeListener( this ); 450 modified( "LineKey: addLineGraph()" ); 451 } 452 453 /** 454 * Add a LineCartesianRenderer and label to the LineKey. 455 * 456 * @param rend LineCartesianRenderer object 457 * @param label descriptive label 458 * @since 3.0 459 */ 460 public void addGraph( CartesianRenderer rend, SGLabel label ) 461 throws IllegalArgumentException 462 { 463 if( !( rend instanceof LineCartesianRenderer ) ) 464 throw new IllegalArgumentException( "Renderer is not a LineCartesianRenderer" ); 465 addLineGraph( (LineCartesianRenderer) rend, label ); 466 } 467 468 /** 469 * Remove a line from the LineKey. 470 */ 471 public void removeLineGraph( SGLabel label ) 472 { 473 } 474 475 /** 476 * Remove a line from the LineKey. 477 */ 478 public void removeLineRenderer( LineCartesianRenderer line ) 479 { 480 } 481 482 /** 483 * Remove a line from the LineKey. 484 */ 485 public void removeLineGraph( String ident ) 486 { 487 } 488 489 /** 490 * Remove all lines from the LineKey. 491 */ 492 public void clearAll() 493 { 494 LineAttribute attr; 495 for( Enumeration e = line_.elements(); e.hasMoreElements(); ) 496 { 497 attr = (LineAttribute) ( (LineCartesianRenderer) e.nextElement() ).getAttribute(); 498 attr.removePropertyChangeListener( this ); 499 } 500 line_.removeAllElements(); 501 label_.removeAllElements(); 502 modified( "LineKey: clearAll()" ); 503 } 504 505 /** 506 * Remove line associated with data id from LineKey. 507 * 508 * @since 2.0 509 */ 510 public void clear( String data_id ) 511 { 512 LineCartesianRenderer lcr; 513 int indx = -1; 514 for( Enumeration it = line_.elements(); it.hasMoreElements(); ) 515 { 516 indx++; 517 lcr = (LineCartesianRenderer) it.nextElement(); 518 if( lcr.getLine().getId().equals( data_id ) ) 519 { 520 lcr.getAttribute().removePropertyChangeListener( this ); 521 line_.removeElement( lcr ); 522 label_.removeElementAt( indx ); 523 modified( "LineKey: clear()" ); 524 break; 525 } 526 } 527 } 528 529 /** 530 * Return rowheight of key in pixels. 531 * 532 * @since 2.0 533 */ 534 public int getRowHeight() 535 { 536 Rectangle bounds; 537 bounds = getBounds(); 538 return ROW_SPACE_ + maxLabelHeight_; 539 } 540 541 /** 542 * Draw the Key. 543 */ 544 public void draw( Graphics g ) 545 { 546 double maxLabelLength, maxLabelHeight; 547 int numLines, numRows, i, lineLength; 548 int col, row, ytemp; 549 double xloc, labelSpace; 550 double[] xp, yp; 551 int[] xd, yd; 552 int[] xout, yout; 553 Rectangle bounds; 554 LineCartesianRenderer render = null; 555 SGLabel label; 556 LineAttribute attr = null; 557 // 558 numLines = line_.size(); 559 if( ( numLines <= 0 ) || !visible_ ) return; 560 561 numRows = numLines / columns_; 562 if( numLines % columns_ != 0 ) numRows++; 563 564 xp = new double[columns_]; 565 xd = new int[columns_]; 566 yp = new double[numRows]; 567 yd = new int[numRows]; 568 xout = new int[2]; 569 yout = new int[2]; 570 571 g.setColor( layer_.getPane().getComponent().getForeground() ); 572 bounds = getBounds(); 573 // 574 // compute location of rows and columns in device and physical coordinates 575 // 576 lineLength = layer_.getXPtoD( lineLengthP_ ) - layer_.getXPtoD( 0.0f ); 577 labelSpace = layer_.getXDtoP( LABEL_SPACE_ ) - layer_.getXDtoP( 0 ); 578 // 579 yd[0] = bounds.y + VERTICAL_BORDER_ + maxLabelHeight_; 580 yp[0] = layer_.getYDtoP( yd[0] ); 581 for( i = 1; i < numRows; i++ ) 582 { 583 yd[i] = yd[i - 1] + ROW_SPACE_ + maxLabelHeight_; 584 yp[i] = layer_.getYDtoP( yd[i] ); 585 } 586 xd[0] = bounds.x + HORIZONTAL_BORDER_; 587 xp[0] = layer_.getXDtoP( xd[0] ); 588 for( i = 1; i < columns_; i++ ) 589 { 590 xd[i] = xd[i - 1] + COLUMN_SPACE_ + lineLength + LABEL_SPACE_ + maxLabelLength_; 591 xp[i] = layer_.getXDtoP( xd[i] ); 592 } 593 // 594 row = 0; 547 595 col = 0; 548 row++; 549 } 550 } 551 switch(style_) { 552 case PLAIN_LINE: 553 g.drawRect(bounds.x, bounds.y, bounds.width-1, bounds.height-1); 554 break; 555 case RAISED: 556 break; 557 default: 558 case NO_BORDER: 559 } 560 } 561 /** 562 * Get the bounding rectangle. 563 * 564 * @return bounding rectangle 565 */ 566 public Rectangle getBounds() { 567 int lineLength; 568 int numLines, rows; 569 int x, y, height, width; 570 571 numLines = line_.size(); 572 if(numLines <= 0) return new Rectangle(0, 0, 0, 0); 573 // 574 // find longest label 575 // 576 maxLabelLength_ = 0; 577 maxLabelHeight_ = 0; 578 SGLabel label; 579 for(Enumeration it = label_.elements(); it.hasMoreElements();) { 580 label = (SGLabel)it.nextElement(); 581 Rectangle sz = label.getBounds(); 582 maxLabelLength_ = Math.max(maxLabelLength_, sz.width); 583 maxLabelHeight_ = Math.max(maxLabelHeight_, sz.height); 584 } 585 // 586 rows = numLines/columns_; 587 if(numLines%columns_ != 0) rows++; 588 lineLength = layer_.getXPtoD(lineLengthP_) - layer_.getXPtoD(0.0f); 589 width = 2*HORIZONTAL_BORDER_ + columns_*(lineLength + LABEL_SPACE_ + maxLabelLength_) 590 + (columns_ - 1)*COLUMN_SPACE_; 591 height = 2*VERTICAL_BORDER_ + rows*maxLabelHeight_ + (rows-1)*ROW_SPACE_; 592 // 593 x = layer_.getXPtoD(porigin_.x); 594 y = layer_.getYPtoD(porigin_.y); 595 switch(halign_) { 596 case RIGHT: 597 x = x - width; 598 break; 599 case CENTER: 600 x = x - width/2; 601 } 602 switch(valign_) { 603 case BOTTOM: 604 y = y - height; 605 break; 606 case MIDDLE: 607 y = y - height/2; 608 } 609 return new Rectangle(x, y, width, height); 610 } 611 public Point getLocation() { 612 Rectangle bnds = getBounds(); 613 return new Point(bnds.x, bnds.y); 614 } 615 public void setLocation(Point loc) { 616 Rectangle bnds = getBounds(); 617 setBounds(loc.x, loc.y, bnds.width, bnds.height); 618 } 619 /** 620 * Set the bounds of the <code>LineKey</code>. 621 */ 622 public void setBounds(Rectangle r) { 623 setBounds(r.x, r.y, r.width, r.height); 624 } 625 /** 626 * Set the bounds of the <code>LineKey</code>. 627 * <BR><B>Property Change:</B> <code>location</code>. 628 */ 629 public void setBounds(int x, int y, int width, int height) { 630 switch(halign_) { 631 case RIGHT: 632 x = x + width; 633 break; 634 case CENTER: 635 x = x + width/2; 636 } 637 switch(valign_) { 638 case BOTTOM: 639 y = y + height; 640 break; 641 case MIDDLE: 642 y = y + height/2; 643 } 644 double xp = layer_.getXDtoP(x); 645 double yp = layer_.getYDtoP(y); 646 if(porigin_.x != xp || porigin_.y != yp) { 647 Point2D.Double temp = porigin_; 648 porigin_.x = xp; 649 porigin_.y = yp; 650 changes_.firePropertyChange("location", 651 temp, 652 new Point2D.Double(xp, yp)); 653 modified("LineKey: setBounds()"); 654 } 655 } 656 Object getObjectAt(Point pt) { 657 Rectangle lbnds; 658 Rectangle bounds; 659 LineCartesianRenderer line; 660 int[] xout, yout; 661 int[] xd, yd; 662 int numLines, numRows; 663 int lineLength; 664 double labelSpace; 665 int i; 666 667 numLines = line_.size(); 668 if(numLines <= 0) return null; 669 670 numRows = numLines/columns_; 671 if(numLines%columns_ != 0) numRows++; 672 673 xd = new int[columns_]; 674 yd = new int[numRows]; 675 xout = new int[2]; 676 yout = new int[2]; 677 bounds = getBounds(); 678 // 679 // compute location of rows and columns in device and physical coordinates 680 // 681 lineLength = layer_.getXPtoD(lineLengthP_) - layer_.getXPtoD(0.0); 682 labelSpace = layer_.getXDtoP(LABEL_SPACE_) - layer_.getXDtoP(0); 683 // 684 yd[0] = bounds.y + VERTICAL_BORDER_ + maxLabelHeight_; 685 for(i=1; i < numRows; i++) { 686 yd[i] = yd[i-1] + ROW_SPACE_ + maxLabelHeight_; 687 } 688 xd[0] = bounds.x + HORIZONTAL_BORDER_; 689 for(i=1; i < columns_; i++) { 690 xd[i] = xd[i-1] + COLUMN_SPACE_ + lineLength + LABEL_SPACE_ + maxLabelLength_; 691 } 692 // loop over all the lines 693 int row = 0; 694 int col = 0; 695 for(Enumeration lineIt = line_.elements(); lineIt.hasMoreElements();) { 696 line = (LineCartesianRenderer)lineIt.nextElement(); 697 xout[0] = xd[col]; 698 xout[1] = xout[0] + lineLength + LABEL_SPACE_ + maxLabelLength_; 596 Object obj; 597 Enumeration labelIt = label_.elements(); 598 for( Enumeration lineIt = line_.elements(); lineIt.hasMoreElements(); ) 599 { 600 obj = lineIt.nextElement(); 601 render = (LineCartesianRenderer) obj; 602 attr = (LineAttribute) render.getAttribute(); 603 label = (SGLabel) labelIt.nextElement(); 604 // 605 // draw line 606 // 607 g.setColor( attr.getColor() ); 608 xout[0] = xd[col]; 609 xout[1] = xout[0] + lineLength; 610 611 // VMIPSL 612 if( 0 != maxLabelHeight_ ) 613 yout[0] = yd[row] - maxLabelHeight_ / 2; 614 else 615 yout[0] = yd[row] - 4; 616 yout[1] = yout[0]; 617 switch( attr.getStyle() ) 618 { 619 case LineAttribute.MARK: 620 // VMIPSL 621 xout[0] = xout[1]; 622 render.drawMark( g, xout, yout, 1, attr ); 623 break; 624 case LineAttribute.HIGHLIGHT: 625 stroke_.drawHighlight( g, xout, yout, 2, attr ); 626 break; 627 case LineAttribute.HEAVY: 628 stroke_.drawHeavy( g, xout, yout, 2, attr ); 629 break; 630 case LineAttribute.DASHED: 631 stroke_.drawDashed( g, xout, yout, 2, attr ); 632 break; 633 case LineAttribute.STROKE: 634 stroke_.drawStroke( g, xout, yout, 2, attr ); 635 break; 636 case LineAttribute.MARK_LINE: 637 render.drawMark( g, xout, yout, 2, attr ); 638 default: 639 case LineAttribute.SOLID: 640 g.drawLine( xout[0], yout[0], xout[1], yout[1] ); 641 } 642 // 643 xloc = xp[col] + lineLengthP_ + labelSpace; 644 label.setLocationP( new Point2D.Double( xloc, yp[row] ) ); 645 try 646 { 647 label.draw( g ); 648 } 649 catch( SGException e ) 650 { 651 } 652 // 653 col++; 654 if( col >= columns_ ) 655 { 656 col = 0; 657 row++; 658 } 659 } 660 switch( style_ ) 661 { 662 case PLAIN_LINE: 663 g.drawRect( bounds.x, bounds.y, bounds.width - 1, bounds.height - 1 ); 664 break; 665 case RAISED: 666 break; 667 default: 668 case NO_BORDER: 669 } 670 } 671 672 /** 673 * Get the bounding rectangle. 674 * 675 * @return bounding rectangle 676 */ 677 public Rectangle getBounds() 678 { 679 int lineLength; 680 int numLines, rows; 681 int x, y, height, width; 682 683 numLines = line_.size(); 684 if( numLines <= 0 ) return new Rectangle( 0, 0, 0, 0 ); 685 // 686 // find longest label 687 // 688 maxLabelLength_ = 0; 689 maxLabelHeight_ = 0; 690 SGLabel label; 691 for( Enumeration it = label_.elements(); it.hasMoreElements(); ) 692 { 693 label = (SGLabel) it.nextElement(); 694 Rectangle sz = label.getBounds(); 695 maxLabelLength_ = Math.max( maxLabelLength_, sz.width ); 696 maxLabelHeight_ = Math.max( maxLabelHeight_, sz.height ); 697 } 698 // 699 rows = numLines / columns_; 700 if( numLines % columns_ != 0 ) rows++; 701 lineLength = layer_.getXPtoD( lineLengthP_ ) - layer_.getXPtoD( 0.0f ); 702 width = 2 * HORIZONTAL_BORDER_ + columns_ * ( lineLength + LABEL_SPACE_ + maxLabelLength_ ) 703 + ( columns_ - 1 ) * COLUMN_SPACE_; 704 height = 2 * VERTICAL_BORDER_ + rows * maxLabelHeight_ + ( rows - 1 ) * ROW_SPACE_; 705 // 706 x = layer_.getXPtoD( porigin_.x ); 707 y = layer_.getYPtoD( porigin_.y ); 708 switch( halign_ ) 709 { 710 case RIGHT: 711 x = x - width; 712 break; 713 case CENTER: 714 x = x - width / 2; 715 } 716 switch( valign_ ) 717 { 718 case BOTTOM: 719 y = y - height; 720 break; 721 case MIDDLE: 722 y = y - height / 2; 723 } 724 return new Rectangle( x, y, width, height ); 725 } 726 727 public Point getLocation() 728 { 729 Rectangle bnds = getBounds(); 730 return new Point( bnds.x, bnds.y ); 731 } 732 733 public void setLocation( Point loc ) 734 { 735 Rectangle bnds = getBounds(); 736 setBounds( loc.x, loc.y, bnds.width, bnds.height ); 737 } 738 739 /** 740 * Set the bounds of the <code>LineKey</code>. 741 */ 742 public void setBounds( Rectangle r ) 743 { 744 setBounds( r.x, r.y, r.width, r.height ); 745 } 746 747 /** 748 * Set the bounds of the <code>LineKey</code>. 749 * <BR><B>Property Change:</B> <code>location</code>. 750 */ 751 public void setBounds( int x, int y, int width, int height ) 752 { 753 switch( halign_ ) 754 { 755 case RIGHT: 756 x = x + width; 757 break; 758 case CENTER: 759 x = x + width / 2; 760 } 761 switch( valign_ ) 762 { 763 case BOTTOM: 764 y = y + height; 765 break; 766 case MIDDLE: 767 y = y + height / 2; 768 } 769 double xp = layer_.getXDtoP( x ); 770 double yp = layer_.getYDtoP( y ); 771 if( porigin_.x != xp || porigin_.y != yp ) 772 { 773 Point2D.Double temp = porigin_; 774 porigin_.x = xp; 775 porigin_.y = yp; 776 changes_.firePropertyChange( "location", 777 temp, 778 new Point2D.Double( xp, yp ) ); 779 modified( "LineKey: setBounds()" ); 780 } 781 } 782 783 Object getObjectAt( Point pt ) 784 { 785 Rectangle lbnds; 786 Rectangle bounds; 787 LineCartesianRenderer line; 788 int[] xout, yout; 789 int[] xd, yd; 790 int numLines, numRows; 791 int lineLength; 792 double labelSpace; 793 int i; 794 795 numLines = line_.size(); 796 if( numLines <= 0 ) return null; 797 798 numRows = numLines / columns_; 799 if( numLines % columns_ != 0 ) numRows++; 800 801 xd = new int[columns_]; 802 yd = new int[numRows]; 803 xout = new int[2]; 804 yout = new int[2]; 805 bounds = getBounds(); 806 // 807 // compute location of rows and columns in device and physical coordinates 808 // 809 lineLength = layer_.getXPtoD( lineLengthP_ ) - layer_.getXPtoD( 0.0 ); 810 labelSpace = layer_.getXDtoP( LABEL_SPACE_ ) - layer_.getXDtoP( 0 ); 811 // 812 yd[0] = bounds.y + VERTICAL_BORDER_ + maxLabelHeight_; 813 for( i = 1; i < numRows; i++ ) 814 { 815 yd[i] = yd[i - 1] + ROW_SPACE_ + maxLabelHeight_; 816 } 817 xd[0] = bounds.x + HORIZONTAL_BORDER_; 818 for( i = 1; i < columns_; i++ ) 819 { 820 xd[i] = xd[i - 1] + COLUMN_SPACE_ + lineLength + LABEL_SPACE_ + maxLabelLength_; 821 } 822 // loop over all the lines 823 int row = 0; 824 int col = 0; 825 for( Enumeration lineIt = line_.elements(); lineIt.hasMoreElements(); ) 826 { 827 line = (LineCartesianRenderer) lineIt.nextElement(); 828 xout[0] = xd[col]; 829 xout[1] = xout[0] + lineLength + LABEL_SPACE_ + maxLabelLength_; 699 830 // xout[1] = xout[0] + lineLength + LABEL_SPACE_; 700 yout[0] = yd[row] - maxLabelHeight_; 701 yout[1] = yd[row]; 702 lbnds = new Rectangle(xout[0], yout[0], 703 xout[1] - xout[0], 704 yout[1] - yout[0]); 705 if(lbnds.contains(pt)) { 706 return line; 707 } 708 // 709 col++; 710 if(col >= columns_) { 711 col = 0; 712 row++; 713 } 714 } 715 if(bounds.contains(pt)) { 716 return this; 717 } 718 return null; 719 } 720 public String toString() { 721 String name = getClass().getName(); 722 return name.substring(name.lastIndexOf(".")+1) + ": " + ident_; 723 } 724 public boolean isVisible() { 725 return visible_; 726 } 727 public void setVisible(boolean visible) { 728 if(visible_ != visible) { 729 visible_ = visible; 730 modified("LineKey: setVisible()"); 731 } 732 } 733 public void propertyChange(PropertyChangeEvent evt) { 831 yout[0] = yd[row] - maxLabelHeight_; 832 yout[1] = yd[row]; 833 lbnds = new Rectangle( xout[0], yout[0], 834 xout[1] - xout[0], 835 yout[1] - yout[0] ); 836 if( lbnds.contains( pt ) ) 837 { 838 return line; 839 } 840 // 841 col++; 842 if( col >= columns_ ) 843 { 844 col = 0; 845 row++; 846 } 847 } 848 if( bounds.contains( pt ) ) 849 { 850 return this; 851 } 852 return null; 853 } 854 855 public String toString() 856 { 857 String name = getClass().getName(); 858 return name.substring( name.lastIndexOf( "." ) + 1 ) + ": " + ident_; 859 } 860 861 public boolean isVisible() 862 { 863 return visible_; 864 } 865 866 public void setVisible( boolean visible ) 867 { 868 if( visible_ != visible ) 869 { 870 visible_ = visible; 871 modified( "LineKey: setVisible()" ); 872 } 873 } 874 875 public void propertyChange( PropertyChangeEvent evt ) 876 { 734 877 // if(Debug.EVENT) { 735 878 // System.out.println("LineKey: " + evt); 736 879 // System.out.println(" " + evt.getPropertyName()); 737 880 // } 738 modified("LineKey: propertyChange(" + 739 evt.getSource().toString() + "[" + 740 evt.getPropertyName() + "]" + ")"); 741 } 742 public void addPropertyChangeListener(PropertyChangeListener l) { 743 changes_.addPropertyChangeListener(l); 744 } 745 public void removePropertyChangeListener(PropertyChangeListener l) { 746 changes_.removePropertyChangeListener(l); 747 } 881 modified( "LineKey: propertyChange(" + 882 evt.getSource().toString() + "[" + 883 evt.getPropertyName() + "]" + ")" ); 884 } 885 886 public void addPropertyChangeListener( PropertyChangeListener l ) 887 { 888 changes_.addPropertyChangeListener( l ); 889 } 890 891 public void removePropertyChangeListener( PropertyChangeListener l ) 892 { 893 changes_.removePropertyChangeListener( l ); 894 } 748 895 } -
ether_megapoli/trunk/service/Service.iml
r178 r188 12 12 <orderEntry type="jdk" jdkName="1.6" jdkType="JavaSDK" /> 13 13 <orderEntry type="sourceFolder" forTests="false" /> 14 <orderEntry type="library" name="lib_sgt" level="project" />15 14 <orderEntry type="library" name="lib_ether" level="project" /> 16 15 <orderEntry type="module" module-name="Persistence" /> -
ether_megapoli/trunk/service/implementation/com/ether/EtherPlotContentServiceImpl.java
r187 r188 81 81 mainPane.add( layer ); 82 82 83 final CartesianGraph graph = new CartesianGraph( "First Graph" ); 84 layer.setGraph( graph ); 85 83 86 final Pair<List, List> listPairOfPIdPIdForTwoAxes = extractListPairForAxes( megapoliPlot.getpIdPIdList() ); 84 87 final List<Pair> pIdPIdListForFirstAxis = listPairOfPIdPIdForTwoAxes.getFirstValue(); 85 88 final List<Pair> pIdPIdListForSecondAxis = listPairOfPIdPIdForTwoAxes.getSecondValue(); 86 89 87 final List<SimpleLine> lines = createLines( pIdPIdListForFirstAxis, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate(), 1 );88 final List<SimpleLine> linesForSecondAxis = createLines( pIdPIdListForSecondAxis, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate(), 10 );90 final List<SimpleLine> lines = createLines( pIdPIdListForFirstAxis, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate(), 1, layer ); 91 final List<SimpleLine> linesForSecondAxis = createLines( pIdPIdListForSecondAxis, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate(), 10, layer ); 89 92 final SimpleLine firstLine = lines.get( 0 ); 90 93 … … 122 125 /** ************ GRAPH ********** **/ 123 126 // Create first CartesianGraph and transforms 124 final CartesianGraph graph = new CartesianGraph( "First Graph" );127 // final CartesianGraph graph = new CartesianGraph( "First Graph" ); 125 128 final LinearTransform xt = new LinearTransform( new Range2D( xstart, xend ), xnRange ); 126 129 graph.setXTransform( xt ); … … 153 156 } 154 157 155 layer.setGraph( graph );158 // layer.setGraph( graph ); 156 159 157 160 final LineAttribute lineAttribute = createRandomLineAttrbute( megapoliPlot.getAxeType() ); … … 165 168 lkey.setLocationP( new Point2D.Double( 0.1, ysize - 0.1 ) ); 166 169 lkey.setBorderStyle( LineKey.NO_BORDER ); 170 lkey.setVAlign( LineKey.TOP ); 171 lkey.setHAlign( LineKey.LEFT ); 167 172 layer.addChild( lkey ); 168 173 lkey.addLineGraph( (LineCartesianRenderer) graph.getRenderer(), firstLine.getKeyTitle() ); … … 203 208 otherGraph.setData( line, otherLineAttribute ); 204 209 lkey.addLineGraph( (LineCartesianRenderer) otherGraph.getRenderer(), line.getKeyTitle() ); 210 // lkey.setColumns( lkey.getColumns() + 1 ); 205 211 } 206 212 } … … 285 291 */ 286 292 @NotNull 287 private List<SimpleLine> createLines( @NotNull final List<Pair> pIdPIdList, @Nullable final Date beginDate, @Nullable final Date endDate, final Integer delta )293 private List<SimpleLine> createLines( @NotNull final List<Pair> pIdPIdList, @Nullable final Date beginDate, @Nullable final Date endDate, final Integer delta, @NotNull final Layer layer ) 288 294 throws ServiceException 289 295 { … … 315 321 SGTMetaData meta = new SGTMetaData( "", "", false, false ); 316 322 line.setXMetaData( meta ); 317 line.setKeyTitle( new SGLabel( "Line", plateform.getPlateformeNom() + "-" + parametre.getParametreNom(), new Point2D.Double( 0.0, 0.0 ) ) ); 323 final SGLabel sgLabel = new SGLabel( "Line", plateform.getPlateformeNom() + "-" + parametre.getParametreNom(), new Point2D.Double( 0.0, 0.0 ) ); 324 line.setKeyTitle( sgLabel ); 318 325 319 326 if( isFirstLine ) … … 863 870 protected static final int ERROR_FONT_SIZE = 10; 864 871 865 protected static final Font _axisFont = new Font( "Helvetica", Font.PLAIN, 6);872 protected static final Font _axisFont = new Font( "Helvetica", Font.PLAIN, 15 ); 866 873 protected static final double _heightAxisFont = 0.14; 867 874 protected static final double _markHeight = 0.1; -
ether_megapoli/trunk/service/test/com/ether/SGTTest.java
r171 r188 4 4 import com.ether.annotation.Mandatory; 5 5 import com.ether.annotation.ParamName; 6 import com.ether.tutorial.src.tutorial.Example1;7 import com.ether.tutorial.src.tutorial.SBExample1;8 import com.ether.tutorial.src.tutorial.SBExample2;9 import com.ether.tutorial.src.tutorial.SBExample3;10 import com.keypoint.PngEncoder;11 import com.keypoint.PngEncoderB;6 //import com.ether.tutorial.src.tutorial.Example1; 7 //import com.ether.tutorial.src.tutorial.SBExample1; 8 //import com.ether.tutorial.src.tutorial.SBExample2; 9 //import com.ether.tutorial.src.tutorial.SBExample3; 10 //import com.keypoint.PngEncoder; 11 //import com.keypoint.PngEncoderB; 12 12 import gov.noaa.pmel.sgt.JPane; 13 13 import gov.noaa.pmel.sgt.LineAttribute; … … 111 111 } 112 112 113 @Test114 public void testExample1()115 throws Exception116 {117 new Example1();118 }119 120 @Test121 public void testSBExamples()122 throws Exception123 {124 final SBExample1 frame = new SBExample1();125 frame.pack();126 frame.setResizable( false );127 frame.setVisible( true );128 Thread.sleep( 100 );129 copyToFile( frame, "service/test/test_SBExample1.jpg" );130 131 final SBExample2 frame2 = new SBExample2();132 frame2.pack();133 frame2.setResizable( false );134 frame2.setVisible( true );135 Thread.sleep( 100 );136 copyToFile( frame2, "service/test/test_SBExample2.jpg" );137 138 final SBExample3 frame3 = new SBExample3();139 frame3.pack();140 //Center the window141 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();142 final Dimension frameSize = frame3.getSize();143 if( frameSize.height > screenSize.height )144 {145 frameSize.height = screenSize.height;146 }147 if( frameSize.width > screenSize.width )148 {149 frameSize.width = screenSize.width;150 }151 frame3.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );152 frame3.setResizable( false );153 frame3.setVisible( true );154 Thread.sleep( 100 );155 copyToFile( frame3, "service/test/test_SBExample3.jpg" );156 }113 // @Test 114 // public void testExample1() 115 // throws Exception 116 // { 117 // new Example1(); 118 // } 119 // 120 // @Test 121 // public void testSBExamples() 122 // throws Exception 123 // { 124 // final SBExample1 frame = new SBExample1(); 125 // frame.pack(); 126 // frame.setResizable( false ); 127 // frame.setVisible( true ); 128 // Thread.sleep( 100 ); 129 // copyToFile( frame, "service/test/test_SBExample1.jpg" ); 130 // 131 // final SBExample2 frame2 = new SBExample2(); 132 // frame2.pack(); 133 // frame2.setResizable( false ); 134 // frame2.setVisible( true ); 135 // Thread.sleep( 100 ); 136 // copyToFile( frame2, "service/test/test_SBExample2.jpg" ); 137 // 138 // final SBExample3 frame3 = new SBExample3(); 139 // frame3.pack(); 140 // //Center the window 141 // final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 142 // final Dimension frameSize = frame3.getSize(); 143 // if( frameSize.height > screenSize.height ) 144 // { 145 // frameSize.height = screenSize.height; 146 // } 147 // if( frameSize.width > screenSize.width ) 148 // { 149 // frameSize.width = screenSize.width; 150 // } 151 // frame3.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 ); 152 // frame3.setResizable( false ); 153 // frame3.setVisible( true ); 154 // Thread.sleep( 100 ); 155 // copyToFile( frame3, "service/test/test_SBExample3.jpg" ); 156 // } 157 157 158 158 @Test … … 214 214 // encoder.encode( bufferedImage ); 215 215 216 PngEncoder pngEncoder = new PngEncoder();216 // PngEncoder pngEncoder = new PngEncoder(); 217 217 // pngEncoder.setImage( image ); 218 byte[] bytes = pngEncoder.pngEncode();218 // byte[] bytes = pngEncoder.pngEncode(); 219 219 } 220 220 … … 305 305 if( bImageKer != null ) 306 306 { 307 final PngEncoderB pngEncoderB = new PngEncoderB();308 pngEncoderB.setImage( bImageKer );307 // final PngEncoderB pngEncoderB = new PngEncoderB(); 308 // pngEncoderB.setImage( bImageKer ); 309 309 310 310 // ImageIO.write( bImageKer, "png", response.getOutputStream() ); … … 507 507 // encoder.encode( bufferedImage ); 508 508 509 PngEncoder pngEncoder = new PngEncoder();509 // PngEncoder pngEncoder = new PngEncoder(); 510 510 // pngEncoder.setImage( image ); 511 byte[] bytes = pngEncoder.pngEncode();511 // byte[] bytes = pngEncoder.pngEncode(); 512 512 513 513 //final BufferedImage bufferedImage = ImageHelper.createBufferedImage( timeSeriesPlot ); … … 517 517 g2.dispose(); 518 518 519 final PngEncoderB pngEncoderB = new PngEncoderB();520 pngEncoderB.setImage( bufferedImage );521 final byte[] bytesB = pngEncoderB.pngEncode();519 // final PngEncoderB pngEncoderB = new PngEncoderB(); 520 // pngEncoderB.setImage( bufferedImage ); 521 // final byte[] bytesB = pngEncoderB.pngEncode(); 522 522 523 523 final JSONObject result = new JSONObject(); 524 result.put( ParameterConstants.PARAMETER_IMAGE, bytesB );524 // result.put( ParameterConstants.PARAMETER_IMAGE, bytesB ); 525 525 return result; 526 526 // return new JSONObject();
Note: See TracChangeset
for help on using the changeset viewer.