20 package net.sf.gridarta.textedit.scripteditor;
22 import java.awt.Frame;
23 import java.awt.Rectangle;
24 import java.awt.geom.RectangularShape;
25 import java.io.BufferedReader;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.prefs.Preferences;
34 import javax.swing.JDialog;
35 import javax.swing.JOptionPane;
36 import javax.swing.JTabbedPane;
37 import javax.swing.SwingConstants;
38 import javax.swing.event.ChangeEvent;
39 import javax.swing.event.ChangeListener;
40 import javax.swing.text.BadLocationException;
47 import net.
sf.japi.swing.action.ActionBuilder;
48 import net.
sf.japi.swing.action.ActionBuilderFactory;
49 import org.apache.log4j.Category;
50 import org.apache.log4j.Logger;
51 import org.jetbrains.annotations.NotNull;
52 import org.jetbrains.annotations.Nullable;
71 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
89 private static final String
WINDOW_X =
"ScriptEditWindow.x";
96 private static final String
WINDOW_Y =
"ScriptEditWindow.y";
136 super(owner,
"Script Pad");
137 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
139 textAreas =
new ArrayList<>();
140 actions =
new Actions(control);
142 setJMenuBar(ACTION_BUILDER.createMenuBar(
true,
"scriptEditMenu"));
144 tabPane =
new JTabbedPane(SwingConstants.TOP);
147 getContentPane().add(tabPane);
151 final RectangularShape screen = getGraphicsConfiguration().getBounds();
152 final int width = preferences.getInt(WINDOW_WIDTH, (
int) (0.8 * screen.getWidth()));
153 final int height = preferences.getInt(WINDOW_HEIGHT, (
int) (0.8 * screen.getHeight()));
154 final int x = preferences.getInt(WINDOW_X, (
int) (screen.getX() + (screen.getWidth() - (double) width) / 2.0));
155 final int y = preferences.getInt(WINDOW_Y, (
int) (screen.getY() + (screen.getHeight() - (double) height) / 2.0));
156 setBounds(x, y, width, height);
161 public void preExitNotify() {
166 public void appExitNotify() {
167 final Rectangle bounds = getBounds();
168 preferences.putInt(WINDOW_X, bounds.x);
169 preferences.putInt(WINDOW_Y, bounds.y);
170 preferences.putInt(WINDOW_WIDTH, bounds.width);
171 preferences.putInt(WINDOW_HEIGHT, bounds.height);
175 public void waitExitNotify() {
180 exiter.addExiterListener(exiterListener);
193 public void addTab(@NotNull
final String title, @Nullable
final File file) {
200 tabPane.addTab(title, ta);
212 update(getGraphics());
215 if (file != null && file.exists()) {
218 try (FileInputStream fis =
new FileInputStream(file)) {
219 try (InputStreamReader isr =
new InputStreamReader(fis)) {
220 try (BufferedReader in =
new BufferedReader(isr)) {
221 boolean firstLine =
true;
222 final StringBuilder buff =
new StringBuilder(
"");
224 final String line = in.readLine();
235 syntaxDocument.insertString(0, buff.toString(), null);
240 }
catch (
final FileNotFoundException e) {
241 if (LOG.isInfoEnabled()) {
242 LOG.info(
"addTab(): File '" + file.getName() +
"' not found.");
244 }
catch (
final IOException e) {
245 if (LOG.isInfoEnabled()) {
246 LOG.info(
"addTab(): I/O-Error while reading '" + file.getName() +
"'.");
248 }
catch (
final BadLocationException e) {
249 if (LOG.isInfoEnabled()) {
250 LOG.info(
"addTab(): Bad Location in Document!");
253 scriptEditUndoActions.
resetUndo(syntaxDocument);
268 if (textAreas.isEmpty()) {
271 final int oldIndex = tabPane.getSelectedIndex();
272 final int newIndex = oldIndex >= tabPane.getTabCount() - 1 ? oldIndex - 1 : oldIndex;
273 tabPane.setSelectedIndex(newIndex);
274 scriptEditUndoActions.
removeDocument(textAreas.get(oldIndex).getDocument());
275 textAreas.remove(oldIndex);
276 tabPane.remove(oldIndex);
278 scriptEditUndoActions.
setCurrentDocument(newIndex == -1 ? null : textAreas.get(newIndex).getDocument());
288 return textAreas.get(tabPane.getSelectedIndex());
298 return tabPane.getSelectedIndex();
305 return tabPane.getTabCount();
313 public void setTitleAt(
final int index, @NotNull
final String title) {
314 tabPane.setTitleAt(index, title);
323 return getTabCount() > 0 ? tabPane.getTitleAt(tabPane.getSelectedIndex()) : null;
334 public boolean askConfirm(@NotNull
final String title, @NotNull
final String message) {
335 return JOptionPane.showConfirmDialog(
this, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION;
345 public void showMessage(@NotNull
final String title, @NotNull
final String message,
final int messageType) {
346 JOptionPane.showMessageDialog(
this, message, title, messageType);
349 public void showMessage(@NotNull
final String title, @NotNull
final String message) {
350 JOptionPane.showMessageDialog(
this, message, title, JOptionPane.INFORMATION_MESSAGE);
374 scriptEditUndoActions.
setCurrentDocument(index == -1 ? null : textAreas.get(index).getDocument());
void setTitleAt(final int index, @NotNull final String title)
Sets the title of the tab at specified index.
void setCurrentDocument(@Nullable final Document currentDocument)
Sets the current document.
static final String WINDOW_X
The key used to store the editor window x-coordinate in preferences file.
A document implementation that can be tokenized by the syntax highlighting system.
void setEditingFocus()
Sets the focus to this TextArea, so this component is instantly registered for key press events...
void resetUndo(@NotNull final Document document)
Forget all undo-able operations for a document.
final List< JEditTextArea > textAreas
boolean getPaintInvalid()
static TokenMarker createTokenMarker(@Nullable final File file)
Creates a suitable TokenMarker for a given file.
static final ActionBuilder ACTION_BUILDER
Action Builder.
final JTabbedPane tabPane
void showMessage(@NotNull final String title, @NotNull final String message, final int messageType)
Shows the given message in the UI.
final Actions actions
The actions for the script editor.
Implements undo and redo actions.
Base package of all Gridarta classes.
void removeDocument(@NotNull final Document document)
Removes a document.
static final long serialVersionUID
Serial Version UID.
boolean askConfirm(@NotNull final String title, @NotNull final String message)
Shows the given confirmation message as popup frame.
A factory for creatingTokenMarker instances for Files.
final ScriptEditView view
Actions used by the script editor.
Interface for listeners interested in Exiter related events.
EditTabListener(@NotNull final ScriptEditView view)
This package contains the other part of the script editor.
Listener for close box on the window.
String getActiveTitle()
Returns the title of the active tab.
void closeActiveTab()
Closes the active script-tab.
void addTab(@NotNull final String title, @Nullable final File file)
Adds a new TextArea Panel to the TabbedPane.
ScriptEditView(@NotNull final ScriptEditControl control, @NotNull final Frame owner, @NotNull final Preferences preferences, @NotNull final Exiter exiter)
Builds a frame but keep it hidden (it is shown when first file is opened).
ScriptEditControl - Manages events and data flow for the script editor entity.
static final String WINDOW_WIDTH
The key used to store the editor window width in preferences file.
void resetModified()
Reset the "modified" state.
void stateChanged(@NotNull final ChangeEvent e)
void refresh()
Refreshes the enable/disable state of all menus.
void showMessage(@NotNull final String title, @NotNull final String message)
void setTextAreaDefaults(@NotNull final TextAreaDefaults textAreaDefaults)
JEditTextArea getActiveTextArea()
Inner class: Listener for ChangeEvents in the tabPane.
TextAreaDefaults textAreaDefaults
The TextAreaDefaults for tabs.
void addDocument(@NotNull final Document document)
Adds a document.
final ScriptEditUndoActions scriptEditUndoActions
The undo related actions for the script editor.
jEdit's text area component.
static final Category LOG
The Logger for printing log messages.
static final String WINDOW_Y
The key used to store the editor window y-coordinate in preferences file.
static final String WINDOW_HEIGHT
The key used to store the editor window height in preferences file.
void setTokenMarker(@Nullable final TokenMarker tokenMarker)
Sets the token marker that is to be used to split lines of this document up into tokens.
Encapsulates default settings for a text area.