001: /*
002: * SidebarList.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: SidebarList.java,v 1.4 2003/07/23 00:28:56 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import javax.swing.JLabel;
027: import javax.swing.JList;
028: import javax.swing.ListCellRenderer;
029: import javax.swing.UIManager;
030: import javax.swing.border.Border;
031: import javax.swing.border.CompoundBorder;
032: import javax.swing.border.EmptyBorder;
033:
034: public abstract class SidebarList extends JList implements
035: NavigationComponent {
036: protected Sidebar sidebar;
037:
038: public SidebarList(Sidebar sidebar) {
039: this .sidebar = sidebar;
040: setCellRenderer(new SidebarListCellRenderer(sidebar));
041: setToolTipText("");
042: int h = Editor.preferences().getIntegerProperty(
043: Property.JLIST_FIXED_CELL_HEIGHT);
044: if (h > 0)
045: setFixedCellHeight(h);
046: setFocusTraversalKeysEnabled(false);
047: }
048:
049: public void refresh() {
050: }
051:
052: public void updatePosition() {
053: }
054:
055: protected void centerIndex(int index) {
056: int first = getFirstVisibleIndex();
057: int last = getLastVisibleIndex();
058: if (first == -1 || last == -1) {
059: ensureIndexIsVisible(index);
060: return;
061: }
062: if (first == 0 && last == getModel().getSize() - 1)
063: return;
064: if (index > first + 2 && index < last - 2)
065: return;
066: int span = last - first;
067: first = index - span / 2;
068: if (first < 0)
069: first = 0;
070: ensureIndexIsVisible(first);
071: if (getFirstVisibleIndex() == first)
072: return;
073: last = first + span;
074: if (last > getModel().getSize() - 1)
075: last = getModel().getSize() - 1;
076: ensureIndexIsVisible(last);
077: }
078:
079: private static final class SidebarListCellRenderer extends JLabel
080: implements ListCellRenderer {
081: private Sidebar sidebar;
082:
083: private static Border noFocusBorder;
084:
085: private static Color noFocusSelectionBackground = new Color(
086: 208, 208, 208);
087:
088: public SidebarListCellRenderer(Sidebar sidebar) {
089: super ();
090: this .sidebar = sidebar;
091: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
092: setOpaque(true);
093: }
094:
095: public Component getListCellRendererComponent(JList list,
096: Object value, int index, boolean isSelected,
097: boolean cellHasFocus) {
098: Frame frame = sidebar.getFrame();
099: if (isSelected) {
100: if (frame.isActive()
101: && frame.getFocusedComponent() == list)
102: setBackground(list.getSelectionBackground());
103: else
104: setBackground(noFocusSelectionBackground);
105: setForeground(list.getSelectionForeground());
106: } else {
107: setBackground(list.getBackground());
108: setForeground(list.getForeground());
109: }
110: Border innerBorder = null;
111: if (value instanceof Buffer) {
112: setText(value.toString());
113: Buffer buffer = (Buffer) value;
114: setIcon(buffer.getIcon());
115: if (buffer.isSecondary())
116: innerBorder = new EmptyBorder(0, 10, 0, 0);
117: } else if (value instanceof LocalTag) {
118: LocalTag tag = (LocalTag) value;
119: setText(tag.getSidebarText());
120: setIcon(tag.getIcon());
121: }
122: setEnabled(list.isEnabled());
123: setFont(list.getFont());
124: final Border outerBorder;
125: if (cellHasFocus)
126: outerBorder = UIManager
127: .getBorder("List.focusCellHighlightBorder");
128: else
129: outerBorder = noFocusBorder;
130: setBorder(new CompoundBorder(outerBorder, innerBorder));
131: return this ;
132: }
133:
134: public void paintComponent(java.awt.Graphics g) {
135: Display.setRenderingHints(g);
136: super.paintComponent(g);
137: }
138: }
139: }
|