1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.osgi.util.TextProcessor; 19 import org.eclipse.ui.IEditorDescriptor; 20 import org.eclipse.ui.IFileEditorMapping; 21 import org.eclipse.ui.ISharedImages; 22 import org.eclipse.ui.internal.WorkbenchImages; 23 24 27 public class FileEditorMapping extends Object implements IFileEditorMapping, 28 Cloneable { 29 30 private static final String STAR = "*"; private static final String DOT = "."; 33 private String name = STAR; 34 35 private String extension; 36 37 private List editors = new ArrayList (1); 40 41 private List deletedEditors = new ArrayList (1); 42 43 private List declaredDefaultEditors = new ArrayList (1); 44 45 50 public FileEditorMapping(String extension) { 51 this(STAR, extension); 52 } 53 54 60 public FileEditorMapping(String name, String extension) { 61 super(); 62 if (name == null || name.length() < 1) { 63 setName(STAR); 64 } else { 65 setName(name); 66 } 67 if (extension == null) { 68 setExtension(""); } else { 70 setExtension(extension); 71 } 72 } 73 74 79 public void addEditor(EditorDescriptor editor) { 80 editors.add(editor); 81 deletedEditors.remove(editor); 82 } 83 84 87 public Object clone() { 88 try { 89 FileEditorMapping clone = (FileEditorMapping) super.clone(); 90 clone.editors = (List ) ((ArrayList ) editors).clone(); 91 return clone; 92 } catch (CloneNotSupportedException e) { 93 return null; 94 } 95 } 96 97 100 public boolean equals(Object obj) { 101 if (this == obj) { 102 return true; 103 } 104 if (!(obj instanceof FileEditorMapping)) { 105 return false; 106 } 107 FileEditorMapping mapping = (FileEditorMapping) obj; 108 if (!this.name.equals(mapping.name)) { 109 return false; 110 } 111 if (!this.extension.equals(mapping.extension)) { 112 return false; 113 } 114 115 if (!compareList(this.editors, mapping.editors)) { 116 return false; 117 } 118 return compareList(this.deletedEditors, mapping.deletedEditors); 119 } 120 121 125 private boolean compareList(List l1, List l2) { 126 if (l1.size() != l2.size()) { 127 return false; 128 } 129 130 Iterator i1 = l1.iterator(); 131 Iterator i2 = l2.iterator(); 132 while (i1.hasNext() && i2.hasNext()) { 133 Object o1 = i1.next(); 134 Object o2 = i2.next(); 135 if (!(o1 == null ? o2 == null : o1.equals(o2))) { 136 return false; 137 } 138 } 139 return true; 140 } 141 142 145 public IEditorDescriptor getDefaultEditor() { 146 147 if (editors.size() == 0) { 148 return null; 149 } 150 151 return (IEditorDescriptor) editors.get(0); 152 } 153 154 157 public IEditorDescriptor[] getEditors() { 158 return (IEditorDescriptor[]) editors 159 .toArray(new IEditorDescriptor[editors.size()]); 160 } 161 162 165 public IEditorDescriptor[] getDeletedEditors() { 166 IEditorDescriptor[] array = new IEditorDescriptor[deletedEditors.size()]; 167 deletedEditors.toArray(array); 168 return array; 169 } 170 171 174 public String getExtension() { 175 return extension; 176 } 177 178 181 public ImageDescriptor getImageDescriptor() { 182 IEditorDescriptor editor = getDefaultEditor(); 183 if (editor == null) { 184 return WorkbenchImages 185 .getImageDescriptor(ISharedImages.IMG_OBJ_FILE); 186 } 187 return editor.getImageDescriptor(); 188 } 189 190 193 public String getLabel() { 194 return TextProcessor.process(name + (extension.length() == 0 ? "" : DOT + extension), STAR + DOT); } 196 197 200 public String getName() { 201 return name; 202 } 203 204 209 public void removeEditor(EditorDescriptor editor) { 210 editors.remove(editor); 211 deletedEditors.add(editor); 212 declaredDefaultEditors.remove(editor); 213 } 214 215 221 public void setDefaultEditor(EditorDescriptor editor) { 222 editors.remove(editor); 223 editors.add(0, editor); 224 declaredDefaultEditors.remove(editor); 225 declaredDefaultEditors.add(0, editor); 226 } 227 228 238 public void setEditorsList(List newEditors) { 239 editors = newEditors; 240 declaredDefaultEditors.retainAll(newEditors); 241 } 242 243 252 public void setDeletedEditorsList(List newDeletedEditors) { 253 deletedEditors = newDeletedEditors; 254 } 255 256 261 public void setExtension(String extension) { 262 this.extension = extension; 263 } 264 265 270 public void setName(String name) { 271 this.name = name; 272 } 273 274 281 public IEditorDescriptor [] getDeclaredDefaultEditors() { 282 return (IEditorDescriptor []) declaredDefaultEditors. 283 toArray(new IEditorDescriptor[declaredDefaultEditors.size()]); 284 } 285 286 293 public boolean isDeclaredDefaultEditor (IEditorDescriptor editor) { 294 return declaredDefaultEditors.contains(editor); 295 } 296 297 303 public void setDefaultEditors(List defaultEditors) { 304 declaredDefaultEditors = defaultEditors; 305 } 306 } 307 | Popular Tags |