1 13 14 package org.eclipse.jface.viewers; 15 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 24 38 public class ListViewer extends AbstractListViewer { 39 40 43 private org.eclipse.swt.widgets.List list; 44 45 53 public ListViewer(Composite parent) { 54 this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 55 } 56 57 66 public ListViewer(Composite parent, int style) { 67 this(new org.eclipse.swt.widgets.List(parent, style)); 68 } 69 70 77 public ListViewer(org.eclipse.swt.widgets.List list) { 78 this.list = list; 79 hookControl(list); 80 } 81 82 85 public Control getControl() { 86 return list; 87 } 88 89 94 public org.eclipse.swt.widgets.List getList() { 95 return list; 96 } 97 98 102 public void reveal(Object element) { 103 Assert.isNotNull(element); 104 int index = getElementIndex(element); 105 if (index == -1) { 106 return; 107 } 108 int count = list.getItemCount(); 110 if (count == 0) { 111 return; 112 } 113 int height = list.getItemHeight(); 114 Rectangle rect = list.getClientArea(); 115 int topIndex = list.getTopIndex(); 116 int visibleCount = Math.max(rect.height / height, 1); 117 int bottomIndex = Math.min(topIndex + visibleCount, count) - 1; 118 if ((topIndex <= index) && (index <= bottomIndex)) { 119 return; 120 } 121 int newTop = Math.min(Math.max(index - (visibleCount / 2), 0), 122 count - 1); 123 list.setTopIndex(newTop); 124 } 125 126 129 protected void listAdd(String string, int index) { 130 list.add(string, index); 131 } 132 133 136 protected void listSetItem(int index, String string) { 137 list.setItem(index, string); 138 } 139 140 143 protected int[] listGetSelectionIndices() { 144 return list.getSelectionIndices(); 145 } 146 147 150 protected int listGetItemCount() { 151 return list.getItemCount(); 152 } 153 154 157 protected void listSetItems(String [] labels) { 158 list.setItems(labels); 159 } 160 161 164 protected void listRemoveAll() { 165 list.removeAll(); 166 } 167 168 171 protected void listRemove(int index) { 172 list.remove(index); 173 } 174 175 178 protected void listSetSelection(int[] ixs) { 179 list.setSelection(ixs); 180 } 181 182 185 protected void listDeselectAll() { 186 list.deselectAll(); 187 } 188 189 192 protected void listShowSelection() { 193 list.showSelection(); 194 } 195 196 199 protected int listGetTopIndex() { 200 return list.getTopIndex(); 201 } 202 203 207 protected void listSetTopIndex(int index) { 208 list.setTopIndex(index); 209 } 210 211 214 protected void setSelectionToWidget(List in, boolean reveal) { 215 if( reveal ) { 216 super.setSelectionToWidget(in, reveal); 217 } else { 218 if (in == null || in.size() == 0) { list.deselectAll(); 220 } else { 221 int n = in.size(); 222 int[] ixs = new int[n]; 223 int count = 0; 224 for (int i = 0; i < n; ++i) { 225 Object el = in.get(i); 226 int ix = getElementIndex(el); 227 if (ix >= 0) { 228 ixs[count++] = ix; 229 } 230 } 231 if (count < n) { 232 System.arraycopy(ixs, 0, ixs = new int[count], 0, count); 233 } 234 list.deselectAll(); 235 list.select(ixs); 236 } 237 } 238 } 239 240 241 } 242 | Popular Tags |