KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > editors > JimpleContentOutlinePage


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20
21 package ca.mcgill.sable.soot.editors;
22
23 import java.io.*;
24 import java.util.ArrayList JavaDoc;
25
26
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.swt.widgets.Composite;
34
35 import org.eclipse.ui.views.contentoutline.*;
36
37 import ca.mcgill.sable.soot.editors.parser.JimpleFile;
38
39 public class JimpleContentOutlinePage extends ContentOutlinePage implements ISelectionChangedListener {
40
41     private IFile input;
42     private JimpleEditor ed;
43     private JimpleFile jimpleFileParser;
44     private TreeViewer viewer;
45     
46     public JimpleContentOutlinePage(IFile file, JimpleEditor ed) {
47         super();
48         setInput(file);
49         setEd(ed);
50         
51     }
52     
53     public void createControl(Composite parent) {
54         super.createControl(parent);
55         
56         setViewer(getTreeViewer());
57         getViewer().setContentProvider(new JimpleOutlineContentProvider());
58         getViewer().setLabelProvider(new JimpleOutlineLabelProvider());
59         getViewer().setInput(getContentOutline());
60         getViewer().expandAll();
61         
62         getViewer().addSelectionChangedListener(this);
63         
64     }
65     
66     public JimpleOutlineObject getContentOutline(){
67     
68         try {
69             BufferedReader br = new BufferedReader(new InputStreamReader(getInput().getContents()));
70             ArrayList JavaDoc text = new ArrayList JavaDoc();
71             while (true) {
72                 String JavaDoc nextLine = br.readLine();
73                 if (nextLine == null) break;// || (nextLine.length() == 0)) break;
74
text.add(nextLine);
75             }
76             getViewer().getLabelProvider().dispose();
77             setJimpleFileParser(new JimpleFile(text));
78             return getJimpleFileParser().getOutline();
79         }
80         catch (IOException e) {
81             return null;
82         }
83         catch (CoreException e) {
84             return null;
85         }
86     }
87     
88     public void selectionChanged(SelectionChangedEvent event) {
89         IStructuredSelection selection = (IStructuredSelection)event.getSelection();
90         if (!selection.isEmpty()) {
91             Object JavaDoc elem = selection.getFirstElement();
92             if (elem instanceof JimpleOutlineObject) {
93                 String JavaDoc toHighlight = ((JimpleOutlineObject)elem).getLabel();
94                 int start = getJimpleFileParser().getStartOfSelected(toHighlight);
95                 int length = getJimpleFileParser().getLength(toHighlight);
96                 getEd().selectAndReveal(start, length);
97             }
98         }
99     }
100     
101     /**
102      * @return IFile
103      */

104     public IFile getInput() {
105         return input;
106     }
107
108     /**
109      * Sets the input.
110      * @param input The input to set
111      */

112     public void setInput(IFile input) {
113         this.input = input;
114     }
115
116     /**
117      * @return
118      */

119     public JimpleEditor getEd() {
120         return ed;
121     }
122
123     /**
124      * @param editor
125      */

126     public void setEd(JimpleEditor editor) {
127         ed = editor;
128     }
129
130     /**
131      * @return
132      */

133     public JimpleFile getJimpleFileParser() {
134         return jimpleFileParser;
135     }
136
137     /**
138      * @param file
139      */

140     public void setJimpleFileParser(JimpleFile file) {
141         jimpleFileParser = file;
142     }
143
144     /**
145      * @return
146      */

147     public TreeViewer getViewer() {
148         return viewer;
149     }
150
151     /**
152      * @param viewer
153      */

154     public void setViewer(TreeViewer viewer) {
155         this.viewer = viewer;
156     }
157
158 }
159
Popular Tags