1
/*******************************************************************************
2
* Copyright (c) 2006 IBM Corporation.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
10
*******************************************************************************/
12
package org.eclipse.linuxtools.systemtap.ui.editor;
15
import java.io.FileNotFoundException;
16
import java.io.FileOutputStream;
17
import java.io.PrintStream;
19
import org.eclipse.core.runtime.IPath;
20
import org.eclipse.core.runtime.Path;
21
import org.eclipse.jface.text.BadLocationException;
22
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
23
import org.eclipse.jface.text.IDocument;
24
import org.eclipse.jface.text.IRegion;
25
import org.eclipse.jface.text.ITextSelection;
26
import org.eclipse.jface.text.TextSelection;
27
import org.eclipse.jface.viewers.ISelection;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.widgets.FileDialog;
30
import org.eclipse.ui.IEditorInput;
31
import org.eclipse.ui.IEditorSite;
32
import org.eclipse.ui.PartInitException;
33
import org.eclipse.ui.PlatformUI;
34
import org.eclipse.ui.editors.text.TextEditor;
36
public class SimpleEditor extends TextEditor {
37
public SimpleEditor() {
39
// make sure we inherit all the text editing commands (delete line etc).
40
setKeyBindingScopes(new String[] { "org.eclipse.ui.textEditorScope" });
44
protected void internal_init() {
45
configureInsertMode(SMART_INSERT, false);
46
setDocumentProvider(new SimpleDocumentProvider());
49
public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
50
super.init(site, input);
51
RecentFileMenuManager.getInstance().registerActionBar(getEditorSite().getActionBars());
55
* Searches the IDocument for the specified string.
57
* @param search string to find
58
* @return the integer line number of the string
60
public int find(String search) {
61
IDocument doc = getSourceViewer().getDocument();
62
FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(doc);
68
IRegion reg = finder.find(0, search, true, false, false, false);
69
int offset = reg.getOffset();
70
line = doc.getLineOfOffset(offset);
71
} catch(BadLocationException ble) {
72
} catch(NullPointerException npe) {
80
* Jumps to the location in the IDocument.
81
* @param line the line you wish to jump to
82
* @param the character you wish to jump to
84
public void jumpToLocation(int line, int character) {
85
IDocument doc = getSourceViewer().getDocument();
88
int offset = doc.getLineOffset(line-1) + character;
89
this.getSelectionProvider().setSelection(new TextSelection(doc, offset, 0));
90
} catch(BadLocationException boe) {}
94
* Selects a line in the IDocument.
95
* @param line the line you wish to select
97
public void selectLine(int line) {
98
IDocument doc = getSourceViewer().getDocument();
101
this.getSelectionProvider().setSelection(new TextSelection(doc, doc.getLineOffset(line-1), doc.getLineLength(line-1)-1));
102
} catch(BadLocationException boe) {}
106
* Performs a SaveAs on the IDocument.
108
public void doSaveAs() {
109
File file = queryFile();
114
IEditorInput inputFile = createEditorInput(file);
116
IDocument doc = getSourceViewer().getDocument();
117
String s = doc.get();
120
FileOutputStream fos = new FileOutputStream(file);
121
PrintStream ps = new PrintStream(fos);
125
} catch(FileNotFoundException fnfe) {}
128
setPartName(inputFile.getName());
132
* Sets up an editor input based on the specified file.
133
* @param file the location of the file you wish to set.
134
* @return input object created.
136
private IEditorInput createEditorInput(File file) {
137
IPath location= new Path(file.getAbsolutePath());
138
PathEditorInput input= new PathEditorInput(location);
143
* Inserts text into the IDocument.
144
* @param text string to insert
146
public void insertText(String text) {
147
IDocument doc = getSourceViewer().getDocument();
148
String s = doc.get();
149
int offset = s.length();
152
this.setHighlightRange(offset,0,true);
156
* Inserts text at the current location.
157
* @param text string to insert
159
public void insertTextAtCurrent(String text) {
160
ISelection selection = this.getSelectionProvider().getSelection();
161
IDocument doc = getSourceViewer().getDocument();
163
if(selection instanceof ITextSelection) {
164
ITextSelection s = (ITextSelection) selection;
165
StringBuffer sb = new StringBuffer(doc.get().substring(0,s.getOffset()));
166
sb.append(text.trim());
167
sb.append(doc.get().substring(s.getOffset() + s.getLength(), doc.get().length()));
168
doc.set(sb.toString());
169
this.setHighlightRange(s.getOffset() + text.trim().length(),0,true);
173
private File queryFile() {
174
FileDialog dialog= new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
175
dialog.setText("New File");
176
String path= dialog.open();
177
if (path != null && path.length() > 0)
178
return new File(path);
183
* Determines whether saving is allowed currently.
184
* @return boolean value indicating whether or not saving is allowed
186
public boolean isSaveAsAllowed() {
190
public static final String ID = "org.eclipse.linuxtools.systemtap.ui.editor.SimpleEditor";