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-2015 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 }
static void registerActivePopup(@NotNull final CFPythonPopup activePopup)
Register last active popup.
MenuActionListener(@NotNull final CFPythonPopup popup, @NotNull final ScriptEditControl control)
boolean isInitialized()
Returns whether this popup menu has been fully initialized and is ready for use.
static final Category LOG
The Logger for printing log messages.
int caretPos
The caret position in the document where this popup was opened.
final boolean isReady
Whether this menu has been fully initialized.
static final long serialVersionUID
Serial Version UID.
void setCaretPosition(final int pos)
Set the caret position where this menu has been invoked.
Base package of all Gridarta classes.
Subclass MenuActionListener handles the action events for the menu items.
This package contains the other part of the script editor.
ScriptEditControl - Manages events and data flow for the script editor entity.
Document getDocument()
Returns the document this text area is editing.
MenuHelper class, inherits from JPopupMenu.
This class implements a popup window which shows all python methods in the &#39;CFPython&#39; package...
List of menu entries (all CFPython commands).
CFPythonPopup(@NotNull final ScriptEditControl scriptEditControl, @NotNull final MenuEntries menuEntries)
Creates a new instance.