Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/altara/mars/swingui/MarsAbstractRenderer.java


1   /* MARS Network Monitor Swing User Interface
2      Copyright (C) 1999 Brian H. Trammell
3      Copyright (C) 2002 Leapfrog Research & Development, LLC
4   
5     This program is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public License
7     as published by the Free Software Foundation; either version 2
8     of the License, or (at your option) any later version.
9   
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, it is available at 
17    http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA  02111-1307, USA.
20  */
21  
22  package org.altara.mars.swingui;
23  
24  import org.altara.util.*;
25  import org.altara.mars.*;
26  import org.altara.mars.engine.*;
27  import java.util.*;
28  import java.awt.*;
29  import java.awt.event.*;
30  import javax.swing.*;
31  import javax.swing.tree.*;
32  import javax.swing.event.*;
33  import javax.swing.border.*;
34  
35  /** Renders a list cell or tree cell using a JLabel. Most of the look and
36    feel of the renderer components is controlled here.
37  */
38  
39  public abstract class MarsAbstractRenderer extends JLabel
40      implements ListCellRenderer, TreeCellRenderer {
41  
42    protected static final Color nominalFg = Color.black;
43    protected static final Color nominalBg = Color.white;
44    protected static final Color selectedFg = Color.black;
45    protected static final Color selectedBg = new Color(0xcc,0xcc,0xff);
46    protected static final Border focusBorder = new EtchedBorder();
47    protected static final Border nominalBorder = new EmptyBorder(2,2,2,2);
48    public static final Font cellFont = new Font("SansSerif",Font.PLAIN,11);
49  
50    protected static Icon downIcon;
51    protected static Icon timeoutIcon;
52    protected static Icon upIcon;
53    protected static Icon unknownIcon;
54    protected static Icon serviceIcon;
55    protected static Icon okHostIcon;
56    protected static Icon faultHostIcon;
57  
58    protected MarsAbstractRenderer() {
59      // load icons if necessary
60      if (downIcon == null) {
61        downIcon = IconService.getIcon("mi2_ssdn.gif");
62        timeoutIcon = IconService.getIcon("mi2_ssto.gif");
63        upIcon = IconService.getIcon("mi2_ssup.gif");
64        unknownIcon = IconService.getIcon("mi2_ssunk.gif");
65        serviceIcon = IconService.getIcon("mi2_stsvc.gif");
66        okHostIcon = IconService.getIcon("mi2_sthost.gif");
67        faultHostIcon = IconService.getIcon("mi2_sthfault.gif");
68      }
69        
70      // initialize default label state
71      setOpaque(true);
72      setFont(cellFont);
73  
74    }
75  
76    protected Component getCellRendererComponent(Object value,
77        boolean isSelected, boolean cellHasFocus) {
78      // decide which selection decoration to use
79      if (isSelected) {
80        setBackground(selectedBg);
81        setForeground(selectedFg);
82      } else {
83        setBackground(nominalBg);
84        setForeground(nominalFg);
85      }
86  
87      // decide which focus decoration to use
88      if (cellHasFocus) {
89        setBorder(focusBorder);
90      } else {
91        setBorder(nominalBorder);
92      }
93  
94      // set the label's icon
95      setIcon(getIconForValue(value));
96  
97      // and its label text
98      setText(getStringForValue(value));
99  
100     // return the label
101     return this;
102   }
103 
104   protected abstract Icon getIconForValue(Object value);
105   protected abstract String getStringForValue(Object value);
106 
107   /* ------------------------------------------------------------
108     Status-handling utility methods
109   --------------------------------------------------------------*/
110 
111   protected Icon getIconForStatus(Status status) {
112   // Select an icon based on status code
113     if (status.getCode().intValue() <= Status.MAX_HARDFAULTCODE)
114       return downIcon;
115     if (status.getCode().intValue() <= Status.MAX_SOFTFAULTCODE)
116       return timeoutIcon;
117     if (status.getCode() == Status.UP)
118       return upIcon;
119     return unknownIcon;
120   }
121 
122     protected Icon getIconForHost(Host host) {
123         // are any of the host's services faulted?
124         if (host.isOK()) {
125             return okHostIcon;
126         } else {
127             return faultHostIcon;
128         }
129     }
130 
131   /* ------------------------------------------------------------
132     ListCellRenderer / TreeCellRenderer implementation
133   --------------------------------------------------------------*/
134 
135   public Component getListCellRendererComponent(
136       JList list, Object value, int index,
137       boolean isSelected, boolean cellHasFocus) {
138     return getCellRendererComponent(value,isSelected,cellHasFocus);
139   }
140 
141   public Component getTreeCellRendererComponent(
142       JTree tree, Object value, boolean isSelected, 
143       boolean isExpanded, boolean isLeaf, int row,
144       boolean cellHasFocus) {
145     if (value instanceof DefaultMutableTreeNode) {
146       value = ((DefaultMutableTreeNode)value).getUserObject();
147     }
148     return getCellRendererComponent(value,isSelected,cellHasFocus);
149   }
150   
151 }
152