1 54 55 package org.jfree.chart.ui; 56 57 import java.awt.BasicStroke ; 58 import java.awt.BorderLayout ; 59 import java.awt.Color ; 60 import java.awt.Paint ; 61 import java.awt.Stroke ; 62 import java.awt.event.ActionEvent ; 63 import java.awt.event.ActionListener ; 64 import java.util.ResourceBundle ; 65 66 import javax.swing.BorderFactory ; 67 import javax.swing.JButton ; 68 import javax.swing.JCheckBox ; 69 import javax.swing.JColorChooser ; 70 import javax.swing.JComboBox ; 71 import javax.swing.JLabel ; 72 import javax.swing.JOptionPane ; 73 import javax.swing.JPanel ; 74 import javax.swing.JTabbedPane ; 75 76 import org.jfree.chart.axis.Axis; 77 import org.jfree.chart.axis.ColorBar; 78 import org.jfree.chart.plot.CategoryPlot; 79 import org.jfree.chart.plot.ContourPlot; 80 import org.jfree.chart.plot.Plot; 81 import org.jfree.chart.plot.PlotOrientation; 82 import org.jfree.chart.plot.XYPlot; 83 import org.jfree.chart.renderer.category.CategoryItemRenderer; 84 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 85 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 86 import org.jfree.chart.renderer.xy.XYItemRenderer; 87 import org.jfree.layout.LCBLayout; 88 import org.jfree.ui.PaintSample; 89 import org.jfree.ui.RectangleInsets; 90 import org.jfree.ui.StrokeChooserPanel; 91 import org.jfree.ui.StrokeSample; 92 import org.jfree.util.BooleanUtilities; 93 94 97 public class PlotPropertyEditPanel extends JPanel implements ActionListener { 98 99 100 private final static String [] orientationNames = {"Vertical", "Horizontal"}; 101 private final static int ORIENTATION_VERTICAL = 0; 102 private final static int ORIENTATION_HORIZONTAL = 1; 103 104 105 private PaintSample backgroundPaintSample; 106 107 108 private StrokeSample outlineStrokeSample; 109 110 111 private PaintSample outlinePaintSample; 112 113 116 private AxisPropertyEditPanel domainAxisPropertyPanel; 117 118 121 private AxisPropertyEditPanel rangeAxisPropertyPanel; 122 123 127 private ColorBarPropertyEditPanel colorBarAxisPropertyPanel; 128 129 130 private StrokeSample[] availableStrokeSamples; 131 132 133 private RectangleInsets plotInsets; 134 135 138 142 private PlotOrientation plotOrientation; 143 144 148 private JComboBox orientationCombo; 149 150 153 private Boolean drawLines; 154 155 158 private JCheckBox drawLinesCheckBox; 159 160 163 private Boolean drawShapes; 164 165 168 private JCheckBox drawShapesCheckBox; 169 170 171 protected static ResourceBundle localizationResources 172 = ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle"); 173 174 185 public PlotPropertyEditPanel(Plot plot) { 186 187 this.plotInsets = plot.getInsets(); 188 this.backgroundPaintSample = new PaintSample(plot.getBackgroundPaint()); 189 this.outlineStrokeSample = new StrokeSample(plot.getOutlineStroke()); 190 this.outlinePaintSample = new PaintSample(plot.getOutlinePaint()); 191 if (plot instanceof CategoryPlot) { 192 this.plotOrientation = ((CategoryPlot) plot).getOrientation(); 193 } 194 else if (plot instanceof XYPlot) { 195 this.plotOrientation = ((XYPlot) plot).getOrientation(); 196 } 197 if (plot instanceof CategoryPlot) { 198 CategoryItemRenderer renderer = ((CategoryPlot) plot).getRenderer(); 199 if (renderer instanceof LineAndShapeRenderer) { 200 LineAndShapeRenderer r = (LineAndShapeRenderer) renderer; 201 this.drawLines = BooleanUtilities.valueOf(r.isLinesVisible()); 202 this.drawShapes = BooleanUtilities.valueOf(r.isShapesVisible()); 203 } 204 } 205 else if (plot instanceof XYPlot) { 206 XYItemRenderer renderer = ((XYPlot) plot).getRenderer(); 207 if (renderer instanceof StandardXYItemRenderer) { 208 StandardXYItemRenderer r = (StandardXYItemRenderer) renderer; 209 this.drawLines = BooleanUtilities.valueOf(r.getPlotLines()); 210 this.drawShapes = BooleanUtilities.valueOf(r.getPlotShapes()); 211 } 212 } 213 214 setLayout(new BorderLayout ()); 215 216 this.availableStrokeSamples = new StrokeSample[3]; 217 this.availableStrokeSamples[0] 218 = new StrokeSample(new BasicStroke (1.0f)); 219 this.availableStrokeSamples[1] 220 = new StrokeSample(new BasicStroke (2.0f)); 221 this.availableStrokeSamples[2] 222 = new StrokeSample(new BasicStroke (3.0f)); 223 224 JPanel panel = new JPanel (new BorderLayout ()); 226 panel.setBorder( 227 BorderFactory.createTitledBorder( 228 BorderFactory.createEtchedBorder(), 229 plot.getPlotType() + localizationResources.getString(":") 230 ) 231 ); 232 233 JPanel general = new JPanel (new BorderLayout ()); 234 general.setBorder( 235 BorderFactory.createTitledBorder( 236 localizationResources.getString("General") 237 ) 238 ); 239 240 JPanel interior = new JPanel (new LCBLayout(7)); 241 interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 242 243 255 interior.add( 256 new JLabel (localizationResources.getString("Outline_stroke")) 257 ); 258 JButton button = new JButton (localizationResources.getString("Select...")); 259 button.setActionCommand("OutlineStroke"); 260 button.addActionListener(this); 261 interior.add(this.outlineStrokeSample); 262 interior.add(button); 263 264 interior.add( 265 new JLabel (localizationResources.getString("Outline_Paint")) 266 ); 267 button = new JButton (localizationResources.getString("Select...")); 268 button.setActionCommand("OutlinePaint"); 269 button.addActionListener(this); 270 interior.add(this.outlinePaintSample); 271 interior.add(button); 272 273 interior.add( 274 new JLabel (localizationResources.getString("Background_paint")) 275 ); 276 button = new JButton (localizationResources.getString("Select...")); 277 button.setActionCommand("BackgroundPaint"); 278 button.addActionListener(this); 279 interior.add(this.backgroundPaintSample); 280 interior.add(button); 281 282 if (this.plotOrientation != null) { 283 boolean isVertical 284 = this.plotOrientation.equals(PlotOrientation.VERTICAL); 285 int index 286 = isVertical ? ORIENTATION_VERTICAL : ORIENTATION_HORIZONTAL; 287 interior.add( 288 new JLabel (localizationResources.getString("Orientation")) 289 ); 290 this.orientationCombo = new JComboBox (orientationNames); 291 this.orientationCombo.setSelectedIndex(index); 292 this.orientationCombo.setActionCommand("Orientation"); 293 this.orientationCombo.addActionListener(this); 294 interior.add(new JPanel ()); 295 interior.add(this.orientationCombo); 296 } 297 298 if (this.drawLines != null) { 299 interior.add( 300 new JLabel (localizationResources.getString("Draw_lines")) 301 ); 302 this.drawLinesCheckBox = new JCheckBox (); 303 this.drawLinesCheckBox.setSelected(this.drawLines.booleanValue()); 304 this.drawLinesCheckBox.setActionCommand("DrawLines"); 305 this.drawLinesCheckBox.addActionListener(this); 306 interior.add(new JPanel ()); 307 interior.add(this.drawLinesCheckBox); 308 } 309 310 if (this.drawShapes != null) { 311 interior.add( 312 new JLabel (localizationResources.getString("Draw_shapes")) 313 ); 314 this.drawShapesCheckBox = new JCheckBox (); 315 this.drawShapesCheckBox.setSelected(this.drawShapes.booleanValue()); 316 this.drawShapesCheckBox.setActionCommand("DrawShapes"); 317 this.drawShapesCheckBox.addActionListener(this); 318 interior.add(new JPanel ()); 319 interior.add(this.drawShapesCheckBox); 320 } 321 322 general.add(interior, BorderLayout.NORTH); 323 324 JPanel appearance = new JPanel (new BorderLayout ()); 325 appearance.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); 326 appearance.add(general, BorderLayout.NORTH); 327 328 JTabbedPane tabs = new JTabbedPane (); 329 tabs.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 330 331 Axis domainAxis = null; 332 if (plot instanceof CategoryPlot) { 333 domainAxis = ((CategoryPlot) plot).getDomainAxis(); 334 } 335 else if (plot instanceof XYPlot) { 336 domainAxis = ((XYPlot) plot).getDomainAxis(); 337 } 338 this.domainAxisPropertyPanel 339 = AxisPropertyEditPanel.getInstance(domainAxis); 340 if (this.domainAxisPropertyPanel != null) { 341 this.domainAxisPropertyPanel.setBorder( 342 BorderFactory.createEmptyBorder(2, 2, 2, 2) 343 ); 344 tabs.add( 345 localizationResources.getString("Domain_Axis"), 346 this.domainAxisPropertyPanel 347 ); 348 } 349 350 Axis rangeAxis = null; 351 if (plot instanceof CategoryPlot) { 352 rangeAxis = ((CategoryPlot) plot).getRangeAxis(); 353 } 354 else if (plot instanceof XYPlot) { 355 rangeAxis = ((XYPlot) plot).getRangeAxis(); 356 } 357 358 this.rangeAxisPropertyPanel 359 = AxisPropertyEditPanel.getInstance(rangeAxis); 360 if (this.rangeAxisPropertyPanel != null) { 361 this.rangeAxisPropertyPanel.setBorder( 362 BorderFactory.createEmptyBorder(2, 2, 2, 2) 363 ); 364 tabs.add( 365 localizationResources.getString("Range_Axis"), 366 this.rangeAxisPropertyPanel 367 ); 368 } 369 370 ColorBar colorBar = null; 372 if (plot instanceof ContourPlot) { 373 colorBar = ((ContourPlot) plot).getColorBar(); 374 } 375 376 this.colorBarAxisPropertyPanel 377 = ColorBarPropertyEditPanel.getInstance(colorBar); 378 if (this.colorBarAxisPropertyPanel != null) { 379 this.colorBarAxisPropertyPanel.setBorder( 380 BorderFactory.createEmptyBorder(2, 2, 2, 2) 381 ); 382 tabs.add( 383 localizationResources.getString("Color_Bar"), 384 this.colorBarAxisPropertyPanel 385 ); 386 } 387 389 tabs.add(localizationResources.getString("Appearance"), appearance); 390 panel.add(tabs); 391 392 add(panel); 393 } 394 395 400 public RectangleInsets getPlotInsets() { 401 if (this.plotInsets == null) { 402 this.plotInsets = new RectangleInsets(0.0, 0.0, 0.0, 0.0); 403 } 404 return this.plotInsets; 405 } 406 407 412 public Paint getBackgroundPaint() { 413 return this.backgroundPaintSample.getPaint(); 414 } 415 416 421 public Stroke getOutlineStroke() { 422 return this.outlineStrokeSample.getStroke(); 423 } 424 425 430 public Paint getOutlinePaint() { 431 return this.outlinePaintSample.getPaint(); 432 } 433 434 440 public AxisPropertyEditPanel getDomainAxisPropertyEditPanel() { 441 return this.domainAxisPropertyPanel; 442 } 443 444 450 public AxisPropertyEditPanel getRangeAxisPropertyEditPanel() { 451 return this.rangeAxisPropertyPanel; 452 } 453 454 458 public void actionPerformed(ActionEvent event) { 459 String command = event.getActionCommand(); 460 if (command.equals("BackgroundPaint")) { 461 attemptBackgroundPaintSelection(); 462 } 463 else if (command.equals("OutlineStroke")) { 464 attemptOutlineStrokeSelection(); 465 } 466 else if (command.equals("OutlinePaint")) { 467 attemptOutlinePaintSelection(); 468 } 469 else if (command.equals("Orientation")) { 473 attemptOrientationSelection(); 474 } 475 else if (command.equals("DrawLines")) { 476 attemptDrawLinesSelection(); 477 } 478 else if (command.equals("DrawShapes")) { 479 attemptDrawShapesSelection(); 480 } 481 } 482 483 486 private void attemptBackgroundPaintSelection() { 487 Color c; 488 c = JColorChooser.showDialog( 489 this, localizationResources.getString("Background_Color"), 490 Color.blue 491 ); 492 if (c != null) { 493 this.backgroundPaintSample.setPaint(c); 494 } 495 } 496 497 500 private void attemptOutlineStrokeSelection() { 501 StrokeChooserPanel panel 502 = new StrokeChooserPanel(null, this.availableStrokeSamples); 503 int result = JOptionPane.showConfirmDialog(this, panel, 504 localizationResources.getString("Stroke_Selection"), 505 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 506 507 if (result == JOptionPane.OK_OPTION) { 508 this.outlineStrokeSample.setStroke(panel.getSelectedStroke()); 509 } 510 } 511 512 516 private void attemptOutlinePaintSelection() { 517 Color c; 518 c = JColorChooser.showDialog( 519 this, localizationResources.getString("Outline_Color"), Color.blue 520 ); 521 if (c != null) { 522 this.outlinePaintSample.setPaint(c); 523 } 524 } 525 526 547 private void attemptOrientationSelection() { 548 549 int index = this.orientationCombo.getSelectedIndex(); 550 551 if (index == ORIENTATION_VERTICAL) { 552 this.plotOrientation = PlotOrientation.VERTICAL; 553 } 554 else { 555 this.plotOrientation = PlotOrientation.HORIZONTAL; 556 } 557 } 558 559 564 private void attemptDrawLinesSelection() { 565 this.drawLines = BooleanUtilities.valueOf( 566 this.drawLinesCheckBox.isSelected() 567 ); 568 } 569 570 574 private void attemptDrawShapesSelection() { 575 this.drawShapes = BooleanUtilities.valueOf( 576 this.drawShapesCheckBox.isSelected() 577 ); 578 } 579 580 585 public void updatePlotProperties(Plot plot) { 586 587 plot.setOutlinePaint(getOutlinePaint()); 589 plot.setOutlineStroke(getOutlineStroke()); 590 plot.setBackgroundPaint(getBackgroundPaint()); 591 plot.setInsets(getPlotInsets()); 592 593 if (this.domainAxisPropertyPanel != null) { 595 Axis domainAxis = null; 596 if (plot instanceof CategoryPlot) { 597 CategoryPlot p = (CategoryPlot) plot; 598 domainAxis = p.getDomainAxis(); 599 } 600 else if (plot instanceof XYPlot) { 601 XYPlot p = (XYPlot) plot; 602 domainAxis = p.getDomainAxis(); 603 } 604 if (domainAxis != null) { 605 this.domainAxisPropertyPanel.setAxisProperties(domainAxis); 606 } 607 } 608 609 if (this.rangeAxisPropertyPanel != null) { 610 Axis rangeAxis = null; 611 if (plot instanceof CategoryPlot) { 612 CategoryPlot p = (CategoryPlot) plot; 613 rangeAxis = p.getRangeAxis(); 614 } 615 else if (plot instanceof XYPlot) { 616 XYPlot p = (XYPlot) plot; 617 rangeAxis = p.getRangeAxis(); 618 } 619 if (rangeAxis != null) { 620 this.rangeAxisPropertyPanel.setAxisProperties(rangeAxis); 621 } 622 } 623 624 if (this.plotOrientation != null) { 625 if (plot instanceof CategoryPlot) { 626 CategoryPlot p = (CategoryPlot) plot; 627 p.setOrientation(this.plotOrientation); 628 } 629 else if (plot instanceof XYPlot) { 630 XYPlot p = (XYPlot) plot; 631 p.setOrientation(this.plotOrientation); 632 } 633 } 634 635 if (this.drawLines != null) { 636 if (plot instanceof CategoryPlot) { 637 CategoryPlot p = (CategoryPlot) plot; 638 CategoryItemRenderer r = p.getRenderer(); 639 if (r instanceof LineAndShapeRenderer) { 640 ((LineAndShapeRenderer) r).setLinesVisible( 641 this.drawLines.booleanValue() 642 ); 643 } 644 } 645 else if (plot instanceof XYPlot) { 646 XYPlot p = (XYPlot) plot; 647 XYItemRenderer r = p.getRenderer(); 648 if (r instanceof StandardXYItemRenderer) { 649 ((StandardXYItemRenderer) r).setPlotLines( 650 this.drawLines.booleanValue() 651 ); 652 } 653 } 654 } 655 656 if (this.drawShapes != null) { 657 if (plot instanceof CategoryPlot) { 658 CategoryPlot p = (CategoryPlot) plot; 659 CategoryItemRenderer r = p.getRenderer(); 660 if (r instanceof LineAndShapeRenderer) { 661 ((LineAndShapeRenderer) r).setShapesVisible( 662 this.drawShapes.booleanValue() 663 ); 664 } 665 } 666 else if (plot instanceof XYPlot) { 667 XYPlot p = (XYPlot) plot; 668 XYItemRenderer r = p.getRenderer(); 669 if (r instanceof StandardXYItemRenderer) { 670 ((StandardXYItemRenderer) r).setPlotShapes( 671 this.drawShapes.booleanValue() 672 ); 673 } 674 } 675 } 676 677 if (this.colorBarAxisPropertyPanel != null) { 679 ColorBar colorBar = null; 680 if (plot instanceof ContourPlot) { 681 ContourPlot p = (ContourPlot) plot; 682 colorBar = p.getColorBar(); 683 } 684 if (colorBar != null) { 685 this.colorBarAxisPropertyPanel.setAxisProperties(colorBar); 686 } 687 } 688 690 } 691 692 } 693 | Popular Tags |