~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.editor/src/org/eclipse/linuxtools/systemtap/ui/editor/SimpleEditor.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.systemtap.ui.editor;
 
13
 
 
14
import java.io.File;
 
15
import java.io.FileNotFoundException;
 
16
import java.io.FileOutputStream;
 
17
import java.io.PrintStream;
 
18
 
 
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;
 
35
 
 
36
public class SimpleEditor extends TextEditor {
 
37
        public SimpleEditor() {
 
38
                super();
 
39
                // make sure we inherit all the text editing commands (delete line etc).
 
40
                setKeyBindingScopes(new String[] { "org.eclipse.ui.textEditorScope" });
 
41
                internal_init();
 
42
        }
 
43
 
 
44
        protected void internal_init() {
 
45
                configureInsertMode(SMART_INSERT, false);
 
46
                setDocumentProvider(new SimpleDocumentProvider());
 
47
        }
 
48
 
 
49
        public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
 
50
                super.init(site, input);
 
51
                RecentFileMenuManager.getInstance().registerActionBar(getEditorSite().getActionBars());
 
52
        }
 
53
 
 
54
        /**
 
55
         * Searches the IDocument for the specified string.
 
56
         * 
 
57
         * @param search string to find
 
58
         * @return the integer line number of the string
 
59
         */
 
60
        public int find(String search) {
 
61
                IDocument doc = getSourceViewer().getDocument();
 
62
                FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(doc);
 
63
                
 
64
                int line = 0;
 
65
                
 
66
                jumpToLocation(0, 0);
 
67
                try {
 
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) {
 
73
                        line = -1;
 
74
                }
 
75
 
 
76
                return line;
 
77
        }
 
78
        
 
79
        /**
 
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
 
83
         */
 
84
        public void jumpToLocation(int line, int character) {
 
85
                IDocument doc = getSourceViewer().getDocument();
 
86
 
 
87
                try {
 
88
                        int offset = doc.getLineOffset(line-1) + character;
 
89
                        this.getSelectionProvider().setSelection(new TextSelection(doc, offset, 0));
 
90
                } catch(BadLocationException boe) {}
 
91
        }
 
92
        
 
93
        /**
 
94
         * Selects a line in the IDocument.
 
95
         * @param line the line you wish to select
 
96
         */
 
97
        public void selectLine(int line) {
 
98
                IDocument doc = getSourceViewer().getDocument();
 
99
 
 
100
                try {
 
101
                        this.getSelectionProvider().setSelection(new TextSelection(doc, doc.getLineOffset(line-1), doc.getLineLength(line-1)-1));
 
102
                } catch(BadLocationException boe) {}
 
103
        }
 
104
        
 
105
        /**
 
106
         * Performs a SaveAs on the IDocument.
 
107
         */
 
108
        public void doSaveAs() {
 
109
                File file = queryFile();
 
110
                if(file == null) {
 
111
                        return;
 
112
                }
 
113
 
 
114
                IEditorInput inputFile = createEditorInput(file);
 
115
 
 
116
                IDocument doc = getSourceViewer().getDocument();
 
117
                String s = doc.get();
 
118
                
 
119
                try {
 
120
                        FileOutputStream fos = new FileOutputStream(file);
 
121
                        PrintStream ps = new PrintStream(fos);
 
122
                        
 
123
                        ps.print(s);
 
124
                        ps.close();
 
125
                } catch(FileNotFoundException fnfe) {}
 
126
                
 
127
                setInput(inputFile);
 
128
                setPartName(inputFile.getName());
 
129
        }
 
130
 
 
131
        /**
 
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.
 
135
         */
 
136
        private IEditorInput createEditorInput(File file) {
 
137
                IPath location= new Path(file.getAbsolutePath());
 
138
                PathEditorInput input= new PathEditorInput(location);
 
139
                return input;
 
140
        }
 
141
        
 
142
        /**
 
143
         * Inserts text into the IDocument.
 
144
         * @param text string to insert
 
145
         */
 
146
        public void insertText(String text) {
 
147
                IDocument doc = getSourceViewer().getDocument();
 
148
                String s = doc.get();
 
149
                int offset = s.length();
 
150
                s += text;
 
151
                doc.set(s);
 
152
                this.setHighlightRange(offset,0,true);
 
153
        }
 
154
 
 
155
        /**
 
156
         * Inserts text at the current location.
 
157
         * @param text string to insert
 
158
         */
 
159
        public void insertTextAtCurrent(String text) {
 
160
                ISelection selection = this.getSelectionProvider().getSelection();
 
161
                IDocument doc = getSourceViewer().getDocument();
 
162
                
 
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);
 
170
                }
 
171
        }
 
172
        
 
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);
 
179
                return null;
 
180
        }
 
181
        
 
182
        /**
 
183
         * Determines whether saving is allowed currently.
 
184
         * @return boolean value indicating whether or not saving is allowed
 
185
         */
 
186
        public boolean isSaveAsAllowed() {
 
187
                return true;
 
188
        }
 
189
        
 
190
        public static final String ID = "org.eclipse.linuxtools.systemtap.ui.editor.SimpleEditor";
 
191
}