|
| 1 | +package org.nodeclipse.enide.editors.gradle.editors; |
| 2 | + |
| 3 | +import org.eclipse.jface.text.DocumentEvent; |
| 4 | +import org.eclipse.jface.text.IDocument; |
| 5 | +import org.eclipse.jface.text.IDocumentListener; |
| 6 | +import org.eclipse.jface.viewers.ITreeContentProvider; |
| 7 | +import org.eclipse.jface.viewers.TreeViewer; |
| 8 | +import org.eclipse.jface.viewers.Viewer; |
| 9 | +import org.eclipse.swt.widgets.Composite; |
| 10 | +import org.eclipse.swt.widgets.Control; |
| 11 | +import org.eclipse.ui.texteditor.IDocumentProvider; |
| 12 | +import org.eclipse.ui.views.contentoutline.ContentOutlinePage; |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +public class GradleOutline extends ContentOutlinePage { |
| 17 | + |
| 18 | + private final IDocumentProvider fDocumentProvider; |
| 19 | + private final GradleEditor fTextEditor; |
| 20 | + private Object fInput = null; |
| 21 | + |
| 22 | + public GradleOutline(IDocumentProvider documentProvider, GradleEditor gradleEditor) { |
| 23 | + this.fDocumentProvider=documentProvider; |
| 24 | + this.fTextEditor=gradleEditor; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Sets the input of the outline page |
| 29 | + * |
| 30 | + * @param input |
| 31 | + * the input of this outline page |
| 32 | + */ |
| 33 | + public void setInput(Object input) { |
| 34 | + fInput = input; |
| 35 | + update(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Updates the outline page. |
| 40 | + */ |
| 41 | + public void update() { |
| 42 | + TreeViewer viewer = getTreeViewer(); |
| 43 | + |
| 44 | + if (viewer != null) { |
| 45 | + Control control = viewer.getControl(); |
| 46 | + if (control != null && !control.isDisposed()) { |
| 47 | + control.setRedraw(false); |
| 48 | + viewer.setInput(fInput); |
| 49 | + viewer.expandAll(); |
| 50 | + control.setRedraw(true); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + * (non-Javadoc) Method declared on ContentOutlinePage |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public void createControl(Composite parent) { |
| 60 | + super.createControl(parent); |
| 61 | + TreeViewer viewer = getTreeViewer(); |
| 62 | + viewer.setContentProvider(new ContentProvider()); |
| 63 | + //... |
| 64 | + viewer.addSelectionChangedListener(this); |
| 65 | + |
| 66 | + if (fInput != null) |
| 67 | + viewer.setInput(fInput); |
| 68 | + |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + public final class ContentProvider implements ITreeContentProvider, IDocumentListener { |
| 73 | + |
| 74 | + private GradlePage fContent; |
| 75 | + // protected List fContent= new ArrayList(10); |
| 76 | + private GradleEditor fTextEditor; |
| 77 | + |
| 78 | + private void parse() { |
| 79 | + if (fTextEditor==null){ |
| 80 | + System.out.println("fTextEditor==null"); |
| 81 | + return; |
| 82 | + } |
| 83 | + fContent = fTextEditor.getGradlePage(); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + /* |
| 88 | + * @see IContentProvider#inputChanged(Viewer, Object, Object) |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 92 | + // Detach from old |
| 93 | + if (oldInput != null) { |
| 94 | + IDocument document = fDocumentProvider.getDocument(oldInput); |
| 95 | + if (document != null) { |
| 96 | + document.removeDocumentListener(this); |
| 97 | + } |
| 98 | + } |
| 99 | + fContent = null; |
| 100 | + // Attach to new |
| 101 | + if (newInput == null) |
| 102 | + return; |
| 103 | + IDocument document = fDocumentProvider.getDocument(newInput); |
| 104 | + if (document == null) |
| 105 | + return; |
| 106 | + fTextEditor = GradleEditor.getEditor(document); |
| 107 | + document.addDocumentListener(this); |
| 108 | + parse(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void documentAboutToBeChanged(DocumentEvent event) { |
| 113 | + // nothing |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void documentChanged(DocumentEvent event) { |
| 118 | + parse(); |
| 119 | + update(); |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * @see IContentProvider#dispose |
| 124 | + */ |
| 125 | + @Override |
| 126 | + public void dispose() { |
| 127 | + fContent = null; |
| 128 | + } |
| 129 | + |
| 130 | +// /* |
| 131 | +// * @see IContentProvider#isDeleted(Object) |
| 132 | +// */ |
| 133 | +// @Override |
| 134 | +// public boolean isDeleted(Object element) { |
| 135 | +// return false; |
| 136 | +// } |
| 137 | + |
| 138 | + /* |
| 139 | + * @see IStructuredContentProvider#getElements(Object) |
| 140 | + */ |
| 141 | + @Override |
| 142 | + public Object[] getElements(Object inputElement) { |
| 143 | +// //TODO |
| 144 | +// if (fContent==null){ |
| 145 | +// System.out.println("fContent==null"); |
| 146 | +// return null; |
| 147 | +// } |
| 148 | +// if (fContent.nodes==null){ |
| 149 | +// System.out.println("fContent.nodes==null"); |
| 150 | +// return null; |
| 151 | +// } |
| 152 | + return fContent.nodes.toArray(); |
| 153 | + } |
| 154 | + |
| 155 | + /* |
| 156 | + * @see ITreeContentProvider#hasChildren(Object) |
| 157 | + */ |
| 158 | + @Override |
| 159 | + public boolean hasChildren(Object element) { |
| 160 | + if (element == fInput) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + if (element instanceof GradlePage.Node) { |
| 164 | + GradlePage.Node node = (GradlePage.Node) element; |
| 165 | + //return header.getSubHeaders().size() > 0; |
| 166 | + return false; |
| 167 | + } |
| 168 | + ; |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + /* |
| 173 | + * @see ITreeContentProvider#getParent(Object) |
| 174 | + */ |
| 175 | + @Override |
| 176 | + public Object getParent(Object element) { |
| 177 | + if (!(element instanceof GradlePage.Node)) |
| 178 | + return null; |
| 179 | + //TODO |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + /* |
| 184 | + * @see ITreeContentProvider#getChildren(Object) |
| 185 | + */ |
| 186 | + @Override |
| 187 | + public Object[] getChildren(Object element) { |
| 188 | + if (element == fInput) { |
| 189 | + return getElements(null); |
| 190 | + } |
| 191 | + if (!(element instanceof GradlePage.Node)) |
| 192 | + return null; |
| 193 | + //return ((MarkdownPage.Header) element).getSubHeaders().toArray(); |
| 194 | + //TODO |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments