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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: BasicTipOfTheDayUI.java,v 1.4 2005/10/10 18:02: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.plaf.basic;
  22. import java.awt.BorderLayout;
  23. import java.awt.Component;
  24. import java.awt.Dialog;
  25. import java.awt.Dimension;
  26. import java.awt.Font;
  27. import java.awt.Frame;
  28. import java.awt.GridLayout;
  29. import java.awt.Window;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.awt.event.KeyEvent;
  33. import java.awt.event.WindowAdapter;
  34. import java.awt.event.WindowEvent;
  35. import java.beans.PropertyChangeEvent;
  36. import java.beans.PropertyChangeListener;
  37. import javax.swing.AbstractAction;
  38. import javax.swing.ActionMap;
  39. import javax.swing.BorderFactory;
  40. import javax.swing.Icon;
  41. import javax.swing.JButton;
  42. import javax.swing.JCheckBox;
  43. import javax.swing.JComponent;
  44. import javax.swing.JDialog;
  45. import javax.swing.JEditorPane;
  46. import javax.swing.JLabel;
  47. import javax.swing.JOptionPane;
  48. import javax.swing.JPanel;
  49. import javax.swing.JScrollPane;
  50. import javax.swing.JTextArea;
  51. import javax.swing.KeyStroke;
  52. import javax.swing.LookAndFeel;
  53. import javax.swing.SwingUtilities;
  54. import javax.swing.UIManager;
  55. import javax.swing.plaf.ActionMapUIResource;
  56. import javax.swing.plaf.ComponentUI;
  57. import org.jdesktop.swingx.JXTipOfTheDay;
  58. import org.jdesktop.swingx.JXTipOfTheDay.ShowOnStartupChoice;
  59. import org.jdesktop.swingx.plaf.TipOfTheDayUI;
  60. import org.jdesktop.swingx.tips.TipOfTheDayModel.Tip;
  61. import org.jdesktop.swingx.util.UIManagerUtils;
  62. /**
  63.  * Base implementation of the <code>JXTipOfTheDay</code> UI.
  64.  * 
  65.  * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
  66.  */
  67. public class BasicTipOfTheDayUI extends TipOfTheDayUI {
  68.   public static ComponentUI createUI(JComponent c) {
  69.     return new BasicTipOfTheDayUI((JXTipOfTheDay)c);
  70.   }
  71.   protected JXTipOfTheDay tipPane;
  72.   protected JPanel tipArea;
  73.   protected Component currentTipComponent;
  74.   protected Font tipFont;
  75.   protected PropertyChangeListener changeListener;
  76.   public BasicTipOfTheDayUI(JXTipOfTheDay tipPane) {
  77.     this.tipPane = tipPane;
  78.   }
  79.   @Override
  80.   public JDialog createDialog(Component parentComponent,
  81.     final ShowOnStartupChoice choice) {
  82.     return createDialog(parentComponent, choice, true);
  83.   }
  84.   
  85.   protected JDialog createDialog(Component parentComponent,
  86.     final ShowOnStartupChoice choice,
  87.     boolean showPreviousButton) {    
  88.     String title = UIManager.getString("TipOfTheDay.dialogTitle");
  89.     final JDialog dialog;
  90.     Window window;
  91.     if (parentComponent == null) {
  92.       window = JOptionPane.getRootFrame();
  93.     } else {
  94.       window = (parentComponent instanceof Window)?(Window)parentComponent
  95.         :SwingUtilities.getWindowAncestor(parentComponent);
  96.     }
  97.     if (window instanceof Frame) {
  98.       dialog = new JDialog((Frame)window, title, true);
  99.     } else {
  100.       dialog = new JDialog((Dialog)window, title, true);
  101.     }
  102.     dialog.getContentPane().setLayout(new BorderLayout(10, 10));
  103.     dialog.getContentPane().add(tipPane, BorderLayout.CENTER);
  104.     ((JComponent)dialog.getContentPane()).setBorder(BorderFactory
  105.       .createEmptyBorder(10, 10, 10, 10));
  106.     final JCheckBox showOnStartupBox;
  107.     // tip controls
  108.     JPanel controls = new JPanel(new BorderLayout());
  109.     dialog.add("South", controls);
  110.     if (choice != null) {
  111.       showOnStartupBox = new JCheckBox(UIManager
  112.         .getString("TipOfTheDay.showOnStartupText"), choice
  113.         .isShowingOnStartup());
  114.       controls.add(showOnStartupBox, BorderLayout.CENTER);
  115.     } else {
  116.       showOnStartupBox = null;
  117.     }
  118.     JPanel buttons =
  119.       new JPanel(new GridLayout(1, showPreviousButton?3:2, 9, 0));
  120.     controls.add(buttons, BorderLayout.LINE_END);
  121.     
  122.     if (showPreviousButton) {
  123.       JButton previousTipButton = new JButton(UIManager
  124.         .getString("TipOfTheDay.previousTipText"));
  125.       buttons.add(previousTipButton);
  126.       previousTipButton.addActionListener(getActionMap().get("previousTip"));
  127.     }
  128.     
  129.     JButton nextTipButton = new JButton(UIManager
  130.       .getString("TipOfTheDay.nextTipText"));
  131.     buttons.add(nextTipButton);
  132.     nextTipButton.addActionListener(getActionMap().get("nextTip"));
  133.     
  134.     JButton closeButton = new JButton(UIManager
  135.       .getString("TipOfTheDay.closeText"));
  136.     buttons.add(closeButton);
  137.     
  138.     final ActionListener saveChoice = new ActionListener() {
  139.       public void actionPerformed(ActionEvent e) {
  140.         if (choice != null) {
  141.           choice.setShowingOnStartup(showOnStartupBox.isSelected());
  142.         }
  143.         dialog.setVisible(false);
  144.       }
  145.     };
  146.     closeButton.addActionListener(new ActionListener() {
  147.       public void actionPerformed(ActionEvent e) {        
  148.         dialog.setVisible(false);
  149.         saveChoice.actionPerformed(null);
  150.       }
  151.     });
  152.     dialog.getRootPane().setDefaultButton(closeButton);
  153.     
  154.     dialog.addWindowListener(new WindowAdapter() {
  155.       public void windowClosing(WindowEvent e) {
  156.         saveChoice.actionPerformed(null);
  157.       }
  158.     });
  159.     
  160.     ((JComponent)dialog.getContentPane()).registerKeyboardAction(saveChoice,
  161.       KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
  162.       JComponent.WHEN_IN_FOCUSED_WINDOW);
  163.     
  164.     dialog.pack();
  165.     dialog.setLocationRelativeTo(parentComponent);
  166.     return dialog;
  167.   }
  168.   @Override
  169.   public void installUI(JComponent c) {
  170.     super.installUI(c);
  171.     installDefaults();
  172.     installKeyboardActions();
  173.     installComponents();
  174.     installListeners();
  175.     showCurrentTip();
  176.   }
  177.   protected void installKeyboardActions() {
  178.     ActionMap map = getActionMap();
  179.     if (map != null) {
  180.       SwingUtilities.replaceUIActionMap(tipPane, map);
  181.     }
  182.   }
  183.   ActionMap getActionMap() {
  184.     ActionMap map = new ActionMapUIResource();
  185.     map.put("previousTip", new PreviousTipAction());
  186.     map.put("nextTip", new NextTipAction());
  187.     return map;
  188.   }
  189.   protected void installListeners() {
  190.     changeListener = createChangeListener();
  191.     tipPane.addPropertyChangeListener(changeListener);
  192.   }
  193.   protected PropertyChangeListener createChangeListener() {
  194.     return new ChangeListener();
  195.   }
  196.   protected void installDefaults() {
  197.     LookAndFeel.installColorsAndFont(tipPane, "TipOfTheDay.background",
  198.       "TipOfTheDay.foreground", "TipOfTheDay.font");
  199.     LookAndFeel.installBorder(tipPane, "TipOfTheDay.border");
  200.     tipFont = UIManager.getFont("TipOfTheDay.tipFont");
  201.     tipPane.setOpaque(true);
  202.   }
  203.   protected void installComponents() {
  204.     tipPane.setLayout(new BorderLayout());
  205.     // tip icon
  206.     JLabel tipIcon = new JLabel(UIManager
  207.       .getString("TipOfTheDay.didYouKnowText"));
  208.     tipIcon.setIcon(UIManager.getIcon("TipOfTheDay.icon"));
  209.     tipIcon.setBorder(BorderFactory.createEmptyBorder(22, 15, 22, 15));
  210.     tipPane.add("North", tipIcon);
  211.     // tip area
  212.     tipArea = new JPanel(new BorderLayout(2, 2));
  213.     tipArea.setOpaque(false);
  214.     tipArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  215.     tipPane.add("Center", tipArea);
  216.   }
  217.   @Override
  218.   public Dimension getPreferredSize(JComponent c) {
  219.     return new Dimension(420, 175);
  220.   }
  221.   protected void showCurrentTip() {
  222.     if (currentTipComponent != null) {
  223.       tipArea.remove(currentTipComponent);
  224.     }
  225.     int currentTip = tipPane.getCurrentTip();
  226.     if (currentTip == -1) {
  227.       JLabel label = new JLabel();
  228.       label.setOpaque(true);
  229.       label.setBackground(UIManager.getColor("TextArea.background"));
  230.       currentTipComponent = label;
  231.       tipArea.add("Center", currentTipComponent);
  232.       return;
  233.     }
  234.     // tip does not fall in current tip range
  235.     if (tipPane.getModel() == null || tipPane.getModel().getTipCount() == 0
  236.       || (currentTip < 0 && currentTip >= tipPane.getModel().getTipCount())) {
  237.       currentTipComponent = new JLabel();
  238.     } else {    
  239.       Tip tip = tipPane.getModel().getTipAt(currentTip);
  240.       Object tipObject = tip.getTip();
  241.       if (tipObject instanceof Component) {
  242.         currentTipComponent = (Component)tipObject;
  243.       } else if (tipObject instanceof Icon) {
  244.         currentTipComponent = new JLabel((Icon)tipObject);
  245.       } else {
  246.         JScrollPane tipScroll = new JScrollPane();
  247.         tipScroll.setBorder(null);
  248.         tipScroll.setOpaque(false);
  249.         tipScroll.getViewport().setOpaque(false);
  250.         tipScroll.setBorder(null);
  251.         String text = tipObject == null?"":tipObject.toString();
  252.         if (text.toLowerCase().startsWith("<html>")) {
  253.           JEditorPane editor = new JEditorPane("text/html", text);
  254.           UIManagerUtils.htmlize(editor, tipPane.getFont());
  255.           editor.setEditable(false);
  256.           editor.setBorder(null);
  257.           editor.setMargin(null);
  258.           editor.setOpaque(false);
  259.           tipScroll.getViewport().setView(editor);
  260.         } else {
  261.           JTextArea area = new JTextArea(text);
  262.           area.setFont(tipPane.getFont());
  263.           area.setEditable(false);
  264.           area.setLineWrap(true);
  265.           area.setWrapStyleWord(true);
  266.           area.setBorder(null);
  267.           area.setMargin(null);
  268.           area.setOpaque(false);
  269.           tipScroll.getViewport().setView(area);
  270.         }
  271.         currentTipComponent = tipScroll;
  272.       }
  273.     }
  274.     
  275.     tipArea.add("Center", currentTipComponent);
  276.     tipArea.revalidate();
  277.     tipArea.repaint();
  278.   }
  279.   @Override
  280.   public void uninstallUI(JComponent c) {
  281.     uninstallListeners();
  282.     uninstallComponents();
  283.     uninstallDefaults();
  284.     super.uninstallUI(c);
  285.   }
  286.   protected void uninstallListeners() {
  287.     tipPane.removePropertyChangeListener(changeListener);
  288.   }
  289.   protected void uninstallComponents() {}
  290.   protected void uninstallDefaults() {}
  291.   class ChangeListener implements PropertyChangeListener {
  292.     public void propertyChange(PropertyChangeEvent evt) {
  293.       if (JXTipOfTheDay.CURRENT_TIP_CHANGED_KEY.equals(evt.getPropertyName())) {
  294.         showCurrentTip();
  295.       }
  296.     }
  297.   }
  298.   class PreviousTipAction extends AbstractAction {
  299.     public PreviousTipAction() {
  300.       super("previousTip");
  301.     }
  302.     public void actionPerformed(ActionEvent e) {
  303.       tipPane.previousTip();
  304.     }
  305.     @Override
  306.     public boolean isEnabled() {
  307.       return tipPane.isEnabled();
  308.     }
  309.   }
  310.   class NextTipAction extends AbstractAction {
  311.     public NextTipAction() {
  312.       super("nextTip");
  313.     }
  314.     public void actionPerformed(ActionEvent e) {
  315.       tipPane.nextTip();
  316.     }
  317.     @Override
  318.     public boolean isEnabled() {
  319.       return tipPane.isEnabled();
  320.     }
  321.   }
  322.   
  323. }