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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXTipOfTheDayDemoPanel.java,v 1.2 2005/08/25 14:32:42 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. package org.jdesktop.demo.swingx;
  8. import java.awt.BorderLayout;
  9. import java.awt.KeyboardFocusManager;
  10. import java.awt.event.ActionEvent;
  11. import java.util.ResourceBundle;
  12. import javax.swing.AbstractAction;
  13. import javax.swing.Action;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.ImageIcon;
  16. import javax.swing.JEditorPane;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JSeparator;
  19. import javax.swing.JTree;
  20. import javax.swing.LookAndFeel;
  21. import javax.swing.SwingConstants;
  22. import javax.swing.UIManager;
  23. import javax.swing.plaf.metal.MetalLookAndFeel;
  24. import org.jdesktop.demo.DemoPanel;
  25. import org.jdesktop.swingx.JXEditorPane;
  26. import org.jdesktop.swingx.JXHyperlink;
  27. import org.jdesktop.swingx.JXPanel;
  28. import org.jdesktop.swingx.JXTipOfTheDay;
  29. import org.jdesktop.swingx.VerticalLayout;
  30. import org.jdesktop.swingx.plaf.LookAndFeelAddons;
  31. import org.jdesktop.swingx.plaf.basic.BasicTipOfTheDayUI;
  32. import org.jdesktop.swingx.plaf.metal.MetalLookAndFeelAddons;
  33. import org.jdesktop.swingx.plaf.windows.WindowsLookAndFeelAddons;
  34. import org.jdesktop.swingx.tips.DefaultTip;
  35. import org.jdesktop.swingx.tips.DefaultTipOfTheDayModel;
  36. public class JXTipOfTheDayDemoPanel extends DemoPanel {
  37.   static ResourceBundle RESOURCE = ResourceBundle
  38.     .getBundle("org.jdesktop.demo.swingx.resources.JXTipOfTheDayDemoPanelRB");
  39.   public JXTipOfTheDayDemoPanel() {
  40.     setName("JXTipOfTheDay Demo");
  41.     initComponents();
  42.   }
  43.   public String getHtmlDescription() {
  44.     return RESOURCE.getString("description");
  45.   }
  46.   public String getName() {
  47.     return RESOURCE.getString("name");
  48.   }
  49.   public void initComponents() {
  50.     setLayout(new VerticalLayout(3));
  51.     // click to show the Tip Of the Day dialog with the default look and feel
  52.     // applies for all but Windows
  53.     addLookAndFeel(
  54.       RESOURCE.getString("defaultlnf.description"), 
  55.       MetalLookAndFeel.class.getName(),
  56.       MetalLookAndFeelAddons.class.getName());
  57.     
  58.     addLookAndFeel(
  59.       RESOURCE.getString("windowslnf.description"), 
  60.       "com.sun.java.swing.plaf.windows.WindowsLookAndFeel",
  61.       WindowsLookAndFeelAddons.class.getName());
  62.     
  63.     //
  64.     // JXTipOfTheDay is also a regular component which can be embedded
  65.     //
  66.     JXEditorPane descriptionPane = new JXEditorPane();
  67.     descriptionPane.setEditable(false);
  68.     descriptionPane.setContentType("text/html");
  69.     descriptionPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
  70.       Boolean.TRUE);
  71.     descriptionPane.setText(RESOURCE.getString("embedded.description"));
  72.     descriptionPane.setOpaque(false);
  73.     descriptionPane.setBorder(BorderFactory.createEmptyBorder(3, 12, 3, 3));
  74.     add(descriptionPane);
  75.     JXPanel panel = new JXPanel(new BorderLayout());
  76.     panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));    
  77.     JXTipOfTheDay embedded = newTOTD();
  78.     panel.add("Center", embedded);
  79.     embedded.setAlpha(0.8f);
  80.     JXHyperlink nextTip = new JXHyperlink(embedded.getActionMap().get("nextTip"));
  81.     nextTip.setText(RESOURCE.getString("embedded.nextTip"));
  82.     nextTip.setVerticalAlignment(SwingConstants.CENTER);
  83.     nextTip.setHorizontalAlignment(SwingConstants.CENTER);
  84.     nextTip.setFocusPainted(false);
  85.     
  86.     add(nextTip);
  87.     add(panel);    
  88.   }
  89.     
  90.   void addLookAndFeel(String description, String lnfClassname,
  91.       String addonClassname) {
  92.     // do not add components if the look and feel is not found on this platform
  93.     try {
  94.       Class.forName(lnfClassname);
  95.     } catch (Exception e) {
  96.       return;
  97.     }
  98.     
  99.     JXEditorPane descriptionPane = new JXEditorPane();
  100.     descriptionPane.setEditable(false);
  101.     descriptionPane.setContentType("text/html");
  102.     descriptionPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
  103.       Boolean.TRUE);
  104.     descriptionPane.setText(description);
  105.     descriptionPane.setOpaque(false);
  106.     descriptionPane.setBorder(BorderFactory.createEmptyBorder(3, 12, 3, 3));
  107.     add(descriptionPane);
  108.     
  109.     Action action = makeAction(
  110.       lnfClassname,
  111.       addonClassname);    
  112.     JXHyperlink link = new JXHyperlink(action);
  113.     link.setVerticalAlignment(SwingConstants.CENTER);
  114.     link.setHorizontalAlignment(SwingConstants.CENTER);
  115.     link.setFocusPainted(false);
  116.     link.setText("Click to open the Tip Of The Day dialog");
  117.     add(link);
  118.     
  119.     add(new JSeparator(JSeparator.HORIZONTAL));
  120.   }
  121.   
  122.   static JXTipOfTheDay newTOTD() {
  123.     // Create a tip model with some tips
  124.     DefaultTipOfTheDayModel tips = new DefaultTipOfTheDayModel();
  125.     
  126.     // plain text
  127.     tips
  128.       .add(new DefaultTip(
  129.         "tip1",
  130.         "This is the first tip " +
  131.         "This is the first tip " +
  132.         "This is the first tip " +
  133.         "This is the first tip " +
  134.         "This is the first tip " +
  135.         "This is the first tipn" +
  136.         "This is the first tip " +
  137.         "This is the first tip"));
  138.     
  139.     // html text
  140.     tips
  141.       .add(new DefaultTip(
  142.         "tip2",
  143.         "<html>This is an html <b>TIP</b><br><center>" +
  144.         "<table border="1">" +
  145.         "<tr><td>1</td><td>entry 1</td></tr>" +
  146.         "<tr><td>2</td><td>entry 2</td></tr>" +
  147.         "<tr><td>3</td><td>entry 3</td></tr>" +
  148.         "</table>"));
  149.     
  150.     // a Component
  151.     tips.add(new DefaultTip("tip3", new JTree()));
  152.     // an Icon
  153.     tips
  154.       .add(new DefaultTip(
  155.         "tip 4",
  156.         new ImageIcon(BasicTipOfTheDayUI.class.getResource("resources/TipOfTheDay24.gif"))));
  157.     
  158.     JXTipOfTheDay totd = new JXTipOfTheDay(tips);
  159.     totd.setCurrentTip(0);
  160.     
  161.     return totd;
  162.   }
  163.   
  164.   static Action makeAction(final String lnf, final String addon) {
  165.     
  166.     // JXTipOfTheDay component can save the "Show Tips on startup" user
  167.     // selection in java.util.prefs.Preferences. Additionally it offers
  168.     // an interface called ShowOnStartupChoice to abstract the place where
  169.     // the user choice is saved.
  170.     // Create a non-persistent ShowOnStartupChoice
  171.     final JXTipOfTheDay.ShowOnStartupChoice fake = new JXTipOfTheDay.ShowOnStartupChoice() {
  172.       private boolean value = true;
  173.       public boolean isShowingOnStartup() {
  174.         return value;
  175.       }
  176.       public void setShowingOnStartup(boolean showOnStartup) {
  177.         value = showOnStartup;
  178.       }
  179.     };
  180.     // This action will set the look and feel and addon given as parameters
  181.     Action action = new AbstractAction() {
  182.       public void actionPerformed(ActionEvent e) {
  183.         LookAndFeel currentLnf = UIManager.getLookAndFeel();
  184.         LookAndFeelAddons currentAddon = LookAndFeelAddons.getAddon();
  185.         
  186.         // if the user previously chooses to not show the tips, show a dialog to
  187.         // cancel this choice. This is only for demo purpose. In a real world
  188.         // application, you would just call showDialog and the JXTipOfTheDay
  189.         // would check if the dialog has to be shown or not.
  190.         if (!fake.isShowingOnStartup()) {
  191.           if (JOptionPane.OK_OPTION == JOptionPane
  192.             .showConfirmDialog(
  193.               null,
  194.               RESOURCE.getString("doyouwanttocancel"),
  195.               RESOURCE.getString("question"),
  196.               JOptionPane.YES_NO_OPTION)) {
  197.             // reset the choice
  198.             fake.setShowingOnStartup(true);
  199.           }
  200.         }
  201.         // change the look and feel and addon
  202.         try {
  203.           UIManager.setLookAndFeel(lnf);
  204.           LookAndFeelAddons.setAddon(addon);
  205.         } catch (Exception ex) {
  206.         }
  207.         
  208.         JXTipOfTheDay totd = newTOTD();
  209.         totd.showDialog(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(), fake);
  210.         
  211.         try {
  212.           UIManager.setLookAndFeel(currentLnf.getClass().getName());
  213.           LookAndFeelAddons.setAddon(currentAddon.getClass().getName());
  214.         } catch (Exception ex) {
  215.         }
  216.       }
  217.     };
  218.     
  219.     return action;
  220.   }
  221. }