1 19 20 package org.netbeans.modules.editor.hints.borrowed; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.LinkedHashSet ; 30 import java.util.List ; 31 import javax.swing.AbstractListModel ; 32 import javax.swing.BorderFactory ; 33 import javax.swing.Icon ; 34 import javax.swing.ImageIcon ; 35 import javax.swing.JList ; 36 import javax.swing.ListCellRenderer ; 37 import javax.swing.SwingUtilities ; 38 import org.netbeans.editor.LocaleSupport; 39 import org.netbeans.modules.editor.hints.HintsUI; 40 import org.netbeans.spi.editor.hints.Fix; 41 import org.netbeans.spi.editor.hints.LazyFixList; 42 import org.openide.awt.HtmlRenderer; 43 44 48 49 public class ListCompletionView extends JList implements ListCellRenderer { 50 51 private final HtmlRenderer.Renderer defaultRenderer = HtmlRenderer.createRenderer(); 52 private Font font; 53 private Icon icon = new ImageIcon (org.openide.util.Utilities.loadImage("org/netbeans/modules/editor/hints/resources/suggestion.gif")); 55 public ListCompletionView() { 56 setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 57 font = getFont(); 58 if (font.getSize() < 15 ) { 59 font = font.deriveFont(font.getSize2D() + 1); 60 } 61 62 setFont( font ); 63 setCellRenderer( this ); 64 setBorder( BorderFactory.createEmptyBorder() ); 65 getAccessibleContext().setAccessibleName(LocaleSupport.getString("ACSN_CompletionView")); 66 getAccessibleContext().setAccessibleDescription(LocaleSupport.getString("ACSD_CompletionView")); 67 } 68 69 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 70 Component c = defaultRenderer.getListCellRendererComponent( list, value instanceof Fix ? ((Fix) value).getText() : value.toString(), index, isSelected, cellHasFocus ); 71 defaultRenderer.setIcon( icon ); 72 defaultRenderer.setParentFocused(true); 73 defaultRenderer.setRenderStyle(HtmlRenderer.STYLE_CLIP); 74 c.setBackground(list.getBackground()); 75 return c; 76 } 77 78 public void setResult(LazyFixList data) { 79 if (data != null) { 80 Model model = new Model (data); 81 82 setModel(model); 83 if (model.fixes != null && !model.fixes.isEmpty()) { 84 setSelectedIndex(0); 85 } 86 } 87 } 88 89 90 public Dimension getPreferredScrollableViewportSize() { 91 return getPreferredSize(); 92 } 93 94 public void up() { 95 if (getModel().getSize() > 0) { 96 setSelectedIndex(Math.max(getSelectedIndex() - 1, 0)); 97 ensureIndexIsVisible(getSelectedIndex()); 98 repaint(); 99 } 100 } 101 102 public void down() { 103 int lastInd = getModel().getSize() - 1; 104 if (lastInd >= 0) { 105 setSelectedIndex(Math.min(getSelectedIndex() + 1, lastInd)); 106 ensureIndexIsVisible(getSelectedIndex()); 107 validate(); 108 } 109 } 110 111 public void pageUp() { 112 if (getModel().getSize() > 0) { 113 int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0); 114 int ind = Math.max(getSelectedIndex() - pageSize, 0); 115 116 setSelectedIndex(ind); 117 ensureIndexIsVisible(ind); 118 } 119 } 120 121 public void pageDown() { 122 int lastInd = getModel().getSize() - 1; 123 if (lastInd >= 0) { 124 int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0); 125 int ind = Math.min(getSelectedIndex() + pageSize, lastInd); 126 127 setSelectedIndex(ind); 128 ensureIndexIsVisible(ind); 129 } 130 } 131 132 public void begin() { 133 if (getModel().getSize() > 0) { 134 setSelectedIndex(0); 135 ensureIndexIsVisible(0); 136 } 137 } 138 139 public void end() { 140 int lastInd = getModel().getSize() - 1; 141 if (lastInd >= 0) { 142 setSelectedIndex(lastInd); 143 ensureIndexIsVisible(lastInd); 144 } 145 } 146 147 static class Model extends AbstractListModel implements PropertyChangeListener { 148 149 private LazyFixList data; 150 private List <Fix> fixes; 151 private boolean computed; 152 153 154 static final long serialVersionUID = 3292276783870598274L; 155 156 public Model(LazyFixList data) { 157 this.data = data; 158 data.addPropertyChangeListener(this); 159 update(); 160 } 161 162 private synchronized void update() { 163 computed = data.isComputed(); 164 if (computed) 165 fixes = new ArrayList <Fix>(new LinkedHashSet <Fix>(data.getFixes())); 166 else 167 data.getFixes(); 168 } 169 170 public synchronized int getSize() { 171 return computed ? fixes.size() : 1; 172 } 173 174 public synchronized Object getElementAt(int index) { 175 if (!computed) { 176 return "computing..."; 177 } else { 178 return fixes.get(index); 179 } 180 } 181 182 public void propertyChange(PropertyChangeEvent evt) { 183 SwingUtilities.invokeLater(new Runnable () { 185 public void run() { 186 HintsUI.getDefault().removePopups(); 187 HintsUI.getDefault().showPopup(); 188 } 189 }); 190 } 191 192 } 193 194 } 195 | Popular Tags |