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);
142 setJMenuBar(
ACTION_BUILDER.createMenuBar(
true,
"scriptEditMenu"));
144 tabPane =
new JTabbedPane(SwingConstants.TOP);
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() - width) / 2.0));
155 final int y = preferences.getInt(
WINDOW_Y, (
int) (screen.getY() + (screen.getHeight() - 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);
175 public void waitExitNotify() {
180 exiter.addExiterListener(exiterListener);
193 public void addTab(@NotNull
final String title, @Nullable
final File file) {
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!");
271 final int oldIndex =
tabPane.getSelectedIndex();
272 final int newIndex = oldIndex >=
tabPane.getTabCount() - 1 ? oldIndex - 1 : oldIndex;
273 tabPane.setSelectedIndex(newIndex);
294 return tabPane.getSelectedIndex();
309 public void setTitleAt(
final int index, @NotNull
final String title) {
310 tabPane.setTitleAt(index, title);
330 public boolean askConfirm(@NotNull
final String title, @NotNull
final String message) {
331 return JOptionPane.showConfirmDialog(
this, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION;
341 public void showMessage(@NotNull
final String title, @NotNull
final String message,
final int messageType) {
342 JOptionPane.showMessageDialog(
this, message, title, messageType);
345 public void showMessage(@NotNull
final String title, @NotNull
final String message) {
346 JOptionPane.showMessageDialog(
this, message, title, JOptionPane.INFORMATION_MESSAGE);