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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXTaskPane.java,v 1.7 2005/10/15 09:33:39 l2fprod 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.BorderLayout;
  23. import java.awt.Component;
  24. import java.awt.Container;
  25. import java.awt.LayoutManager;
  26. import java.beans.PropertyChangeEvent;
  27. import java.beans.PropertyChangeListener;
  28. import javax.swing.Action;
  29. import javax.swing.Icon;
  30. import javax.swing.JComponent;
  31. import javax.swing.JPanel;
  32. import javax.swing.UIManager;
  33. import org.jdesktop.swingx.plaf.JXTaskPaneAddon;
  34. import org.jdesktop.swingx.plaf.LookAndFeelAddons;
  35. import org.jdesktop.swingx.plaf.TaskPaneUI;
  36. /**
  37.  * <code>JXTaskPane</code> is a container for tasks and other
  38.  * arbitrary components.
  39.  * 
  40.  * <p>
  41.  * Several <code>JXTaskPane</code>s are usually grouped together within a
  42.  * {@link org.jdesktop.swingx.JXTaskPaneContainer}. However it is not mandatory
  43.  * to use a JXTaskPaneContainer as the parent for JXTaskPane. The JXTaskPane can
  44.  * be added to any other container. See
  45.  * {@link org.jdesktop.swingx.JXTaskPaneContainer} to understand the benefits of
  46.  * using it as the parent container.
  47.  * 
  48.  * <p>
  49.  * <code>JXTaskPane</code> provides control to expand and
  50.  * collapse the content area in order to show or hide the task list. It can have an
  51.  * <code>icon</code>, a <code>title</code> and can be marked as
  52.  * <code>special</code>. Marking a <code>JXTaskPane</code> as
  53.  * <code>special</code> ({@link #setSpecial(boolean)} is only a hint for
  54.  * the pluggable UI which will usually paint it differently (by example by
  55.  * using another color for the border of the pane).
  56.  * 
  57.  * <p> 
  58.  * When the JXTaskPane is expanded or collapsed, it will be
  59.  * animated with a fade effect. The animated can be disabled on a per
  60.  * component basis through {@link #setAnimated(boolean)}.
  61.  * 
  62.  * To disable the animation for all newly created <code>JXTaskPane</code>,
  63.  * use the UIManager property:
  64.  * <code>UIManager.put("TaskPane.animate", Boolean.FALSE);</code>.
  65.  * 
  66.  * <p>
  67.  * Example:
  68.  * <pre>
  69.  * <code>
  70.  * JXFrame frame = new JXFrame();
  71.  * 
  72.  * // a container to put all JXTaskPane together
  73.  * JXTaskPaneContainer taskPaneContainer = new JXTaskPaneContainer();
  74.  * 
  75.  * // create a first taskPane with common actions
  76.  * JXTaskPane actionPane = new JXTaskPane();
  77.  * actionPane.setTitle("Files and Folders");
  78.  * actionPane.setSpecial(true);
  79.  * 
  80.  * // actions can be added, an hyperlink will be created
  81.  * Action renameSelectedFile = createRenameFileAction();
  82.  * actionPane.add(renameSelectedFile);
  83.  * actionPane.add(createDeleteFileAction());
  84.  * 
  85.  * // add this taskPane to the taskPaneContainer
  86.  * taskPaneContainer.add(actionPane);
  87.  * 
  88.  * // create another taskPane, it will show details of the selected file
  89.  * JXTaskPane details = new JXTaskPane();
  90.  * details.setTitle("Details");
  91.  *  
  92.  * // add standard components to the details taskPane
  93.  * JLabel searchLabel = new JLabel("Search:");
  94.  * JTextField searchField = new JTextField("");
  95.  * details.add(searchLabel);
  96.  * details.add(searchField);
  97.  * 
  98.  * taskPaneContainer.add(details);
  99.  * 
  100.  * // put the action list on the left 
  101.  * frame.add(taskPaneContainer, BorderLayout.EAST);
  102.  * 
  103.  * // and a file browser in the middle
  104.  * frame.add(fileBrowser, BorderLayout.CENTER);
  105.  * 
  106.  * frame.pack().
  107.  * frame.setVisible(true);
  108.  * </code>
  109.  * </pre>
  110.  * 
  111.  * @see org.jdesktop.swingx.JXTaskPaneContainer
  112.  * @see org.jdesktop.swingx.JXCollapsiblePane
  113.  * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
  114.  * 
  115.  * @javabean.attribute
  116.  *          name="isContainer"
  117.  *          value="Boolean.TRUE"
  118.  *          rtexpr="true"
  119.  *          
  120.  * @javabean.attribute
  121.  *          name="containerDelegate"
  122.  *          value="getContentPane"
  123.  *          
  124.  * @javabean.class
  125.  *          name="JXTaskPane"
  126.  *          shortDescription="JXTaskPane is a container for tasks and other arbitrary components."
  127.  *          stopClass="java.awt.Component"
  128.  * 
  129.  * @javabean.icons
  130.  *          mono16="JXTaskPane16-mono.gif"
  131.  *          color16="JXTaskPane16.gif"
  132.  *          mono32="JXTaskPane32-mono.gif"
  133.  *          color32="JXTaskPane32.gif"
  134.  */
  135. public class JXTaskPane extends JPanel implements
  136.   JXCollapsiblePane.JCollapsiblePaneContainer {
  137.   /**
  138.    * JXTaskPane pluggable UI key <i>swingx/TaskPaneUI</i> 
  139.    */
  140.   public final static String uiClassID = "swingx/TaskPaneUI";
  141.   
  142.   // ensure at least the default ui is registered
  143.   static {
  144.     LookAndFeelAddons.contribute(new JXTaskPaneAddon());
  145.   }
  146.   /**
  147.    * Used when generating PropertyChangeEvents for the "expanded" property
  148.    */
  149.   public static final String EXPANDED_CHANGED_KEY = "expanded";
  150.   /**
  151.    * Used when generating PropertyChangeEvents for the "scrollOnExpand" property
  152.    */
  153.   public static final String SCROLL_ON_EXPAND_CHANGED_KEY = "scrollOnExpand";
  154.   /**
  155.    * Used when generating PropertyChangeEvents for the "title" property
  156.    */
  157.   public static final String TITLE_CHANGED_KEY = "title";
  158.   /**
  159.    * Used when generating PropertyChangeEvents for the "icon" property
  160.    */
  161.   public static final String ICON_CHANGED_KEY = "icon";
  162.   /**
  163.    * Used when generating PropertyChangeEvents for the "special" property
  164.    */
  165.   public static final String SPECIAL_CHANGED_KEY = "special";
  166.   /**
  167.    * Used when generating PropertyChangeEvents for the "animated" property
  168.    */
  169.   public static final String ANIMATED_CHANGED_KEY = "animated";
  170.   private String title;
  171.   private Icon icon;
  172.   private boolean special;
  173.   private boolean expanded = true;
  174.   private boolean scrollOnExpand;
  175.   private JXCollapsiblePane collapsePane;
  176.   
  177.   /**
  178.    * Creates a new empty <code>JXTaskPane</code>.
  179.    */
  180.   public JXTaskPane() {
  181.     collapsePane = new JXCollapsiblePane();
  182.     super.setLayout(new BorderLayout(0, 0));
  183.     super.addImpl(collapsePane, BorderLayout.CENTER, -1);
  184.     
  185.     updateUI();
  186.     setFocusable(true);
  187.     setOpaque(false);
  188.     // disable animation if specified in UIManager
  189.     setAnimated(!Boolean.FALSE.equals(UIManager.get("TaskPane.animate")));
  190.     
  191.     // listen for animation events and forward them to registered listeners
  192.     collapsePane.addPropertyChangeListener(
  193.       JXCollapsiblePane.ANIMATION_STATE_KEY, new PropertyChangeListener() {
  194.         public void propertyChange(PropertyChangeEvent evt) {
  195.           JXTaskPane.this.firePropertyChange(evt.getPropertyName(), evt
  196.             .getOldValue(), evt.getNewValue());
  197.         }
  198.       });
  199.   }
  200.   /**
  201.    * Returns the contentPane object for this JXTaskPane.
  202.    * @return the contentPane property
  203.    */
  204.   public Container getContentPane() {
  205.     return collapsePane.getContentPane();
  206.   }
  207.   
  208.   /**
  209.    * Notification from the <code>UIManager</code> that the L&F has changed.
  210.    * Replaces the current UI object with the latest version from the <code>UIManager</code>.
  211.    * 
  212.    * @see javax.swing.JComponent#updateUI
  213.    */
  214.   public void updateUI() {
  215.     // collapsePane is null when updateUI() is called by the "super()"
  216.     // constructor
  217.     if (collapsePane == null) {
  218.       return;
  219.     }
  220.     setUI((TaskPaneUI)LookAndFeelAddons.getUI(this, TaskPaneUI.class));
  221.   }
  222.   
  223.   /**
  224.    * Sets the L&F object that renders this component.
  225.    * 
  226.    * @param ui the <code>TaskPaneUI</code> L&F object
  227.    * @see javax.swing.UIDefaults#getUI
  228.    * 
  229.    * @beaninfo bound: true hidden: true description: The UI object that
  230.    * implements the taskpane group's LookAndFeel.
  231.    */
  232.   public void setUI(TaskPaneUI ui) {
  233.     super.setUI(ui);
  234.   }
  235.   /**
  236.    * Returns the name of the L&F class that renders this component.
  237.    * 
  238.    * @return the string {@link #uiClassID}
  239.    * @see javax.swing.JComponent#getUIClassID
  240.    * @see javax.swing.UIDefaults#getUI
  241.    */
  242.   public String getUIClassID() {
  243.     return uiClassID;
  244.   }
  245.   /**
  246.    * Returns the title currently displayed in the border of this pane.
  247.    * 
  248.    * @return the title currently displayed in the border of this pane
  249.    */
  250.   public String getTitle() {
  251.     return title;
  252.   }
  253.   /**
  254.    * Sets the title to be displayed in the border of this pane.
  255.    * 
  256.    * @param title the title to be displayed in the border of this pane
  257.    * @javabean.property
  258.    *          bound="true"
  259.    *          preferred="true"
  260.    */
  261.   public void setTitle(String title) {
  262.     String old = this.title;
  263.     this.title = title;
  264.     firePropertyChange(TITLE_CHANGED_KEY, old, title);
  265.   }
  266.   /**
  267.    * Returns the icon currently displayed in the border of this pane.
  268.    * 
  269.    * @return the icon currently displayed in the border of this pane
  270.    */
  271.   public Icon getIcon() {
  272.     return icon;
  273.   }
  274.   /**
  275.    * Sets the icon to be displayed in the border of this pane. Some pluggable
  276.    * UIs may impose size constraints for the icon. A size of 16x16 pixels is
  277.    * the recommended icon size.
  278.    * 
  279.    * @param icon the icon to be displayed in the border of this pane
  280.    * @javabean.property
  281.    *          bound="true"
  282.    *          preferred="true"
  283.    */
  284.   public void setIcon(Icon icon) {
  285.     Icon old = this.icon;
  286.     this.icon = icon;
  287.     firePropertyChange(ICON_CHANGED_KEY, old, icon);
  288.   }
  289.   /**
  290.    * Returns true if this pane is "special".
  291.    * 
  292.    * @return true if this pane is "special"
  293.    * @see #setSpecial(boolean)
  294.    */
  295.   public boolean isSpecial() {
  296.     return special;
  297.   }
  298.   /**
  299.    * Sets this pane to be "special" or not. Marking a <code>JXTaskPane</code>
  300.    * as <code>special</code> is only a hint for the pluggable UI which will
  301.    * usually paint it differently (by example by using another color for the
  302.    * border of the pane).
  303.    * 
  304.    * <p>
  305.    * Usually the first JXTaskPane in a JXTaskPaneContainer is marked as special
  306.    * because it contains the default set of actions which can be executed given
  307.    * the current context.
  308.    * 
  309.    * @param special
  310.    *          true if this pane is "special", false otherwise
  311.    * @javabean.property bound="true" preferred="true"
  312.    */
  313.   public void setSpecial(boolean special) {
  314.     if (this.special != special) {
  315.       this.special = special;
  316.       firePropertyChange(SPECIAL_CHANGED_KEY, !special, special);
  317.     }
  318.   }
  319.   /**
  320.    * Should this group be scrolled to be visible on expand.
  321.    * 
  322.    * @param scrollOnExpand true to scroll this group to be
  323.    * visible if this group is expanded.
  324.    * 
  325.    * @see #setExpanded(boolean)
  326.    * 
  327.    * @javabean.property
  328.    *          bound="true"
  329.    *          preferred="true"
  330.    */
  331.   public void setScrollOnExpand(boolean scrollOnExpand) {
  332.     if (this.scrollOnExpand != scrollOnExpand) {
  333.       this.scrollOnExpand = scrollOnExpand;
  334.       firePropertyChange(SCROLL_ON_EXPAND_CHANGED_KEY,
  335.         !scrollOnExpand, scrollOnExpand);
  336.     }
  337.   }
  338.   
  339.   /**
  340.    * Should this group scroll to be visible after
  341.    * this group was expanded.
  342.    * 
  343.    * @return true if we should scroll false if nothing
  344.    * should be done.
  345.    */
  346.   public boolean isScrollOnExpand() {
  347.     return scrollOnExpand;
  348.   }
  349.   
  350.   /**
  351.    * Expands or collapses this group.
  352.    * 
  353.    * @param expanded true to expand the group, false to collapse it
  354.    * @javabean.property
  355.    *          bound="true"
  356.    *          preferred="true"
  357.    */
  358.   public void setExpanded(boolean expanded) {
  359.     if (this.expanded != expanded) {
  360.       this.expanded = expanded;
  361.       collapsePane.setCollapsed(!expanded);
  362.       firePropertyChange(EXPANDED_CHANGED_KEY, !expanded, expanded);
  363.     }
  364.   }
  365.   /**
  366.    * Returns true if this taskpane is expanded, false if it is collapsed.
  367.    * 
  368.    * @return true if this taskpane is expanded, false if it is collapsed.
  369.    */
  370.   public boolean isExpanded() {
  371.     return expanded;
  372.   }
  373.   /**
  374.    * Enables or disables animation during expand/collapse transition.
  375.    * 
  376.    * @param animated
  377.    * @javabean.property
  378.    *          bound="true"
  379.    *          preferred="true"
  380.    */
  381.   public void setAnimated(boolean animated) {
  382.     if (isAnimated() != animated) {
  383.       collapsePane.setAnimated(animated);
  384.       firePropertyChange(ANIMATED_CHANGED_KEY, !isAnimated(), isAnimated());
  385.     }
  386.   }
  387.   
  388.   /**
  389.    * Returns true if this taskpane is animated during expand/collapse
  390.    * transition.
  391.    * 
  392.    * @return true if this taskpane is animated during expand/collapse
  393.    *         transition.
  394.    */
  395.   public boolean isAnimated() {
  396.     return collapsePane.isAnimated();
  397.   }
  398.   
  399.   /**
  400.    * Adds an action to this <code>JXTaskPane</code>. Returns a
  401.    * component built from the action. The returned component has been
  402.    * added to the <code>JXTaskPane</code>.
  403.    * 
  404.    * @param action
  405.    * @return a component built from the action
  406.    */
  407.   public Component add(Action action) {
  408.     Component c = ((TaskPaneUI)ui).createAction(action);
  409.     add(c);
  410.     return c;
  411.   }
  412.   /**
  413.    * @see JXCollapsiblePane.JCollapsiblePaneContainer
  414.    */
  415.   public Container getValidatingContainer() {
  416.     return getParent();
  417.   }
  418.   
  419.   /**
  420.    * Overriden to redirect call to the content pane.
  421.    */
  422.   protected void addImpl(Component comp, Object constraints, int index) {
  423.     getContentPane().add(comp, constraints, index);
  424.   }
  425.   /**
  426.    * Overriden to redirect call to the content pane.
  427.    */
  428.   public void setLayout(LayoutManager mgr) {
  429.     if (collapsePane != null) {
  430.       getContentPane().setLayout(mgr);
  431.     }
  432.   }
  433.   
  434.   /**
  435.    * Overriden to redirect call to the content pane
  436.    */
  437.   public void remove(Component comp) {
  438.     getContentPane().remove(comp);
  439.   }
  440.   /**
  441.    * Overriden to redirect call to the content pane.
  442.    */
  443.   public void remove(int index) {
  444.     getContentPane().remove(index);
  445.   }
  446.   
  447.   /**
  448.    * Overriden to redirect call to the content pane.
  449.    */
  450.   public void removeAll() {
  451.     getContentPane().removeAll();
  452.   }
  453.   
  454.   /**
  455.    * @see JComponent#paramString()
  456.    */
  457.   protected String paramString() {
  458.     return super.paramString()
  459.       + ",title="
  460.       + getTitle()
  461.       + ",icon="
  462.       + getIcon()
  463.       + ",expanded="
  464.       + String.valueOf(isExpanded())
  465.       + ",special="
  466.       + String.valueOf(isSpecial())
  467.       + ",scrollOnExpand=" 
  468.       + String.valueOf(isScrollOnExpand())
  469.       + ",ui=" + getUI();
  470.   }
  471. }