1 48 49 package org.jfree.chart.ui; 50 51 import java.awt.BasicStroke ; 52 import java.awt.BorderLayout ; 53 import java.awt.Color ; 54 import java.awt.Font ; 55 import java.awt.Paint ; 56 import java.awt.Stroke ; 57 import java.awt.event.ActionEvent ; 58 import java.awt.event.ActionListener ; 59 import java.util.ResourceBundle ; 60 61 import javax.swing.BorderFactory ; 62 import javax.swing.JButton ; 63 import javax.swing.JCheckBox ; 64 import javax.swing.JColorChooser ; 65 import javax.swing.JLabel ; 66 import javax.swing.JOptionPane ; 67 import javax.swing.JPanel ; 68 69 import org.jfree.chart.JFreeChart; 70 import org.jfree.chart.OldLegend; 71 import org.jfree.chart.DefaultOldLegend; 72 import org.jfree.layout.LCBLayout; 73 import org.jfree.ui.FontChooserPanel; 74 import org.jfree.ui.FontDisplayField; 75 import org.jfree.ui.PaintSample; 76 import org.jfree.ui.StrokeChooserPanel; 77 import org.jfree.ui.StrokeSample; 78 79 82 class LegendPropertyEditPanel extends JPanel implements ActionListener { 83 84 85 private boolean showLegend; 86 87 88 private JCheckBox showLegendCheckBox; 89 90 91 private StrokeSample outlineStroke; 92 93 94 private JButton selectOutlineStrokeButton; 95 96 97 private PaintSample outlinePaint; 98 99 100 private JButton selectOutlinePaintButton; 101 102 103 private PaintSample backgroundPaint; 104 105 106 private JButton selectBackgroundPaintButton; 107 108 109 private Font seriesFont; 110 111 112 private JButton selectSeriesFontButton; 113 114 115 private PaintSample seriesPaint; 116 117 118 private JButton selectSeriesPaintButton; 119 120 121 private StrokeSample[] availableStrokeSamples; 122 123 124 private FontDisplayField fontDisplayField; 125 126 127 protected static ResourceBundle localizationResources 128 = ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle"); 129 130 137 public LegendPropertyEditPanel(OldLegend legend) { 138 139 DefaultOldLegend l = (legend != null 140 ? (DefaultOldLegend) legend : new DefaultOldLegend()); 141 this.showLegend = (legend != null); 142 this.outlineStroke = new StrokeSample(l.getOutlineStroke()); 143 this.outlinePaint = new PaintSample(l.getOutlinePaint()); 144 this.backgroundPaint = new PaintSample(l.getBackgroundPaint()); 145 this.seriesFont = l.getItemFont(); 146 this.seriesPaint = new PaintSample(l.getItemPaint()); 147 148 this.availableStrokeSamples = new StrokeSample[4]; 149 this.availableStrokeSamples[0] 150 = new StrokeSample(new BasicStroke (1.0f)); 151 this.availableStrokeSamples[1] 152 = new StrokeSample(new BasicStroke (2.0f)); 153 this.availableStrokeSamples[2] 154 = new StrokeSample(new BasicStroke (3.0f)); 155 this.availableStrokeSamples[3] 156 = new StrokeSample(new BasicStroke (4.0f)); 157 158 setLayout(new BorderLayout ()); 159 160 JPanel general = new JPanel (new BorderLayout ()); 161 general.setBorder( 162 BorderFactory.createTitledBorder( 163 BorderFactory.createEtchedBorder(), 164 localizationResources.getString("General") 165 ) 166 ); 167 168 JPanel interior = new JPanel (new LCBLayout(6)); 169 interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 170 171 interior.add( 172 new JLabel (localizationResources.getString("Show_Legend")) 173 ); 174 this.showLegendCheckBox = new JCheckBox (); 175 this.showLegendCheckBox.setSelected(this.showLegend); 176 this.showLegendCheckBox.setActionCommand("ShowLegend"); 177 this.showLegendCheckBox.addActionListener(this); 178 interior.add(new JPanel ()); 179 interior.add(this.showLegendCheckBox); 180 181 interior.add(new JLabel (localizationResources.getString("Outline"))); 182 interior.add(this.outlineStroke); 183 this.selectOutlineStrokeButton 184 = new JButton (localizationResources.getString("Select...")); 185 this.selectOutlineStrokeButton.setActionCommand("OutlineStroke"); 186 this.selectOutlineStrokeButton.addActionListener(this); 187 interior.add(this.selectOutlineStrokeButton); 188 189 interior.add( 190 new JLabel (localizationResources.getString("Outline_Paint")) 191 ); 192 this.selectOutlinePaintButton 193 = new JButton (localizationResources.getString("Select...")); 194 this.selectOutlinePaintButton.setActionCommand("OutlinePaint"); 195 this.selectOutlinePaintButton.addActionListener(this); 196 interior.add(this.outlinePaint); 197 interior.add(this.selectOutlinePaintButton); 198 199 interior.add(new JLabel (localizationResources.getString("Background"))); 200 this.selectBackgroundPaintButton = new JButton ( 201 localizationResources.getString("Select...") 202 ); 203 this.selectBackgroundPaintButton.setActionCommand("BackgroundPaint"); 204 this.selectBackgroundPaintButton.addActionListener(this); 205 interior.add(this.backgroundPaint); 206 interior.add(this.selectBackgroundPaintButton); 207 208 interior.add( 209 new JLabel (localizationResources.getString("Series_label_font")) 210 ); 211 this.selectSeriesFontButton 212 = new JButton (localizationResources.getString("Select...")); 213 this.selectSeriesFontButton.setActionCommand("SeriesFont"); 214 this.selectSeriesFontButton.addActionListener(this); 215 this.fontDisplayField = new FontDisplayField(this.seriesFont); 216 interior.add(this.fontDisplayField); 217 interior.add(this.selectSeriesFontButton); 218 219 interior.add( 220 new JLabel (localizationResources.getString("Series_label_paint")) 221 ); 222 this.selectSeriesPaintButton 223 = new JButton (localizationResources.getString("Select...")); 224 this.selectSeriesPaintButton.setActionCommand("SeriesPaint"); 225 this.selectSeriesPaintButton.addActionListener(this); 226 interior.add(this.seriesPaint); 227 interior.add(this.selectSeriesPaintButton); 228 229 this.enableOrDisableControls(); 230 231 general.add(interior); 232 add(general, BorderLayout.NORTH); 233 } 234 235 240 public Stroke getOutlineStroke() { 241 return this.outlineStroke.getStroke(); 242 } 243 244 249 public Paint getOutlinePaint() { 250 return this.outlinePaint.getPaint(); 251 } 252 253 258 public Paint getBackgroundPaint() { 259 return this.backgroundPaint.getPaint(); 260 } 261 262 267 public Font getSeriesFont() { 268 return this.seriesFont; 269 } 270 271 276 public Paint getSeriesPaint() { 277 return this.seriesPaint.getPaint(); 278 } 279 280 285 public void actionPerformed(ActionEvent event) { 286 String command = event.getActionCommand(); 287 if (command.equals("OutlineStroke")) { 288 attemptModifyOutlineStroke(); 289 } 290 else if (command.equals("OutlinePaint")) { 291 attemptModifyOutlinePaint(); 292 } 293 else if (command.equals("BackgroundPaint")) { 294 attemptModifyBackgroundPaint(); 295 } 296 else if (command.equals("SeriesFont")) { 297 attemptModifySeriesFont(); 298 } 299 else if (command.equals("SeriesPaint")) { 300 attemptModifySeriesPaint(); 301 } 302 else if (command.equals("ShowLegend")) { 303 attemptModifyShowLegend(); 304 } 305 } 306 307 310 private void attemptModifyOutlineStroke() { 311 312 StrokeChooserPanel panel = new StrokeChooserPanel( 313 this.outlineStroke, this.availableStrokeSamples 314 ); 315 int result = JOptionPane.showConfirmDialog( 316 this, panel, 317 localizationResources.getString("Pen_Stroke_Selection"), 318 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 319 ); 320 321 if (result == JOptionPane.OK_OPTION) { 322 this.outlineStroke.setStroke(panel.getSelectedStroke()); 323 } 324 325 } 326 327 330 private void attemptModifyOutlinePaint() { 331 Color c; 332 c = JColorChooser.showDialog( 333 this, localizationResources.getString("Outline_Color"), 334 Color.blue 335 ); 336 if (c != null) { 337 this.outlinePaint.setPaint(c); 338 } 339 } 340 341 344 private void attemptModifyBackgroundPaint() { 345 Color c; 346 c = JColorChooser.showDialog( 347 this, localizationResources.getString("Background_Color"), 348 Color.blue 349 ); 350 if (c != null) { 351 this.backgroundPaint.setPaint(c); 352 } 353 } 354 355 358 public void attemptModifySeriesFont() { 359 360 FontChooserPanel panel = new FontChooserPanel(this.seriesFont); 361 int result = JOptionPane.showConfirmDialog( 362 this, panel, localizationResources.getString("Font_Selection"), 363 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 364 ); 365 366 if (result == JOptionPane.OK_OPTION) { 367 this.seriesFont = panel.getSelectedFont(); 368 this.fontDisplayField.setText( 369 this.seriesFont.getFontName() + ", " + this.seriesFont.getSize() 370 ); 371 } 372 373 } 374 375 378 private void attemptModifySeriesPaint() { 379 Color c; 380 c = JColorChooser.showDialog( 381 this, localizationResources.getString("Series_Label_Color"), 382 Color.blue 383 ); 384 if (c != null) { 385 this.seriesPaint.setPaint(c); 386 } 387 } 388 389 393 private void attemptModifyShowLegend() { 394 this.showLegend = this.showLegendCheckBox.isSelected(); 395 this.enableOrDisableControls(); 396 } 397 398 402 private void enableOrDisableControls() { 403 boolean enabled = (this.showLegend == true); 404 this.selectOutlineStrokeButton.setEnabled(enabled); 405 this.selectOutlinePaintButton.setEnabled(enabled); 406 this.selectBackgroundPaintButton.setEnabled(enabled); 407 this.selectSeriesFontButton.setEnabled(enabled); 408 this.selectSeriesPaintButton.setEnabled(enabled); 409 } 410 411 417 public void setLegendProperties(JFreeChart chart) { 418 if (this.showLegend) { 419 OldLegend legend = chart.getOldLegend(); 420 if (legend == null) { 421 legend = new DefaultOldLegend(); 422 chart.setOldLegend(legend); 423 } 424 if (legend instanceof DefaultOldLegend) { 425 DefaultOldLegend standard = (DefaultOldLegend) legend; 427 standard.setOutlineStroke(getOutlineStroke()); 428 standard.setOutlinePaint(getOutlinePaint()); 429 standard.setBackgroundPaint(getBackgroundPaint()); 430 standard.setItemFont(getSeriesFont()); 431 standard.setItemPaint(getSeriesPaint()); 432 } 433 else { 434 } 436 } 437 else { 438 chart.setOldLegend(null); 439 } 440 } 441 442 } 443 | Popular Tags |