1 19 20 package org.netbeans.editor.ext; 21 22 import java.awt.Dimension ; 23 import java.util.Collections ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.ResourceBundle ; 27 import javax.swing.JList ; 28 import javax.swing.ListCellRenderer ; 29 import javax.swing.AbstractListModel ; 30 import org.openide.util.NbBundle; 31 32 33 40 41 public class ListCompletionView extends JList implements CompletionView { 42 43 ListCellRenderer renderer; 44 ListCellRenderer defaultRenderer; 45 46 public ListCompletionView() { 47 this(null); 48 } 49 50 public ListCompletionView(ListCellRenderer renderer) { 51 setSelectionMode( javax.swing.ListSelectionModel.SINGLE_SELECTION ); 52 this.renderer = renderer; 53 defaultRenderer = getCellRenderer(); 54 59 ResourceBundle bundle = NbBundle.getBundle(org.netbeans.editor.BaseKit.class); 60 getAccessibleContext().setAccessibleName(bundle.getString("ACSN_CompletionView")); 61 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CompletionView")); 62 63 64 } 65 66 67 public void setResult(CompletionQuery.Result result) { 68 if (result != null) { 69 setResult(result.getData()); 70 } else { 71 setResult(Collections.EMPTY_LIST); 72 } 73 } 74 75 public void setResult(List data) { 76 if (data != null) { 77 if (data.size() == 0) { 78 setCellRenderer(defaultRenderer); 79 data = new LinkedList (); 80 setModel(new Model (data)); 82 clearSelection(); 83 } else { 84 if (renderer != null) { 85 setCellRenderer(renderer); 86 } 87 setModel(new Model (data)); 88 setSelectedIndex(0); 89 } 90 } 91 } 92 93 public void displayWaitStatus() { 94 if (getCellRenderer() == defaultRenderer) { 95 List data = new LinkedList (); 96 data.add(NbBundle.getBundle(org.netbeans.editor.BaseKit.class). 97 getString("please-wait")); setModel(new Model (data)); 99 clearSelection(); 100 } 101 } 102 103 public boolean showingData() { 104 return getCellRenderer() == renderer; 105 } 106 107 108 public Dimension getPreferredScrollableViewportSize() { 109 return getPreferredSize(); 110 } 111 112 public void up() { 113 if (getModel().getSize() > 0) { 114 setSelectedIndex(getSelectedIndex() - 1); 115 ensureIndexIsVisible(getSelectedIndex()); 116 } 117 } 118 119 public void down() { 120 int lastInd = getModel().getSize() - 1; 121 if (lastInd >= 0) { 122 setSelectedIndex(Math.min(getSelectedIndex() + 1, lastInd)); 123 ensureIndexIsVisible(getSelectedIndex()); 124 } 125 } 126 127 public void pageUp() { 128 if (getModel().getSize() > 0) { 129 int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0); 130 int ind = Math.max(getSelectedIndex() - pageSize, 0); 131 132 setSelectedIndex(ind); 133 ensureIndexIsVisible(ind); 134 } 135 } 136 137 public void pageDown() { 138 int lastInd = getModel().getSize() - 1; 139 if (lastInd >= 0) { 140 int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0); 141 int ind = Math.min(getSelectedIndex() + pageSize, lastInd); 142 143 setSelectedIndex(ind); 144 ensureIndexIsVisible(ind); 145 } 146 } 147 148 public void begin() { 149 if (getModel().getSize() > 0) { 150 setSelectedIndex(0); 151 ensureIndexIsVisible(0); 152 } 153 } 154 155 public void end() { 156 int lastInd = getModel().getSize() - 1; 157 if (lastInd >= 0) { 158 setSelectedIndex(lastInd); 159 ensureIndexIsVisible(lastInd); 160 } 161 } 162 163 public void setVisible(boolean visible) { 164 super.setVisible(visible); 167 } 168 169 static class Model extends AbstractListModel { 170 171 List data; 172 173 static final long serialVersionUID = 3292276783870598274L; 174 175 public Model(List data) { 176 this.data = data; 177 } 178 179 public int getSize() { 180 return data.size(); 181 } 182 183 public Object getElementAt(int index) { 184 return (index >= 0 && index < data.size()) ? data.get(index) : null; 185 } 186 187 List getData() { 188 return data; 189 } 190 191 } 192 193 } 194 | Popular Tags |