KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > SidebarList


1 /*
2  * SidebarList.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: SidebarList.java,v 1.4 2003/07/23 00:28:56 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JList JavaDoc;
28 import javax.swing.ListCellRenderer JavaDoc;
29 import javax.swing.UIManager JavaDoc;
30 import javax.swing.border.Border JavaDoc;
31 import javax.swing.border.CompoundBorder JavaDoc;
32 import javax.swing.border.EmptyBorder JavaDoc;
33
34 public abstract class SidebarList extends JList JavaDoc implements NavigationComponent
35 {
36     protected Sidebar sidebar;
37
38     public SidebarList(Sidebar sidebar)
39     {
40         this.sidebar = sidebar;
41         setCellRenderer(new SidebarListCellRenderer(sidebar));
42         setToolTipText("");
43         int h = Editor.preferences().getIntegerProperty(Property.JLIST_FIXED_CELL_HEIGHT);
44         if (h > 0)
45             setFixedCellHeight(h);
46         setFocusTraversalKeysEnabled(false);
47     }
48
49     public void refresh()
50     {
51     }
52
53     public void updatePosition()
54     {
55     }
56
57     protected void centerIndex(int index)
58     {
59         int first = getFirstVisibleIndex();
60         int last = getLastVisibleIndex();
61         if (first == -1 || last == -1) {
62             ensureIndexIsVisible(index);
63             return;
64         }
65         if (first == 0 && last == getModel().getSize()-1)
66             return;
67         if (index > first + 2 && index < last-2)
68             return;
69         int span = last - first;
70         first = index - span / 2;
71         if (first < 0)
72             first = 0;
73         ensureIndexIsVisible(first);
74         if (getFirstVisibleIndex() == first)
75             return;
76         last = first + span;
77         if (last > getModel().getSize()-1)
78             last = getModel().getSize()-1;
79         ensureIndexIsVisible(last);
80     }
81
82     private static final class SidebarListCellRenderer extends JLabel JavaDoc
83         implements ListCellRenderer JavaDoc
84     {
85         private Sidebar sidebar;
86
87         private static Border JavaDoc noFocusBorder;
88
89         private static Color JavaDoc noFocusSelectionBackground = new Color JavaDoc(208, 208, 208);
90
91         public SidebarListCellRenderer(Sidebar sidebar)
92         {
93             super();
94             this.sidebar = sidebar;
95             noFocusBorder = new EmptyBorder JavaDoc(1, 1, 1, 1);
96             setOpaque(true);
97         }
98
99         public Component JavaDoc getListCellRendererComponent(
100             JList JavaDoc list,
101             Object JavaDoc value,
102             int index,
103             boolean isSelected,
104             boolean cellHasFocus)
105         {
106             Frame frame = sidebar.getFrame();
107             if (isSelected) {
108                 if (frame.isActive() && frame.getFocusedComponent() == list)
109                     setBackground(list.getSelectionBackground());
110                 else
111                     setBackground(noFocusSelectionBackground);
112                 setForeground(list.getSelectionForeground());
113             } else {
114                 setBackground(list.getBackground());
115                 setForeground(list.getForeground());
116             }
117             Border JavaDoc innerBorder = null;
118             if (value instanceof Buffer) {
119                 setText(value.toString());
120                 Buffer buffer = (Buffer) value;
121                 setIcon(buffer.getIcon());
122                 if (buffer.isSecondary())
123                     innerBorder = new EmptyBorder JavaDoc(0, 10, 0, 0);
124             } else if (value instanceof LocalTag) {
125                 LocalTag tag = (LocalTag) value;
126                 setText(tag.getSidebarText());
127                 setIcon(tag.getIcon());
128             }
129             setEnabled(list.isEnabled());
130             setFont(list.getFont());
131             final Border JavaDoc outerBorder;
132             if (cellHasFocus)
133                 outerBorder = UIManager.getBorder("List.focusCellHighlightBorder");
134             else
135                 outerBorder = noFocusBorder;
136             setBorder(new CompoundBorder JavaDoc(outerBorder, innerBorder));
137             return this;
138         }
139
140         public void paintComponent(java.awt.Graphics JavaDoc g)
141         {
142             Display.setRenderingHints(g);
143             super.paintComponent(g);
144         }
145     }
146 }
147
Popular Tags