001: /*******************************************************************************
002: * Copyright (c) 2007 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.toc;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.runtime.IPath;
014:
015: /**
016: * The TocTopic class represents a topic element in a TOC.
017: * A topic can link to a specific Help page. It can also have
018: * children, which can be more topics.
019: */
020: public class TocTopic extends TocObject {
021: /**
022: *
023: */
024: private static final long serialVersionUID = 1L;
025:
026: /**
027: * Constructs a topic with the given model and parent.
028: *
029: * @param model The model associated with the new topic.
030: * @param parent The parent TocObject of the new topic.
031: */
032: public TocTopic(TocModel model) {
033: super (model, ELEMENT_TOPIC);
034: }
035:
036: /**
037: * Constructs a subclass of a topic with the given model and parent.
038: *
039: * @param model The model associated with the new topic.
040: * @param parent The parent TocObject of the new topic.
041: */
042: public TocTopic(TocModel model, String tagName) {
043: super (model, tagName);
044: }
045:
046: /**
047: * Constructs a topic with the given model, parent and file.
048: *
049: * @param model The model associated with the new link.
050: * @param parent The parent TocObject of the new link.
051: * @param file The page to link to.
052: */
053: public TocTopic(TocModel model, IFile file) {
054: super (model, ELEMENT_TOPIC);
055:
056: IPath path = file.getFullPath();
057: if (file.getProject().equals(
058: getSharedModel().getUnderlyingResource().getProject())) { //If the file is from the same project,
059: //remove the project name segment
060: setFieldRef(path.removeFirstSegments(1).toString()); //$NON-NLS-1$
061: } else { //If the file is from another project, add ".."
062: //to traverse outside this model's project
063: setFieldRef(".." + path.toString()); //$NON-NLS-1$
064: }
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.pde.internal.core.toc.TocObject#canBeParent()
069: */
070: public boolean canBeParent() {
071: return true;
072: }
073:
074: /* (non-Javadoc)
075: * @see org.eclipse.pde.internal.core.toc.TocObject#getName()
076: */
077: public String getName() {
078: return getFieldLabel();
079: }
080:
081: /* (non-Javadoc)
082: * @see org.eclipse.pde.internal.core.text.toc.TocObject#getPath()
083: */
084: public String getPath() {
085: return getFieldRef();
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.pde.internal.core.toc.TocObject#getType()
090: */
091: public int getType() {
092: return TYPE_TOPIC;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.eclipse.pde.internal.core.toc.TocObject#isFirstChildObject(org.eclipse.pde.internal.core.toc.TocObject)
097: */
098: public boolean isFirstChildObject(TocObject tocObject) {
099: return super .isFirstChildNode(tocObject, TocObject.class);
100: }
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.pde.internal.core.toc.TocObject#isLastChildObject(org.eclipse.pde.internal.core.toc.TocObject)
104: */
105: public boolean isLastChildObject(TocObject tocObject) {
106: return super .isLastChildNode(tocObject, TocObject.class);
107: }
108:
109: /**
110: * Add a TocObject child to this topic
111: * and signal the model if necessary.
112: *
113: * @param child The child to add to the TocObject
114: */
115: public void addChild(TocObject child) {
116: addChildNode(child, true);
117: }
118:
119: /**
120: * Add a TocObject child to this topic
121: * beside a specified sibling
122: * and signal the model if necessary.
123: *
124: * @param child The child to add to the TocObject
125: * @param sibling The object that will become the child's direct sibling
126: * @param insertBefore If the object should be inserted before the sibling
127: */
128: public void addChild(TocObject child, TocObject sibling,
129: boolean insertBefore) {
130: int currentIndex = indexOf(sibling);
131: if (!insertBefore) {
132: currentIndex++;
133: }
134:
135: addChildNode(child, currentIndex, true);
136: }
137:
138: /* (non-Javadoc)
139: * @see org.eclipse.pde.internal.core.toc.TocObject#moveChild(org.eclipse.pde.internal.core.toc.TocObject, int)
140: */
141: public void moveChild(TocObject tocObject, int newRelativeIndex) {
142: moveChildNode(tocObject, newRelativeIndex, true);
143: }
144:
145: /**
146: * Remove a TocObject child from this topic
147: * and signal the model if necessary.
148: *
149: * @param child The child to add to the TocObject
150: */
151: public void removeChild(TocObject tocObject) {
152: removeChildNode(tocObject, true);
153: }
154:
155: /**
156: * @return the label associated with this topic.
157: */
158: public String getFieldLabel() {
159: return getXMLAttributeValue(ATTRIBUTE_LABEL);
160: }
161:
162: /**
163: * Change the value of the label field and
164: * signal a model change if needed.
165: *
166: * @param name The new label for the topic
167: */
168: public void setFieldLabel(String name) {
169: setXMLAttribute(ATTRIBUTE_LABEL, name);
170: }
171:
172: /**
173: * @return the link associated with this topic, <br />
174: * or <code>null</code> if none exists.
175: */
176: public String getFieldRef() {
177: return getXMLAttributeValue(ATTRIBUTE_HREF);
178: }
179:
180: /**
181: * Change the value of the link field and
182: * signal a model change if needed.
183: *
184: * @param value The new page location to be linked by this topic
185: */
186: public void setFieldRef(String value) {
187: setXMLAttribute(ATTRIBUTE_HREF, value);
188: }
189: }
|