JXDialog.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXDialog.java,v 1.4 2005/10/10 18:01:53 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.awt.Dimension;
  23. import java.awt.Frame;
  24. import java.awt.GraphicsConfiguration;
  25. import java.awt.GraphicsEnvironment;
  26. import java.awt.Rectangle;
  27. import java.awt.event.KeyEvent;
  28. import javax.swing.Action;
  29. import javax.swing.BorderFactory;
  30. import javax.swing.Box;
  31. import javax.swing.BoxLayout;
  32. import javax.swing.InputMap;
  33. import javax.swing.JButton;
  34. import javax.swing.JComponent;
  35. import javax.swing.JDialog;
  36. import javax.swing.JPanel;
  37. import javax.swing.KeyStroke;
  38. import javax.swing.UIManager;
  39. import javax.swing.plaf.basic.BasicOptionPaneUI;
  40. import org.jdesktop.swingx.action.BoundAction;
  41. import org.jdesktop.swingx.plaf.LookAndFeelAddons;
  42. /**
  43.  * First cut for enhanced Dialog.
  44.  * 
  45.  * <ul>
  46.  * <li> registers stand-in actions for close/execute with the dialog's RootPane
  47.  * <li> registers keyStrokes for esc/enter to trigger the close/execute actions
  48.  * <li> takes care of building the button panel using the close/execute actions.
  49.  * <li> accepts a content and configures itself from content's properties - 
  50.  *  replaces the execute action from the appropriate action in content's action map (if any)
  51.  *  and set's its title from the content's name. 
  52.  * </ul> 
  53.  * 
  54.  * 
  55.  * PENDING: add support for vetoing the close.
  56.  * PENDING: add complete set of constructors
  57.  * PENDING: add windowListener to delegate to close action
  58.  * 
  59.  * @author Jeanette Winzenburg
  60.  */
  61. public class JXDialog extends JDialog {
  62.     static {
  63.         // Hack to enforce loading of SwingX framework ResourceBundle
  64.         LookAndFeelAddons.getAddon();
  65.     }
  66.     
  67.     public static final String EXECUTE_ACTION_COMMAND = "execute";
  68.     public static final String CLOSE_ACTION_COMMAND = "close";
  69.     public static final String UIPREFIX = "XDialog.";
  70.     JComponent content;
  71.     
  72.     public JXDialog(Frame frame, JComponent content) {
  73.         super(frame);
  74.         setContent(content);
  75.     }
  76.     
  77.     private void setContent(JComponent content) {
  78.         if (this.content != null) {
  79.             throw new IllegalStateException("content must not be set more than once");
  80.         }
  81.         initActions();
  82.         Action contentCloseAction = content.getActionMap().get(CLOSE_ACTION_COMMAND);
  83.         if (contentCloseAction != null) {
  84.             putAction(CLOSE_ACTION_COMMAND, contentCloseAction);
  85.         }
  86.         Action contentExecuteAction = content.getActionMap().get(EXECUTE_ACTION_COMMAND);
  87.         if (contentExecuteAction != null) {
  88.             putAction(EXECUTE_ACTION_COMMAND, contentExecuteAction);
  89.         }
  90.         this.content = content;
  91.         build();
  92.         setTitle(content.getName());
  93.     }
  94.     /**
  95.      * pre: content != null.
  96.      *
  97.      */
  98.     private void build() {
  99.         JComponent contentBox = new Box(BoxLayout.PAGE_AXIS); 
  100.         contentBox.add(content);
  101.         JComponent buttonPanel = createButtonPanel();
  102.         contentBox.add(buttonPanel);
  103.         contentBox.setBorder(BorderFactory.createEmptyBorder(14, 14, 14, 14));
  104. //        content.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  105.         
  106. //        fieldPanel.setAlignmentX();
  107. //      buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
  108.         add(contentBox);
  109.         
  110.     }
  111. //    /**
  112. //     * 
  113. //     */
  114. //    private void locate() {
  115. //        GraphicsConfiguration gc =
  116. //            GraphicsEnvironment.getLocalGraphicsEnvironment().
  117. //            getDefaultScreenDevice().getDefaultConfiguration();
  118. //        Rectangle bounds = gc.getBounds();
  119. //        int x = bounds.x+bounds.width/3;
  120. //        int y = bounds.y+bounds.height/3;
  121. //
  122. //        setLocation(x, y);
  123. //    }
  124.     public void setVisible(boolean visible) {
  125.         if (content == null) throw 
  126.             new IllegalStateException("content must be built before showing the dialog");
  127.         super.setVisible(visible);
  128.     }
  129.     public void doClose() {
  130.         dispose();
  131.     }
  132.     
  133.     private void initActions() {
  134.         // PENDING: factor a common dialog containing the following
  135.         Action defaultAction = createCloseAction();
  136.         putAction(CLOSE_ACTION_COMMAND, defaultAction);
  137.         putAction(EXECUTE_ACTION_COMMAND, defaultAction);
  138.     }
  139.     private Action createCloseAction() {
  140.         String actionName = getUIString(CLOSE_ACTION_COMMAND);
  141.         BoundAction action = new BoundAction(actionName,
  142.                 CLOSE_ACTION_COMMAND);
  143.         action.registerCallback(this, "doClose");
  144.         return action;
  145.     }
  146.     /**
  147.      * create the dialog button controls.
  148.      * 
  149.      * 
  150.      * @return
  151.      */
  152.     protected JComponent createButtonPanel() {
  153.         // PENDING: this is a hack until we have a dedicated ButtonPanel!
  154.         JPanel panel = new JPanel(new BasicOptionPaneUI.ButtonAreaLayout(true, 6))
  155.         {
  156.             public Dimension getMaximumSize() {
  157.                 return getPreferredSize();
  158.             }
  159.         };
  160.         panel.setBorder(BorderFactory.createEmptyBorder(9, 0, 0, 0));
  161.         Action findAction = getAction(EXECUTE_ACTION_COMMAND);
  162.         Action closeAction = getAction(CLOSE_ACTION_COMMAND);
  163.         JButton findButton;
  164.         panel.add(findButton = new JButton(findAction));
  165.         panel.add(new JButton(closeAction));
  166.         KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
  167.         KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
  168.         InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  169.         inputMap.put(enterKey, EXECUTE_ACTION_COMMAND);
  170.         inputMap.put(escapeKey, CLOSE_ACTION_COMMAND);
  171.         getRootPane().setDefaultButton(findButton);
  172.         return panel;
  173.     }
  174.     /**
  175.      * convenience wrapper to access rootPane's actionMap.
  176.      * @param key
  177.      * @param action
  178.      */
  179.     private void putAction(Object key, Action action) {
  180.         getRootPane().getActionMap().put(key, action);
  181.     }
  182.     
  183.     /**
  184.      * convenience wrapper to access rootPane's actionMap.
  185.      * 
  186.      * @param key
  187.      * @return
  188.      */
  189.     private Action getAction(Object key) {
  190.         return getRootPane().getActionMap().get(key);
  191.     }
  192.     /**
  193.      * tries to find a String value from the UIManager, prefixing the
  194.      * given key with the UIPREFIX. 
  195.      * 
  196.      * TODO: move to utilities?
  197.      * 
  198.      * @param key 
  199.      * @return the String as returned by the UIManager or key if the returned
  200.      *   value was null.
  201.      */
  202.     private String getUIString(String key) {
  203.         String text = UIManager.getString(UIPREFIX + key);
  204.         return text != null ? text : key;
  205.     }
  206. }