KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > tooltip > ToolTipList


1 package org.antlr.works.tooltip;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.KeyAdapter JavaDoc;
6 import java.awt.event.KeyEvent JavaDoc;
7 import java.awt.event.MouseAdapter JavaDoc;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.awt.font.TextLayout JavaDoc;
10 /*
11
12 [The "BSD licence"]
13 Copyright (c) 2005 Jean Bovet
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19
20 1. Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22 2. Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25 3. The name of the author may not be used to endorse or promote products
26 derived from this software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 */

40
41 public class ToolTipList extends JPanel {
42
43     protected static final int VISIBLE_TIPS = 10;
44     protected static final Color BACKGROUND_COLOR = new Color(1.0f, 1.0f, 0.6f, 0.8f);
45
46     protected DefaultListModel tipsModel;
47     protected JList tipsList;
48     protected JScrollPane tipsScrollPane;
49     protected ToolTipListDelegate delegate;
50
51     public ToolTipList(ToolTipListDelegate delegate) {
52         super(new BorderLayout());
53
54         this.delegate = delegate;
55
56         tipsModel = new DefaultListModel();
57
58         tipsList = new JList(tipsModel);
59         tipsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
60         tipsList.setBackground(BACKGROUND_COLOR);
61         tipsList.setSelectionForeground(Color.black);
62         tipsList.setSelectionBackground(BACKGROUND_COLOR);
63         tipsList.setPrototypeCellValue("This is a rule name g");
64         tipsList.addKeyListener(new MyListKeyAdapter());
65         tipsList.setFocusable(false);
66
67         tipsScrollPane = new JScrollPane(tipsList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
68         tipsScrollPane.setFocusable(false);
69         tipsScrollPane.setBorder(BorderFactory.createLineBorder(Color.lightGray));
70
71         addMouseListener(new MyMouseAdapter());
72
73         add(tipsScrollPane, BorderLayout.CENTER);
74     }
75
76     protected void notifyHide() {
77         if(delegate != null)
78             delegate.toolTipListHide();
79     }
80
81     public void clear() {
82         tipsModel.clear();
83     }
84
85     public void setText(String JavaDoc text) {
86         String JavaDoc[] lines = text.split("\n");
87         clear();
88         for(int index=0; index<lines.length; index++)
89             addLine(lines[index]);
90     }
91     public void addLine(String JavaDoc text) {
92         tipsModel.addElement(text);
93     }
94
95     public void selectFirstLine() {
96         tipsList.setSelectedIndex(0);
97     }
98
99     public void resize() {
100         int height = tipsList.getFixedCellHeight();
101         int size = tipsModel.size();
102         if(size > 0) {
103             int width = 0;
104             for(int i=0; i<tipsModel.size(); i++) {
105                 String JavaDoc e = (String JavaDoc)tipsModel.getElementAt(i);
106                 TextLayout JavaDoc layout = new TextLayout JavaDoc(e, tipsList.getFont(), ((Graphics2D)tipsList.getGraphics()).getFontRenderContext());
107                 width = Math.max(width, (int)layout.getBounds().getWidth());
108             }
109             height = height*Math.min(VISIBLE_TIPS, size)+5;
110             Dimension d = new Dimension(width+10, height);
111             setSize(d);
112             tipsList.setSize(d);
113             tipsScrollPane.setSize(d);
114         }
115     }
116
117     protected class MyMouseAdapter extends MouseAdapter JavaDoc {
118         public void mouseEntered(MouseEvent JavaDoc e) {
119             notifyHide();
120         }
121
122         public void mousePressed(MouseEvent JavaDoc e) {
123             notifyHide();
124         }
125     }
126
127     protected class MyListKeyAdapter extends KeyAdapter JavaDoc {
128
129         public void selectNextListElement(int direction) {
130             int index = tipsList.getSelectedIndex();
131             index += direction;
132             index = Math.min(Math.max(0, index), tipsModel.size()-1);
133
134             tipsList.setSelectedIndex(index);
135             tipsList.scrollRectToVisible(tipsList.getCellBounds(index, index));
136         }
137
138         public void keyPressed(KeyEvent JavaDoc e) {
139             if(e.isConsumed())
140                 return;
141
142             switch(e.getKeyCode()) {
143                 case KeyEvent.VK_ESCAPE:
144                 case KeyEvent.VK_ENTER:
145                     notifyHide();
146                     e.consume();
147                     break;
148
149                 case KeyEvent.VK_UP:
150                     selectNextListElement(-1);
151                     e.consume();
152                     break;
153
154                 case KeyEvent.VK_DOWN:
155                     selectNextListElement(1);
156                     e.consume();
157                     break;
158             }
159         }
160     }
161
162 }
163
Popular Tags