1 77 78 package org.jfree.chart.demo; 79 80 import java.awt.BorderLayout ; 81 import java.awt.Dimension ; 82 import java.awt.Font ; 83 import java.awt.event.ActionEvent ; 84 import java.awt.event.ActionListener ; 85 import java.awt.event.WindowAdapter ; 86 import java.awt.event.WindowEvent ; 87 import java.awt.event.WindowListener ; 88 import java.util.ResourceBundle ; 89 import java.util.Vector ; 90 91 import javax.swing.BorderFactory ; 92 import javax.swing.JButton ; 93 import javax.swing.JFrame ; 94 import javax.swing.JMenu ; 95 import javax.swing.JMenuBar ; 96 import javax.swing.JMenuItem ; 97 import javax.swing.JOptionPane ; 98 import javax.swing.JPanel ; 99 import javax.swing.JScrollPane ; 100 import javax.swing.JTabbedPane ; 101 import javax.swing.JTextArea ; 102 103 import org.jfree.chart.ChartFrame; 104 import org.jfree.chart.ChartPanel; 105 import org.jfree.chart.JFreeChart; 106 import org.jfree.layout.LCBLayout; 107 import org.jfree.ui.RefineryUtilities; 108 import org.jfree.ui.about.AboutFrame; 109 110 115 public class JFreeChartDemo extends JFrame implements ActionListener , WindowListener { 116 117 118 public static final String EXIT_COMMAND = "EXIT"; 119 120 121 public static final String ABOUT_COMMAND = "ABOUT"; 122 123 124 private static final JFreeChartDemoBase DEMO = new JFreeChartDemoBase(); 125 126 127 private static final String [][] CHART_COMMANDS = JFreeChartDemoBase.CHART_COMMANDS; 128 129 130 private ResourceBundle resources; 131 132 133 private ChartFrame[] frame = new ChartFrame[CHART_COMMANDS.length]; 134 135 136 private JPanel [] panels = null; 137 138 139 public static final Dimension PREFERRED_SIZE = new Dimension (780, 400); 140 141 142 private AboutFrame aboutFrame; 143 144 147 public JFreeChartDemo() { 148 super(JFreeChart.INFO.getName() + " " + JFreeChart.INFO.getVersion() + " Demo"); 149 addWindowListener(new WindowAdapter () { 150 public void windowClosing(WindowEvent e) { 151 dispose(); 152 System.exit(0); 153 } 154 }); 155 resources = DEMO.getResources(); 156 JMenuBar menuBar = createMenuBar(resources); 158 setJMenuBar(menuBar); 159 160 JPanel content = new JPanel (new BorderLayout ()); 161 content.add(createTabbedPane(resources)); 162 setContentPane(content); 163 164 } 165 166 171 public Dimension getPreferredSize() { 172 return PREFERRED_SIZE; 173 } 174 175 180 public void actionPerformed(ActionEvent event) { 181 182 String command = event.getActionCommand(); 183 if (command.equals(EXIT_COMMAND)) { 184 attemptExit(); 185 } 186 else if (command.equals(ABOUT_COMMAND)) { 187 about(); 188 } 189 else { 190 int chartnum = -1; 192 int i = CHART_COMMANDS.length; 193 while (i > 0) { 194 --i; 195 if (command.equals(CHART_COMMANDS[i][0])) { 196 chartnum = i; 197 i = 0; 198 } 199 } 200 201 if ((chartnum >= 0) && (chartnum < frame.length)) { 203 if (frame[chartnum] == null) { 205 DEMO.getChart(chartnum); 207 208 String str = resources.getString(CHART_COMMANDS[chartnum][2] + ".title"); 210 frame[chartnum] = new ChartFrame(str, DEMO.getChart(chartnum)); 211 frame[chartnum].getChartPanel().setPreferredSize( 212 new java.awt.Dimension (500, 270)); 213 frame[chartnum].pack(); 214 RefineryUtilities.positionFrameRandomly(frame[chartnum]); 215 216 try { 218 str = resources.getString(CHART_COMMANDS[chartnum][2] + ".zoom"); 219 if ((str != null) && (str.toLowerCase().equals("true"))) { 220 ChartPanel panel = frame[chartnum].getChartPanel(); 221 panel.setMouseZoomable(true); 222 panel.setHorizontalAxisTrace(true); 223 panel.setVerticalAxisTrace(true); 224 } 225 } 226 catch (Exception ex) { 227 if (ex.getMessage().indexOf("MissingResourceException") == 0) { 230 ex.printStackTrace(); 231 } 232 } 233 234 frame[chartnum].show(); 235 236 } 237 else { 238 frame[chartnum].show(); 239 frame[chartnum].requestFocus(); 240 } 241 } 242 } 243 } 244 245 248 private void attemptExit() { 249 250 String title = resources.getString("dialog.exit.title"); 251 String message = resources.getString("dialog.exit.message"); 252 int result = JOptionPane.showConfirmDialog(this, message, title, 253 JOptionPane.YES_NO_OPTION, 254 JOptionPane.QUESTION_MESSAGE); 255 if (result == JOptionPane.YES_OPTION) { 256 dispose(); 257 System.exit(0); 258 } 259 } 260 261 264 private void about() { 265 266 String title = this.resources.getString("about.title"); 267 if (aboutFrame == null) { 269 aboutFrame = new AboutFrame(title, JFreeChart.INFO); 270 aboutFrame.pack(); 271 RefineryUtilities.centerFrameOnScreen(aboutFrame); 272 } 273 aboutFrame.show(); 274 aboutFrame.requestFocus(); 275 276 } 277 278 283 public static void main(String [] args) { 284 285 JFreeChartDemo f = new JFreeChartDemo(); 286 f.pack(); 287 RefineryUtilities.centerFrameOnScreen(f); 288 f.setVisible(true); 289 } 290 291 296 public void windowActivated(WindowEvent e) { 297 } 298 299 304 public void windowClosed(WindowEvent e) { 305 306 if (e.getWindow() == this.aboutFrame) { 307 aboutFrame = null; 308 } 309 310 } 311 312 317 public void windowClosing(WindowEvent e) { 318 } 319 320 325 public void windowDeactivated(WindowEvent e) { 326 } 327 328 333 public void windowDeiconified(WindowEvent e) { 334 } 335 336 341 public void windowIconified(WindowEvent e) { 342 } 343 344 349 public void windowOpened(WindowEvent e) { 350 } 351 352 359 private JMenuBar createMenuBar(ResourceBundle resources) { 360 361 JMenuBar menuBar = new JMenuBar (); 363 364 String label; 365 Character mnemonic; 366 367 label = resources.getString("menu.file"); 369 mnemonic = (Character ) resources.getObject("menu.file.mnemonic"); 370 JMenu fileMenu = new JMenu (label, true); 371 fileMenu.setMnemonic(mnemonic.charValue()); 372 373 label = resources.getString("menu.file.exit"); 374 mnemonic = (Character ) resources.getObject("menu.file.exit.mnemonic"); 375 JMenuItem exitItem = new JMenuItem (label, mnemonic.charValue()); 376 exitItem.setActionCommand(EXIT_COMMAND); 377 exitItem.addActionListener(this); 378 fileMenu.add(exitItem); 379 380 label = resources.getString("menu.help"); 382 mnemonic = (Character ) resources.getObject("menu.help.mnemonic"); 383 JMenu helpMenu = new JMenu (label); 384 helpMenu.setMnemonic(mnemonic.charValue()); 385 386 label = resources.getString("menu.help.about"); 387 mnemonic = (Character ) resources.getObject("menu.help.about.mnemonic"); 388 JMenuItem aboutItem = new JMenuItem (label, mnemonic.charValue()); 389 aboutItem.setActionCommand(ABOUT_COMMAND); 390 aboutItem.addActionListener(this); 391 helpMenu.add(aboutItem); 392 393 menuBar.add(fileMenu); 395 menuBar.add(helpMenu); 396 397 return menuBar; 398 399 } 400 401 408 private JTabbedPane createTabbedPane(ResourceBundle resources) { 409 410 Font font = new Font ("Dialog", Font.PLAIN, 12); 411 JTabbedPane tabs = new JTabbedPane (); 412 413 int tab = 1; 414 Vector titles = new Vector (0); 415 String [] tabTitles; 416 String title = null; 417 418 while (tab > 0) { 419 try { 420 title = resources.getString("tabs." + tab); 421 if (title != null) { 422 titles.add(title); 423 } 424 else { 425 tab = -1; 426 } 427 ++tab; 428 } 429 catch (Exception ex) { 430 tab = -1; 431 } 432 } 433 434 if (titles.size() == 0) { 435 titles.add("Default"); 436 } 437 438 tab = titles.size(); 439 panels = new JPanel [tab]; 440 tabTitles = new String [tab]; 441 442 --tab; 443 for (; tab >= 0; --tab) { 444 title = titles.get(tab).toString(); 445 tabTitles[tab] = title; 446 } 447 titles.removeAllElements(); 448 449 for (int i = 0; i < tabTitles.length; ++i) { 450 panels[i] = new JPanel (); 451 panels[i].setLayout(new LCBLayout(20)); 452 panels[i].setPreferredSize(new Dimension (360, 20)); 453 panels[i].setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 454 tabs.add(tabTitles[i], new JScrollPane (panels[i])); 455 } 456 457 String description; 458 String buttonText = resources.getString("charts.display"); 459 JButton b1; 460 461 String usage = null; 463 for (int i = 0; i <= CHART_COMMANDS.length - 1; ++i) { 464 try { 465 usage = resources.getString(CHART_COMMANDS[i][2] + ".usage"); 466 } 467 catch (Exception ex) { 468 usage = null; 469 } 470 471 if ((usage == null) || usage.equalsIgnoreCase("All") 472 || usage.equalsIgnoreCase("Swing")) { 473 474 title = resources.getString(CHART_COMMANDS[i][2] + ".title"); 475 description = resources.getString(CHART_COMMANDS[i][2] + ".description"); 476 try { 477 tab = Integer.parseInt(resources.getString(CHART_COMMANDS[i][2] + ".tab")); 478 --tab; 479 } 480 catch (Exception ex) { 481 System.err.println("Demo : Error retrieving tab identifier for chart " 482 + CHART_COMMANDS[i][2]); 483 System.err.println("Demo : Error = " + ex.getMessage()); 484 tab = 0; 485 } 486 if ((tab < 0) || (tab >= panels.length)) { 487 tab = 0; 488 } 489 490 System.out.println("Demo : adding " + CHART_COMMANDS[i][0] + " to panel " + tab); 491 panels[tab].add(RefineryUtilities.createJLabel(title, font)); 492 panels[tab].add(new DescriptionPanel(new JTextArea (description))); 493 b1 = RefineryUtilities.createJButton(buttonText, font); 494 b1.setActionCommand(CHART_COMMANDS[i][0]); 495 b1.addActionListener(this); 496 panels[tab].add(b1); 497 } 498 } 499 500 return tabs; 501 502 } 503 504 } 505 | Popular Tags |