KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SidebarPanel.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: SidebarPanel.java,v 1.4 2003/07/24 19:40:44 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.Component JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.awt.event.MouseListener JavaDoc;
29 import javax.swing.BorderFactory JavaDoc;
30 import javax.swing.BoxLayout JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JScrollPane JavaDoc;
35
36 public final class SidebarPanel extends JPanel JavaDoc implements MouseListener JavaDoc
37 {
38     private final Sidebar sidebar;
39     private JLabel JavaDoc label;
40     private JScrollPane JavaDoc scrollPane;
41
42     public SidebarPanel(Sidebar sidebar)
43     {
44         super();
45         this.sidebar = sidebar;
46         setLayout(new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS));
47     }
48
49     public void removeAll()
50     {
51         label = null;
52         scrollPane = null;
53         super.removeAll();
54     }
55
56     public void setLabelText(String JavaDoc s)
57     {
58         if (s == null || s.length() == 0) {
59             // Remove label.
60
if (label != null) {
61                 remove(label);
62                 label = null;
63             }
64         } else {
65             if (label == null) {
66                 // Add label.
67
if (scrollPane != null) {
68                     remove(scrollPane);
69                     add(label = new Label());
70                     add(scrollPane);
71                     validate();
72                 }
73             }
74             if (label != null && !s.equals(label.getText())) {
75                 label.setText(s);
76                 // Don't let width of label constrain width of sidebar.
77
Dimension JavaDoc dim = label.getPreferredSize();
78                 dim.width = 0;
79                 label.setMinimumSize(dim);
80             }
81         }
82     }
83
84     public void addScrollPane(JScrollPane JavaDoc scrollPane)
85     {
86         if (label == null)
87             add(label = new Label());
88         if (scrollPane.getVerticalScrollBar() != null)
89             scrollPane.getVerticalScrollBar().addMouseListener(this);
90         this.scrollPane = scrollPane;
91         add(scrollPane);
92     }
93
94     public void mouseClicked(MouseEvent JavaDoc e) {}
95
96     public void mouseEntered(MouseEvent JavaDoc e) {}
97
98     public void mouseExited(MouseEvent JavaDoc e) {}
99
100     public void mousePressed(MouseEvent JavaDoc e)
101     {
102         if (scrollPane != null) {
103             if (scrollPane.getViewport() != null) {
104                 Component JavaDoc c = scrollPane.getViewport().getView();
105                 if (c instanceof JComponent JavaDoc)
106                     sidebar.getEditor().setFocus((JComponent JavaDoc)c);
107             }
108         }
109     }
110
111     public void mouseReleased(MouseEvent JavaDoc e) {}
112
113     private static class Label extends JLabel JavaDoc
114     {
115         private Label()
116         {
117             super();
118             setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
119         }
120
121         public void paintComponent(Graphics JavaDoc g)
122         {
123             Display.setRenderingHints(g);
124             super.paintComponent(g);
125         }
126     }
127 }
128
Popular Tags