Gridarta Editor
CFPythonPopup.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.textedit.scripteditor;
21 
22 import java.awt.Color;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import javax.swing.JComboBox;
26 import javax.swing.JPopupMenu;
27 import javax.swing.text.BadLocationException;
29 import org.apache.log4j.Category;
30 import org.apache.log4j.Logger;
31 import org.jetbrains.annotations.NotNull;
32 
44 public class CFPythonPopup extends JComboBox<Object> {
45 
49  @NotNull
50  private static final Category LOG = Logger.getLogger(CFPythonPopup.class);
51 
55  private static final long serialVersionUID = 1L;
56 
61  @NotNull
62  private final JPopupMenu menu;
63 
68  private final boolean isReady;
69 
74  private int caretPos;
75 
81  public CFPythonPopup(@NotNull final ScriptEditControl scriptEditControl, @NotNull final MenuEntries menuEntries) {
82  setBackground(Color.white); // white background
83 
84  menu = new CFPythonPopupMenu(this);
85 
86  for (final String menuEntry : menuEntries) {
87  addItem(" " + menuEntry);
88  }
89 
90  // listener for selection events
91  addActionListener(new MenuActionListener(this, scriptEditControl));
92 
93  isReady = !menuEntries.isEmpty();
94 
95  setRequestFocusEnabled(true);
96  }
97 
103  public boolean isInitialized() {
104  return isReady;
105  }
106 
111  public void setCaretPosition(final int pos) {
112  caretPos = pos;
113  menu.requestFocus();
115  }
116 
117  @NotNull
118  public JPopupMenu getMenu() {
119  return menu;
120  }
121 
126  private class MenuActionListener implements ActionListener {
127 
128  @NotNull
129  private final ScriptEditControl control;
130 
131  @NotNull
132  private final CFPythonPopup popup;
133 
134  private boolean ignore; // while true, all ActionEvents get ignored
135 
136  private MenuActionListener(@NotNull final CFPythonPopup popup, @NotNull final ScriptEditControl control) {
137  this.popup = popup;
138  this.control = control;
139  }
140 
141  @Override
142  public void actionPerformed(@NotNull final ActionEvent e) {
143  if (!ignore) {
144  // get method name to insert
145  String method = popup.getSelectedItem().toString();
146  method = method.substring(0, method.indexOf('(')).trim() + "()";
147 
148  final JEditTextArea activeTextArea = control.getActiveTextArea();
149  if (activeTextArea != null) {
150  try {
151  // insert method into the document
152  activeTextArea.getDocument().insertString(caretPos, method, null);
153  } catch (final BadLocationException ex) {
154  LOG.error("BadLocationException", ex);
155  }
156  }
157 
158  ignore = true;
159  popup.setSelectedIndex(0);
160  ignore = false;
161  popup.getMenu().setVisible(false); // in some JRE versions, this doesn't happen automatically
162  }
163  }
164 
165  }
166 
167 }
net.sf.gridarta.textedit.textarea.JEditTextArea.getDocument
Document getDocument()
Returns the document this text area is editing.
Definition: JEditTextArea.java:500
net.sf.gridarta.textedit.textarea.JEditTextArea
jEdit's text area component.
Definition: JEditTextArea.java:91
net.sf.gridarta.textedit.textarea
This package contains the other part of the script editor.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.CFPythonPopup
CFPythonPopup(@NotNull final ScriptEditControl scriptEditControl, @NotNull final MenuEntries menuEntries)
Creates a new instance.
Definition: CFPythonPopup.java:81
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.setCaretPosition
void setCaretPosition(final int pos)
Set the caret position where this menu has been invoked.
Definition: CFPythonPopup.java:111
net.sf
net.sf.gridarta.textedit.scripteditor.ScriptEditControl.getActiveTextArea
JEditTextArea getActiveTextArea()
Definition: ScriptEditControl.java:418
net.sf.gridarta.textedit
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.MenuActionListener.actionPerformed
void actionPerformed(@NotNull final ActionEvent e)
Definition: CFPythonPopup.java:142
net
net.sf.gridarta.textedit.scripteditor.MenuEntries
List of menu entries (all CFPython commands).
Definition: MenuEntries.java:42
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.isReady
final boolean isReady
Whether this menu has been fully initialized.
Definition: CFPythonPopup.java:68
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.MenuActionListener.MenuActionListener
MenuActionListener(@NotNull final CFPythonPopup popup, @NotNull final ScriptEditControl control)
Definition: CFPythonPopup.java:136
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.menu
final JPopupMenu menu
The popup menu.
Definition: CFPythonPopup.java:62
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.MenuActionListener.ignore
boolean ignore
Definition: CFPythonPopup.java:134
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.MenuActionListener.control
final ScriptEditControl control
Definition: CFPythonPopup.java:129
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.LOG
static final Category LOG
The Logger for printing log messages.
Definition: CFPythonPopup.java:50
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.MenuActionListener
Subclass MenuActionListener handles the action events for the menu items.
Definition: CFPythonPopup.java:126
net.sf.gridarta.textedit.scripteditor.CFPythonPopup
This class implements a popup window which shows all python methods in the 'CFPython' package.
Definition: CFPythonPopup.java:44
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.caretPos
int caretPos
The caret position in the document where this popup was opened.
Definition: CFPythonPopup.java:74
net.sf.gridarta.textedit.scripteditor.CFPythonPopupMenu
MenuHelper class, inherits from JPopupMenu.
Definition: CFPythonPopupMenu.java:30
net.sf.gridarta.textedit.scripteditor.ScriptEditControl
ScriptEditControl - Manages events and data flow for the script editor entity.
Definition: ScriptEditControl.java:59
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.isInitialized
boolean isInitialized()
Returns whether this popup menu has been fully initialized and is ready for use.
Definition: CFPythonPopup.java:103
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.serialVersionUID
static final long serialVersionUID
Serial Version UID.
Definition: CFPythonPopup.java:55
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.getMenu
JPopupMenu getMenu()
Definition: CFPythonPopup.java:118
net.sf.gridarta.textedit.scripteditor.CFPythonPopup.MenuActionListener.popup
final CFPythonPopup popup
Definition: CFPythonPopup.java:132
net.sf.gridarta.textedit.scripteditor.ScriptEditControl.registerActivePopup
static void registerActivePopup(@NotNull final CFPythonPopup activePopup)
Register last active popup.
Definition: ScriptEditControl.java:138