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 TocLink object represents a link to another TOC.
017: * Links in TOCs are much like import or include directives, in the
018: * sense that they bring all the contents of the linked TOC into the
019: * TOC that has this link.
020: *
021: * TOC links cannot have any content within them, so they are leaf objects.
022: */
023: public class TocLink extends TocObject {
024:
025: private static final long serialVersionUID = 1L;
026:
027: /**
028: * Constructs a link with the given model and parent.
029: *
030: * @param model The model associated with the new link.
031: * @param parent The parent TocObject of the new link.
032: */
033: public TocLink(TocModel model) {
034: super (model, ELEMENT_LINK);
035: }
036:
037: /**
038: * Constructs a link with the given model, parent and file.
039: *
040: * @param model The model associated with the new link.
041: * @param parent The parent TocObject of the new link.
042: * @param file The TOC file to link to.
043: */
044: public TocLink(TocModel model, IFile file) {
045: super (model, ELEMENT_LINK);
046:
047: IPath path = file.getFullPath();
048: if (file.getProject().equals(
049: getSharedModel().getUnderlyingResource().getProject())) { //If the file is from the same project,
050: //remove the project name segment
051: setFieldTocPath(path.removeFirstSegments(1).toString()); //$NON-NLS-1$
052: } else { //If the file is from another project, add ".."
053: //to traverse outside this model's project
054: setFieldTocPath(".." + path.toString()); //$NON-NLS-1$
055: }
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.pde.internal.core.text.toc.TocObject#canBeParent()
060: */
061: public boolean canBeParent() {
062: return false;
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.pde.internal.core.toc.TocObject#getType()
067: */
068: public int getType() {
069: return TYPE_LINK;
070: }
071:
072: /* (non-Javadoc)
073: * @see org.eclipse.pde.internal.core.toc.TocObject#getName()
074: */
075: public String getName() {
076: return getFieldTocPath();
077: }
078:
079: public String getPath() {
080: return getFieldTocPath();
081: }
082:
083: /**
084: * @return the path associated with this TOC link
085: */
086: public String getFieldTocPath() {
087: return getXMLAttributeValue(ATTRIBUTE_TOC);
088: }
089:
090: /**
091: * Change the value of the link path and
092: * signal a model change if needed.
093: *
094: * @param id The new path to associate with the link
095: */
096: public void setFieldTocPath(String path) {
097: setXMLAttribute(ATTRIBUTE_TOC, path);
098: }
099:
100: }
|