Skip to content

Commit

Permalink
first step for making Gradle Outline: basic parser out plain tree in
Browse files Browse the repository at this point in the history
Outline; Outline is done a-la Winterstein Markdown Editor
  • Loading branch information
paulvi committed Apr 16, 2015
1 parent e0b53eb commit fc9c8a9
Show file tree
Hide file tree
Showing 5 changed files with 531 additions and 3 deletions.
3 changes: 2 additions & 1 deletion org.nodeclipse.enide.editors.gradle/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.resources,
org.eclipse.ui.ide,
org.eclipse.jface.text,
org.eclipse.ui.editors
org.eclipse.ui.editors,
org.eclipse.ui.views
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: org.nodeclipse.enide.editors.gradle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package org.nodeclipse.enide.editors.gradle.editors;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;

import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

/**
* copied from NodeclipseNodejsEditor
* TODO base classes, color theme support
* TODO consider base classes
*
* @author Paul Verest
*/
Expand All @@ -21,6 +28,12 @@ public class GradleEditor extends TextEditor {
public final static String EDITOR_MATCHING_BRACKETS_COLOR = "matchingBracketsColor";

private DefaultCharacterPairMatcher matcher;

//+
private GradleOutline fOutlinePage = null;
private boolean pageDirty = true;
private GradlePage page;
IDocument oldDoc = null; //used only in doSetInput(IEditorInput input)

public GradleEditor() {
setSourceViewerConfiguration(new NodeSourceViewerConfiguration());
Expand Down Expand Up @@ -48,6 +61,71 @@ protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupp
store.setDefault(EDITOR_MATCHING_BRACKETS, true);
store.setDefault(EDITOR_MATCHING_BRACKETS_COLOR, "128,128,128");
}

//+ block for Outline taken from Winterstein Markdown Editor {
public Object getAdapter(Class required) {
if (IContentOutlinePage.class.equals(required)) {
if (fOutlinePage == null) {
fOutlinePage= new GradleOutline(getDocumentProvider(), this);
if (getEditorInput() != null)
fOutlinePage.setInput(getEditorInput());
}
return fOutlinePage;
}
return super.getAdapter(required);
}

public GradlePage getGradlePage() {
if (pageDirty) updateGradlePage();
return page;
}

private void updateGradlePage() {
String text = getText();
if (text==null) text="";
page = new GradlePage(text); //GradleParser.parse(text); /
pageDirty = false;
}

/**
* @return The text of the editor's document, or null if unavailable.
*/
public String getText() {
IDocument doc = getDocument();
return doc==null? null : doc.get();
}

public IDocument getDocument() {
IEditorInput input = getEditorInput();
IDocumentProvider docProvider = getDocumentProvider();
return docProvider==null? null : docProvider.getDocument(input);
}

public static GradleEditor getEditor(IDocument doc) {
return doc2editor.get(doc);
}

private static final Map<IDocument, GradleEditor> doc2editor = new HashMap<IDocument, GradleEditor>();

@Override
protected void doSetInput(IEditorInput input) throws CoreException {
// Detach from old
if (oldDoc!= null) {
//oldDoc.removeDocumentListener(this);
if (doc2editor.get(oldDoc) == this) doc2editor.remove(oldDoc);
}
// Set
super.doSetInput(input);
// Attach as a listener to new doc
IDocument doc = getDocument();
oldDoc = doc;
if (doc==null) return;
//doc.addDocumentListener(this);
doc2editor.put(doc, this);
// // Initialise code folding
// haveRunFolding = false;
// updateSectionFoldingAnnotations(null);
}
//}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package org.nodeclipse.enide.editors.gradle.editors;

import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;



public class GradleOutline extends ContentOutlinePage {

private final IDocumentProvider fDocumentProvider;
private final GradleEditor fTextEditor;
private Object fInput = null;

public GradleOutline(IDocumentProvider documentProvider, GradleEditor gradleEditor) {
this.fDocumentProvider=documentProvider;
this.fTextEditor=gradleEditor;
}

/**
* Sets the input of the outline page
*
* @param input
* the input of this outline page
*/
public void setInput(Object input) {
fInput = input;
update();
}

/**
* Updates the outline page.
*/
public void update() {
TreeViewer viewer = getTreeViewer();

if (viewer != null) {
Control control = viewer.getControl();
if (control != null && !control.isDisposed()) {
control.setRedraw(false);
viewer.setInput(fInput);
viewer.expandAll();
control.setRedraw(true);
}
}
}

/*
* (non-Javadoc) Method declared on ContentOutlinePage
*/
@Override
public void createControl(Composite parent) {
super.createControl(parent);
TreeViewer viewer = getTreeViewer();
viewer.setContentProvider(new ContentProvider());
//...
viewer.addSelectionChangedListener(this);

if (fInput != null)
viewer.setInput(fInput);


}

public final class ContentProvider implements ITreeContentProvider, IDocumentListener {

private GradlePage fContent;
// protected List fContent= new ArrayList(10);
private GradleEditor fTextEditor;

private void parse() {
if (fTextEditor==null){
System.out.println("fTextEditor==null");
return;
}
fContent = fTextEditor.getGradlePage();
}


/*
* @see IContentProvider#inputChanged(Viewer, Object, Object)
*/
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// Detach from old
if (oldInput != null) {
IDocument document = fDocumentProvider.getDocument(oldInput);
if (document != null) {
document.removeDocumentListener(this);
}
}
fContent = null;
// Attach to new
if (newInput == null)
return;
IDocument document = fDocumentProvider.getDocument(newInput);
if (document == null)
return;
fTextEditor = GradleEditor.getEditor(document);
document.addDocumentListener(this);
parse();
}

@Override
public void documentAboutToBeChanged(DocumentEvent event) {
// nothing
}

@Override
public void documentChanged(DocumentEvent event) {
parse();
update();
}

/*
* @see IContentProvider#dispose
*/
@Override
public void dispose() {
fContent = null;
}

// /*
// * @see IContentProvider#isDeleted(Object)
// */
// @Override
// public boolean isDeleted(Object element) {
// return false;
// }

/*
* @see IStructuredContentProvider#getElements(Object)
*/
@Override
public Object[] getElements(Object inputElement) {
// //TODO
// if (fContent==null){
// System.out.println("fContent==null");
// return null;
// }
// if (fContent.nodes==null){
// System.out.println("fContent.nodes==null");
// return null;
// }
return fContent.nodes.toArray();
}

/*
* @see ITreeContentProvider#hasChildren(Object)
*/
@Override
public boolean hasChildren(Object element) {
if (element == fInput) {
return true;
}
if (element instanceof GradlePage.Node) {
GradlePage.Node node = (GradlePage.Node) element;
//return header.getSubHeaders().size() > 0;
return false;
}
;
return false;
}

/*
* @see ITreeContentProvider#getParent(Object)
*/
@Override
public Object getParent(Object element) {
if (!(element instanceof GradlePage.Node))
return null;
//TODO
return null;
}

/*
* @see ITreeContentProvider#getChildren(Object)
*/
@Override
public Object[] getChildren(Object element) {
if (element == fInput) {
return getElements(null);
}
if (!(element instanceof GradlePage.Node))
return null;
//return ((MarkdownPage.Header) element).getSubHeaders().toArray();
//TODO
return null;
}



}

}
Loading

0 comments on commit fc9c8a9

Please sign in to comment.