001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.javaeditor;
011:
012: import java.text.MessageFormat;
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import org.eclipse.jface.text.*;
017: import org.eclipse.jface.viewers.*;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.ui.texteditor.IDocumentProvider;
021: import org.eclipse.ui.texteditor.ITextEditor;
022: import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
023:
024: /**
025: * A content outline page which always represents the content of the
026: * connected editor in 10 segments.
027: */
028: public class JavaContentOutlinePage extends ContentOutlinePage {
029:
030: /**
031: * A segment element.
032: */
033: protected static class Segment {
034: public String name;
035: public Position position;
036:
037: public Segment(String name, Position position) {
038: this .name = name;
039: this .position = position;
040: }
041:
042: public String toString() {
043: return name;
044: }
045: }
046:
047: /**
048: * Divides the editor's document into ten segments and provides elements for them.
049: */
050: protected class ContentProvider implements ITreeContentProvider {
051:
052: protected final static String SEGMENTS = "__java_segments"; //$NON-NLS-1$
053: protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(
054: SEGMENTS);
055: protected List fContent = new ArrayList(10);
056:
057: protected void parse(IDocument document) {
058:
059: int lines = document.getNumberOfLines();
060: int increment = Math.max(Math.round(lines / 10), 10);
061:
062: for (int line = 0; line < lines; line += increment) {
063:
064: int length = increment;
065: if (line + increment > lines)
066: length = lines - line;
067:
068: try {
069:
070: int offset = document.getLineOffset(line);
071: int end = document.getLineOffset(line + length);
072: length = end - offset;
073: Position p = new Position(offset, length);
074: document.addPosition(SEGMENTS, p);
075: fContent
076: .add(new Segment(
077: MessageFormat
078: .format(
079: JavaEditorMessages
080: .getString("OutlinePage.segment.title_pattern"), new Object[] { new Integer(offset) }), p)); //$NON-NLS-1$
081:
082: } catch (BadPositionCategoryException x) {
083: } catch (BadLocationException x) {
084: }
085: }
086: }
087:
088: /*
089: * @see IContentProvider#inputChanged(Viewer, Object, Object)
090: */
091: public void inputChanged(Viewer viewer, Object oldInput,
092: Object newInput) {
093: if (oldInput != null) {
094: IDocument document = fDocumentProvider
095: .getDocument(oldInput);
096: if (document != null) {
097: try {
098: document.removePositionCategory(SEGMENTS);
099: } catch (BadPositionCategoryException x) {
100: }
101: document.removePositionUpdater(fPositionUpdater);
102: }
103: }
104:
105: fContent.clear();
106:
107: if (newInput != null) {
108: IDocument document = fDocumentProvider
109: .getDocument(newInput);
110: if (document != null) {
111: document.addPositionCategory(SEGMENTS);
112: document.addPositionUpdater(fPositionUpdater);
113:
114: parse(document);
115: }
116: }
117: }
118:
119: /*
120: * @see IContentProvider#dispose
121: */
122: public void dispose() {
123: if (fContent != null) {
124: fContent.clear();
125: fContent = null;
126: }
127: }
128:
129: /*
130: * @see IContentProvider#isDeleted(Object)
131: */
132: public boolean isDeleted(Object element) {
133: return false;
134: }
135:
136: /*
137: * @see IStructuredContentProvider#getElements(Object)
138: */
139: public Object[] getElements(Object element) {
140: return fContent.toArray();
141: }
142:
143: /*
144: * @see ITreeContentProvider#hasChildren(Object)
145: */
146: public boolean hasChildren(Object element) {
147: return element == fInput;
148: }
149:
150: /*
151: * @see ITreeContentProvider#getParent(Object)
152: */
153: public Object getParent(Object element) {
154: if (element instanceof Segment)
155: return fInput;
156: return null;
157: }
158:
159: /*
160: * @see ITreeContentProvider#getChildren(Object)
161: */
162: public Object[] getChildren(Object element) {
163: if (element == fInput)
164: return fContent.toArray();
165: return new Object[0];
166: }
167: }
168:
169: protected Object fInput;
170: protected IDocumentProvider fDocumentProvider;
171: protected ITextEditor fTextEditor;
172:
173: /**
174: * Creates a content outline page using the given provider and the given editor.
175: *
176: * @param provider the document provider
177: * @param editor the editor
178: */
179: public JavaContentOutlinePage(IDocumentProvider provider,
180: ITextEditor editor) {
181: super ();
182: fDocumentProvider = provider;
183: fTextEditor = editor;
184: }
185:
186: /* (non-Javadoc)
187: * Method declared on ContentOutlinePage
188: */
189: public void createControl(Composite parent) {
190:
191: super .createControl(parent);
192:
193: TreeViewer viewer = getTreeViewer();
194: viewer.setContentProvider(new ContentProvider());
195: viewer.setLabelProvider(new LabelProvider());
196: viewer.addSelectionChangedListener(this );
197:
198: if (fInput != null)
199: viewer.setInput(fInput);
200: }
201:
202: /* (non-Javadoc)
203: * Method declared on ContentOutlinePage
204: */
205: public void selectionChanged(SelectionChangedEvent event) {
206:
207: super .selectionChanged(event);
208:
209: ISelection selection = event.getSelection();
210: if (selection.isEmpty())
211: fTextEditor.resetHighlightRange();
212: else {
213: Segment segment = (Segment) ((IStructuredSelection) selection)
214: .getFirstElement();
215: int start = segment.position.getOffset();
216: int length = segment.position.getLength();
217: try {
218: fTextEditor.setHighlightRange(start, length, true);
219: } catch (IllegalArgumentException x) {
220: fTextEditor.resetHighlightRange();
221: }
222: }
223: }
224:
225: /**
226: * Sets the input of the outline page
227: *
228: * @param input the input of this outline page
229: */
230: public void setInput(Object input) {
231: fInput = input;
232: update();
233: }
234:
235: /**
236: * Updates the outline page.
237: */
238: public void update() {
239: TreeViewer viewer = getTreeViewer();
240:
241: if (viewer != null) {
242: Control control = viewer.getControl();
243: if (control != null && !control.isDisposed()) {
244: control.setRedraw(false);
245: viewer.setInput(fInput);
246: viewer.expandAll();
247: control.setRedraw(true);
248: }
249: }
250: }
251: }
|