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.ui.examples.readmetool;
011:
012: import java.util.Vector;
013:
014: import org.eclipse.core.runtime.IAdaptable;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.ui.model.IWorkbenchAdapter;
017: import org.eclipse.ui.views.properties.IPropertySource;
018:
019: /**
020: * This class represents a marked location in the Readme
021: * file text.
022: *
023: * TIP: By implementing the <code>IWorkbenchAdapter</code> interface,
024: * we can easily add objects of this type to viewers and parts in
025: * the workbench. When a viewer contains <code>IWorkbenchAdapter</code>,
026: * the generic <code>WorkbenchContentProvider</code> and
027: * <code>WorkbenchLabelProvider</code> can be used to provide
028: * navigation and display for that viewer.
029: */
030: public class MarkElement implements IWorkbenchAdapter, IAdaptable {
031: private String headingName;
032:
033: private IAdaptable parent;
034:
035: private int offset;
036:
037: private int numberOfLines;
038:
039: private int length;
040:
041: private Vector children;
042:
043: /**
044: * Creates a new MarkElement and stores parent element and
045: * location in the text.
046: *
047: * @param parent the parent of this element
048: * @param heading text corresponding to the heading
049: * @param offset the offset into the Readme text
050: * @param length the length of the element
051: */
052: public MarkElement(IAdaptable parent, String heading, int offset,
053: int length) {
054: this .parent = parent;
055: if (parent instanceof MarkElement) {
056: ((MarkElement) parent).addChild(this );
057: }
058: this .headingName = heading;
059: this .offset = offset;
060: this .length = length;
061: }
062:
063: /**
064: * Adds a child to this element
065: */
066: private void addChild(MarkElement child) {
067: if (children == null) {
068: children = new Vector();
069: }
070: children.add(child);
071: }
072:
073: /* (non-Javadoc)
074: * Method declared on IAdaptable
075: */
076: public Object getAdapter(Class adapter) {
077: if (adapter == IWorkbenchAdapter.class)
078: return this ;
079: if (adapter == IPropertySource.class)
080: return new MarkElementProperties(this );
081: return null;
082: }
083:
084: /* (non-Javadoc)
085: * Method declared on IWorkbenchAdapter
086: */
087: public Object[] getChildren(Object object) {
088: if (children != null) {
089: return children.toArray();
090: }
091: return new Object[0];
092: }
093:
094: /* (non-Javadoc)
095: * Method declared on IWorkbenchAdapter
096: */
097: public ImageDescriptor getImageDescriptor(Object object) {
098: IWorkbenchAdapter parentElement = (IWorkbenchAdapter) parent
099: .getAdapter(IWorkbenchAdapter.class);
100: if (parentElement != null) {
101: return parentElement.getImageDescriptor(object);
102: }
103: return null;
104: }
105:
106: /* (non-Javadoc)
107: * Method declared on IWorkbenchAdapter
108: */
109: public String getLabel(Object o) {
110: return headingName;
111: }
112:
113: /**
114: * Returns the number of characters in this section.
115: */
116: public int getLength() {
117: return length;
118: }
119:
120: /**
121: * Returns the number of lines in the element.
122: *
123: * @return the number of lines in the element
124: */
125: public int getNumberOfLines() {
126: return numberOfLines;
127: }
128:
129: /* (non-Javadoc)
130: * Method declared on IWorkbenchAdapter
131: */
132: public Object getParent(Object o) {
133: return null;
134: }
135:
136: /**
137: * Returns the offset of this section in the file.
138: */
139: public int getStart() {
140: return offset;
141: }
142:
143: /**
144: * Sets the number of lines in the element
145: *
146: * @param newNumberOfLines the number of lines in the element
147: */
148: public void setNumberOfLines(int newNumberOfLines) {
149: numberOfLines = newNumberOfLines;
150: }
151: }
|