001: /*
002: * SidebarPanel.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: SidebarPanel.java,v 1.4 2003/07/24 19:40:44 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.Component;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.event.MouseEvent;
028: import java.awt.event.MouseListener;
029: import javax.swing.BorderFactory;
030: import javax.swing.BoxLayout;
031: import javax.swing.JComponent;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.JScrollPane;
035:
036: public final class SidebarPanel extends JPanel implements MouseListener {
037: private final Sidebar sidebar;
038: private JLabel label;
039: private JScrollPane scrollPane;
040:
041: public SidebarPanel(Sidebar sidebar) {
042: super ();
043: this .sidebar = sidebar;
044: setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
045: }
046:
047: public void removeAll() {
048: label = null;
049: scrollPane = null;
050: super .removeAll();
051: }
052:
053: public void setLabelText(String s) {
054: if (s == null || s.length() == 0) {
055: // Remove label.
056: if (label != null) {
057: remove(label);
058: label = null;
059: }
060: } else {
061: if (label == null) {
062: // Add label.
063: if (scrollPane != null) {
064: remove(scrollPane);
065: add(label = new Label());
066: add(scrollPane);
067: validate();
068: }
069: }
070: if (label != null && !s.equals(label.getText())) {
071: label.setText(s);
072: // Don't let width of label constrain width of sidebar.
073: Dimension dim = label.getPreferredSize();
074: dim.width = 0;
075: label.setMinimumSize(dim);
076: }
077: }
078: }
079:
080: public void addScrollPane(JScrollPane scrollPane) {
081: if (label == null)
082: add(label = new Label());
083: if (scrollPane.getVerticalScrollBar() != null)
084: scrollPane.getVerticalScrollBar().addMouseListener(this );
085: this .scrollPane = scrollPane;
086: add(scrollPane);
087: }
088:
089: public void mouseClicked(MouseEvent e) {
090: }
091:
092: public void mouseEntered(MouseEvent e) {
093: }
094:
095: public void mouseExited(MouseEvent e) {
096: }
097:
098: public void mousePressed(MouseEvent e) {
099: if (scrollPane != null) {
100: if (scrollPane.getViewport() != null) {
101: Component c = scrollPane.getViewport().getView();
102: if (c instanceof JComponent)
103: sidebar.getEditor().setFocus((JComponent) c);
104: }
105: }
106: }
107:
108: public void mouseReleased(MouseEvent e) {
109: }
110:
111: private static class Label extends JLabel {
112: private Label() {
113: super ();
114: setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
115: }
116:
117: public void paintComponent(Graphics g) {
118: Display.setRenderingHints(g);
119: super.paintComponent(g);
120: }
121: }
122: }
|