1 package com.quantum.editors;
4 import java.util.StringTokenizer;
6 import com.quantum.ImageStore;
7 import com.quantum.sql.parser.SQLParser;
9 import org.eclipse.jface.text.DocumentEvent;
10 import org.eclipse.jface.text.IDocument;
11 import org.eclipse.jface.text.IDocumentListener;
12 import org.eclipse.jface.viewers.ILabelProvider;
13 import org.eclipse.jface.viewers.ILabelProviderListener;
14 import org.eclipse.jface.viewers.ITreeContentProvider;
15 import org.eclipse.jface.viewers.TreeViewer;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
26 public class SQLContentOutlinePage extends ContentOutlinePage {
28 class LabelProviderImpl implements ILabelProvider {
29 public Image getImage(Object element) {
30 return ImageStore.getImage(ImageStore.SCRIPT);
33 public String getText(Object element) {
34 if (element != null && element instanceof String) {
35 StringTokenizer tokenizer = new StringTokenizer((String) element);
37 String token = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
39 if ("select".equalsIgnoreCase(token)
40 || "update".equalsIgnoreCase(token)
41 || "insert".equalsIgnoreCase(token)
42 || "delete".equalsIgnoreCase(token)
43 || "grant".equalsIgnoreCase(token)) {
45 } else if (tokenizer.hasMoreTokens()) {
46 return token + " " + tokenizer.nextToken();
51 return element == null ? "" : element.toString();
55 public void addListener(ILabelProviderListener listener) {
58 public void dispose() {
61 public boolean isLabelProperty(Object element, String property) {
65 public void removeListener(ILabelProviderListener listener) {
69 class ContentProviderImpl implements ITreeContentProvider {
71 public Object[] getElements(Object inputElement) {
72 if (inputElement != null && inputElement instanceof IDocument) {
73 String text = ((IDocument) inputElement).get();
74 List sql = SQLParser.parse(text);
81 public void dispose() {
84 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
87 public Object[] getChildren(Object parentElement) {
91 public Object getParent(Object element) {
95 public boolean hasChildren(Object element) {
100 private IDocumentListener listener;
102 private final SQLDocumentProvider documentProvider;
103 private IEditorInput editorInput;
105 * @param documentProvider
108 public SQLContentOutlinePage(SQLDocumentProvider documentProvider, SQLEditor editor) {
109 this.documentProvider = documentProvider;
114 public void createControl(Composite parent) {
115 super.createControl(parent);
117 final TreeViewer treeViewer = getTreeViewer();
119 IDocument document = this.documentProvider.getDocument(this.editorInput);
120 if (document != null) {
121 treeViewer.setLabelProvider(new LabelProviderImpl());
122 treeViewer.setContentProvider(new ContentProviderImpl());
123 treeViewer.setInput(document);
125 this.listener = new IDocumentListener() {
127 public void documentAboutToBeChanged(DocumentEvent event) {
130 public void documentChanged(DocumentEvent event) {
131 treeViewer.refresh();
134 document.addDocumentListener(this.listener);
140 public void setInput(IEditorInput editorInput) {
141 this.editorInput = editorInput;
145 public void dispose() {
146 if (this.listener != null) {
147 IDocument document = this.documentProvider.getDocument(this.editorInput);
148 if (document != null) {
149 document.removeDocumentListener(this.listener);
150 this.listener = null;