1 20 21 package org.armedbear.j; 22 23 import java.awt.Dimension ; 24 import java.awt.event.InputEvent ; 25 import java.awt.event.MouseEvent ; 26 import java.awt.event.MouseListener ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import javax.swing.JList ; 30 import javax.swing.JScrollPane ; 31 32 public final class ListTagsDialog extends AbstractDialog implements MouseListener 33 { 34 private Editor editor; 35 private List tags; 36 private JList list; 37 private Tag tag; 38 39 public ListTagsDialog(String title, List 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 title, List 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 (); 74 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 [] array = new String [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 (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 scroller = new JScrollPane (list); 98 mainPanel.add(scroller); 99 addVerticalStrut(); 100 addOKCancel(); 101 pack(); 102 int maxWidth = editor.getSize().width - 12; 104 if (getSize().width > maxWidth) 105 setSize(new Dimension (maxWidth, getSize().height)); 106 list.setSelectedIndex(index); 107 list.ensureIndexIsVisible(index); 108 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 e) 133 { 134 if (e.getClickCount() == 2) 135 ok(); 136 } 137 138 public void mousePressed(MouseEvent 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 e) {} 148 149 public void mouseEntered(MouseEvent e) {} 150 151 public void mouseExited(MouseEvent 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 |