/** * @(#)ActionDemo1.java 1.4 03/02/04 * * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * */ package demo; import java.awt.Component; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; import java.net.URL; import java.net.MalformedURLException; import javax.swing.*; import com.sun.jaf.ui.ActionManager; import com.sun.jaf.ui.UIFactory; /** * This class demonstrates how to create an application that uses the * XML Actions architecture. */ public class ActionDemo1 extends JFrame { /** * @param url The url of a file to load. */ public ActionDemo1(URL url) { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Load the core actions into the action manager. URL actionURL = ActionDemo1.class.getResource("resources/actions-demo.xml"); ActionManager manager = ActionManager.getInstance(); try { manager.loadActions(actionURL); } catch (IOException ioe) { System.out.println("ERROR parsing: " + ioe); } // Construct a user interface based on a action-layout document UIFactory factory = UIFactory.getInstance(); JMenuBar menubar = factory.createMenuBar("main-menu"); if (menubar != null) { setJMenuBar(menubar); } JToolBar toolbar = factory.createToolBar("main-toolbar"); if (toolbar != null) { getContentPane().add(toolbar, BorderLayout.NORTH); } JEditorPane editor = new JEditorPane(); JScrollPane scrollPane = new JScrollPane(editor); scrollPane.setPreferredSize(new Dimension(400,300)); getContentPane().add(scrollPane, BorderLayout.CENTER); // Show a popup. It's using the toolbar action-list // to demonstrate that the action-lists are reusable. final JPopupMenu popup = factory.createPopup("main-toolbar"); editor.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) { if (popup != null) { popup.show((Component)e.getSource(), e.getX(), e.getY()); } } } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { if (popup != null) { popup.show((Component)e.getSource(), e.getX(), e.getY()); } } } }); String documentName; if (url != null) { documentName = url.getFile(); try { editor.setPage(url); } catch (IOException ex) { // Some sort of exception with the URL, re-init System.err.println(ex.getMessage()); } } else { documentName = "[untitled]"; editor.setDocument(editor.getEditorKit().createDefaultDocument()); } setTitle(documentName); pack(); } /** * Entry point of the demo. */ public static void main(String[] args) { URL url = null; if (args.length > 0) { try { url = ActionDemo1.class.getResource(args[0]); if (url == null) { url = new File(args[0]).toURL(); } } catch (MalformedURLException mfe) { System.out.println("ERROR: " + mfe.getMessage()); System.out.println("Using default action-set"); } } ActionDemo1 demo = new ActionDemo1(url); demo.setVisible(true); } }