001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2003 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package ca.mcgill.sable.soot.ui;
021:
022: import org.eclipse.jface.text.*;
023: import org.eclipse.jface.action.*;
024: import org.eclipse.swt.widgets.*;
025: import org.eclipse.ui.part.*;
026: import org.eclipse.swt.*;
027: import org.eclipse.ui.*;
028:
029: public class SootOutputView extends ViewPart implements ITextListener,
030: IDocumentListener {
031: private TextViewer textViewer;
032:
033: private Control control;
034: private Action selectAllAction;
035: private Action copyAction;
036:
037: public SootOutputView() {
038: super ();
039: }
040:
041: public void createPartControl(Composite parent) {
042: setTextViewer(new TextViewer(parent, getSWTStyles()));
043: getTextViewer().setEditable(false);
044: setControl(parent);
045: createActions();
046: createContextMenu();
047: hookGlobalActions();
048: }
049:
050: private void createActions() {
051: selectAllAction = new Action("selectAll") {
052: public void run() {
053: selectAll();
054: }
055: };
056: copyAction = new Action("copy") {
057: public void run() {
058: copy();
059: }
060: };
061: }
062:
063: private void selectAll() {
064: getTextViewer().setSelection(
065: new TextSelection(getTextViewer()
066: .getTopIndexStartOffset(), getTextViewer()
067: .getDocument().getLength()));
068: }
069:
070: private void copy() {
071: getTextViewer().doOperation(ITextOperationTarget.COPY);
072: }
073:
074: private void createContextMenu() {
075: // Create menu manager.
076: MenuManager menuMgr = new MenuManager();
077: menuMgr.setRemoveAllWhenShown(true);
078: menuMgr.addMenuListener(new IMenuListener() {
079: public void menuAboutToShow(IMenuManager mgr) {
080: fillContextMenu(mgr);
081: }
082: });
083:
084: // Create menu.
085: Menu menu = menuMgr.createContextMenu(getTextViewer()
086: .getControl());
087: getTextViewer().getControl().setMenu(menu);
088:
089: // Register menu for extension.
090: getSite().registerContextMenu(menuMgr, getTextViewer());
091: }
092:
093: private void fillContextMenu(IMenuManager mgr) {
094: mgr
095: .add(new GroupMarker(
096: IWorkbenchActionConstants.MB_ADDITIONS));
097: mgr.add(new Separator());
098: mgr.add(copyAction);
099: mgr.add(selectAllAction);
100: mgr.add(new Separator());
101:
102: }
103:
104: private void hookGlobalActions() {
105: IActionBars bars = getViewSite().getActionBars();
106: bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY,
107: copyAction);
108: bars.setGlobalActionHandler(
109: IWorkbenchActionConstants.SELECT_ALL, selectAllAction);
110:
111: }
112:
113: private static int getSWTStyles() {
114: int styles = SWT.H_SCROLL | SWT.V_SCROLL;
115: return styles;
116: }
117:
118: public void setFocus() {
119: getTextViewer().getControl().setFocus();
120: }
121:
122: /**
123: * Returns the control.
124: * @return Control
125: */
126: public Control getControl() {
127: return control;
128: }
129:
130: /**
131: * Sets the control.
132: * @param control The control to set
133: */
134: public void setControl(Control control) {
135: this .control = control;
136: }
137:
138: public void textChanged(TextEvent e) {
139:
140: }
141:
142: public void documentAboutToBeChanged(DocumentEvent e) {
143: }
144:
145: public void documentChanged(DocumentEvent e) {
146:
147: }
148:
149: /**
150: * Returns the viewer.
151: * @return TextViewer
152: */
153: public TextViewer getTextViewer() {
154: return textViewer;
155: }
156:
157: /**
158: * Sets the viewer.
159: * @param viewer The viewer to set
160: */
161: public void setTextViewer(TextViewer textViewer) {
162: this.textViewer = textViewer;
163: }
164:
165: }
|