| ResourceRenderer |

    1   /*
    2    *  Copyright (c) 1998-2001, The University of Sheffield.
    3    *
    4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    5    *  software, licenced under the GNU Library General Public License,
    6    *  Version 2, June 1991 (in the distribution as file licence.html,
    7    *  and also available at http://gate.ac.uk/gate/licence.html).
    8    *
    9    *  Valentin Tablan 02/10/2001
    10   *
    11   *  $Id: ResourceRenderer.java,v 1.5 2001/11/12 18:06:08 valyt Exp $
    12   *
    13   */
    14  package gate.gui;
    15
    16  import gate.*;
    17  import gate.creole.*;
    18
    19  import java.awt.*;
    20  import javax.swing.*;
    21  import javax.swing.border.*;
    22  import javax.swing.table.*;
    23  import javax.swing.tree.*;
    24
    25  /**
    26   * Renders a {@link Resource} for tables, trees and lists.
    27   * It will use the icon info from the creole register, the name of the resource
    28   * as the rendered string and the type of the resource as the tooltip.
    29   */
    30  public class ResourceRenderer extends JLabel
    31                                implements ListCellRenderer, TableCellRenderer,
    32                                           TreeCellRenderer {
    33
    34    public ResourceRenderer(){
    35      setOpaque(true);
    36    }
    37
    38    public Component getListCellRendererComponent(JList list,
    39                                                  Object value,
    40                                                  int index,
    41                                                  boolean isSelected,
    42                                                  boolean cellHasFocus){
    43      prepareRenderer(list, value, isSelected, hasFocus());
    44      return this;
    45    }
    46
    47
    48    public Component getTableCellRendererComponent(JTable table,
    49                                                   Object value,
    50                                                   boolean isSelected,
    51                                                   boolean hasFocus,
    52                                                   int row,
    53                                                   int column){
    54      prepareRenderer(table, value, isSelected, hasFocus);
    55      return this;
    56    }
    57
    58    public Component getTreeCellRendererComponent(JTree tree,
    59                                                  Object value,
    60                                                  boolean selected,
    61                                                  boolean expanded,
    62                                                  boolean leaf,
    63                                                  int row,
    64                                                  boolean hasFocus){
    65      prepareRenderer(tree, value, selected, hasFocus);
    66      return this;
    67    }
    68
    69    private void prepareRenderer(JComponent ownerComponent, Object value,
    70                                 boolean isSelected, boolean hasFocus){
    71
    72      if (isSelected) {
    73        if(ownerComponent instanceof JTable){
    74          setForeground(((JTable)ownerComponent).getSelectionForeground());
    75          setBackground(((JTable)ownerComponent).getSelectionBackground());
    76        }else if(ownerComponent instanceof JTree){
    77          setForeground(UIManager.getColor("Tree.selectionForeground"));
    78          setBackground(UIManager.getColor("Tree.selectionBackground"));
    79        }else if(ownerComponent instanceof JList){
    80          setForeground(((JList)ownerComponent).getSelectionForeground());
    81          setBackground(((JList)ownerComponent).getSelectionBackground());
    82        }
    83      }else{
    84        if(ownerComponent instanceof JTable){
    85          JTable table = (JTable)ownerComponent;
    86          setForeground(ownerComponent.getForeground());
    87          setBackground(ownerComponent.getBackground());
    88        }else{
    89          setForeground(ownerComponent.getForeground());
    90          setBackground(ownerComponent.getBackground());
    91        }
    92      }
    93
    94      setFont(ownerComponent.getFont());
    95
    96      if (hasFocus) {
    97        setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
    98      }else{
    99        setBorder(noFocusBorder);
    100     }
    101
    102     String text;
    103     String toolTipText;
    104     Icon icon;
    105     ResourceData rData = null;
    106     if(value instanceof Resource){
    107       text = ((Resource)value).getName();
    108
    109       rData = (ResourceData)Gate.getCreoleRegister().
    110                                  get(value.getClass().getName());
    111     }else{
    112       text = (value == null) ? "<none>" : value.toString();
    113     }
    114     if(rData != null){
    115       toolTipText = "<HTML>Type: <b>" + rData.getName() + "</b></HTML>";
    116       String iconName = rData.getIcon();
    117       if(iconName == null){
    118         if(value instanceof LanguageResource) iconName = "lr.gif";
    119         else if(value instanceof ProcessingResource) iconName = "pr.gif";
    120         else if(value instanceof Controller) iconName = "controller.gif";
    121       }
    122       icon = (iconName == null) ? null : MainFrame.getIcon(iconName);
    123     }else{
    124       icon = null;
    125       toolTipText = null;
    126     }
    127
    128     setText(text);
    129     setIcon(icon);
    130     setToolTipText(toolTipText);
    131   }
    132
    133   protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    134 }

| ResourceRenderer |