|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Symbian Foundation License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This file is part of com.nokia.tools.variant.preview component. |
|
15 */ |
|
16 |
|
17 package com.nokia.tools.variant.internal.preview; |
|
18 |
|
19 import java.io.File; |
|
20 import java.io.InputStream; |
|
21 |
|
22 import org.eclipse.core.resources.IFile; |
|
23 import org.eclipse.core.resources.IStorage; |
|
24 import org.eclipse.core.runtime.CoreException; |
|
25 import org.eclipse.core.runtime.IAdapterFactory; |
|
26 import org.eclipse.core.runtime.IConfigurationElement; |
|
27 import org.eclipse.core.runtime.IPath; |
|
28 import org.eclipse.core.runtime.Platform; |
|
29 import org.eclipse.core.runtime.content.IContentDescription; |
|
30 import org.eclipse.core.runtime.content.IContentType; |
|
31 import org.eclipse.core.runtime.content.IContentTypeManager; |
|
32 |
|
33 import com.nokia.tools.variant.common.core.utils.PlatformUtil; |
|
34 import com.nokia.tools.variant.preview.IPreviewDescriptor; |
|
35 import com.nokia.tools.variant.preview.IPreviewDescriptorFactory; |
|
36 import com.nokia.tools.variant.preview.UnsupportedContentDescriptor; |
|
37 |
|
38 /** |
|
39 * Adapter factory for handling the preview support for IStorage (and IFile) |
|
40 * element |
|
41 * |
|
42 */ |
|
43 public class IStoragePreviewAdapterFactory implements IAdapterFactory { |
|
44 |
|
45 /** Adapter types */ |
|
46 static final Class<?>[] adapters = { IPreviewDescriptor.class }; |
|
47 |
|
48 @SuppressWarnings("unchecked") |
|
49 public Object getAdapter(Object adaptableObject, Class adapterType) { |
|
50 if (!(adaptableObject instanceof IStorage)) { |
|
51 return null; |
|
52 } |
|
53 if (!adapterType.equals(IPreviewDescriptor.class)) { |
|
54 return null; |
|
55 } |
|
56 IContentTypeManager ctManager = Platform.getContentTypeManager(); |
|
57 IContentDescription descr = null; |
|
58 IContentType sourceCT = null; |
|
59 try { |
|
60 if (adaptableObject instanceof IFile) { |
|
61 descr = ((IFile) adaptableObject).getContentDescription(); |
|
62 } else { |
|
63 IStorage storage = (IStorage) adaptableObject; |
|
64 descr = PlatformUtil.getAdapter(storage, |
|
65 IContentDescription.class, false); |
|
66 if (descr == null) { |
|
67 InputStream contents = storage.getContents(); |
|
68 descr = ctManager.getDescriptionFor(contents, storage |
|
69 .getName(), IContentDescription.ALL); |
|
70 if (contents != null) { |
|
71 contents.close(); |
|
72 } |
|
73 } |
|
74 } |
|
75 } catch (Exception e) { |
|
76 // ignore |
|
77 descr = null; |
|
78 // e.printStackTrace(); |
|
79 } |
|
80 if (descr != null) { |
|
81 sourceCT = descr.getContentType(); |
|
82 } |
|
83 if (sourceCT == null) { |
|
84 if (adaptableObject instanceof IStorage) { |
|
85 IStorage storage = (IStorage) adaptableObject; |
|
86 IPath fullPath = storage.getFullPath(); |
|
87 File f = new File(fullPath.toString()); |
|
88 return new UnsupportedContentDescriptor(f); |
|
89 } |
|
90 } |
|
91 |
|
92 IConfigurationElement[] extensions = Platform.getExtensionRegistry() |
|
93 .getConfigurationElementsFor( |
|
94 "com.nokia.tools.variant.preview.contentpreview"); |
|
95 for (IConfigurationElement el : extensions) { |
|
96 String name = el.getName(); |
|
97 if (!name.equals("descriptorFactory")) { |
|
98 continue; |
|
99 } |
|
100 for (IConfigurationElement ctb : el |
|
101 .getChildren("contentTypeBinding")) { |
|
102 String ctid = ctb.getAttribute("contentTypeId"); |
|
103 if (ctid == null) { |
|
104 continue; |
|
105 } |
|
106 IContentType ct = ctManager.getContentType(ctid); |
|
107 if (ct == null) { |
|
108 continue; |
|
109 } |
|
110 |
|
111 if (!sourceCT.isKindOf(ct)) { |
|
112 continue; |
|
113 } |
|
114 Object ext; |
|
115 try { |
|
116 ext = el.createExecutableExtension("class"); |
|
117 } catch (CoreException e) { |
|
118 ext = null; |
|
119 } |
|
120 if (!(ext instanceof IPreviewDescriptorFactory)) { |
|
121 continue; |
|
122 } |
|
123 return ((IPreviewDescriptorFactory) ext) |
|
124 .createPreviewDescriptor((IStorage) adaptableObject, |
|
125 descr); |
|
126 } |
|
127 } |
|
128 |
|
129 File f = null; |
|
130 if (adaptableObject instanceof IStorage) { |
|
131 IStorage storage = (IStorage) adaptableObject; |
|
132 IPath fullPath = storage.getFullPath(); |
|
133 f = new File(fullPath.toString()); |
|
134 return new UnsupportedContentDescriptor(f); |
|
135 } |
|
136 |
|
137 return new UnsupportedContentDescriptor(f); |
|
138 } |
|
139 |
|
140 public Class<?>[] getAdapterList() { |
|
141 return adapters.clone(); |
|
142 } |
|
143 } |