01: /*******************************************************************************
02: * Copyright (c) 2005, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.core.text.bundle;
11:
12: import java.io.PrintWriter;
13:
14: import org.eclipse.osgi.service.resolver.ExportPackageDescription;
15: import org.eclipse.osgi.util.ManifestElement;
16: import org.eclipse.pde.internal.core.ICoreConstants;
17: import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
18: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
19: import org.osgi.framework.Constants;
20: import org.osgi.framework.Version;
21:
22: public class ImportPackageObject extends PackageObject {
23:
24: private static final long serialVersionUID = 1L;
25:
26: private static String getVersion(ExportPackageDescription desc) {
27: String version = desc.getVersion().toString();
28: if (!version.equals(Version.emptyVersion.toString()))
29: return desc.getVersion().toString();
30: return null;
31: }
32:
33: public ImportPackageObject(ManifestHeader header,
34: ManifestElement element, String versionAttribute) {
35: super (header, element, versionAttribute);
36: }
37:
38: public ImportPackageObject(ManifestHeader header,
39: ExportPackageDescription desc, String versionAttribute) {
40: super (header, desc.getName(), getVersion(desc),
41: versionAttribute);
42: }
43:
44: public ImportPackageObject(ManifestHeader header, String id,
45: String version, String versionAttribute) {
46: super (header, id, version, versionAttribute);
47: }
48:
49: public boolean isOptional() {
50: int manifestVersion = BundlePluginBase
51: .getBundleManifestVersion(getHeader().getBundle());
52: if (manifestVersion > 1)
53: return Constants.RESOLUTION_OPTIONAL
54: .equals(getDirective(Constants.RESOLUTION_DIRECTIVE));
55: return "true".equals(getAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE)); //$NON-NLS-1$
56: }
57:
58: public void setOptional(boolean optional) {
59: boolean old = isOptional();
60: int manifestVersion = BundlePluginBase
61: .getBundleManifestVersion(getHeader().getBundle());
62: if (optional) {
63: if (manifestVersion > 1)
64: setDirective(Constants.RESOLUTION_DIRECTIVE,
65: Constants.RESOLUTION_OPTIONAL);
66: else
67: setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, "true"); //$NON-NLS-1$
68: } else {
69: setDirective(Constants.RESOLUTION_DIRECTIVE, null);
70: setAttribute(ICoreConstants.OPTIONAL_ATTRIBUTE, null);
71: }
72: fHeader.update();
73: firePropertyChanged(this , Constants.RESOLUTION_DIRECTIVE,
74: Boolean.toString(old), Boolean.toString(optional));
75: }
76:
77: /**
78: * @param model
79: * @param header
80: * @param versionAttribute
81: */
82: public void reconnect(IBundleModel model,
83: ImportPackageHeader header, String versionAttribute) {
84: super .reconnect(model, header, versionAttribute);
85: // No transient fields
86: }
87:
88: /* (non-Javadoc)
89: * @see org.eclipse.pde.internal.core.bundle.BundleObject#write(java.lang.String, java.io.PrintWriter)
90: */
91: public void write(String indent, PrintWriter writer) {
92: // Used for text transfers for copy, cut, paste operations
93: writer.write(write());
94: }
95:
96: }
|