KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > FileEditorMapping


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
25  * Implementation of IFileEditorMapping.
26  */

27 public class FileEditorMapping extends Object JavaDoc implements IFileEditorMapping,
28         Cloneable JavaDoc {
29     
30     private static final String JavaDoc STAR = "*"; //$NON-NLS-1$
31
private static final String JavaDoc DOT = "."; //$NON-NLS-1$
32

33     private String JavaDoc name = STAR;
34
35     private String JavaDoc extension;
36
37     // Collection of EditorDescriptor, where the first one
38
// if considered the default one.
39
private List JavaDoc editors = new ArrayList JavaDoc(1);
40
41     private List JavaDoc deletedEditors = new ArrayList JavaDoc(1);
42
43     private List JavaDoc declaredDefaultEditors = new ArrayList JavaDoc(1);
44
45     /**
46      * Create an instance of this class.
47      *
48      * @param extension java.lang.String
49      */

50     public FileEditorMapping(String JavaDoc extension) {
51         this(STAR, extension);
52     }
53
54     /**
55      * Create an instance of this class.
56      *
57      * @param name java.lang.String
58      * @param extension java.lang.String
59      */

60     public FileEditorMapping(String JavaDoc name, String JavaDoc 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("");//$NON-NLS-1$
69
} else {
70             setExtension(extension);
71         }
72     }
73
74     /**
75      * Add the given editor to the list of editors registered.
76      *
77      * @param editor the editor to add
78      */

79     public void addEditor(EditorDescriptor editor) {
80         editors.add(editor);
81         deletedEditors.remove(editor);
82     }
83
84     /**
85      * Clone the receiver.
86      */

87     public Object JavaDoc clone() {
88         try {
89             FileEditorMapping clone = (FileEditorMapping) super.clone();
90             clone.editors = (List JavaDoc) ((ArrayList JavaDoc) editors).clone();
91             return clone;
92         } catch (CloneNotSupportedException JavaDoc e) {
93             return null;
94         }
95     }
96     
97     /* (non-Javadoc)
98      * @see java.lang.Object#equals(java.lang.Object)
99      */

100     public boolean equals(Object JavaDoc 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     /**
122      * Compare the editor ids from both lists and return true if they
123      * are equals.
124      */

125     private boolean compareList(List JavaDoc l1, List JavaDoc l2) {
126         if (l1.size() != l2.size()) {
127             return false;
128         }
129
130         Iterator JavaDoc i1 = l1.iterator();
131         Iterator JavaDoc i2 = l2.iterator();
132         while (i1.hasNext() && i2.hasNext()) {
133             Object JavaDoc o1 = i1.next();
134             Object JavaDoc o2 = i2.next();
135             if (!(o1 == null ? o2 == null : o1.equals(o2))) {
136                 return false;
137             }
138         }
139         return true;
140     }
141
142     /* (non-Javadoc)
143      * Method declared on IFileEditorMapping.
144      */

145     public IEditorDescriptor getDefaultEditor() {
146
147         if (editors.size() == 0) {
148             return null;
149         }
150         
151         return (IEditorDescriptor) editors.get(0);
152     }
153
154     /* (non-Javadoc)
155      * Method declared on IFileEditorMapping.
156      */

157     public IEditorDescriptor[] getEditors() {
158         return (IEditorDescriptor[]) editors
159                 .toArray(new IEditorDescriptor[editors.size()]);
160     }
161
162     /* (non-Javadoc)
163      * Method declared on IFileEditorMapping.
164      */

165     public IEditorDescriptor[] getDeletedEditors() {
166         IEditorDescriptor[] array = new IEditorDescriptor[deletedEditors.size()];
167         deletedEditors.toArray(array);
168         return array;
169     }
170
171     /* (non-Javadoc)
172      * Method declared on IFileEditorMapping.
173      */

174     public String JavaDoc 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 JavaDoc getLabel() {
194         return TextProcessor.process(name + (extension.length() == 0 ? "" : DOT + extension), STAR + DOT); //$NON-NLS-1$
195
}
196
197     /* (non-Javadoc)
198      * Method declared on IFileEditorMapping.
199      */

200     public String JavaDoc getName() {
201         return name;
202     }
203
204     /**
205      * Remove the given editor from the set of editors registered.
206      *
207      * @param editor the editor to remove
208      */

209     public void removeEditor(EditorDescriptor editor) {
210         editors.remove(editor);
211         deletedEditors.add(editor);
212         declaredDefaultEditors.remove(editor);
213     }
214
215     /**
216      * Set the default editor registered for file type
217      * described by this mapping.
218      *
219      * @param editor the editor to be set as default
220      */

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     /**
229      * Set the collection of all editors (EditorDescriptor)
230      * registered for the file type described by this mapping.
231      * Typically an editor is registered either through a plugin or explicitly by
232      * the user modifying the associations in the preference pages.
233      * This modifies the internal list to share the passed list.
234      * (hence the clear indication of list in the method name)
235      *
236      * @param newEditors the new list of associated editors
237      */

238     public void setEditorsList(List JavaDoc newEditors) {
239         editors = newEditors;
240         declaredDefaultEditors.retainAll(newEditors);
241     }
242
243     /**
244      * Set the collection of all editors (EditorDescriptor)
245      * formally registered for the file type described by this mapping
246      * which have been deleted by the user.
247      * This modifies the internal list to share the passed list.
248      * (hence the clear indication of list in the method name)
249      *
250      * @param newDeletedEditors the new list of associated (but deleted) editors
251      */

252     public void setDeletedEditorsList(List JavaDoc newDeletedEditors) {
253         deletedEditors = newDeletedEditors;
254     }
255
256     /**
257      * Set the file's extension.
258      *
259      * @param extension the file extension for this mapping
260      */

261     public void setExtension(String JavaDoc extension) {
262         this.extension = extension;
263     }
264
265     /**
266      * Set the file's name.
267      *
268      * @param name the file name for this mapping
269      */

270     public void setName(String JavaDoc name) {
271         this.name = name;
272     }
273
274     /**
275      * Get the editors that have been declared as default. This may be via plugin
276      * declarations or the preference page.
277      *
278      * @return the editors the default editors
279      * @since 3.1
280      */

281     public IEditorDescriptor [] getDeclaredDefaultEditors() {
282         return (IEditorDescriptor []) declaredDefaultEditors.
283                 toArray(new IEditorDescriptor[declaredDefaultEditors.size()]);
284     }
285     
286     /**
287      * Return whether the editor is declared default.
288      *
289      * @param editor the editor to test
290      * @return whether the editor is declared default
291      * @since 3.1
292      */

293     public boolean isDeclaredDefaultEditor (IEditorDescriptor editor) {
294         return declaredDefaultEditors.contains(editor);
295     }
296
297     /**
298      * Set the default editors for this mapping.
299      *
300      * @param defaultEditors the editors
301      * @since 3.1
302      */

303     public void setDefaultEditors(List JavaDoc defaultEditors) {
304         declaredDefaultEditors = defaultEditors;
305     }
306 }
307
Popular Tags