1 19 20 package org.netbeans.modules.editor.completion; 21 22 23 import java.awt.Color ; 24 import java.awt.Dimension ; 25 import java.awt.Point ; 26 import java.awt.Rectangle ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.MouseListener ; 30 import java.util.List ; 31 import javax.swing.AbstractAction ; 32 import javax.swing.Action ; 33 import javax.swing.BorderFactory ; 34 import javax.swing.JLabel ; 35 import javax.swing.JScrollPane ; 36 import javax.swing.KeyStroke ; 37 import javax.swing.event.ListSelectionListener ; 38 import javax.swing.plaf.TextUI ; 39 import javax.swing.text.JTextComponent ; 40 import javax.swing.text.Keymap ; 41 import javax.swing.text.EditorKit ; 42 import org.netbeans.editor.BaseKit; 43 import org.netbeans.editor.ext.ExtKit; 44 import org.netbeans.spi.editor.completion.CompletionItem; 45 46 53 54 public class CompletionScrollPane extends JScrollPane { 55 56 private static final String ESCAPE = "escape"; private static final String COMPLETION_UP = "completion-up"; private static final String COMPLETION_DOWN = "completion-down"; private static final String COMPLETION_PGUP = "completion-pgup"; private static final String COMPLETION_PGDN = "completion-pgdn"; private static final String COMPLETION_BEGIN = "completion-begin"; private static final String COMPLETION_END = "completion-end"; 64 private static final int ACTION_ESCAPE = 0; 65 private static final int ACTION_COMPLETION_UP = 1; 66 private static final int ACTION_COMPLETION_DOWN = 2; 67 private static final int ACTION_COMPLETION_PGUP = 3; 68 private static final int ACTION_COMPLETION_PGDN = 4; 69 private static final int ACTION_COMPLETION_BEGIN = 5; 70 private static final int ACTION_COMPLETION_END = 6; 71 72 private CompletionJList view; 73 74 private List dataObj; 75 76 private JLabel topLabel; 77 78 public CompletionScrollPane(JTextComponent editorComponent, 79 ListSelectionListener listSelectionListener, MouseListener mouseListener) { 80 81 setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER); 82 setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED); 83 84 setMaximumSize(CompletionSettings.INSTANCE.completionPopupMaximumSize()); 86 int maxVisibleRowCount = Math.max(2, 88 getMaximumSize().height / CompletionLayout.COMPLETION_ITEM_HEIGHT - 1); 89 90 view = new CompletionJList(maxVisibleRowCount, mouseListener); 92 if (listSelectionListener != null) { 93 view.addListSelectionListener(listSelectionListener); 94 } 95 setViewportView(view); 96 installKeybindings(editorComponent); 97 } 98 99 public void setData(List data, String title, int selectedIndex) { 100 dataObj = data; 101 view.setData(data); 102 view.setSelectedIndex(selectedIndex); 103 Point p = view.indexToLocation(selectedIndex); 104 if (p != null) 105 view.scrollRectToVisible(new Rectangle (p)); 106 setTitle(title); 107 setViewportView(getViewport().getView()); 114 } 115 116 public CompletionItem getSelectedCompletionItem() { 117 Object ret = view.getSelectedValue(); 118 return ret instanceof CompletionItem ? (CompletionItem) ret : null; 119 } 120 121 public Dimension getPreferredSize() { 122 Dimension prefSize = super.getPreferredSize(); 123 Dimension labelSize = topLabel != null ? topLabel.getPreferredSize() : new Dimension (0, 0); 124 Dimension maxSize = getMaximumSize(); 125 if (labelSize.width > prefSize.width) { 126 prefSize.width = labelSize.width; 127 } 128 if (prefSize.width > maxSize.width) { 129 prefSize.width = maxSize.width; 130 } 131 return prefSize; 133 } 134 135 private void setTitle(String title) { 136 if (title == null) { 137 if (topLabel != null) { 138 setColumnHeader(null); 139 topLabel = null; 140 } 141 } else { 142 if (topLabel != null) { 143 topLabel.setText(title); 144 } else { 145 topLabel = new JLabel (title); 146 topLabel.setForeground(Color.blue); 147 topLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); 148 setColumnHeaderView(topLabel); 149 } 150 } 151 } 152 153 154 private KeyStroke [] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) { 155 KeyStroke [] ret = new KeyStroke [] { defaultKey }; 158 if (component != null) { 159 TextUI ui = component.getUI(); 160 Keymap km = component.getKeymap(); 161 if (ui != null && km != null) { 162 EditorKit kit = ui.getEditorKit(component); 163 if (kit instanceof BaseKit) { 164 Action a = ((BaseKit)kit).getActionByName(editorActionName); 165 if (a != null) { 166 KeyStroke [] keys = km.getKeyStrokesForAction(a); 167 if (keys != null && keys.length > 0) { 168 ret = keys; 169 } 170 } 171 } 172 } 173 } 174 return ret; 175 } 176 177 private void registerKeybinding(int action, String actionName, KeyStroke stroke, String editorActionName, JTextComponent component){ 178 KeyStroke [] keys = findEditorKeys(editorActionName, stroke, component); 179 for (int i = 0; i < keys.length; i++) { 180 getInputMap().put(keys[i], actionName); 181 } 182 getActionMap().put(actionName, new CompletionPaneAction(action)); 183 } 184 185 private void installKeybindings(JTextComponent component) { 186 registerKeybinding(ACTION_ESCAPE, ESCAPE, 188 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 189 ExtKit.escapeAction, component); 190 191 registerKeybinding(ACTION_COMPLETION_UP, COMPLETION_UP, 193 KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), 194 BaseKit.upAction, component); 195 196 registerKeybinding(ACTION_COMPLETION_DOWN, COMPLETION_DOWN, 198 KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), 199 BaseKit.downAction, component); 200 201 registerKeybinding(ACTION_COMPLETION_PGDN, COMPLETION_PGDN, 203 KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0), 204 BaseKit.pageDownAction, component); 205 206 registerKeybinding(ACTION_COMPLETION_PGUP, COMPLETION_PGUP, 208 KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0), 209 BaseKit.pageUpAction, component); 210 211 registerKeybinding(ACTION_COMPLETION_BEGIN, COMPLETION_BEGIN, 213 KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0), 214 BaseKit.beginLineAction, component); 215 216 registerKeybinding(ACTION_COMPLETION_END, COMPLETION_END, 218 KeyStroke.getKeyStroke(KeyEvent.VK_END, 0), 219 BaseKit.endLineAction, component); 220 } 221 222 List testGetData() { 223 return dataObj; 224 } 225 226 private class CompletionPaneAction extends AbstractAction { 227 private int action; 228 229 private CompletionPaneAction(int action) { 230 this.action = action; 231 } 232 233 public void actionPerformed(ActionEvent actionEvent) { 234 switch (action) { 235 case ACTION_ESCAPE: 236 CompletionImpl.get().hideCompletion(false); 237 break; 238 case ACTION_COMPLETION_UP: 239 view.up(); 240 break; 241 case ACTION_COMPLETION_DOWN: 242 view.down(); 243 break; 244 case ACTION_COMPLETION_PGUP: 245 view.pageUp(); 246 break; 247 case ACTION_COMPLETION_PGDN: 248 view.pageDown(); 249 break; 250 case ACTION_COMPLETION_BEGIN: 251 view.begin(); 252 break; 253 case ACTION_COMPLETION_END: 254 view.end(); 255 break; 256 } 257 } 258 } 259 } 260 | Popular Tags |