Gridarta Editor
FunctionMenu.java
Go to the documentation of this file.
1 /*
2  * InputHandler.java - Manages key bindings and executes actions
3  * Copyright (C) 1999 Slava Pestov
4  * Copyright (C) 2000-2015 The Gridarta Developers.
5  *
6  * You may use and modify this package for any purpose. Redistribution is
7  * permitted, in both source and binary form, provided that this notice
8  * remains intact in all source distributions of this package.
9  */
10 
11 package net.sf.gridarta.textedit.textarea.actions;
12 
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import javax.swing.text.BadLocationException;
21 import org.jetbrains.annotations.NotNull;
22 
28 public class FunctionMenu implements ActionListener {
29 
30  // popup menu for CFPython function
31 
33 
39  public FunctionMenu(@NotNull final ScriptEditControl scriptEditControl, @NotNull final MenuEntries menuEntries) {
40  cfPythonPopup = new CFPythonPopup(scriptEditControl, menuEntries);
41  }
42 
47  @Override
48  public void actionPerformed(final ActionEvent e) {
49  final JEditTextArea textArea = InputHandler.getTextArea(e);
50  final int caretPos = textArea.getCaretPosition(); // caret position
51 
52  try {
53  // insert the '.' character
54  final String selectedText = textArea.getSelectedText();
55  if (selectedText != null && !selectedText.isEmpty()) {
56  textArea.setSelectedText(".");
57  } else {
58  textArea.getDocument().insertString(caretPos, ".", null);
59  }
60 
61  if (caretPos >= 7 && cfPythonPopup.isInitialized()) {
62  final String fileName = textArea.getDocument().getText(textArea.getCaretPosition() - 9, 8);
63  if (fileName.equalsIgnoreCase("cfpython")) {
64  final int line = textArea.getCaretLine();
65  final int offset = textArea.getCaretPosition() - textArea.getLineStartOffset(line);
66 
67  cfPythonPopup.setCaretPosition(caretPos + 1);
68  cfPythonPopup.getMenu().show(textArea, textArea.offsetToX2(line, offset), 30 + textArea.lineToY(line));
69  }
70  }
71  } catch (final BadLocationException ignored) {
72  }
73  }
74 
75 }
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.
int lineToY(final int line)
Converts a line index to a y co-ordinate.
void actionPerformed(final ActionEvent e)
Get content of the system clipboard and insert it at caret position.
boolean isInitialized()
Returns whether this popup menu has been fully initialized and is ready for use.
void setCaretPosition(final int pos)
Set the caret position where this menu has been invoked.
An input handler converts the user's key strokes into concrete actions.
Base package of all Gridarta classes.
This package contains the classes for the script editor used within the editor to create and modify P...
Definition: Actions.java:20
int offsetToX2(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
String getSelectedText()
Returns the selected text, or null if no selection is active.
This package contains the other part of the script editor.
int getCaretPosition()
Returns the caret position.
ScriptEditControl - Manages events and data flow for the script editor entity.
Document getDocument()
Returns the document this text area is editing.
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
This class implements a popup window which shows all python methods in the 'CFPython' package...
List of menu entries (all CFPython commands).
FunctionMenu(@NotNull final ScriptEditControl scriptEditControl, @NotNull final MenuEntries menuEntries)
Creates a new instance.