001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.registry;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.osgi.util.TextProcessor;
018: import org.eclipse.ui.IEditorDescriptor;
019: import org.eclipse.ui.IFileEditorMapping;
020: import org.eclipse.ui.ISharedImages;
021: import org.eclipse.ui.internal.WorkbenchImages;
022:
023: /**
024: * Implementation of IFileEditorMapping.
025: */
026: public class FileEditorMapping extends Object implements
027: IFileEditorMapping, Cloneable {
028:
029: private static final String STAR = "*"; //$NON-NLS-1$
030: private static final String DOT = "."; //$NON-NLS-1$
031:
032: private String name = STAR;
033:
034: private String extension;
035:
036: // Collection of EditorDescriptor, where the first one
037: // if considered the default one.
038: private List editors = new ArrayList(1);
039:
040: private List deletedEditors = new ArrayList(1);
041:
042: private List declaredDefaultEditors = new ArrayList(1);
043:
044: /**
045: * Create an instance of this class.
046: *
047: * @param extension java.lang.String
048: */
049: public FileEditorMapping(String extension) {
050: this (STAR, extension);
051: }
052:
053: /**
054: * Create an instance of this class.
055: *
056: * @param name java.lang.String
057: * @param extension java.lang.String
058: */
059: public FileEditorMapping(String name, String extension) {
060: super ();
061: if (name == null || name.length() < 1) {
062: setName(STAR);
063: } else {
064: setName(name);
065: }
066: if (extension == null) {
067: setExtension("");//$NON-NLS-1$
068: } else {
069: setExtension(extension);
070: }
071: }
072:
073: /**
074: * Add the given editor to the list of editors registered.
075: *
076: * @param editor the editor to add
077: */
078: public void addEditor(EditorDescriptor editor) {
079: editors.add(editor);
080: deletedEditors.remove(editor);
081: }
082:
083: /**
084: * Clone the receiver.
085: */
086: public Object clone() {
087: try {
088: FileEditorMapping clone = (FileEditorMapping) super .clone();
089: clone.editors = (List) ((ArrayList) editors).clone();
090: return clone;
091: } catch (CloneNotSupportedException e) {
092: return null;
093: }
094: }
095:
096: /* (non-Javadoc)
097: * @see java.lang.Object#equals(java.lang.Object)
098: */
099: public boolean equals(Object obj) {
100: if (this == obj) {
101: return true;
102: }
103: if (!(obj instanceof FileEditorMapping)) {
104: return false;
105: }
106: FileEditorMapping mapping = (FileEditorMapping) obj;
107: if (!this .name.equals(mapping.name)) {
108: return false;
109: }
110: if (!this .extension.equals(mapping.extension)) {
111: return false;
112: }
113:
114: if (!compareList(this .editors, mapping.editors)) {
115: return false;
116: }
117: return compareList(this .deletedEditors, mapping.deletedEditors);
118: }
119:
120: /**
121: * Compare the editor ids from both lists and return true if they
122: * are equals.
123: */
124: private boolean compareList(List l1, List l2) {
125: if (l1.size() != l2.size()) {
126: return false;
127: }
128:
129: Iterator i1 = l1.iterator();
130: Iterator i2 = l2.iterator();
131: while (i1.hasNext() && i2.hasNext()) {
132: Object o1 = i1.next();
133: Object o2 = i2.next();
134: if (!(o1 == null ? o2 == null : o1.equals(o2))) {
135: return false;
136: }
137: }
138: return true;
139: }
140:
141: /* (non-Javadoc)
142: * Method declared on IFileEditorMapping.
143: */
144: public IEditorDescriptor getDefaultEditor() {
145:
146: if (editors.size() == 0) {
147: return null;
148: }
149:
150: return (IEditorDescriptor) editors.get(0);
151: }
152:
153: /* (non-Javadoc)
154: * Method declared on IFileEditorMapping.
155: */
156: public IEditorDescriptor[] getEditors() {
157: return (IEditorDescriptor[]) editors
158: .toArray(new IEditorDescriptor[editors.size()]);
159: }
160:
161: /* (non-Javadoc)
162: * Method declared on IFileEditorMapping.
163: */
164: public IEditorDescriptor[] getDeletedEditors() {
165: IEditorDescriptor[] array = new IEditorDescriptor[deletedEditors
166: .size()];
167: deletedEditors.toArray(array);
168: return array;
169: }
170:
171: /* (non-Javadoc)
172: * Method declared on IFileEditorMapping.
173: */
174: public String getExtension() {
175: return extension;
176: }
177:
178: /* (non-Javadoc)
179: * Method declared on IFileEditorMapping.
180: */
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: /* (non-Javadoc)
191: * Method declared on IFileEditorMapping.
192: */
193: public String getLabel() {
194: return TextProcessor
195: .process(
196: name
197: + (extension.length() == 0 ? "" : DOT + extension), STAR + DOT); //$NON-NLS-1$
198: }
199:
200: /* (non-Javadoc)
201: * Method declared on IFileEditorMapping.
202: */
203: public String getName() {
204: return name;
205: }
206:
207: /**
208: * Remove the given editor from the set of editors registered.
209: *
210: * @param editor the editor to remove
211: */
212: public void removeEditor(EditorDescriptor editor) {
213: editors.remove(editor);
214: deletedEditors.add(editor);
215: declaredDefaultEditors.remove(editor);
216: }
217:
218: /**
219: * Set the default editor registered for file type
220: * described by this mapping.
221: *
222: * @param editor the editor to be set as default
223: */
224: public void setDefaultEditor(EditorDescriptor editor) {
225: editors.remove(editor);
226: editors.add(0, editor);
227: declaredDefaultEditors.remove(editor);
228: declaredDefaultEditors.add(0, editor);
229: }
230:
231: /**
232: * Set the collection of all editors (EditorDescriptor)
233: * registered for the file type described by this mapping.
234: * Typically an editor is registered either through a plugin or explicitly by
235: * the user modifying the associations in the preference pages.
236: * This modifies the internal list to share the passed list.
237: * (hence the clear indication of list in the method name)
238: *
239: * @param newEditors the new list of associated editors
240: */
241: public void setEditorsList(List newEditors) {
242: editors = newEditors;
243: declaredDefaultEditors.retainAll(newEditors);
244: }
245:
246: /**
247: * Set the collection of all editors (EditorDescriptor)
248: * formally registered for the file type described by this mapping
249: * which have been deleted by the user.
250: * This modifies the internal list to share the passed list.
251: * (hence the clear indication of list in the method name)
252: *
253: * @param newDeletedEditors the new list of associated (but deleted) editors
254: */
255: public void setDeletedEditorsList(List newDeletedEditors) {
256: deletedEditors = newDeletedEditors;
257: }
258:
259: /**
260: * Set the file's extension.
261: *
262: * @param extension the file extension for this mapping
263: */
264: public void setExtension(String extension) {
265: this .extension = extension;
266: }
267:
268: /**
269: * Set the file's name.
270: *
271: * @param name the file name for this mapping
272: */
273: public void setName(String name) {
274: this .name = name;
275: }
276:
277: /**
278: * Get the editors that have been declared as default. This may be via plugin
279: * declarations or the preference page.
280: *
281: * @return the editors the default editors
282: * @since 3.1
283: */
284: public IEditorDescriptor[] getDeclaredDefaultEditors() {
285: return (IEditorDescriptor[]) declaredDefaultEditors
286: .toArray(new IEditorDescriptor[declaredDefaultEditors
287: .size()]);
288: }
289:
290: /**
291: * Return whether the editor is declared default.
292: *
293: * @param editor the editor to test
294: * @return whether the editor is declared default
295: * @since 3.1
296: */
297: public boolean isDeclaredDefaultEditor(IEditorDescriptor editor) {
298: return declaredDefaultEditors.contains(editor);
299: }
300:
301: /**
302: * Set the default editors for this mapping.
303: *
304: * @param defaultEditors the editors
305: * @since 3.1
306: */
307: public void setDefaultEditors(List defaultEditors) {
308: declaredDefaultEditors = defaultEditors;
309: }
310: }
|