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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LinkRenderer.java,v 1.11 2005/10/12 11:26:52 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.  * 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.Component;
  23. import java.awt.Point;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import javax.swing.AbstractCellEditor;
  27. import javax.swing.JList;
  28. import javax.swing.JTable;
  29. import javax.swing.ListCellRenderer;
  30. import javax.swing.table.TableCellEditor;
  31. import javax.swing.table.TableCellRenderer;
  32. import org.jdesktop.swingx.action.LinkAction;
  33. /**
  34.  * A Renderer/Editor for Links.
  35.  * 
  36.  * internally uses JXHyperlink for both (Note: don't reuse the same
  37.  * instance for both functions).
  38.  * 
  39.  * PENDING: make renderer respect selected cell state.
  40.  * 
  41.  * @author Jeanette Winzenburg
  42.  */
  43. public class LinkRenderer extends AbstractCellEditor implements
  44.         TableCellRenderer, TableCellEditor, ListCellRenderer {
  45.     private JXHyperlink linkButton;
  46.     private LinkAction linkAction;
  47.     public LinkRenderer() {
  48.         this(null);
  49.     }
  50.     public LinkRenderer(ActionListener visitingDelegate) {
  51.         linkAction = new LinkAction(null);
  52.         linkButton = new JXHyperlink(linkAction);
  53.         linkButton.addActionListener(createEditorActionListener());
  54.         setVisitingDelegate(visitingDelegate);
  55.     }
  56.     public void setVisitingDelegate(ActionListener openAction) {
  57.         linkAction.setVisitingDelegate(openAction);
  58.         
  59.     }
  60.     public Component getListCellRendererComponent(JList list, Object value, 
  61.             int index, boolean isSelected, boolean cellHasFocus) {
  62.         linkAction.setLink(value instanceof LinkModel ? (LinkModel) value : null);
  63.         Point p = (Point) list
  64.             .getClientProperty(RolloverProducer.ROLLOVER_KEY);
  65.         if (cellHasFocus || (p != null && (p.y >= 0) && (p.y == index))) {
  66.              linkButton.getModel().setRollover(true);
  67.         } else {
  68.              linkButton.getModel().setRollover(false);
  69.         }
  70.         return linkButton;
  71.     }
  72.     
  73. //------------------------ TableCellRenderer
  74.     
  75.     public Component getTableCellRendererComponent(JTable table, Object value,
  76.             boolean isSelected, boolean hasFocus, int row, int column) {
  77.         linkAction.setLink(value instanceof LinkModel ? (LinkModel) value : null);
  78.         Point p = (Point) table
  79.                 .getClientProperty(RolloverProducer.ROLLOVER_KEY);
  80.         if (hasFocus || (p != null && (p.x >= 0) && (p.x == column) && (p.y == row))) {
  81.              linkButton.getModel().setRollover(true);
  82.         } else {
  83.              linkButton.getModel().setRollover(false);
  84.         }
  85.         return linkButton;
  86.     }
  87. //-------------------------- TableCellEditor
  88.     
  89.     public Component getTableCellEditorComponent(JTable table, Object value,
  90.             boolean isSelected, int row, int column) {
  91.         linkAction.setLink(value instanceof LinkModel ? (LinkModel) value : null);
  92.         linkButton.getModel().setRollover(true); 
  93.         return linkButton;
  94.     }
  95.     public Object getCellEditorValue() {
  96.         return linkAction.getLink();
  97.     }
  98.     private ActionListener createEditorActionListener() {
  99.         ActionListener l = new ActionListener() {
  100.             public void actionPerformed(ActionEvent e) {
  101.                 fireEditingStopped();
  102.             }
  103.         };
  104.         return l;
  105.     }
  106. }