/*
* Copyright 2011 University of California, San Diego.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.ucsd.hep.rootrunnerutil.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import edu.ucsd.hep.rootrunnerutil.PipeCommandRunnerListener;
import edu.ucsd.hep.rootrunnerutil.AHUtils;
import edu.ucsd.hep.rootrunnerutil.PipeCommandRunner;
/**
*
* @author holzner
*/
public class CommandDisplayPanel extends javax.swing.JPanel implements PipeCommandRunnerListener
{
//----------------------------------------------------------------------
// data
//----------------------------------------------------------------------
JFileChooser chooser;
/** we need this to send commands typed by the user. */
private final PipeCommandRunner command_runner;
private final Color commandOutputColor = new Color(0x800000);
//----------------------------------------------------------------------
/** Creates new form CommandDisplayPanel
@param command_runner can also be null in which case
the interactive input is disabled
*/
public CommandDisplayPanel(PipeCommandRunner command_runner)
{
this.command_runner = command_runner;
initComponents();
if (command_runner == null)
{
this.userCommandInputComboBox.setEnabled(false);
this.userCommandInputComboBox.setEditable(false);
}
}
//----------------------------------------------------------------------
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
clearTextMenuItem = new javax.swing.JMenuItem();
insertSeparatorMenuItem = new javax.swing.JMenuItem();
jMenuItem1 = new javax.swing.JMenuItem();
jScrollPane1 = new javax.swing.JScrollPane();
commandBulkInputTextArea = new javax.swing.JTextArea();
userCommandInputComboBox = new javax.swing.JComboBox();
jScrollPane2 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
clearTextMenuItem.setText("clear");
clearTextMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearTextMenuItemActionPerformed(evt);
}
});
jPopupMenu1.add(clearTextMenuItem);
insertSeparatorMenuItem.setText("insert separator");
insertSeparatorMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insertSeparatorMenuItemActionPerformed(evt);
}
});
jPopupMenu1.add(insertSeparatorMenuItem);
jMenuItem1.setText("Save as...");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jPopupMenu1.add(jMenuItem1);
commandBulkInputTextArea.setColumns(20);
commandBulkInputTextArea.setEditable(false);
commandBulkInputTextArea.setFont(new Font(Font.MONOSPACED,Font.BOLD,12));
commandBulkInputTextArea.setRows(5);
commandBulkInputTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
commandBulkInputTextAreaMouseClicked(evt);
}
});
jScrollPane1.setViewportView(commandBulkInputTextArea);
userCommandInputComboBox.setEditable(true);
userCommandInputComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
userCommandInputComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
userCommandInputComboBoxActionPerformed(evt);
}
});
jTextPane1.setEditable(false);
jTextPane1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTextPane1MouseClicked(evt);
}
});
jScrollPane2.setViewportView(jTextPane1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
.addComponent(userCommandInputComboBox, 0, 560, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(userCommandInputComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void clearTextMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_clearTextMenuItemActionPerformed
{//GEN-HEADEREND:event_clearTextMenuItemActionPerformed
this.commandBulkInputTextArea.setText("");
this.jTextPane1.setText("");
}//GEN-LAST:event_clearTextMenuItemActionPerformed
//----------------------------------------------------------------------
private void commandBulkInputTextAreaMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_commandBulkInputTextAreaMouseClicked
{//GEN-HEADEREND:event_commandBulkInputTextAreaMouseClicked
if (evt.getButton() == MouseEvent.BUTTON3)
{
this.jPopupMenu1.show(evt.getComponent(),
evt.getX(), evt.getY());
}
}//GEN-LAST:event_commandBulkInputTextAreaMouseClicked
//----------------------------------------------------------------------
private void insertSeparatorMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_insertSeparatorMenuItemActionPerformed
{//GEN-HEADEREND:event_insertSeparatorMenuItemActionPerformed
this.appendText(Color.BLACK, "\n----------------------------------------\n");
}//GEN-LAST:event_insertSeparatorMenuItemActionPerformed
//----------------------------------------------------------------------
public void appendText(Color color, String text)
{
this.commandBulkInputTextArea.append(text);
// seems only to work when the text panel is editable
{
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet attr_set = sc.addAttribute(SimpleAttributeSet.EMPTY,
StyleConstants.Foreground, color);
int len = jTextPane1.getDocument().getLength();
jTextPane1.setCaretPosition(len); // place cursor at the end (with no selection)
jTextPane1.setCharacterAttributes(attr_set, false);
jTextPane1.setEditable(true);
jTextPane1.replaceSelection(text); // there is no selection, so inserts at cursor (end)
jTextPane1.setEditable(false);
}
}
//----------------------------------------------------------------------
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItem1ActionPerformed
{//GEN-HEADEREND:event_jMenuItem1ActionPerformed
if (chooser == null)
{
chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("ROOT Macros", "C");
chooser.setFileFilter(filter);
}
int res = chooser.showDialog(this, "Save as");
if (res != JFileChooser.APPROVE_OPTION)
return;
// TODO: we should check for overwriting existing files
try
{
// AHUtils.writeStringToFile(chooser.getSelectedFile(), this.commandBulkInputTextArea.getText());
AHUtils.writeStringToFile(chooser.getSelectedFile(), this.jTextPane1.getText());
} catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(this, "Error saving to file: " + ex.getMessage());
}
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void userCommandInputComboBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_userCommandInputComboBoxActionPerformed
{//GEN-HEADEREND:event_userCommandInputComboBoxActionPerformed
// System.out.println("evt=" + evt);
if (!evt.getActionCommand().equals("comboBoxEdited"))
// avoid sending the ocmmand twice
return;
String line = (String)this.userCommandInputComboBox.getSelectedItem();
System.out.println("line=" + line);
this.command_runner.writeLine(line);
// TODO: add this command to the history
// this.userCommandInputComboBox.getList
}//GEN-LAST:event_userCommandInputComboBoxActionPerformed
private void jTextPane1MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTextPane1MouseClicked
{//GEN-HEADEREND:event_jTextPane1MouseClicked
commandBulkInputTextAreaMouseClicked(evt);
}//GEN-LAST:event_jTextPane1MouseClicked
//----------------------------------------------------------------------
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem clearTextMenuItem;
private javax.swing.JTextArea commandBulkInputTextArea;
private javax.swing.JMenuItem insertSeparatorMenuItem;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JComboBox userCommandInputComboBox;
// End of variables declaration//GEN-END:variables
//----------------------------------------------------------------------
/** adds a line which has been sent to the process */
public void appendCommandInputLine(String line)
{
this.appendText(Color.BLUE, line + "\n");
}
//----------------------------------------------------------------------
public void appendCommandOutput(String data)
{
this.appendText(commandOutputColor, data);
}
//----------------------------------------------------------------------
public JFrame makeFrame()
{
JFrame frame = new JFrame("command display");
frame.getContentPane().add(this);
frame.pack();
return frame;
}
//----------------------------------------------------------------------
}