KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ListTagsDialog.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */

20
21 package org.armedbear.j;
22
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.InputEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.awt.event.MouseListener JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.JList JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31
32 public final class ListTagsDialog extends AbstractDialog implements MouseListener JavaDoc
33 {
34     private Editor editor;
35     private List JavaDoc tags;
36     private JList JavaDoc list;
37     private Tag tag;
38
39     public ListTagsDialog(String JavaDoc title, List JavaDoc tags)
40     {
41         super(Editor.currentEditor(), title, true);
42         editor = Editor.currentEditor();
43         this.tags = tags;
44         init();
45     }
46
47     public ListTagsDialog(Editor editor, String JavaDoc title, List JavaDoc tags)
48     {
49         super(editor, title, true);
50         this.editor = editor;
51         this.tags = tags;
52         init();
53     }
54
55     private void init()
56     {
57         int index = 0;
58         if (tags == null) {
59             Buffer buffer = editor.getBuffer();
60             tags = buffer.getTags();
61             if (tags == null) {
62                 if (buffer.getMode() != null) {
63                     Tagger tagger = buffer.getMode().getTagger(buffer);
64                     if (tagger != null) {
65                         editor.setWaitCursor();
66                         tagger.run();
67                         editor.setDefaultCursor();
68                         tags = buffer.getTags();
69                     }
70                 }
71             }
72             if (tags == null)
73                 tags = new ArrayList JavaDoc();
74             // We want to set the selection to the next tag after the cursor
75
// position in the current editor.
76
for (int i = 0; i < tags.size(); i++) {
77                 LocalTag t = (LocalTag) tags.get(i);
78                 if (t.lineNumber() > editor.getDotLineNumber())
79                     break;
80                 index = i;
81             }
82         }
83         final int size = tags.size();
84         String JavaDoc[] array = new String JavaDoc[size];
85         for (int i = size-1; i >= 0; i--) {
86             Tag t = (Tag) tags.get(i);
87             array[i] = t.getLongName();
88         }
89         list = new JList JavaDoc(array);
90         int h = Editor.preferences().getIntegerProperty(Property.JLIST_FIXED_CELL_HEIGHT);
91         if (h > 0)
92             list.setFixedCellHeight(h);
93         int numVisibleRows = Math.max(Math.min(size, 13), 5);
94         list.setVisibleRowCount(numVisibleRows);
95         list.addKeyListener(this);
96         list.addMouseListener(this);
97         JScrollPane JavaDoc scroller = new JScrollPane JavaDoc(list);
98         mainPanel.add(scroller);
99         addVerticalStrut();
100         addOKCancel();
101         pack();
102         // The dialog should be no wider than the current frame..
103
int maxWidth = editor.getSize().width - 12;
104         if (getSize().width > maxWidth)
105             setSize(new Dimension JavaDoc(maxWidth, getSize().height));
106         list.setSelectedIndex(index);
107         list.ensureIndexIsVisible(index);
108         // If the selected item is the last row in the list box, try to get it
109
// to be centered.
110
if (list.getLastVisibleIndex() == index) {
111             index += numVisibleRows / 2;
112             if (index > size - 1)
113                 index = size - 1;
114             list.ensureIndexIsVisible(index);
115         }
116         list.requestFocus();
117     }
118
119     public final Tag getTag()
120     {
121         return tag;
122     }
123
124     protected void ok()
125     {
126         dispose();
127         int index = list.getSelectedIndex();
128         if (tags != null && index >= 0 && tags.size() > index)
129             tag = (Tag) tags.get(index);
130     }
131
132     public void mouseClicked(MouseEvent JavaDoc e)
133     {
134         if (e.getClickCount() == 2)
135             ok();
136     }
137
138     public void mousePressed(MouseEvent JavaDoc e)
139     {
140         if (e.getModifiers() == InputEvent.BUTTON2_MASK) {
141             int index = list.locationToIndex(e.getPoint());
142             list.setSelectedIndex(index);
143             ok();
144         }
145     }
146
147     public void mouseReleased(MouseEvent JavaDoc e) {}
148
149     public void mouseEntered(MouseEvent JavaDoc e) {}
150
151     public void mouseExited(MouseEvent JavaDoc e) {}
152
153     public static void listTags()
154     {
155         final Editor editor = Editor.currentEditor();
156         final Buffer buffer = editor.getBuffer();
157         FastStringBuffer sb = new FastStringBuffer("List Tags");
158         if (buffer.getFile() != null) {
159             sb.append(" ");
160             sb.append(buffer.getFile().getName());
161         }
162         ListTagsDialog d = new ListTagsDialog(sb.toString(), null);
163         editor.centerDialog(d);
164         d.show();
165         Tag tag = d.getTag();
166         if (tag instanceof LocalTag)
167             TagCommands.gotoLocalTag(editor, (LocalTag)tag, false);
168     }
169 }
170
Popular Tags