1 package org.antlr.works.tooltip; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.KeyAdapter ; 6 import java.awt.event.KeyEvent ; 7 import java.awt.event.MouseAdapter ; 8 import java.awt.event.MouseEvent ; 9 import java.awt.font.TextLayout ; 10 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 text) { 86 String [] 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 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 e = (String )tipsModel.getElementAt(i); 106 TextLayout layout = new TextLayout (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 { 118 public void mouseEntered(MouseEvent e) { 119 notifyHide(); 120 } 121 122 public void mousePressed(MouseEvent e) { 123 notifyHide(); 124 } 125 } 126 127 protected class MyListKeyAdapter extends KeyAdapter { 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 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 |