Gridarta Editor
MenuEntries.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.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.io.Serializable;
28 import java.io.UnsupportedEncodingException;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Iterator;
32 import java.util.List;
33 import net.sf.gridarta.utils.IOUtils;
34 import org.apache.log4j.Category;
35 import org.apache.log4j.Logger;
36 import org.jetbrains.annotations.NotNull;
37 
42 public class MenuEntries implements Iterable<String>, Serializable {
43 
47  private static final long serialVersionUID = 1L;
48 
52  @NotNull
53  private static final String PYTHON_MENU_FILE = "/cfpython_menu.def";
54 
58  @NotNull
59  private static final Category LOG = Logger.getLogger(MenuEntries.class);
60 
65  @NotNull
66  private final List<String> menuEntries = new ArrayList<>();
67 
71  public MenuEntries() {
72  try {
73  final InputStream inputStream = CFPythonPopup.class.getResourceAsStream(PYTHON_MENU_FILE);
74  if (inputStream == null) {
75  LOG.error("Resource '" + PYTHON_MENU_FILE + "' not found");
76  return;
77  }
78  try {
79  try (Reader reader = new InputStreamReader(inputStream, IOUtils.MAP_ENCODING)) {
80  try (BufferedReader bufferedReader = new BufferedReader(reader)) {
81  // read file into the cmdList vector:
82  while (true) {
83  final String inputLine = bufferedReader.readLine();
84  if (inputLine == null) {
85  break;
86  }
87  final String line = inputLine.trim();
88  if (!line.isEmpty() && !line.startsWith("#")) {
89  // ATM, the descriptive info about method headers is cut out
90  // (TODO: parse and show the full info in a status bar)
91  final int k = line.indexOf('(');
92  if (k > 0) {
93  menuEntries.add(line.substring(0, k) + "()");
94  } else {
95  LOG.error("Parse error in " + PYTHON_MENU_FILE + ":");
96  LOG.error(" \"" + line + "\" missing '()'");
97  menuEntries.add(line + "()"); // that line is probably garbage, but will work
98  }
99  }
100  }
101  menuEntries.sort(String.CASE_INSENSITIVE_ORDER);
102  }
103  }
104  } finally {
105  inputStream.close();
106  }
107  } catch (final UnsupportedEncodingException ex) {
108  LOG.error("Cannot decode file '" + PYTHON_MENU_FILE + "': " + ex.getMessage());
109  } catch (final IOException ex) {
110  LOG.error("Cannot read file '" + PYTHON_MENU_FILE + "': " + ex.getMessage());
111  }
112  }
113 
114  @NotNull
115  @Override
116  public Iterator<String> iterator() {
117  return Collections.unmodifiableCollection(menuEntries).iterator();
118  }
119 
124  public boolean isEmpty() {
125  return menuEntries.isEmpty();
126  }
127 
128 }
net.sf.gridarta.textedit.scripteditor.MenuEntries.isEmpty
boolean isEmpty()
Returns whether no menu entries have been loaded.
Definition: MenuEntries.java:124
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.textedit.scripteditor.MenuEntries.menuEntries
final List< String > menuEntries
List of menu entries (all CFPython commands).
Definition: MenuEntries.java:66
net.sf
net.sf.gridarta.textedit.scripteditor.MenuEntries.LOG
static final Category LOG
The Logger for printing log messages.
Definition: MenuEntries.java:59
net.sf.gridarta.textedit.scripteditor.MenuEntries.iterator
Iterator< String > iterator()
Definition: MenuEntries.java:116
net
net.sf.gridarta.textedit.scripteditor.MenuEntries
List of menu entries (all CFPython commands).
Definition: MenuEntries.java:42
net.sf.gridarta.textedit.scripteditor.MenuEntries.MenuEntries
MenuEntries()
Creates a new instance.
Definition: MenuEntries.java:71
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.MenuEntries.PYTHON_MENU_FILE
static final String PYTHON_MENU_FILE
Python menu definitions.
Definition: MenuEntries.java:53
net.sf.gridarta.textedit.scripteditor.MenuEntries.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: MenuEntries.java:47
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:52