1 44 45 package org.jfree.chart.editor; 46 47 import java.awt.BasicStroke ; 48 import java.awt.BorderLayout ; 49 import java.awt.Color ; 50 import java.awt.Paint ; 51 import java.awt.Stroke ; 52 import java.awt.event.ActionEvent ; 53 import java.awt.event.ActionListener ; 54 import java.util.ResourceBundle ; 55 56 import javax.swing.BorderFactory ; 57 import javax.swing.JButton ; 58 import javax.swing.JCheckBox ; 59 import javax.swing.JColorChooser ; 60 import javax.swing.JComboBox ; 61 import javax.swing.JLabel ; 62 import javax.swing.JOptionPane ; 63 import javax.swing.JPanel ; 64 import javax.swing.JTabbedPane ; 65 66 import org.jfree.chart.axis.Axis; 67 import org.jfree.chart.axis.ColorBar; 68 import org.jfree.chart.plot.CategoryPlot; 69 import org.jfree.chart.plot.ContourPlot; 70 import org.jfree.chart.plot.Plot; 71 import org.jfree.chart.plot.PlotOrientation; 72 import org.jfree.chart.plot.XYPlot; 73 import org.jfree.chart.renderer.category.CategoryItemRenderer; 74 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 75 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 76 import org.jfree.chart.renderer.xy.XYItemRenderer; 77 import org.jfree.layout.LCBLayout; 78 import org.jfree.ui.PaintSample; 79 import org.jfree.ui.RectangleInsets; 80 import org.jfree.ui.StrokeChooserPanel; 81 import org.jfree.ui.StrokeSample; 82 import org.jfree.util.BooleanUtilities; 83 84 87 class DefaultPlotEditor extends JPanel implements ActionListener { 88 89 90 private final static String [] orientationNames = {"Vertical", "Horizontal"}; 91 private final static int ORIENTATION_VERTICAL = 0; 92 private final static int ORIENTATION_HORIZONTAL = 1; 93 94 95 private PaintSample backgroundPaintSample; 96 97 98 private StrokeSample outlineStrokeSample; 99 100 101 private PaintSample outlinePaintSample; 102 103 106 private DefaultAxisEditor domainAxisPropertyPanel; 107 108 111 private DefaultAxisEditor rangeAxisPropertyPanel; 112 113 117 private DefaultColorBarEditor colorBarAxisPropertyPanel; 118 119 120 private StrokeSample[] availableStrokeSamples; 121 122 123 private RectangleInsets plotInsets; 124 125 129 private PlotOrientation plotOrientation; 130 131 135 private JComboBox orientationCombo; 136 137 140 private Boolean drawLines; 141 142 145 private JCheckBox drawLinesCheckBox; 146 147 150 private Boolean drawShapes; 151 152 155 private JCheckBox drawShapesCheckBox; 156 157 158 protected static ResourceBundle localizationResources 159 = ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle"); 160 161 172 public DefaultPlotEditor(Plot plot) { 173 174 this.plotInsets = plot.getInsets(); 175 this.backgroundPaintSample = new PaintSample(plot.getBackgroundPaint()); 176 this.outlineStrokeSample = new StrokeSample(plot.getOutlineStroke()); 177 this.outlinePaintSample = new PaintSample(plot.getOutlinePaint()); 178 if (plot instanceof CategoryPlot) { 179 this.plotOrientation = ((CategoryPlot) plot).getOrientation(); 180 } 181 else if (plot instanceof XYPlot) { 182 this.plotOrientation = ((XYPlot) plot).getOrientation(); 183 } 184 if (plot instanceof CategoryPlot) { 185 CategoryItemRenderer renderer = ((CategoryPlot) plot).getRenderer(); 186 if (renderer instanceof LineAndShapeRenderer) { 187 LineAndShapeRenderer r = (LineAndShapeRenderer) renderer; 188 this.drawLines 189 = BooleanUtilities.valueOf(r.getBaseLinesVisible()); 190 this.drawShapes 191 = BooleanUtilities.valueOf(r.getBaseShapesVisible()); 192 } 193 } 194 else if (plot instanceof XYPlot) { 195 XYItemRenderer renderer = ((XYPlot) plot).getRenderer(); 196 if (renderer instanceof StandardXYItemRenderer) { 197 StandardXYItemRenderer r = (StandardXYItemRenderer) renderer; 198 this.drawLines = BooleanUtilities.valueOf(r.getPlotLines()); 199 this.drawShapes = BooleanUtilities.valueOf(r.getBaseShapesVisible()); 200 } 201 } 202 203 setLayout(new BorderLayout ()); 204 205 this.availableStrokeSamples = new StrokeSample[3]; 206 this.availableStrokeSamples[0] 207 = new StrokeSample(new BasicStroke (1.0f)); 208 this.availableStrokeSamples[1] 209 = new StrokeSample(new BasicStroke (2.0f)); 210 this.availableStrokeSamples[2] 211 = new StrokeSample(new BasicStroke (3.0f)); 212 213 JPanel panel = new JPanel (new BorderLayout ()); 215 panel.setBorder( 216 BorderFactory.createTitledBorder( 217 BorderFactory.createEtchedBorder(), 218 plot.getPlotType() + localizationResources.getString(":") 219 ) 220 ); 221 222 JPanel general = new JPanel (new BorderLayout ()); 223 general.setBorder( 224 BorderFactory.createTitledBorder( 225 localizationResources.getString("General") 226 ) 227 ); 228 229 JPanel interior = new JPanel (new LCBLayout(7)); 230 interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 231 232 244 interior.add( 245 new JLabel (localizationResources.getString("Outline_stroke")) 246 ); 247 JButton button = new JButton (localizationResources.getString("Select...")); 248 button.setActionCommand("OutlineStroke"); 249 button.addActionListener(this); 250 interior.add(this.outlineStrokeSample); 251 interior.add(button); 252 253 interior.add( 254 new JLabel (localizationResources.getString("Outline_Paint")) 255 ); 256 button = new JButton (localizationResources.getString("Select...")); 257 button.setActionCommand("OutlinePaint"); 258 button.addActionListener(this); 259 interior.add(this.outlinePaintSample); 260 interior.add(button); 261 262 interior.add( 263 new JLabel (localizationResources.getString("Background_paint")) 264 ); 265 button = new JButton (localizationResources.getString("Select...")); 266 button.setActionCommand("BackgroundPaint"); 267 button.addActionListener(this); 268 interior.add(this.backgroundPaintSample); 269 interior.add(button); 270 271 if (this.plotOrientation != null) { 272 boolean isVertical 273 = this.plotOrientation.equals(PlotOrientation.VERTICAL); 274 int index 275 = isVertical ? ORIENTATION_VERTICAL : ORIENTATION_HORIZONTAL; 276 interior.add( 277 new JLabel (localizationResources.getString("Orientation")) 278 ); 279 this.orientationCombo = new JComboBox (orientationNames); 280 this.orientationCombo.setSelectedIndex(index); 281 this.orientationCombo.setActionCommand("Orientation"); 282 this.orientationCombo.addActionListener(this); 283 interior.add(new JPanel ()); 284 interior.add(this.orientationCombo); 285 } 286 287 if (this.drawLines != null) { 288 interior.add( 289 new JLabel (localizationResources.getString("Draw_lines")) 290 ); 291 this.drawLinesCheckBox = new JCheckBox (); 292 this.drawLinesCheckBox.setSelected(this.drawLines.booleanValue()); 293 this.drawLinesCheckBox.setActionCommand("DrawLines"); 294 this.drawLinesCheckBox.addActionListener(this); 295 interior.add(new JPanel ()); 296 interior.add(this.drawLinesCheckBox); 297 } 298 299 if (this.drawShapes != null) { 300 interior.add( 301 new JLabel (localizationResources.getString("Draw_shapes")) 302 ); 303 this.drawShapesCheckBox = new JCheckBox (); 304 this.drawShapesCheckBox.setSelected(this.drawShapes.booleanValue()); 305 this.drawShapesCheckBox.setActionCommand("DrawShapes"); 306 this.drawShapesCheckBox.addActionListener(this); 307 interior.add(new JPanel ()); 308 interior.add(this.drawShapesCheckBox); 309 } 310 311 general.add(interior, BorderLayout.NORTH); 312 313 JPanel appearance = new JPanel (new BorderLayout ()); 314 appearance.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); 315 appearance.add(general, BorderLayout.NORTH); 316 317 JTabbedPane tabs = new JTabbedPane (); 318 tabs.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 319 320 Axis domainAxis = null; 321 if (plot instanceof CategoryPlot) { 322 domainAxis = ((CategoryPlot) plot).getDomainAxis(); 323 } 324 else if (plot instanceof XYPlot) { 325 domainAxis = ((XYPlot) plot).getDomainAxis(); 326 } 327 this.domainAxisPropertyPanel 328 = DefaultAxisEditor.getInstance(domainAxis); 329 if (this.domainAxisPropertyPanel != null) { 330 this.domainAxisPropertyPanel.setBorder( 331 BorderFactory.createEmptyBorder(2, 2, 2, 2) 332 ); 333 tabs.add( 334 localizationResources.getString("Domain_Axis"), 335 this.domainAxisPropertyPanel 336 ); 337 } 338 339 Axis rangeAxis = null; 340 if (plot instanceof CategoryPlot) { 341 rangeAxis = ((CategoryPlot) plot).getRangeAxis(); 342 } 343 else if (plot instanceof XYPlot) { 344 rangeAxis = ((XYPlot) plot).getRangeAxis(); 345 } 346 347 this.rangeAxisPropertyPanel 348 = DefaultAxisEditor.getInstance(rangeAxis); 349 if (this.rangeAxisPropertyPanel != null) { 350 this.rangeAxisPropertyPanel.setBorder( 351 BorderFactory.createEmptyBorder(2, 2, 2, 2) 352 ); 353 tabs.add( 354 localizationResources.getString("Range_Axis"), 355 this.rangeAxisPropertyPanel 356 ); 357 } 358 359 ColorBar colorBar = null; 361 if (plot instanceof ContourPlot) { 362 colorBar = ((ContourPlot) plot).getColorBar(); 363 } 364 365 this.colorBarAxisPropertyPanel 366 = DefaultColorBarEditor.getInstance(colorBar); 367 if (this.colorBarAxisPropertyPanel != null) { 368 this.colorBarAxisPropertyPanel.setBorder( 369 BorderFactory.createEmptyBorder(2, 2, 2, 2) 370 ); 371 tabs.add( 372 localizationResources.getString("Color_Bar"), 373 this.colorBarAxisPropertyPanel 374 ); 375 } 376 378 tabs.add(localizationResources.getString("Appearance"), appearance); 379 panel.add(tabs); 380 381 add(panel); 382 } 383 384 389 public RectangleInsets getPlotInsets() { 390 if (this.plotInsets == null) { 391 this.plotInsets = new RectangleInsets(0.0, 0.0, 0.0, 0.0); 392 } 393 return this.plotInsets; 394 } 395 396 401 public Paint getBackgroundPaint() { 402 return this.backgroundPaintSample.getPaint(); 403 } 404 405 410 public Stroke getOutlineStroke() { 411 return this.outlineStrokeSample.getStroke(); 412 } 413 414 419 public Paint getOutlinePaint() { 420 return this.outlinePaintSample.getPaint(); 421 } 422 423 429 public DefaultAxisEditor getDomainAxisPropertyEditPanel() { 430 return this.domainAxisPropertyPanel; 431 } 432 433 439 public DefaultAxisEditor getRangeAxisPropertyEditPanel() { 440 return this.rangeAxisPropertyPanel; 441 } 442 443 447 public void actionPerformed(ActionEvent event) { 448 String command = event.getActionCommand(); 449 if (command.equals("BackgroundPaint")) { 450 attemptBackgroundPaintSelection(); 451 } 452 else if (command.equals("OutlineStroke")) { 453 attemptOutlineStrokeSelection(); 454 } 455 else if (command.equals("OutlinePaint")) { 456 attemptOutlinePaintSelection(); 457 } 458 else if (command.equals("Orientation")) { 462 attemptOrientationSelection(); 463 } 464 else if (command.equals("DrawLines")) { 465 attemptDrawLinesSelection(); 466 } 467 else if (command.equals("DrawShapes")) { 468 attemptDrawShapesSelection(); 469 } 470 } 471 472 475 private void attemptBackgroundPaintSelection() { 476 Color c; 477 c = JColorChooser.showDialog( 478 this, localizationResources.getString("Background_Color"), 479 Color.blue 480 ); 481 if (c != null) { 482 this.backgroundPaintSample.setPaint(c); 483 } 484 } 485 486 489 private void attemptOutlineStrokeSelection() { 490 StrokeChooserPanel panel 491 = new StrokeChooserPanel(null, this.availableStrokeSamples); 492 int result = JOptionPane.showConfirmDialog(this, panel, 493 localizationResources.getString("Stroke_Selection"), 494 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 495 496 if (result == JOptionPane.OK_OPTION) { 497 this.outlineStrokeSample.setStroke(panel.getSelectedStroke()); 498 } 499 } 500 501 505 private void attemptOutlinePaintSelection() { 506 Color c; 507 c = JColorChooser.showDialog( 508 this, localizationResources.getString("Outline_Color"), Color.blue 509 ); 510 if (c != null) { 511 this.outlinePaintSample.setPaint(c); 512 } 513 } 514 515 536 private void attemptOrientationSelection() { 537 538 int index = this.orientationCombo.getSelectedIndex(); 539 540 if (index == ORIENTATION_VERTICAL) { 541 this.plotOrientation = PlotOrientation.VERTICAL; 542 } 543 else { 544 this.plotOrientation = PlotOrientation.HORIZONTAL; 545 } 546 } 547 548 553 private void attemptDrawLinesSelection() { 554 this.drawLines = BooleanUtilities.valueOf( 555 this.drawLinesCheckBox.isSelected() 556 ); 557 } 558 559 563 private void attemptDrawShapesSelection() { 564 this.drawShapes = BooleanUtilities.valueOf( 565 this.drawShapesCheckBox.isSelected() 566 ); 567 } 568 569 574 public void updatePlotProperties(Plot plot) { 575 576 plot.setOutlinePaint(getOutlinePaint()); 578 plot.setOutlineStroke(getOutlineStroke()); 579 plot.setBackgroundPaint(getBackgroundPaint()); 580 plot.setInsets(getPlotInsets()); 581 582 if (this.domainAxisPropertyPanel != null) { 584 Axis domainAxis = null; 585 if (plot instanceof CategoryPlot) { 586 CategoryPlot p = (CategoryPlot) plot; 587 domainAxis = p.getDomainAxis(); 588 } 589 else if (plot instanceof XYPlot) { 590 XYPlot p = (XYPlot) plot; 591 domainAxis = p.getDomainAxis(); 592 } 593 if (domainAxis != null) { 594 this.domainAxisPropertyPanel.setAxisProperties(domainAxis); 595 } 596 } 597 598 if (this.rangeAxisPropertyPanel != null) { 599 Axis rangeAxis = null; 600 if (plot instanceof CategoryPlot) { 601 CategoryPlot p = (CategoryPlot) plot; 602 rangeAxis = p.getRangeAxis(); 603 } 604 else if (plot instanceof XYPlot) { 605 XYPlot p = (XYPlot) plot; 606 rangeAxis = p.getRangeAxis(); 607 } 608 if (rangeAxis != null) { 609 this.rangeAxisPropertyPanel.setAxisProperties(rangeAxis); 610 } 611 } 612 613 if (this.plotOrientation != null) { 614 if (plot instanceof CategoryPlot) { 615 CategoryPlot p = (CategoryPlot) plot; 616 p.setOrientation(this.plotOrientation); 617 } 618 else if (plot instanceof XYPlot) { 619 XYPlot p = (XYPlot) plot; 620 p.setOrientation(this.plotOrientation); 621 } 622 } 623 624 if (this.drawLines != null) { 625 if (plot instanceof CategoryPlot) { 626 CategoryPlot p = (CategoryPlot) plot; 627 CategoryItemRenderer r = p.getRenderer(); 628 if (r instanceof LineAndShapeRenderer) { 629 ((LineAndShapeRenderer) r).setLinesVisible( 630 this.drawLines.booleanValue() 631 ); 632 } 633 } 634 else if (plot instanceof XYPlot) { 635 XYPlot p = (XYPlot) plot; 636 XYItemRenderer r = p.getRenderer(); 637 if (r instanceof StandardXYItemRenderer) { 638 ((StandardXYItemRenderer) r).setPlotLines( 639 this.drawLines.booleanValue() 640 ); 641 } 642 } 643 } 644 645 if (this.drawShapes != null) { 646 if (plot instanceof CategoryPlot) { 647 CategoryPlot p = (CategoryPlot) plot; 648 CategoryItemRenderer r = p.getRenderer(); 649 if (r instanceof LineAndShapeRenderer) { 650 ((LineAndShapeRenderer) r).setShapesVisible( 651 this.drawShapes.booleanValue() 652 ); 653 } 654 } 655 else if (plot instanceof XYPlot) { 656 XYPlot p = (XYPlot) plot; 657 XYItemRenderer r = p.getRenderer(); 658 if (r instanceof StandardXYItemRenderer) { 659 ((StandardXYItemRenderer) r).setBaseShapesVisible( 660 this.drawShapes.booleanValue()); 661 } 662 } 663 } 664 665 if (this.colorBarAxisPropertyPanel != null) { 667 ColorBar colorBar = null; 668 if (plot instanceof ContourPlot) { 669 ContourPlot p = (ContourPlot) plot; 670 colorBar = p.getColorBar(); 671 } 672 if (colorBar != null) { 673 this.colorBarAxisPropertyPanel.setAxisProperties(colorBar); 674 } 675 } 676 678 } 679 680 } 681 | Popular Tags |