1 13 14 package org.eclipse.jface.viewers; 15 16 import java.util.LinkedList ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.swt.graphics.Color; 20 import org.eclipse.swt.graphics.Font; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Tree; 25 import org.eclipse.swt.widgets.TreeItem; 26 import org.eclipse.swt.widgets.Widget; 27 28 33 public class TreeViewerRow extends ViewerRow { 34 private TreeItem item; 35 36 40 TreeViewerRow(TreeItem item) { 41 this.item = item; 42 } 43 44 47 public Rectangle getBounds(int columnIndex) { 48 return item.getBounds(columnIndex); 49 } 50 51 54 public Rectangle getBounds() { 55 return item.getBounds(); 56 } 57 58 61 public int getColumnCount() { 62 return item.getParent().getColumnCount(); 63 } 64 65 68 public Widget getItem() { 69 return item; 70 } 71 72 75 public Color getBackground(int columnIndex) { 76 return item.getBackground(columnIndex); 77 } 78 79 82 public Font getFont(int columnIndex) { 83 return item.getFont(columnIndex); 84 } 85 86 89 public Color getForeground(int columnIndex) { 90 return item.getForeground(columnIndex); 91 } 92 93 96 public Image getImage(int columnIndex) { 97 return item.getImage(columnIndex); 98 } 99 100 103 public String getText(int columnIndex) { 104 return item.getText(columnIndex); 105 } 106 107 110 public void setBackground(int columnIndex, Color color) { 111 item.setBackground(columnIndex, color); 112 } 113 114 117 public void setFont(int columnIndex, Font font) { 118 item.setFont(columnIndex, font); 119 } 120 121 124 public void setForeground(int columnIndex, Color color) { 125 item.setForeground(columnIndex, color); 126 } 127 128 131 public void setImage(int columnIndex, Image image) { 132 Image oldImage = item.getImage(columnIndex); 133 if (image != oldImage) { 134 item.setImage(columnIndex, image); 135 } 136 } 137 138 141 public void setText(int columnIndex, String text) { 142 item.setText(columnIndex, text == null ? "" : text); } 144 145 148 public Control getControl() { 149 return item.getParent(); 150 } 151 152 153 public ViewerRow getNeighbor(int direction, boolean sameLevel) { 154 if( direction == ViewerRow.ABOVE ) { 155 return getRowAbove(sameLevel); 156 } else if( direction == ViewerRow.BELOW ) { 157 return getRowBelow(sameLevel); 158 } else { 159 throw new IllegalArgumentException ("Illegal value of direction argument."); } 161 } 162 163 private ViewerRow getRowBelow(boolean sameLevel) { 164 Tree tree = item.getParent(); 165 166 if( item.getParentItem() == null ) { 168 if( sameLevel || ! item.getExpanded() ) { 169 int index = tree.indexOf(item) + 1; 170 171 if( index < tree.getItemCount() ) { 172 return new TreeViewerRow(tree.getItem(index)); 173 } 174 } else if( item.getExpanded() && item.getItemCount() > 0 ) { 175 return new TreeViewerRow(item.getItem(0)); 176 } 177 } else { 178 if( sameLevel || ! item.getExpanded() ) { 179 TreeItem parentItem = item.getParentItem(); 180 181 int nextIndex = parentItem.indexOf(item) + 1; 182 int totalIndex = parentItem.getItemCount(); 183 184 TreeItem itemAfter; 185 186 if( nextIndex == totalIndex ) { 188 itemAfter = findNextItem( parentItem ); 189 } else { 190 itemAfter = parentItem.getItem(nextIndex); 191 } 192 193 if( itemAfter != null ) { 194 return new TreeViewerRow(itemAfter); 195 } 196 197 } else if( item.getExpanded() && item.getItemCount() > 0 ) { 198 return new TreeViewerRow(item.getItem(0)); 199 } 200 } 201 202 return null; 203 } 204 205 private ViewerRow getRowAbove(boolean sameLevel) { 206 Tree tree = item.getParent(); 207 208 if( item.getParentItem() == null ) { 210 int index = tree.indexOf(item) - 1; 211 TreeItem nextTopItem = null; 212 213 if( index >= 0 ) { 214 nextTopItem = tree.getItem(index); 215 } 216 217 if( nextTopItem != null ) { 218 if( sameLevel ) { 219 return new TreeViewerRow(nextTopItem); 220 } 221 222 return new TreeViewerRow(findLastVisibleItem(nextTopItem)); 223 } 224 } else { 225 TreeItem parentItem = item.getParentItem(); 226 int previousIndex = parentItem.indexOf(item) - 1; 227 228 TreeItem itemBefore; 229 if( previousIndex >= 0 ) { 230 if( sameLevel ) { 231 itemBefore = parentItem.getItem(previousIndex); 232 } else { 233 itemBefore = findLastVisibleItem(parentItem.getItem(previousIndex)); 234 } 235 } else { 236 itemBefore = parentItem; 237 } 238 239 if( itemBefore != null ) { 240 return new TreeViewerRow(itemBefore); 241 } 242 } 243 244 return null; 245 } 246 247 private TreeItem findLastVisibleItem(TreeItem parentItem) { 248 TreeItem rv = parentItem; 249 250 while( rv.getExpanded() && rv.getItemCount() > 0 ) { 251 rv = rv.getItem(rv.getItemCount()-1); 252 } 253 254 return rv; 255 } 256 257 private TreeItem findNextItem(TreeItem item) { 258 TreeItem rv = null; 259 Tree tree = item.getParent(); 260 TreeItem parentItem = item.getParentItem(); 261 262 int nextIndex; 263 int totalItems; 264 265 if( parentItem == null ) { 266 nextIndex = tree.indexOf(item) + 1; 267 totalItems = tree.getItemCount(); 268 } else { 269 nextIndex = parentItem.indexOf(item) + 1; 270 totalItems = parentItem.getItemCount(); 271 } 272 273 if( nextIndex == totalItems ) { 276 if( item.getParentItem() != null ) { 277 rv = findNextItem(item.getParentItem()); 278 } 279 } else { 280 if( parentItem == null ) { 281 rv = tree.getItem(nextIndex); 282 } else { 283 rv = parentItem.getItem(nextIndex); 284 } 285 } 286 287 return rv; 288 } 289 290 public TreePath getTreePath() { 291 TreeItem tItem = item; 292 LinkedList segments = new LinkedList (); 293 while (tItem != null) { 294 Object segment = tItem.getData(); 295 Assert.isNotNull(segment); 296 segments.addFirst(segment); 297 tItem = tItem.getParentItem(); 298 } 299 300 return new TreePath(segments.toArray()); 301 } 302 303 void setItem(TreeItem item) { 304 this.item = item; 305 } 306 307 public Object clone() { 308 return new TreeViewerRow(item); 309 } 310 311 public Object getElement() { 312 return item.getData(); 313 } 314 } 315 | Popular Tags |