Eclipseplugins
EditorTools.java
1 package com.proalpha.java.oea.plugins.querygenerator;
2 
3 import java.util.Scanner;
4 
5 import org.eclipse.jface.dialogs.MessageDialog;
6 import org.eclipse.jface.text.BadLocationException;
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.ITextOperationTarget;
9 import org.eclipse.jface.text.ITextSelection;
10 import org.eclipse.jface.text.ITextViewer;
11 import org.eclipse.jface.text.link.LinkedModeModel;
12 import org.eclipse.jface.text.link.LinkedModeUI;
13 import org.eclipse.jface.text.link.LinkedPosition;
14 import org.eclipse.jface.text.link.LinkedPositionGroup;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.ui.IEditorPart;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.texteditor.IDocumentProvider;
22 import org.eclipse.ui.texteditor.ITextEditor;
23 
24 public class EditorTools {
25 
26  private static EditorTools editorTools = null;
27 
28  private EditorTools() {
29  super();
30  }
31 
32  public static EditorTools getInstance() {
33 
34  if (editorTools == null)
35  editorTools = new EditorTools();
36 
37  return editorTools;
38 
39  }
40 
41  public void insertText(String insertString) {
42 
43  this.insertText(getActiveEditor(), insertString);
44 
45  }
46 
47  public String getSelectionText() {
48 
49  return getSelectionText(getActiveEditor());
50 
51  }
52 
53  public String getSelectionText(ITextEditor editor) {
54 
55  ISelectionProvider selectionProvider = editor.getSelectionProvider();
56  ISelection selection = selectionProvider.getSelection();
57 
58  if (selection instanceof ITextSelection) {
59 
60  ITextSelection textSelection = (ITextSelection) selection;
61  return textSelection.getText();
62 
63  }
64 
65  return "";
66 
67  }
68 
69  public void insertText(ITextEditor editor, String insertString) {
70 
71  if (editor != null) {
72 
73  IDocumentProvider documentProvider = editor.getDocumentProvider();
74  IDocument document = documentProvider.getDocument(editor.getEditorInput());
75 
76  ISelectionProvider selectionProvider = editor.getSelectionProvider();
77  ISelection selection = selectionProvider.getSelection();
78 
79  if (selection instanceof ITextSelection) {
80 
81  try {
82 
83  // Get selected text
84  ITextSelection textSelection = (ITextSelection) selection;
85 
86  int line = textSelection.getStartLine();
87  int selectionOffset = textSelection.getOffset();
88 
89  int indentation = selectionOffset - document.getLineOffset(line);
90  insertString = indent(insertString, indentation, 1);
91 
92  // Parse for variables in text
93  VariableParser variableParser = new VariableParser(insertString);
94  variableParser.parse();
95 
96  // Insert text at current cursor position.
97  insertString = variableParser.getResultText();
98  document.replace(selectionOffset, textSelection.getLength(), insertString);
99 
100  // Add linked positions for parsed variables
101  Variable[] variables = variableParser.getVariables();
102 
103  if (variables.length > 0) {
104 
105  ITextViewer viewer = (ITextViewer) editor.getAdapter(ITextOperationTarget.class);
106  LinkedModeModel model= new LinkedModeModel();
107 
108  int posCount = 0;
109  int exitPosition = selectionOffset + insertString.length();
110 
111  for (int i = 0; i < variables.length; i++) {
112 
113  switch (variables[i].getVariableType()) {
114 
115  case CURSOR_POS:
116  exitPosition = selectionOffset + variables[i].getOffset();
117  break;
118 
119  default:
120 
121  LinkedPositionGroup group = new LinkedPositionGroup();
122 
123  group.addPosition(new LinkedPosition(document, selectionOffset + variables[i].getOffset(), variables[i].getLength()));
124  model.addGroup(group);
125 
126  posCount++;
127 
128  }
129 
130  }
131 
132  if (posCount == 0)
133  editor.selectAndReveal(exitPosition, 0);
134 
135  else {
136 
137  model.forceInstall();
138 
139  LinkedModeUI ui = new LinkedModeUI(model, viewer);
140  ui.setExitPosition(viewer, exitPosition, 0, Integer.MAX_VALUE);
141  ui.enter();
142 
143  }
144 
145  }
146 
147  } catch (BadLocationException e) {
148  IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
149  MessageDialog.openInformation(window.getShell(), "QueryGenerator", e.getMessage());
150  }
151 
152  /*
153  OETextEditor oeEditor = (OETextEditor) editorPart.getAdapter(OETextEditor.class);
154 
155  if (oeEditor != null) {
156 
157  ContextTypeRegistry registry = OETemplateSettings.getContextTypeRegistry();
158  TemplateContextType type = registry.getContextType(OETemplateContextType.OE_CONTEXT_TYPE);
159 
160  Template template = new Template(
161  "QueryTemplate",
162  "QueryTemplate",
163  type.getId(),
164  insertString,
165  true);
166 
167  OETemplateContext context = new OETemplateContext(type, document, selectionOffset, 0);
168 
169  Region region = new Region(selectionOffset, 0);
170  OETemplateProposal proposal = new OETemplateProposal(template, context, region, null);
171 
172  ITextViewer viewer = (ITextViewer) editor.getAdapter(ITextOperationTarget.class);
173  proposal.apply(viewer, ' ', 0, selectionOffset);
174 
175  }
176  */
177 
178  /*
179  ITextViewer viewer = (ITextViewer) editor.getAdapter(ITextOperationTarget.class);
180 
181  LinkedModeModel model= new LinkedModeModel();
182 
183  LinkedPositionGroup group= new LinkedPositionGroup();
184  group.addPosition(new LinkedPosition(document, selectionOffset + 5, 5));
185  model.addGroup(group);
186 
187  model.forceInstall();
188 
189  LinkedModeUI ui= new LinkedModeUI(model, viewer);
190  ui.setExitPosition(viewer, selectionOffset + insertString.length(), 0, Integer.MAX_VALUE);
191  ui.enter();
192  */
193 
194 
195  // Place cursor at end of inserted text.
196 // editor.selectAndReveal(selectionOffset, insertString.length());
197 // editor.setHighlightRange(selectionOffset, insertString.length(), true);
198 
199  }
200 
201  }
202 
203  }
204 
205  private ITextEditor getActiveEditor() {
206 
207  IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
208  IWorkbenchPage page = window.getActivePage();
209  IEditorPart editorPart = page.getActiveEditor();
210 
211  return (ITextEditor) editorPart.getAdapter(ITextEditor.class);
212 
213  }
214 
215  private String indent(String text, int spaceCount, int skipLines) {
216 
217  String lineSeparator = System.getProperty("line.separator");
218 
219  StringBuilder stringBuilder = new StringBuilder();
220  Scanner scanner = new Scanner(text);
221 
222  for (int lineNo = 1; scanner.hasNextLine(); lineNo++) {
223 
224  if (lineNo > 1)
225  stringBuilder.append(lineSeparator);
226 
227  if (lineNo > skipLines)
228  for (int i = 0; i < spaceCount; i++)
229  stringBuilder.append(" ");
230 
231  stringBuilder.append(scanner.nextLine());
232 
233  }
234 
235  scanner.close();
236 
237  return stringBuilder.toString();
238 
239  }
240 
241 
242 }