20 package net.sf.gridarta.textedit.scripteditor;
22 import java.awt.Frame;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.Writer;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.prefs.Preferences;
31 import javax.swing.Action;
32 import javax.swing.JFileChooser;
33 import javax.swing.JOptionPane;
34 import javax.swing.filechooser.FileFilter;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import net.
sf.japi.swing.action.ActionMethod;
43 import org.apache.log4j.Category;
44 import org.apache.log4j.Logger;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
66 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
77 private final List<String>
tabs;
91 public ScriptEditControl(@NotNull
final FileFilter scriptFileFilter, @NotNull
final String scriptSuffix, @NotNull
final Frame owner,
final File defaultScriptDir, @NotNull
final Preferences preferences, @NotNull
final Exiter exiter) {
94 tabs =
new ArrayList<>();
119 view.
addTab(
"<New Script>", null);
126 final File file =
new File(pathName);
128 if (!file.exists() || !file.isFile()) {
129 if (LOG.isInfoEnabled()) {
130 LOG.info(
"Error in ScriptEditControl.openScriptFile():");
131 LOG.info(
" File '" + pathName +
"' doesn't exist.");
136 tabs.add(file.getAbsolutePath());
137 view.
addTab(file.getName(), file);
148 final JFileChooser fileChooser =
new JFileChooser();
149 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
150 fileChooser.setMultiSelectionEnabled(
false);
151 fileChooser.setFileFilter(scriptFileFilter);
154 if (defaultScriptDir.exists() && defaultScriptDir.isDirectory()) {
166 openFileChooser.setDialogTitle(
"Open Script File");
169 final int returnVal = openFileChooser.showOpenDialog(view);
170 if (returnVal != JFileChooser.APPROVE_OPTION) {
174 final File file = openFileChooser.getSelectedFile();
175 if (file.exists() && !file.isDirectory()) {
196 if (activeTextArea != null && activeTextArea.
isModified() && !ACTION_BUILDER.showQuestionDialog(view,
"scriptEdit.confirmClose", title)) {
207 if (activePopup != null && (activePopup.isShowing() || activePopup.
getMenu().isShowing())) {
208 activePopup.
getMenu().setVisible(
false);
210 view.setVisible(
false);
235 final String activePath = getActiveFilePath();
237 if (activeTextArea == null) {
240 final String text = activeTextArea.
getText();
244 openFileChooser.setDialogTitle(
"Save Script File As");
247 if (activePath != null) {
248 final File file =
new File(activePath);
250 if (file.getParentFile().exists() && file.getParentFile().isDirectory()) {
251 openFileChooser.setCurrentDirectory(file.getParentFile());
252 openFileChooser.setSelectedFile(file);
256 final int returnVal = openFileChooser.showSaveDialog(view);
257 if (returnVal != JFileChooser.APPROVE_OPTION) {
261 File file = openFileChooser.getSelectedFile();
262 if (!file.getName().endsWith(scriptSuffix)) {
263 final String fileName = file.getAbsolutePath();
264 file =
new File(fileName + scriptSuffix);
268 if (!file.exists() || (activePath != null && file.getAbsolutePath().equals(activePath)) || view.
askConfirm(
"Overwrite?",
"A file named \"" + file.getName() +
"\" already exists.\n" +
"Are you sure you want to overwrite it?")) {
275 if (tabIndex >= 0 && tabs.size() > tabIndex) {
281 tabs.set(tabIndex, file.getAbsolutePath());
291 if (getActiveFilePath() != null) {
292 final File file =
new File(getActiveFilePath());
298 LOG.error(
"ScriptEditControl.saveActiveTab(): Cannot save file without name!");
311 public boolean saveTextToFile(@NotNull
final File file, @NotNull
final String text) {
313 try (FileOutputStream fos =
new FileOutputStream(file)) {
314 try (Writer osw =
new OutputStreamWriter(fos)) {
318 }
catch (
final IOException e) {
320 view.
showMessage(
"Write Error",
"The file \"" + file.getName() +
"\" could not be written.\nPlease use the 'Save As...' menu.", JOptionPane.ERROR_MESSAGE);
339 String getActiveFilePath() {
347 if (path == null || path.isEmpty() || path.equals(
"<>")) {
355 public void setAction(@NotNull
final Action action, @NotNull
final String name) {
final List< String > tabs
void setTitleAt(final int index, @NotNull final String title)
Sets the title of the tab at specified index.
boolean isModified()
Return whether the text content has been modified from the "unmodified" state.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
static final ActionBuilder ACTION_BUILDER
Action Builder.
final ScriptEditView view
static void registerActivePopup(@NotNull final CFPythonPopup activePopup)
Register last active popup.
Utility class for JFileChooser related functions.
ScriptEditControl(@NotNull final FileFilter scriptFileFilter, @NotNull final String scriptSuffix, @NotNull final Frame owner, final File defaultScriptDir, @NotNull final Preferences preferences, @NotNull final Exiter exiter)
void openScriptFile(@NotNull final String pathName)
Open a new empty script document.
void showMessage(@NotNull final String title, @NotNull final String message, final int messageType)
Shows the given message in the UI.
boolean closeAllTabs()
Close all opened script-tabs.
Base package of all Gridarta classes.
static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir)
Calls JFileChooser#setCurrentDirectory(File).
boolean askConfirm(@NotNull final String title, @NotNull final String message)
Shows the given confirmation message as popup frame.
void openUser()
Open a file which is chosen by the user.
void setTextAreaDefaults(@NotNull final TextAreaDefaults textAreaDefaults)
This package contains the other part of the script editor.
String getActiveTitle()
Returns the title of the active tab.
JFileChooser createOpenFileChooser(@NotNull final File defaultScriptDir)
Creates the JFileChooser for opening a script file.
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.
ScriptEditControl - Manages events and data flow for the script editor entity.
String getText()
Returns the entire text of this text area.
void resetModified()
Reset the "modified" state.
final FileFilter scriptFileFilter
void saveAsActiveTab()
Open a file browser and prompt the user for a location/name to store this file.
void setTextAreaDefaults(@NotNull final TextAreaDefaults textAreaDefaults)
static CFPythonPopup activePopup
boolean closeActiveTab()
Close the active script-tab.
JEditTextArea getActiveTextArea()
static void sanitizeCurrentDirectory(@NotNull final JFileChooser fileChooser)
Makes sure the current directory of a JFileChooser is valid.
void newScript()
Open a new empty script document.
jEdit's text area component.
void saveActiveTab()
Save the active script-tab to the stored file path.
final String scriptSuffix
final JFileChooser openFileChooser
JFileChooser for opening script files.
Encapsulates default settings for a text area.
boolean saveTextToFile(@NotNull final File file, @NotNull final String text)
Write the given text into the specified file.
static final Category LOG
The Logger for printing log messages.