001: /*
002: * ListTagsDialog.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package org.armedbear.j;
022:
023: import java.awt.Dimension;
024: import java.awt.event.InputEvent;
025: import java.awt.event.MouseEvent;
026: import java.awt.event.MouseListener;
027: import java.util.ArrayList;
028: import java.util.List;
029: import javax.swing.JList;
030: import javax.swing.JScrollPane;
031:
032: public final class ListTagsDialog extends AbstractDialog implements
033: MouseListener {
034: private Editor editor;
035: private List tags;
036: private JList list;
037: private Tag tag;
038:
039: public ListTagsDialog(String title, List tags) {
040: super (Editor.currentEditor(), title, true);
041: editor = Editor.currentEditor();
042: this .tags = tags;
043: init();
044: }
045:
046: public ListTagsDialog(Editor editor, String title, List tags) {
047: super (editor, title, true);
048: this .editor = editor;
049: this .tags = tags;
050: init();
051: }
052:
053: private void init() {
054: int index = 0;
055: if (tags == null) {
056: Buffer buffer = editor.getBuffer();
057: tags = buffer.getTags();
058: if (tags == null) {
059: if (buffer.getMode() != null) {
060: Tagger tagger = buffer.getMode().getTagger(buffer);
061: if (tagger != null) {
062: editor.setWaitCursor();
063: tagger.run();
064: editor.setDefaultCursor();
065: tags = buffer.getTags();
066: }
067: }
068: }
069: if (tags == null)
070: tags = new ArrayList();
071: // We want to set the selection to the next tag after the cursor
072: // position in the current editor.
073: for (int i = 0; i < tags.size(); i++) {
074: LocalTag t = (LocalTag) tags.get(i);
075: if (t.lineNumber() > editor.getDotLineNumber())
076: break;
077: index = i;
078: }
079: }
080: final int size = tags.size();
081: String[] array = new String[size];
082: for (int i = size - 1; i >= 0; i--) {
083: Tag t = (Tag) tags.get(i);
084: array[i] = t.getLongName();
085: }
086: list = new JList(array);
087: int h = Editor.preferences().getIntegerProperty(
088: Property.JLIST_FIXED_CELL_HEIGHT);
089: if (h > 0)
090: list.setFixedCellHeight(h);
091: int numVisibleRows = Math.max(Math.min(size, 13), 5);
092: list.setVisibleRowCount(numVisibleRows);
093: list.addKeyListener(this );
094: list.addMouseListener(this );
095: JScrollPane scroller = new JScrollPane(list);
096: mainPanel.add(scroller);
097: addVerticalStrut();
098: addOKCancel();
099: pack();
100: // The dialog should be no wider than the current frame..
101: int maxWidth = editor.getSize().width - 12;
102: if (getSize().width > maxWidth)
103: setSize(new Dimension(maxWidth, getSize().height));
104: list.setSelectedIndex(index);
105: list.ensureIndexIsVisible(index);
106: // If the selected item is the last row in the list box, try to get it
107: // to be centered.
108: if (list.getLastVisibleIndex() == index) {
109: index += numVisibleRows / 2;
110: if (index > size - 1)
111: index = size - 1;
112: list.ensureIndexIsVisible(index);
113: }
114: list.requestFocus();
115: }
116:
117: public final Tag getTag() {
118: return tag;
119: }
120:
121: protected void ok() {
122: dispose();
123: int index = list.getSelectedIndex();
124: if (tags != null && index >= 0 && tags.size() > index)
125: tag = (Tag) tags.get(index);
126: }
127:
128: public void mouseClicked(MouseEvent e) {
129: if (e.getClickCount() == 2)
130: ok();
131: }
132:
133: public void mousePressed(MouseEvent e) {
134: if (e.getModifiers() == InputEvent.BUTTON2_MASK) {
135: int index = list.locationToIndex(e.getPoint());
136: list.setSelectedIndex(index);
137: ok();
138: }
139: }
140:
141: public void mouseReleased(MouseEvent e) {
142: }
143:
144: public void mouseEntered(MouseEvent e) {
145: }
146:
147: public void mouseExited(MouseEvent e) {
148: }
149:
150: public static void listTags() {
151: final Editor editor = Editor.currentEditor();
152: final Buffer buffer = editor.getBuffer();
153: FastStringBuffer sb = new FastStringBuffer("List Tags");
154: if (buffer.getFile() != null) {
155: sb.append(" ");
156: sb.append(buffer.getFile().getName());
157: }
158: ListTagsDialog d = new ListTagsDialog(sb.toString(), null);
159: editor.centerDialog(d);
160: d.show();
161: Tag tag = d.getTag();
162: if (tag instanceof LocalTag)
163: TagCommands.gotoLocalTag(editor, (LocalTag) tag, false);
164: }
165: }
|