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