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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXHyperlinkDemoPanel.java,v 1.17 2005/10/12 08:48:11 kleopatra 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.net.MalformedURLException;
  9. import java.net.URL;
  10. import javax.swing.Action;
  11. import javax.swing.DefaultListModel;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.ListModel;
  15. import javax.swing.table.DefaultTableModel;
  16. import javax.swing.table.TableModel;
  17. import org.jdesktop.demo.DemoPanel;
  18. import org.jdesktop.swingx.EditorPaneLinkVisitor;
  19. import org.jdesktop.swingx.JXHyperlink;
  20. import org.jdesktop.swingx.JXList;
  21. import org.jdesktop.swingx.JXTable;
  22. import org.jdesktop.swingx.LinkModel;
  23. import org.jdesktop.swingx.action.LinkAction;
  24. import com.jgoodies.forms.builder.PanelBuilder;
  25. import com.jgoodies.forms.layout.CellConstraints;
  26. import com.jgoodies.forms.layout.FormLayout;
  27. /**
  28.  * Demonstrates the JXHyperlink component.
  29.  *
  30.  * @author Richard Bair
  31.  * @author Jeanette Winzenburg
  32.  * 
  33.  */
  34. public class JXHyperlinkDemoPanel extends DemoPanel {
  35.     //---------------- demo views    
  36.     private JXTable table;
  37.     private JXList list;
  38.     private JXHyperlink textOnlyHyperlink;
  39.     
  40.     //---------------- models
  41.     private EditorPaneLinkVisitor linkVisitor;
  42.     /** Creates new form JXHyperlinkDemoPanel */
  43.     public JXHyperlinkDemoPanel() {
  44.         setName("JXHyperlink Demo");
  45.         initLinkVisitor();
  46.         initComponents();
  47.         configureComponents();
  48.         build();
  49.     }
  50.     private void initLinkVisitor() {
  51.         // the link visitor - the action which is triggered on clicking the link
  52.         linkVisitor = new EditorPaneLinkVisitor();
  53.     }
  54.     private void configureComponents() {
  55.         TableModel commonModel = createTableModel();
  56.         table.setModel(commonModel);
  57.         table.setDefaultLinkVisitor(linkVisitor);
  58.         table.setColumnControlVisible(true);
  59.         table.packAll();
  60.         list.setModel(createListModel(commonModel));
  61.         list.setLinkVisitor(linkVisitor);
  62.         LinkAction textOnly = createLinkAction("SwingX", "https://swingx.dev.java.net");
  63.         textOnlyHyperlink.setAction(textOnly);
  64. //        textOnly.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("/org/jdesktop/demo/swingx/resources/duke_thumbsup.gif")));
  65.     }
  66. //------------------------ create models
  67.     
  68.     private TableModel createTableModel() {
  69.         DefaultTableModel model = new DefaultTableModel(new Object[] { "Description", "LinkModel"}, 0) {
  70.             public Class getColumnClass(int column) {
  71.                 return getValueAt(0, column).getClass();
  72.             }
  73.         };
  74.         model.addRow(new Object[] {"not existing", createLink("Nirvana", "http://www.nirvana.all") });
  75.         model.addRow(new Object[] {"local resource", new LinkModel("Bike me!", null,getClass().getResource("resources/test.html")) });
  76.         model.addRow(new Object[] {"external resource", createLink("SwingX", "https://swingx.dev.java.net")});
  77.         model.addRow(new Object[] {"closed", createLink("Heaven", "http://www.hell.all") });
  78.         return model;
  79.     }
  80.     private ListModel createListModel(TableModel tableModel) {
  81.         int linkColumn = -1;
  82.         for (int i = 0; i < tableModel.getColumnCount(); i++) {
  83.             if (LinkModel.class == tableModel.getColumnClass(i)) {
  84.                 linkColumn = i;
  85.                 break;
  86.             }
  87.         }
  88.         DefaultListModel model = new DefaultListModel();
  89.         for (int i = 0; i < tableModel.getRowCount(); i++) {
  90.             LinkModel link = (LinkModel) tableModel.getValueAt(i, linkColumn);
  91.             model.addElement(new LinkModel(link.getText(), null, link.getURL()));
  92.         }
  93.  
  94.         return model;
  95.     }
  96.     /** This method is called from within the constructor to
  97.      * initialize the form.
  98.      */
  99.     private void initComponents() {
  100.         // a link-aware JTable
  101.         table = new JXTable();
  102.         
  103.         // a link-aware JList
  104.         list = new JXList();
  105.         
  106.         textOnlyHyperlink = new JXHyperlink();
  107.     }
  108.     
  109.     private void build() {
  110.      
  111. //        COLUMN SPECS:
  112. //            f:d:n, l:7dluX:n, f:d:g
  113. //            ROW SPECS:   
  114. //            c:d:n, t:4dluY:n, c:d:n, t:7dluY:n, f:d:g, t:7dluY:n, c:d:n
  115. //
  116. //            COLUMN GROUPS:  {}
  117. //            ROW GROUPS:     {}
  118. //
  119. //            COMPONENT CONSTRAINTS
  120. //            ( 1,  1,  1,  1, "d=f, d=c"); javax.swing.JLabel      "linkTable"; name=linkTableLabel
  121. //            ( 3,  1,  1,  1, "d=f, d=c"); javax.swing.JLabel      "outputPane"; name=outputPaneLabel
  122. //            ( 1,  3,  1,  1, "d=f, d=c"); de.kleopatra.view.JButton; name=textButton
  123. //            ( 3,  3,  1,  5, "d=f, d=f"); javax.swing.JScrollPane; name=outputPane
  124. //            ( 1,  5,  1,  1, "d=f, d=f"); javax.swing.JScrollPane; name=linkTable
  125. //            ( 1,  7,  1,  1, "d=f, d=c"); javax.swing.JScrollPane; name=linkList
  126. //
  127.         FormLayout formLayout = new FormLayout(
  128.                 "f:d:n, l:7dlu:n, f:d:g", // columns
  129.                 "c:d:n, t:4dlu:n, c:d:n, t:7dlu:n, f:d:g, t:7dlu:n, c:d:n"); // rows
  130.         PanelBuilder builder = new PanelBuilder(this, formLayout);
  131.         builder.setDefaultDialogBorder();
  132.         CellConstraints cc = new CellConstraints();
  133.         CellConstraints cl = new CellConstraints();
  134.         
  135.         builder.addLabel("Links: ", cl.xywh(1, 1, 1, 1), 
  136.                 textOnlyHyperlink, cc.xywh(1,  3,  1,  1));
  137.         builder.addLabel("Link Content: ", cl.xywh(3, 1, 1, 1), 
  138.                 new JScrollPane(linkVisitor.getOutputComponent()), cc.xywh(3,  3,  1,  5));
  139.         builder.add(new JScrollPane(table), cc.xywh(1, 5,  1,  1) );
  140.         builder.add(new JScrollPane(list), cc.xywh(1, 7, 1, 1));
  141.     }
  142.     private LinkAction createLinkAction(String description, String urlString) {
  143.         LinkModel swingXLink = createLink(description, urlString);
  144.         LinkAction textOnly = new LinkAction(swingXLink);
  145.         textOnly.setVisitingDelegate(linkVisitor);
  146.         return textOnly;
  147.     }
  148.     private LinkModel createLink(String description, String urlString) {
  149.         try {
  150.             return new LinkModel(description, null, new URL(urlString));
  151.         } catch (MalformedURLException e) {
  152.             // ignore - something went wrong
  153.         }
  154.         return null;
  155.     }
  156. //---------------------- override super    
  157.     public String getHtmlDescription() {
  158.         return "<html>" +
  159.                 "<b>JXHyperlink component </b> - is a button which " +
  160.                 "by default looks like a classic HTML link element. " +
  161.                 "The button is typically configured with a LinkAction  " +
  162.                 "which guarantees to stay in synch with an underlying LinkModel object. " +
  163.                 "A LinkAction can be configured with a link visitor - that's an" +
  164.                 "actionListener which tries to visit the link. " +
  165.                 "There are predefined link visitors: " +
  166.                 "<ul>" +
  167.                 "<li> EditorPaneLinkVisitor tries to connect to the link url and show its" +
  168.                 " content in the contained EditorPane." +
  169.                 "<li> ApplicationLinkVisitor (in jdnc-api) tries to use the context " +
  170.                 " dependent default browser/output to show the link url " +
  171.                 "(TODO: not working)." +
  172.                 "</ul>" +
  173.                 "<b>Hyperlink in collective views </b> - are supported for JX/Tree/Table, " +
  174.                 " JXList, (TODO: JXTree) which have a rolloverEnabled property (disabled by default)." +
  175.                 " Internal collaborators: " +
  176.                 "<ul>" +
  177.                 "<li> LinkRenderer/LinkEditor using a JXHyperlink to show/visit a LinkModel." +
  178.                 "<li> RolloverProducer/LinkController tracking mouseEvents to trigger " +
  179.                 " rollover effects, re-/setting the link cursor and visit a LinkModel." +
  180.                 "</ul>" +
  181.                 "</html>";
  182.     }
  183.     public String getName() {
  184.         return "Hyperlinks";
  185.     }
  186.     
  187.     public String getInformationTitle() {
  188.         return "JXHyperlink";
  189.     }
  190. }