/*
* Copyright (C) 2014 Vinu K.N
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.domainmath.gui;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
public class ScriptRunConfigDialog extends javax.swing.JDialog {
private String fileName;
private File file;
private int selectedIndex;
private List<RunConfiguration> runConfiguration;
public List<RunConfiguration> getRunConfiguration() {
return runConfiguration;
}
public void setRunConfiguration(List<RunConfiguration> runConfiguration) {
this.runConfiguration = runConfiguration;
}
private final DefaultListModel model;
private RunConfigurations runConfig;
/**
* Creates new form ScriptRunConfigDialog
* @param parent
*/
public ScriptRunConfigDialog(java.awt.Frame parent, boolean modal,File file, File fileName) {
super(parent, modal);
initComponents();
this.expressionArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_OCTAVE);
this.rTextScrollPane1.getGutter().setVisible(true);
this.rTextScrollPane1.getGutter().setBookmarkingEnabled(true);
this.rTextScrollPane1.setLineNumbersEnabled(true);
this.rTextScrollPane1.setFoldIndicatorEnabled(true);
this.rTextScrollPane1.setWheelScrollingEnabled(true);
this.file = file;
String f = fileName.getName();
this.fileName= fileName.getName().substring(0, f.lastIndexOf("."));
model = new DefaultListModel();
this.jList1.setModel(model);
openConfig();
}
public int getSelectedIndex() {
return selectedIndex;
}
public RunConfigurations getRunConfig() {
return runConfig;
}
public void setSelectedIndex(int selectedIndex) {
this.selectedIndex = selectedIndex;
}
public void setRunConfig(RunConfigurations runConfig) {
this.runConfig = runConfig;
}
private void openConfig() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(RunConfigurations.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
runConfig = (RunConfigurations) jaxbUnmarshaller.unmarshal(file);
selectedIndex = runConfig.getIndex();
runConfiguration = runConfig.getRunConfiguration();
this.setRunConfiguration(runConfiguration);
this.setRunConfig(runConfig);
for(int i=0; i<runConfiguration.size();i++) {
model.addElement(runConfiguration.get(i).getName());
System.out.println(runConfiguration.get(i).getName());
}
this.jList1.setSelectedIndex(selectedIndex);
this.jTextField1.setText(this.jList1.getSelectedValue().toString());
this.expressionArea.setText(runConfiguration.get(selectedIndex).getConfig());
//System.out.println(customer.getRunConfiguration().get(customer.getIndex()));
} catch ( JAXBException ex) {
this.jTextField1.setText(fileName);
model.addElement(fileName);
this.jList1.setSelectedIndex(0);
expressionArea.setText("# Modify expression to add input arguments.\n# Example:\n#\tvalue=5;\n#\tfoo(value)\n\n"+fileName);
runConfig = new RunConfigurations();
String newConfig = this.jTextField1.getText();
if(!newConfig.isEmpty()) {
RunConfiguration r = new RunConfiguration();
r.setName(newConfig);
if(!this.expressionArea.getText().isEmpty()) {
r.setConfig(this.expressionArea.getText());
}else{
r.setConfig("# Modify expression to add input arguments.\n# Example:\n#\tvalue=5;\n#\tfoo(value)\n\n"+fileName);
}
List<RunConfiguration> c = new ArrayList<>();
c.add(r);
runConfig.setRunConfiguration(c);
this.setRunConfig(runConfig);
}
}
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
rTextScrollPane1 = new org.fife.ui.rtextarea.RTextScrollPane();
expressionArea = new org.fife.ui.rsyntaxtextarea.RSyntaxTextArea();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Edit Script Configuration");
setModal(true);
jLabel1.setText("Octave Expression:");
jButton1.setText("Cancel");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Run");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
expressionArea.setColumns(20);
expressionArea.setRows(5);
rTextScrollPane1.setViewportView(expressionArea);
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
jList1ValueChanged(evt);
}
});
jScrollPane1.setViewportView(jList1);
jLabel2.setText("Run Configurations:");
jLabel4.setText("Configuration Name:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(rTextScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(127, 127, 127)
.addComponent(jLabel3)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(113, 113, 113)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jTextField1)))))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3)
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(rTextScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2});
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
dispose();
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
String newConfig = this.jTextField1.getText();
if(!model.contains(newConfig)){
if(!newConfig.isEmpty()) {
RunConfiguration r = new RunConfiguration();
r.setName(newConfig);
if(!this.expressionArea.getText().isEmpty()) {
r.setConfig(this.expressionArea.getText());
}else{
r.setConfig("# Modify expression to add input arguments.\n# Example:\n#\tvalue=5;\n#\tfoo(value)\n\n"+fileName);
}
List<RunConfiguration> c = new ArrayList<>();
c.add(r);
if(this.getRunConfiguration() != null) {
c.addAll(this.runConfiguration);
}
this.getRunConfig().setRunConfiguration(c);
this.setRunConfig(runConfig);
save(runConfig);
MainFrame.octavePanel.evalWithOutput(expressionArea.getText());
dispose();
}
}else{
this.runConfig.setIndex(this.jList1.getSelectedIndex());
this.setRunConfig(runConfig);
MainFrame.octavePanel.evalWithOutput(expressionArea.getText());
save(runConfig);
dispose();
}
}//GEN-LAST:event_jButton2ActionPerformed
private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
if (evt.getValueIsAdjusting()) {
this.jTextField1.setText(this.jList1.getSelectedValue().toString());
this.expressionArea.setText(runConfiguration.get(this.jList1.getSelectedIndex()).getConfig());
}
}//GEN-LAST:event_jList1ValueChanged
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ScriptRunConfigDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ScriptRunConfigDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ScriptRunConfigDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ScriptRunConfigDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ScriptRunConfigDialog dialog = new ScriptRunConfigDialog(new javax.swing.JFrame(), true,new File("D:/file2.xml"),new File("test"));
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private org.fife.ui.rsyntaxtextarea.RSyntaxTextArea expressionArea;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private org.fife.ui.rtextarea.RTextScrollPane rTextScrollPane1;
// End of variables declaration//GEN-END:variables
private void save(RunConfigurations r) {
if(r != null) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(RunConfigurations.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(r, file);
jaxbMarshaller.marshal(r, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}else{
RunConfiguration r1 = new RunConfiguration();
r1.setName(fileName);
r1.setConfig("# Modify expression to add input arguments.\n# Example:\n#\tvalue=5;\n#\tfoo(value)\n\n"+fileName);
List<RunConfiguration> c = new ArrayList<>();
c.add(r1);
RunConfigurations runConfigurations = new RunConfigurations();
runConfigurations.setIndex(0);
runConfigurations.setRunConfiguration(c);
this.setRunConfig(runConfig);
try {
JAXBContext jaxbContext = JAXBContext.newInstance(RunConfigurations.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(runConfigurations, file);
jaxbMarshaller.marshal(runConfigurations, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
}