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.pde.internal.core.text.bundle;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.jface.text.BadLocationException;
016: import org.eclipse.jface.text.IDocument;
017: import org.eclipse.pde.internal.core.bundle.BundleObject;
018: import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
019: import org.eclipse.pde.internal.core.ibundle.IBundle;
020: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
021: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
022: import org.eclipse.pde.internal.core.text.IEditingModel;
023:
024: public class ManifestHeader extends BundleObject implements
025: IManifestHeader {
026: private static final long serialVersionUID = 1L;
027:
028: private int fOffset = -1;
029: private int fLength = -1;
030:
031: protected String fName;
032: protected String fValue;
033:
034: protected transient IBundle fBundle;
035: protected String fLineDelimiter;
036:
037: public ManifestHeader() {
038: }
039:
040: public ManifestHeader(String name, String value, IBundle bundle,
041: String lineDelimiter) {
042: fName = name;
043: fBundle = bundle;
044: fLineDelimiter = lineDelimiter;
045: processValue(value);
046: setModel(fBundle.getModel());
047: }
048:
049: protected void processValue(String value) {
050: fValue = value;
051: }
052:
053: public String getLineLimiter() {
054: return fLineDelimiter;
055: }
056:
057: /* (non-Javadoc)
058: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setName(java.lang.String)
059: */
060: public void setName(String name) {
061: fName = name;
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getName()
066: */
067: public String getName() {
068: return fName;
069: }
070:
071: public String getValue() {
072: return fValue;
073: }
074:
075: public void setValue(String value) {
076: String old = fValue;
077: fValue = value;
078: fBundle.getModel().fireModelObjectChanged(this , getName(), old,
079: value);
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setOffset(int)
084: */
085: public void setOffset(int offset) {
086: fOffset = offset;
087: }
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getOffset()
091: */
092: public int getOffset() {
093: return fOffset;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#setLength(int)
098: */
099: public void setLength(int length) {
100: fLength = length;
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#getLength()
105: */
106: public int getLength() {
107: return fLength;
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.pde.internal.ui.model.IDocumentKey#write()
112: */
113: public String write() {
114: StringBuffer sb = new StringBuffer(fName);
115: sb.append(": "); //$NON-NLS-1$
116: try {
117: if (fOffset != -1) {
118: IBundleModel model = fBundle.getModel();
119: if (model instanceof IEditingModel) {
120: IDocument doc = ((IEditingModel) model)
121: .getDocument();
122: int line = doc.getLineOfOffset(fOffset);
123: String text = doc.get(fOffset,
124: doc.getLineLength(line)).trim();
125: // respect a line break after a ":", if the user had entered it
126: // bug 113098
127: if (text.length() == fName.length() + 1) {
128: sb.append(fLineDelimiter);
129: sb.append(" "); //$NON-NLS-1$
130: }
131: }
132: }
133: } catch (BadLocationException e) {
134: }
135: sb.append(getValue());
136: sb.append(fLineDelimiter);
137: return sb.toString();
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
142: */
143: public void write(String indent, PrintWriter writer) {
144: }
145:
146: public void setBundle(IBundle bundle) {
147: fBundle = bundle;
148: }
149:
150: public IBundle getBundle() {
151: return fBundle;
152: }
153:
154: public String getKey() {
155: return getName();
156: }
157:
158: public void setKey(String key) throws CoreException {
159: setName(key);
160: }
161:
162: protected int getManifestVersion() {
163: return BundlePluginBase.getBundleManifestVersion(fBundle);
164: }
165:
166: public void update() {
167: // TODO
168: // should do something for headers that don't have their own class
169: // (and don't override this method)
170: }
171:
172: public void update(boolean notify) {
173: }
174: }
|