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.JTextField ; 62 63 import org.jfree.chart.JFreeChart; 64 import org.jfree.chart.title.Title; 65 import org.jfree.chart.title.TextTitle; 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 71 74 class DefaultTitleEditor extends JPanel implements ActionListener { 75 76 77 private boolean showTitle; 78 79 80 private JCheckBox showTitleCheckBox; 81 82 83 private JTextField titleField; 84 85 86 private Font titleFont; 87 88 89 private JTextField fontfield; 90 91 92 private JButton selectFontButton; 93 94 95 private PaintSample titlePaint; 96 97 98 private JButton selectPaintButton; 99 100 101 protected static ResourceBundle localizationResources 102 = ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle"); 103 104 110 public DefaultTitleEditor(Title title) { 111 112 TextTitle t = (title != null ? (TextTitle) title 113 : new TextTitle(localizationResources.getString("Title"))); 114 this.showTitle = (title != null); 115 this.titleFont = t.getFont(); 116 this.titleField = new JTextField (t.getText()); 117 this.titlePaint = new PaintSample(t.getPaint()); 118 119 setLayout(new BorderLayout ()); 120 121 JPanel general = new JPanel (new BorderLayout ()); 122 general.setBorder( 123 BorderFactory.createTitledBorder( 124 BorderFactory.createEtchedBorder(), 125 localizationResources.getString("General") 126 ) 127 ); 128 129 JPanel interior = new JPanel (new LCBLayout(4)); 130 interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 131 132 interior.add(new JLabel (localizationResources.getString("Show_Title"))); 133 this.showTitleCheckBox = new JCheckBox (); 134 this.showTitleCheckBox.setSelected(this.showTitle); 135 this.showTitleCheckBox.setActionCommand("ShowTitle"); 136 this.showTitleCheckBox.addActionListener(this); 137 interior.add(new JPanel ()); 138 interior.add(this.showTitleCheckBox); 139 140 JLabel titleLabel = new JLabel (localizationResources.getString("Text")); 141 interior.add(titleLabel); 142 interior.add(this.titleField); 143 interior.add(new JPanel ()); 144 145 JLabel fontLabel = new JLabel (localizationResources.getString("Font")); 146 this.fontfield = new FontDisplayField(this.titleFont); 147 this.selectFontButton = new JButton ( 148 localizationResources.getString("Select...") 149 ); 150 this.selectFontButton.setActionCommand("SelectFont"); 151 this.selectFontButton.addActionListener(this); 152 interior.add(fontLabel); 153 interior.add(this.fontfield); 154 interior.add(this.selectFontButton); 155 156 JLabel colorLabel = new JLabel ( 157 localizationResources.getString("Color") 158 ); 159 this.selectPaintButton = new JButton ( 160 localizationResources.getString("Select...") 161 ); 162 this.selectPaintButton.setActionCommand("SelectPaint"); 163 this.selectPaintButton.addActionListener(this); 164 interior.add(colorLabel); 165 interior.add(this.titlePaint); 166 interior.add(this.selectPaintButton); 167 168 this.enableOrDisableControls(); 169 170 general.add(interior); 171 add(general, BorderLayout.NORTH); 172 } 173 174 179 public String getTitleText() { 180 return this.titleField.getText(); 181 } 182 183 188 public Font getTitleFont() { 189 return this.titleFont; 190 } 191 192 197 public Paint getTitlePaint() { 198 return this.titlePaint.getPaint(); 199 } 200 201 207 public void actionPerformed(ActionEvent event) { 208 209 String command = event.getActionCommand(); 210 211 if (command.equals("SelectFont")) { 212 attemptFontSelection(); 213 } 214 else if (command.equals("SelectPaint")) { 215 attemptPaintSelection(); 216 } 217 else if (command.equals("ShowTitle")) { 218 attemptModifyShowTitle(); 219 } 220 } 221 222 225 public void attemptFontSelection() { 226 227 FontChooserPanel panel = new FontChooserPanel(this.titleFont); 228 int result = 229 JOptionPane.showConfirmDialog( 230 this, panel, localizationResources.getString("Font_Selection"), 231 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 232 ); 233 234 if (result == JOptionPane.OK_OPTION) { 235 this.titleFont = panel.getSelectedFont(); 236 this.fontfield.setText( 237 this.titleFont.getFontName() + " " + this.titleFont.getSize() 238 ); 239 } 240 } 241 242 248 public void attemptPaintSelection() { 249 Paint p = this.titlePaint.getPaint(); 250 Color defaultColor = (p instanceof Color ? (Color ) p : Color.blue); 251 Color c = JColorChooser.showDialog( 252 this, localizationResources.getString("Title_Color"), defaultColor 253 ); 254 if (c != null) { 255 this.titlePaint.setPaint(c); 256 } 257 } 258 259 263 private void attemptModifyShowTitle() { 264 this.showTitle = this.showTitleCheckBox.isSelected(); 265 this.enableOrDisableControls(); 266 } 267 268 272 private void enableOrDisableControls() { 273 boolean enabled = (this.showTitle == true); 274 this.titleField.setEnabled(enabled); 275 this.selectFontButton.setEnabled(enabled); 276 this.selectPaintButton.setEnabled(enabled); 277 } 278 279 285 public void setTitleProperties(JFreeChart chart) { 286 if (this.showTitle) { 287 TextTitle title = chart.getTitle(); 288 if (title == null) { 289 title = new TextTitle(); 290 chart.setTitle(title); 291 } 292 title.setText(getTitleText()); 293 title.setFont(getTitleFont()); 294 title.setPaint(getTitlePaint()); 295 } 296 else { 297 chart.setTitle((TextTitle) null); 298 } 299 } 300 301 } 302 | Popular Tags |