1 43 44 package org.jfree.chart.editor; 45 46 import java.awt.BorderLayout ; 47 import java.awt.Color ; 48 import java.awt.Font ; 49 import java.awt.Paint ; 50 import java.awt.event.ActionEvent ; 51 import java.awt.event.ActionListener ; 52 import java.util.ResourceBundle ; 53 54 import javax.swing.BorderFactory ; 55 import javax.swing.JButton ; 56 import javax.swing.JCheckBox ; 57 import javax.swing.JColorChooser ; 58 import javax.swing.JLabel ; 59 import javax.swing.JOptionPane ; 60 import javax.swing.JPanel ; 61 import javax.swing.JTabbedPane ; 62 import javax.swing.JTextField ; 63 64 import org.jfree.chart.axis.Axis; 65 import org.jfree.chart.axis.NumberAxis; 66 import org.jfree.layout.LCBLayout; 67 import org.jfree.ui.FontChooserPanel; 68 import org.jfree.ui.FontDisplayField; 69 import org.jfree.ui.PaintSample; 70 import org.jfree.ui.RectangleInsets; 71 72 75 class DefaultAxisEditor extends JPanel implements ActionListener { 76 77 78 private JTextField label; 79 80 81 private Font labelFont; 82 83 84 private PaintSample labelPaintSample; 85 86 87 private JTextField labelFontField; 88 89 90 private Font tickLabelFont; 91 92 96 private JTextField tickLabelFontField; 97 98 99 private PaintSample tickLabelPaintSample; 100 101 105 private JPanel slot1; 106 107 111 private JPanel slot2; 112 113 114 private JCheckBox showTickLabelsCheckBox; 115 116 117 private JCheckBox showTickMarksCheckBox; 118 119 125 126 private RectangleInsets tickLabelInsets; 127 128 129 private RectangleInsets labelInsets; 130 131 132 private JTabbedPane otherTabs; 133 134 135 protected static ResourceBundle localizationResources = 136 ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle"); 137 138 147 public static DefaultAxisEditor getInstance(Axis axis) { 148 149 if (axis != null) { 150 if (axis instanceof NumberAxis) { 153 return new DefaultNumberAxisEditor((NumberAxis) axis); 154 } 155 else { 156 return new DefaultAxisEditor(axis); 157 } 158 } 159 else { 160 return null; 161 } 162 163 } 164 165 172 public DefaultAxisEditor(Axis axis) { 173 174 this.labelFont = axis.getLabelFont(); 175 this.labelPaintSample = new PaintSample(axis.getLabelPaint()); 176 this.tickLabelFont = axis.getTickLabelFont(); 177 this.tickLabelPaintSample = new PaintSample(axis.getTickLabelPaint()); 178 179 this.tickLabelInsets = axis.getTickLabelInsets(); 181 this.labelInsets = axis.getLabelInsets(); 182 183 setLayout(new BorderLayout ()); 184 185 JPanel general = new JPanel (new BorderLayout ()); 186 general.setBorder( 187 BorderFactory.createTitledBorder( 188 BorderFactory.createEtchedBorder(), 189 localizationResources.getString("General") 190 ) 191 ); 192 193 JPanel interior = new JPanel (new LCBLayout(5)); 194 interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 195 interior.add(new JLabel (localizationResources.getString("Label"))); 196 this.label = new JTextField (axis.getLabel()); 197 interior.add(this.label); 198 interior.add(new JPanel ()); 199 200 interior.add(new JLabel (localizationResources.getString("Font"))); 201 this.labelFontField = new FontDisplayField(this.labelFont); 202 interior.add(this.labelFontField); 203 JButton b = new JButton (localizationResources.getString("Select...")); 204 b.setActionCommand("SelectLabelFont"); 205 b.addActionListener(this); 206 interior.add(b); 207 208 interior.add(new JLabel (localizationResources.getString("Paint"))); 209 interior.add(this.labelPaintSample); 210 b = new JButton (localizationResources.getString("Select...")); 211 b.setActionCommand("SelectLabelPaint"); 212 b.addActionListener(this); 213 interior.add(b); 214 215 236 general.add(interior); 237 238 add(general, BorderLayout.NORTH); 239 240 this.slot1 = new JPanel (new BorderLayout ()); 241 242 JPanel other = new JPanel (new BorderLayout ()); 243 other.setBorder(BorderFactory.createTitledBorder( 244 BorderFactory.createEtchedBorder(), 245 localizationResources.getString("Other"))); 246 247 this.otherTabs = new JTabbedPane (); 248 this.otherTabs.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 249 250 JPanel ticks = new JPanel (new LCBLayout(3)); 251 ticks.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 252 253 this.showTickLabelsCheckBox = new JCheckBox ( 254 localizationResources.getString("Show_tick_labels"), 255 axis.isTickLabelsVisible() 256 ); 257 ticks.add(this.showTickLabelsCheckBox); 258 ticks.add(new JPanel ()); 259 ticks.add(new JPanel ()); 260 261 ticks.add( 262 new JLabel (localizationResources.getString("Tick_label_font")) 263 ); 264 this.tickLabelFontField = new FontDisplayField(this.tickLabelFont); 265 ticks.add(this.tickLabelFontField); 266 b = new JButton (localizationResources.getString("Select...")); 267 b.setActionCommand("SelectTickLabelFont"); 268 b.addActionListener(this); 269 ticks.add(b); 270 271 this.showTickMarksCheckBox = new JCheckBox ( 272 localizationResources.getString("Show_tick_marks"), 273 axis.isTickMarksVisible() 274 ); 275 ticks.add(this.showTickMarksCheckBox); 276 ticks.add(new JPanel ()); 277 ticks.add(new JPanel ()); 278 279 this.otherTabs.add(localizationResources.getString("Ticks"), ticks); 280 281 other.add(this.otherTabs); 282 283 this.slot1.add(other); 284 285 this.slot2 = new JPanel (new BorderLayout ()); 286 this.slot2.add(this.slot1, BorderLayout.NORTH); 287 add(this.slot2); 288 289 } 290 291 296 public String getLabel() { 297 return this.label.getText(); 298 } 299 300 305 public Font getLabelFont() { 306 return this.labelFont; 307 } 308 309 314 public Paint getLabelPaint() { 315 return this.labelPaintSample.getPaint(); 316 } 317 318 323 public boolean isTickLabelsVisible() { 324 return this.showTickLabelsCheckBox.isSelected(); 325 } 326 327 332 public Font getTickLabelFont() { 333 return this.tickLabelFont; 334 } 335 336 341 public Paint getTickLabelPaint() { 342 return this.tickLabelPaintSample.getPaint(); 343 } 344 345 351 public boolean isTickMarksVisible() { 352 return this.showTickMarksCheckBox.isSelected(); 353 } 354 355 360 public RectangleInsets getTickLabelInsets() { 361 return (this.tickLabelInsets == null) 362 ? new RectangleInsets(0, 0, 0, 0) 363 : this.tickLabelInsets; 364 } 365 366 371 public RectangleInsets getLabelInsets() { 372 return (this.labelInsets == null) 373 ? new RectangleInsets(0, 0, 0, 0) : this.labelInsets; 374 } 375 376 381 public JTabbedPane getOtherTabs() { 382 return this.otherTabs; 383 } 384 385 391 public void actionPerformed(ActionEvent event) { 392 String command = event.getActionCommand(); 393 if (command.equals("SelectLabelFont")) { 394 attemptLabelFontSelection(); 395 } 396 else if (command.equals("SelectLabelPaint")) { 397 attemptModifyLabelPaint(); 398 } 399 else if (command.equals("SelectTickLabelFont")) { 400 attemptTickLabelFontSelection(); 401 } 402 } 409 410 413 private void attemptLabelFontSelection() { 414 415 FontChooserPanel panel = new FontChooserPanel(this.labelFont); 416 int result = JOptionPane.showConfirmDialog(this, panel, 417 localizationResources.getString("Font_Selection"), 418 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 419 420 if (result == JOptionPane.OK_OPTION) { 421 this.labelFont = panel.getSelectedFont(); 422 this.labelFontField.setText( 423 this.labelFont.getFontName() + " " + this.labelFont.getSize() 424 ); 425 } 426 427 } 428 429 432 private void attemptModifyLabelPaint() { 433 Color c; 434 c = JColorChooser.showDialog( 435 this, localizationResources.getString("Label_Color"), Color.blue 436 ); 437 if (c != null) { 438 this.labelPaintSample.setPaint(c); 439 } 440 } 441 442 445 public void attemptTickLabelFontSelection() { 446 447 FontChooserPanel panel = new FontChooserPanel(this.tickLabelFont); 448 int result = JOptionPane.showConfirmDialog(this, panel, 449 localizationResources.getString("Font_Selection"), 450 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 451 452 if (result == JOptionPane.OK_OPTION) { 453 this.tickLabelFont = panel.getSelectedFont(); 454 this.tickLabelFontField.setText( 455 this.tickLabelFont.getFontName() + " " 456 + this.tickLabelFont.getSize() 457 ); 458 } 459 460 } 461 462 498 504 public void setAxisProperties(Axis axis) { 505 axis.setLabel(getLabel()); 506 axis.setLabelFont(getLabelFont()); 507 axis.setLabelPaint(getLabelPaint()); 508 axis.setTickMarksVisible(isTickMarksVisible()); 509 axis.setTickLabelsVisible(isTickLabelsVisible()); 511 axis.setTickLabelFont(getTickLabelFont()); 512 axis.setTickLabelPaint(getTickLabelPaint()); 513 axis.setTickLabelInsets(getTickLabelInsets()); 514 axis.setLabelInsets(getLabelInsets()); 515 } 516 517 } 518 | Popular Tags |