KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > launching > SootDocument


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 package ca.mcgill.sable.soot.launching;
21
22 import org.eclipse.jface.text.*;
23 import org.eclipse.ui.*;
24 import ca.mcgill.sable.soot.*;
25 import ca.mcgill.sable.soot.ui.*;
26
27 /**
28  * Document for SootOutputView
29  */

30 public class SootDocument extends Document implements ISootOutputEventListener {
31
32     private SootOutputView viewer;
33     private int newStreamWriteEnd = 0;
34     private int oldStreamWriteEnd = 0;
35     private boolean viewShown = false;
36     
37     public SootDocument() {
38     }
39     
40     public void startUp() {
41         SootPlugin.getDefault().addSootOutputEventListener(this);
42     }
43     
44     
45     public void handleSootOutputEvent(SootOutputEvent event) {
46         if (!viewShown) {
47             showSootOutputView();
48             notifySootOutputView();
49             getViewer().getTextViewer().setDocument(this);
50             viewShown = true;
51         }
52         switch (event.getEventType()) {
53             case ISootOutputEventConstants.SOOT_CLEAR_EVENT:
54                 clearText();
55                 break;
56             case ISootOutputEventConstants.SOOT_NEW_TEXT_EVENT:
57                 appendText(event.getTextToAppend());
58                 break;
59             default:
60                 break;
61         }
62     }
63     
64     private void notifySootOutputView() {
65         IWorkbench workbench= PlatformUI.getWorkbench();
66         IWorkbenchWindow[] windows= workbench.getWorkbenchWindows();
67         for (int i = 0; i < windows.length; i++) {
68             IWorkbenchWindow iWorkbenchWindow = windows[i];
69             IWorkbenchPage[] pages= iWorkbenchWindow.getPages();
70             for (int j = 0; j < pages.length; j++) {
71                 IWorkbenchPage iWorkbenchPage = pages[j];
72                 IViewPart part= iWorkbenchPage.findView(ISootConstants.SOOT_OUTPUT_VIEW_ID);
73                 if (part == null) {
74                     IViewReference refs [] = iWorkbenchPage.getViewReferences();
75                 }
76                 if (part instanceof SootOutputView) {
77                     setViewer((SootOutputView)part);
78                     this.addDocumentListener(getViewer());
79                 }
80             }
81         }
82     }
83     
84     private void clearText() {
85         set(new String JavaDoc());
86         setNewStreamWriteEnd(0);
87         setOldStreamWriteEnd(0);
88         showSootOutputView();
89     }
90     
91     private void appendText(final String JavaDoc text) {
92         
93         update(new Runnable JavaDoc() {
94             public void run() {
95                 int appendedLength= text.length();
96                 setNewStreamWriteEnd(getOldStreamWriteEnd() + appendedLength);
97                 replace0(getOldStreamWriteEnd(), 0, text);
98                 setOldStreamWriteEnd(getNewStreamWriteEnd());
99                 getViewer().getTextViewer().setTopIndex(getNumberOfLines());
100             }
101         });
102         
103         
104     }
105     
106     protected void replace0(int pos, int replaceLength, String JavaDoc text) {
107         try {
108             super.replace(pos, replaceLength, text);
109         } catch (BadLocationException ble) {
110         }
111     }
112     
113      
114     private void showSootOutputView() {
115         IWorkbenchWindow window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
116         
117         if (window != null) {
118             IWorkbenchPage page= window.getActivePage();
119             if (page != null) {
120                 try {
121                     IViewPart sootOutputViewer = page.findView("ca.mcgill.sable.soot.ui.sootoutputview.view");
122                     if(sootOutputViewer == null) {
123                         IWorkbenchPart activePart= page.getActivePart();
124                         page.showView("ca.mcgill.sable.soot.ui.sootoutputview.view");
125                         //restore focus stolen by the creation of the console
126
page.activate(activePart);
127                         sootOutputViewer = page.findView("ca.mcgill.sable.soot.ui.sootoutputview.view");
128                     
129                     }
130                     else {
131                         page.bringToTop(sootOutputViewer);
132                     }
133                 }
134                 catch (PartInitException pie) {
135                     System.out.println(pie.getMessage());
136                 }
137             }
138         }
139     }
140     
141     private void update(Runnable JavaDoc runnable) {
142         if (getViewer().getTextViewer() != null && getViewer().getTextViewer().getControl() != null && getViewer().getControl().getDisplay() != null) {
143             getViewer().getTextViewer().getControl().getDisplay().asyncExec(runnable);
144         }
145     }
146     
147     /**
148      * Returns the viewer.
149      * @return SootOutputView
150      */

151     public SootOutputView getViewer() {
152         return viewer;
153     }
154
155     /**
156      * Sets the viewer.
157      * @param viewer The viewer to set
158      */

159     public void setViewer(SootOutputView viewer) {
160         this.viewer = viewer;
161     }
162
163     /**
164      * Returns the newStreamWriteEnd.
165      * @return int
166      */

167     public int getNewStreamWriteEnd() {
168         return newStreamWriteEnd;
169     }
170
171     /**
172      * Returns the oldStreamWriteEnd.
173      * @return int
174      */

175     public int getOldStreamWriteEnd() {
176         return oldStreamWriteEnd;
177     }
178
179     /**
180      * Sets the newStreamWriteEnd.
181      * @param newStreamWriteEnd The newStreamWriteEnd to set
182      */

183     public void setNewStreamWriteEnd(int newStreamWriteEnd) {
184         this.newStreamWriteEnd = newStreamWriteEnd;
185     }
186
187     /**
188      * Sets the oldStreamWriteEnd.
189      * @param oldStreamWriteEnd The oldStreamWriteEnd to set
190      */

191     public void setOldStreamWriteEnd(int oldStreamWriteEnd) {
192         this.oldStreamWriteEnd = oldStreamWriteEnd;
193     }
194
195 }
196
Popular Tags