1. Fix #1589403 - wrap double/single quoted strings
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / editors / SQLContentOutlinePage.java
1 package com.quantum.editors;
2
3 import java.util.List;
4 import java.util.StringTokenizer;
5
6 import com.quantum.ImageStore;
7 import com.quantum.sql.parser.SQLParser;
8
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;
21
22
23 /**
24  * @author BC
25  */
26 public class SQLContentOutlinePage extends ContentOutlinePage {
27         
28         class LabelProviderImpl implements ILabelProvider {
29                 public Image getImage(Object element) {
30                         return ImageStore.getImage(ImageStore.SCRIPT);
31                 }
32
33                 public String getText(Object element) {
34                         if (element != null && element instanceof String) {
35                                 StringTokenizer tokenizer = new StringTokenizer((String) element);
36                                 
37                                 String token = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
38                                 
39                                 if ("select".equalsIgnoreCase(token) 
40                                                 || "update".equalsIgnoreCase(token)
41                                                 || "insert".equalsIgnoreCase(token)
42                                                 || "delete".equalsIgnoreCase(token)
43                                                 || "grant".equalsIgnoreCase(token)) {
44                                         return token;
45                                 } else if (tokenizer.hasMoreTokens()) {
46                                         return token + " " + tokenizer.nextToken();
47                                 } else {
48                                         return token;
49                                 }
50                         } else {
51                                 return element == null ? "" : element.toString();
52                         }
53                 }
54
55                 public void addListener(ILabelProviderListener listener) {
56                 }
57
58                 public void dispose() {
59                 }
60
61                 public boolean isLabelProperty(Object element, String property) {
62                         return false;
63                 }
64
65                 public void removeListener(ILabelProviderListener listener) {
66                 }
67         }
68         
69         class ContentProviderImpl implements ITreeContentProvider {
70                 
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);
75                                 return sql.toArray();
76                         } else {
77                                 return null;
78                         }
79                 }
80
81                 public void dispose() {
82                 }
83
84                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
85                 }
86
87                 public Object[] getChildren(Object parentElement) {
88                         return null;
89                 }
90
91                 public Object getParent(Object element) {
92                         return null;
93                 }
94
95                 public boolean hasChildren(Object element) {
96                         return false;
97                 }
98         }
99         
100         private IDocumentListener listener;
101
102         private final SQLDocumentProvider documentProvider;
103         private IEditorInput editorInput;
104         /**
105          * @param documentProvider
106          * @param editor
107          */
108         public SQLContentOutlinePage(SQLDocumentProvider documentProvider, SQLEditor editor) {
109                 this.documentProvider = documentProvider;
110         }
111         
112         
113
114         public void createControl(Composite parent) {
115                 super.createControl(parent);
116                 
117                 final TreeViewer treeViewer = getTreeViewer();
118                 
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);
124                         
125                         this.listener = new IDocumentListener() {
126
127                                 public void documentAboutToBeChanged(DocumentEvent event) {
128                                 }
129
130                                 public void documentChanged(DocumentEvent event) {
131                                         treeViewer.refresh();
132                                 }
133                         };
134                         document.addDocumentListener(this.listener);
135                 }
136         }
137         /**
138          * @param editorInput
139          */
140         public void setInput(IEditorInput editorInput) {
141                 this.editorInput = editorInput;
142         }
143         
144         
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;
151                         }
152                 }
153                 super.dispose();
154         }
155 }