1 53 54 package org.jfree.chart.ui; 55 56 import java.awt.BorderLayout ; 57 import java.awt.Color ; 58 import java.awt.Font ; 59 import java.awt.Paint ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.ResourceBundle ; 63 64 import javax.swing.BorderFactory ; 65 import javax.swing.JButton ; 66 import javax.swing.JCheckBox ; 67 import javax.swing.JColorChooser ; 68 import javax.swing.JLabel ; 69 import javax.swing.JOptionPane ; 70 import javax.swing.JPanel ; 71 import javax.swing.JTextField ; 72 73 import org.jfree.chart.JFreeChart; 74 import org.jfree.chart.title.Title; 75 import org.jfree.chart.title.TextTitle; 76 import org.jfree.layout.LCBLayout; 77 import org.jfree.ui.FontChooserPanel; 78 import org.jfree.ui.FontDisplayField; 79 import org.jfree.ui.PaintSample; 80 81 84 public class TitlePropertyEditPanel extends JPanel implements ActionListener { 85 86 87 private boolean showTitle; 88 89 90 private JCheckBox showTitleCheckBox; 91 92 93 private JTextField titleField; 94 95 96 private Font titleFont; 97 98 99 private JTextField fontfield; 100 101 102 private JButton selectFontButton; 103 104 105 private PaintSample titlePaint; 106 107 108 private JButton selectPaintButton; 109 110 111 protected static ResourceBundle localizationResources 112 = ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle"); 113 114 120 public TitlePropertyEditPanel(Title title) { 121 122 TextTitle t = (title != null ? (TextTitle) title 123 : new TextTitle(localizationResources.getString("Title"))); 124 this.showTitle = (title != null); 125 this.titleFont = t.getFont(); 126 this.titleField = new JTextField (t.getText()); 127 this.titlePaint = new PaintSample(t.getPaint()); 128 129 setLayout(new BorderLayout ()); 130 131 JPanel general = new JPanel (new BorderLayout ()); 132 general.setBorder( 133 BorderFactory.createTitledBorder( 134 BorderFactory.createEtchedBorder(), 135 localizationResources.getString("General") 136 ) 137 ); 138 139 JPanel interior = new JPanel (new LCBLayout(4)); 140 interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); 141 142 interior.add(new JLabel (localizationResources.getString("Show_Title"))); 143 this.showTitleCheckBox = new JCheckBox (); 144 this.showTitleCheckBox.setSelected(this.showTitle); 145 this.showTitleCheckBox.setActionCommand("ShowTitle"); 146 this.showTitleCheckBox.addActionListener(this); 147 interior.add(new JPanel ()); 148 interior.add(this.showTitleCheckBox); 149 150 JLabel titleLabel = new JLabel (localizationResources.getString("Text")); 151 interior.add(titleLabel); 152 interior.add(this.titleField); 153 interior.add(new JPanel ()); 154 155 JLabel fontLabel = new JLabel (localizationResources.getString("Font")); 156 this.fontfield = new FontDisplayField(this.titleFont); 157 this.selectFontButton = new JButton ( 158 localizationResources.getString("Select...") 159 ); 160 this.selectFontButton.setActionCommand("SelectFont"); 161 this.selectFontButton.addActionListener(this); 162 interior.add(fontLabel); 163 interior.add(this.fontfield); 164 interior.add(this.selectFontButton); 165 166 JLabel colorLabel = new JLabel ( 167 localizationResources.getString("Color") 168 ); 169 this.selectPaintButton = new JButton ( 170 localizationResources.getString("Select...") 171 ); 172 this.selectPaintButton.setActionCommand("SelectPaint"); 173 this.selectPaintButton.addActionListener(this); 174 interior.add(colorLabel); 175 interior.add(this.titlePaint); 176 interior.add(this.selectPaintButton); 177 178 this.enableOrDisableControls(); 179 180 general.add(interior); 181 add(general, BorderLayout.NORTH); 182 } 183 184 189 public String getTitleText() { 190 return this.titleField.getText(); 191 } 192 193 198 public Font getTitleFont() { 199 return this.titleFont; 200 } 201 202 207 public Paint getTitlePaint() { 208 return this.titlePaint.getPaint(); 209 } 210 211 217 public void actionPerformed(ActionEvent event) { 218 219 String command = event.getActionCommand(); 220 221 if (command.equals("SelectFont")) { 222 attemptFontSelection(); 223 } 224 else if (command.equals("SelectPaint")) { 225 attemptPaintSelection(); 226 } 227 else if (command.equals("ShowTitle")) { 228 attemptModifyShowTitle(); 229 } 230 } 231 232 235 public void attemptFontSelection() { 236 237 FontChooserPanel panel = new FontChooserPanel(this.titleFont); 238 int result = 239 JOptionPane.showConfirmDialog( 240 this, panel, localizationResources.getString("Font_Selection"), 241 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE 242 ); 243 244 if (result == JOptionPane.OK_OPTION) { 245 this.titleFont = panel.getSelectedFont(); 246 this.fontfield.setText( 247 this.titleFont.getFontName() + " " + this.titleFont.getSize() 248 ); 249 } 250 } 251 252 258 public void attemptPaintSelection() { 259 Paint p = this.titlePaint.getPaint(); 260 Color defaultColor = (p instanceof Color ? (Color ) p : Color.blue); 261 Color c = JColorChooser.showDialog( 262 this, localizationResources.getString("Title_Color"), defaultColor 263 ); 264 if (c != null) { 265 this.titlePaint.setPaint(c); 266 } 267 } 268 269 273 private void attemptModifyShowTitle() { 274 this.showTitle = this.showTitleCheckBox.isSelected(); 275 this.enableOrDisableControls(); 276 } 277 278 282 private void enableOrDisableControls() { 283 boolean enabled = (this.showTitle == true); 284 this.titleField.setEnabled(enabled); 285 this.selectFontButton.setEnabled(enabled); 286 this.selectPaintButton.setEnabled(enabled); 287 } 288 289 295 public void setTitleProperties(JFreeChart chart) { 296 if (this.showTitle) { 297 TextTitle title = chart.getTitle(); 298 if (title == null) { 299 title = new TextTitle(); 300 chart.setTitle(title); 301 } 302 title.setText(getTitleText()); 303 title.setFont(getTitleFont()); 304 title.setPaint(getTitlePaint()); 305 } 306 else { 307 chart.setTitle((TextTitle) null); 308 } 309 } 310 311 } 312 | Popular Tags |