1 11 package org.eclipse.jface.viewers; 12 13 import java.util.Arrays ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.core.runtime.Assert; 19 20 27 public class StructuredSelection implements IStructuredSelection { 28 29 32 private Object [] elements; 33 34 38 public static final StructuredSelection EMPTY = new StructuredSelection(); 39 40 46 public StructuredSelection() { 47 } 48 49 54 public StructuredSelection(Object [] elements) { 55 this.elements = new Object [elements.length]; 56 System.arraycopy(elements, 0, this.elements, 0, elements.length); 57 } 58 59 65 public StructuredSelection(Object element) { 66 Assert.isNotNull(element); 67 elements = new Object [] { element }; 68 } 69 70 74 public StructuredSelection(List elements) { 75 Assert.isNotNull(elements); 76 this.elements = elements.toArray(); 77 } 78 79 87 public boolean equals(Object o) { 88 if (this == o) { 89 return true; 90 } 91 if (!(o instanceof StructuredSelection)) { 93 return false; 94 } 95 StructuredSelection s2 = (StructuredSelection) o; 96 97 if (isEmpty()) { 99 return s2.isEmpty(); 100 } 101 if (s2.isEmpty()) { 102 return false; 103 } 104 105 int myLen = elements.length; 107 if (myLen != s2.elements.length) { 108 return false; 109 } 110 for (int i = 0; i < myLen; i++) { 112 if (!elements[i].equals(s2.elements[i])) { 113 return false; 114 } 115 } 116 return true; 117 } 118 119 122 public Object getFirstElement() { 123 return isEmpty() ? null : elements[0]; 124 } 125 126 129 public boolean isEmpty() { 130 return elements == null || elements.length == 0; 131 } 132 133 136 public Iterator iterator() { 137 return Arrays.asList(elements == null ? new Object [0] : elements) 138 .iterator(); 139 } 140 141 144 public int size() { 145 return elements == null ? 0 : elements.length; 146 } 147 148 151 public Object [] toArray() { 152 return elements == null ? new Object [0] : (Object []) elements.clone(); 153 } 154 155 158 public List toList() { 159 return Arrays.asList(elements == null ? new Object [0] : elements); 160 } 161 162 168 public String toString() { 169 return isEmpty() ? JFaceResources.getString("<empty_selection>") : toList().toString(); } 171 } 172 | Popular Tags |