package jas2.swingstudio;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import jas2.util.*;
import jas2.util.xmlmenus.*;
public class JASConsoleTextArea extends JTextArea implements HasCommandProcessor {
public JASConsoleTextArea(JASConsoleScrollPane sp) {
super();
theScrollPane = sp;
loggingOn = false;
logFileOpened = false;
commandProcessor = new JASConsoleTextAreaCommandProcessor();
theJASConsoleTabbedPanePopupMenu =
JavaAnalysisStudio.getApp().getXMLMenuBuilder().getPopupMenu("consolePopupMenu");
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public CommandProcessor getCommandProcessor()
{
return commandProcessor;
}
protected JASConsoleTextArea getJASConsoleTextArea()
{
return this;
}
protected void processMouseEvent(final MouseEvent e) {
super.processMouseEvent(e);
if (e.isPopupTrigger()) {
commandProcessor.forceSetChanged();
theJASConsoleTabbedPanePopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
public boolean isLogFileOpened() {
return logFileOpened;
}
public void setLogFileOpened(boolean newState) {
logFileOpened = newState;
}
public boolean isLoggingOn() {
return loggingOn;
}
public void setLoggingOn(boolean newState) {
loggingOn = newState;
}
public void forceCommandProcessorSetChanged() {
commandProcessor.forceSetChanged();
}
protected String lineSeparator = (String)System.getProperty("line.separator");
private boolean logFileOpened;
private boolean loggingOn;
private JPopupMenu theJASConsoleTabbedPanePopupMenu;
public FileWriter getLogWriter() {
return logOutput;
}
private FileWriter logOutput;
private JASConsoleTextAreaCommandProcessor commandProcessor;
private JASConsoleScrollPane theScrollPane;
private class JASConsoleTextAreaCommandProcessor extends JASCommandProcessor {
public JASConsoleTextAreaCommandProcessor() {
super();
}
public void forceSetChanged()
{
setChanged();
}
public void onCopyConsoleSelection() {
copy();
}
public void enableCopyConsoleSelection(JASState state) {
state.setEnabled(getJASConsoleTextArea().getSelectedText() != null);
}
public void onClear() {
JASConsoleDocumentUtilities.clearDoc(getDocument());
}
public void enableClear(JASState state) {
state.setEnabled(getDocument().getLength() > 0);
}
public void onSelectAll() {
selectAll();
}
public void enableSelectAll(JASState state) {
state.setEnabled(getDocument().getLength() > 0);
}
public void onSetBufferSize() {
}
public void enableSetBufferSize(JASState state) {
state.setEnabled(false);
//disable this until the buffer size is in lines
}
public void onCloseTab() {
if (isLoggingOn()) {
setLogFileOpened(false);
setLoggingOn(false);
}
theScrollPane.removeTab();
}
public void enableCloseTab(JASState state) {
//Every text pane can be closed.
state.setEnabled(true);
}
public void onOpenLogFile() {
JFileChooser chooser = new JFileChooser(
JavaAnalysisStudio.getApp().getUserProperties().getString(
"LastProgramPath", System.getProperty("user.home")));
chooser.setDialogTitle("Open or Create Log File...");
int returnVal = chooser.showOpenDialog(JASConsoleTextArea.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
final File f = chooser.getSelectedFile();
boolean failed = false;
if (f.exists()) {
if (!f.canWrite()) {
JavaAnalysisStudio.getApp().error("Cannot write to the file " + f);
failed = true;
} else {
if (!f.delete()) {
JavaAnalysisStudio.getApp().error("Could not overwrite the file " + f);
failed = true;
}
}
}
if (!failed) {
try {
logOutput = new FileWriter(f);
logOutput.write("Log file created on " + new Date() + lineSeparator);
logOutput.flush();
setLogFileOpened(true);
} catch (IOException eh) {
JavaAnalysisStudio.getApp().error("Couldn't write to " + f.getName());
}
}
}
if (!isLoggingOn()) {
onEnableLogging(true);
}
}
public void enableOpenLogFile(JASState state) {
state.setEnabled(!isLogFileOpened());
}
public void onEnableLogging(boolean b) {
setLoggingOn(b);
setChanged();
}
public void enableEnableLogging(JASCheckboxState state) {
state.setCheckbox(isLoggingOn());
state.setEnabled(isLogFileOpened());
}
public void onCloseLogFile() {
try {
logOutput.write("Log file closed on " + new Date() + lineSeparator);
logOutput.flush();
logOutput.close();
} catch (IOException eek) {
}
setLogFileOpened(false);
setLoggingOn(false);
}
public void enableCloseLogFile(JASState state) {
state.setEnabled(isLogFileOpened());
}
}
}
Ads help maintain this website.