Commit 11e64229 authored by Christophe Demarey's avatar Christophe Demarey

Add an editor able to load procedures / execute commands / load content from a file on the FS.

parent 9fe2f1b5
......@@ -67,6 +67,17 @@
<artifactId>frascati-fscript</artifactId>
</dependency>
<!-- Swing Application framework -->
<dependency>
<groupId>org.jdesktop</groupId>
<artifactId>appframework</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swing-layout</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
<!-- ========= -->
......
......@@ -28,27 +28,44 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.StringTokenizer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import org.mortbay.log.Log;
import org.objectweb.fractal.fscript.FScriptException;
import org.objectweb.fractal.fscript.InvalidScriptException;
import org.ow2.frascati.explorer.ExplorerGUI;
import org.ow2.frascati.factory.runtime.domain.api.DomainConfig;
import org.ow2.frascati.fscript.FraSCAtiScript;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.swing.JFileChooser;
/**
* A GUI console allowing to execute FraSCAtiScript queries / commands.
*
......@@ -59,11 +76,21 @@ public class Console extends JFrame {
private static final String prompt = "FraSCAtiScript> ";
/** Graphical components. */
private javax.swing.JTextField input = new JTextField(40);
private javax.swing.JScrollPane outputScrollPane = new JScrollPane();
private javax.swing.JTextPane output = new JTextPane();
private JTextField input = new JTextField(40);
private JScrollPane outputScrollPane = new JScrollPane();
private JTextPane output = new JTextPane();
private JPanel editorPanel = new JPanel();
private JButton loadButton = new JButton();
private JButton editorButton = new JButton();
private JButton executeButton = new JButton();
private JButton registerButton = new JButton();
private JEditorPane editorPane = new JEditorPane();
private JScrollPane editorScrollPane= new JScrollPane();
/** the output document */
private StyledDocument doc = output.getStyledDocument();
/** The {@link JFileChooser} instance used to load scripts. */
protected static JFileChooser fileChooser = null;
/**
* Default constructor.
......@@ -96,26 +123,217 @@ public class Console extends JFrame {
input.setMinimumSize( new Dimension(4,19) );
input.setMaximumSize( new Dimension(800,19) );
input.addActionListener(
new ActionListener() {
input.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd = input.getText();
if ( cmd.trim().length() > 0 ) {
executeCommand( input.getText() );
} else {
// Display the command on the output console
try {
doc.insertString( doc.getLength(), prompt+"\n", doc.getStyle("prompt") );
} catch (BadLocationException e2) {
e2.printStackTrace(); // Should not happen
}
input.setText("");
inputActionPerformed(e);
}
});
editorButton.setText("Editor");
editorButton.setName("editorButton");
editorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editorButtonActionPerformed(e);
}
});
editorPane.setName("editorPane");
editorScrollPane.setName("editorScrollPane");
editorScrollPane.setViewportView(editorPane);
loadButton.setText("Load script from filesystem");
loadButton.setName("LoadButton");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadButtonActionPerformed(e);
}
});
registerButton.setText("Register procedures");
registerButton.setName("registerButton");
registerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
registerButtonActionPerformed(e);
}
});
executeButton.setText("Execute");
executeButton.setName("executeButton");
executeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
executeButtonActionPerformed(e);
}
});
// Layout
org.jdesktop.layout.GroupLayout editorPanelLayout = new org.jdesktop.layout.GroupLayout(editorPanel);
editorPanel.setLayout(editorPanelLayout);
editorPanelLayout.setHorizontalGroup(
editorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(editorPanelLayout.createSequentialGroup()
.add(loadButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 49, Short.MAX_VALUE)
.add(registerButton)
.add(18, 18, 18)
.add(executeButton))
.add(editorScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
);
editorPanelLayout.setVerticalGroup(
editorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, editorPanelLayout.createSequentialGroup()
.add(editorScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(editorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(loadButton)
.add(executeButton)
.add(registerButton))
.addContainerGap())
);
org.jdesktop.layout.GroupLayout GuiConsoleLayout = new org.jdesktop.layout.GroupLayout(this.getContentPane());
this.getContentPane().setLayout(GuiConsoleLayout);
GuiConsoleLayout.setHorizontalGroup(
GuiConsoleLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, GuiConsoleLayout.createSequentialGroup()
.addContainerGap()
.add(GuiConsoleLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, editorPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(GuiConsoleLayout.createSequentialGroup()
.add(input, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(editorButton))
.add(org.jdesktop.layout.GroupLayout.LEADING, outputScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE))
.addContainerGap())
);
GuiConsoleLayout.setVerticalGroup(
GuiConsoleLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(GuiConsoleLayout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(outputScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 244, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(GuiConsoleLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(input, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(editorButton))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(editorPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
editorPanel.setVisible(false);
}
/**
* Listener on register button click events.
* Read editor content and register procedures in Fscript engine.
*
* @param e - The source event.
*/
protected void registerButtonActionPerformed(ActionEvent e) {
try {
Set<String> procs = null;
Reader source = new StringReader( editorPane.getText() );
displayMessage("Loading procedures from editor");
procs = FraSCAtiScript.getSingleton().getLoader().load(source);
for (String proc : procs)
displayMessage("+-> Adding procedure '"+ proc +"'");
} catch (InvalidScriptException ise) {
displayError(ise);
}
}
/**
* Listener on execute button click events.
* Read editor content and execute commands line by line.
*
* @param e - The source event.
*/
protected void executeButtonActionPerformed(ActionEvent e) {
StringTokenizer st = new StringTokenizer( editorPane.getText(), "\n" );
while (st.hasMoreTokens()) {
executeCommand( st.nextToken() );
}
}
/**
* Listener on load button click events.
* open a file chooser and load input file content into editor.
*
* @param e - The source event.
*/
protected void loadButtonActionPerformed(ActionEvent e) {
// At the first call, creates the file chooser.
if(fileChooser == null) {
fileChooser = new JFileChooser();
// Sets to the user's current directory.
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
// Filters SCA files.
fileChooser.setFileFilter(
new FileFilter() {
/** Whether the given file is accepted by this filter. */
public boolean accept(File f) {
String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1);
return extension.equalsIgnoreCase("fscript") || f.isDirectory();
}
/** The description of this filter. */
public String getDescription() {
return "Fscript files";
}
}
} );
);
}
// Open the file chooser to select a file.
int returnVal = fileChooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
InputStream is = null;
try {
is = new FileInputStream(f);
BufferedReader reader = new BufferedReader( new InputStreamReader(is) );
String text = "",
line = null;
while ((line = reader.readLine()) != null) {
text += line + "\n";
}
editorPane.setText(text);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
/**
* Listener on input text field validation.
* On input validation, execute the command and display output.
*
* @param e - The source event.
*/
protected void inputActionPerformed(ActionEvent e) {
String cmd = input.getText();
this.getContentPane().add(outputScrollPane);
this.getContentPane().add(input);
if ( cmd.trim().length() > 0 ) {
executeCommand( input.getText() );
} else {
// Display a new line on the output console
try {
doc.insertString( doc.getLength(), prompt+"\n", doc.getStyle("prompt") );
} catch (BadLocationException e2) {
e2.printStackTrace(); // Should not happen
}
input.setText("");
}
}
/**
* Listener on editor button click events.
* On editor button click, show/hide editor panel.
* @param evt - The source event.
*/
protected void editorButtonActionPerformed(ActionEvent evt) {
editorPanel.setVisible( !editorPanel.isVisible() );
this.pack();
this.repaint();
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment