/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* FunctionEditor.java
*
* Created on 20.04.2013, 9:26:01
*/
package graphical.editor;
import functions.ArrayTabulatedFunction;
import functions.Function;
import functions.FunctionPoint;
import functions.FunctionPointIndexOutOfBoundsException;
import functions.InappropriateFunctionPointException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author true
*/
public class FunctionEditor extends javax.swing.JFrame {
private FunctionCreator creator;
private DataTable functionTable;
private JFileChooser mainFileChooser;
private FromFileClassLoader loader;
/** Creates new form FunctionEditor */
public FunctionEditor() {
initComponents();
functionTable = new DataTable(new ArrayTabulatedFunction(0, 10, 11));
creator = new FunctionCreator(this, true);
mainFileChooser = new JFileChooser();
loader = new FromFileClassLoader();
mainTable.setModel(new FunctionTableModel(functionTable, this));
}
/** 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() {
newPointXField = new javax.swing.JTextField();
newPointXLabel = new javax.swing.JLabel();
newPointYLabel = new javax.swing.JLabel();
newPointYField = new javax.swing.JTextField();
addPointButton = new javax.swing.JButton();
deletePointButton = new javax.swing.JButton();
MainTable = new javax.swing.JScrollPane();
mainTable = new javax.swing.JTable();
mainMenubar = new javax.swing.JMenuBar();
fileMenuBarElement = new javax.swing.JMenu();
newMenuItem = new javax.swing.JMenuItem();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
tabulateMenuBarElement = new javax.swing.JMenu();
tabulateFunctionItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Tabulated Functions");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closingMainWindowAction(evt);
}
});
newPointXField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
newPointXField.setText("0");
newPointXField.setToolTipText("");
newPointXLabel.setText("New point X");
newPointYLabel.setText("New point Y");
newPointYField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
newPointYField.setText("0");
addPointButton.setText("Add point");
addPointButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addPointButtonActionPerformed(evt);
}
});
deletePointButton.setText("Delete point");
deletePointButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deletePointButtonActionPerformed(evt);
}
});
mainTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
MainTable.setViewportView(mainTable);
mainMenubar.setToolTipText("");
fileMenuBarElement.setText("File");
newMenuItem.setText("New...");
newMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
creatingNewObj(evt);
}
});
fileMenuBarElement.add(newMenuItem);
openMenuItem.setText("Open..");
openMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openFileAction(evt);
}
});
fileMenuBarElement.add(openMenuItem);
saveMenuItem.setText("Save");
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveAction(evt);
}
});
fileMenuBarElement.add(saveMenuItem);
saveAsMenuItem.setText("Save as...");
saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveAsAction(evt);
}
});
fileMenuBarElement.add(saveAsMenuItem);
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitAction(evt);
}
});
fileMenuBarElement.add(exitMenuItem);
mainMenubar.add(fileMenuBarElement);
tabulateMenuBarElement.setText("Tabulate");
tabulateFunctionItem.setText("Tabulate function...");
tabulateFunctionItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tabulateAction(evt);
}
});
tabulateMenuBarElement.add(tabulateFunctionItem);
mainMenubar.add(tabulateMenuBarElement);
setJMenuBar(mainMenubar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(newPointYLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(newPointYField))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(newPointXLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(newPointXField, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addPointButton, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
.addComponent(deletePointButton, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
.addContainerGap())
.addComponent(MainTable, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(MainTable, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(newPointXLabel)
.addComponent(newPointXField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addPointButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(newPointYLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(newPointYField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(deletePointButton))))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void creatingNewObj(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_creatingNewObj
if (creator.showDialog() == creator.OK) {
functionTable.newFunction(creator.getLeftDomainBorder(), creator.getRightDomainBorder(), creator.getPointsCount());
mainTable.revalidate();
mainTable.repaint();
}
}//GEN-LAST:event_creatingNewObj
private void openFileAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileAction
int res = mainFileChooser.showDialog(this, "Open file");
if (res == JFileChooser.APPROVE_OPTION) {
File tmp = mainFileChooser.getSelectedFile();
try {
functionTable.loadFunction(tmp.getAbsolutePath());
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(rootPane, "File not found!");
} catch (IOException e) {
JOptionPane.showMessageDialog(rootPane, "Something went wrong when trying to open!");
}
mainTable.revalidate();
mainTable.repaint();
}
}//GEN-LAST:event_openFileAction
private void saveAsAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsAction
int res = mainFileChooser.showDialog(this, "Save file");
if (res == JFileChooser.APPROVE_OPTION) {
File tmp = mainFileChooser.getSelectedFile();
try {
functionTable.saveFunctionAs(tmp.getAbsolutePath());
} catch (IOException e) {
JOptionPane.showMessageDialog(rootPane, "Something went wrong when trying to save!");
}
functionTable = new DataTable(functionTable, tmp.getAbsolutePath());
}
}//GEN-LAST:event_saveAsAction
private void saveAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAction
if (functionTable.isFileNameAssigned()) {
try {
functionTable.saveFunction();
} catch (IOException e) {
JOptionPane.showMessageDialog(rootPane, "Something went wrong when trying to save!");
}
} else {
saveAsAction(evt);
}
}//GEN-LAST:event_saveAction
private void exitAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitAction
if (functionTable.isModified()) {
int res = JOptionPane.showConfirmDialog(rootPane, "Document is not saved. Are you sure you want to exit?", "Confirm exit", JOptionPane.YES_NO_OPTION);
if (res == JOptionPane.YES_OPTION) {
dispose();
}
} else {
dispose();
}
}//GEN-LAST:event_exitAction
private void closingMainWindowAction(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closingMainWindowAction
exitAction(null);
}//GEN-LAST:event_closingMainWindowAction
private void deletePointButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletePointButtonActionPerformed
try {
functionTable.deletePoint(mainTable.getSelectedRow());
} catch (FunctionPointIndexOutOfBoundsException | IllegalStateException e) {
JOptionPane.showMessageDialog(rootPane, e);
}
mainTable.revalidate();
mainTable.repaint();
}//GEN-LAST:event_deletePointButtonActionPerformed
private void addPointButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPointButtonActionPerformed
try {
FunctionPoint point = new FunctionPoint(Double.parseDouble(newPointXField.getText()), Double.parseDouble(newPointYField.getText()));
try {
functionTable.addPoint(point);
} catch (InappropriateFunctionPointException ex) {
JOptionPane.showMessageDialog(rootPane, "Such point already exist!");
}
mainTable.revalidate();
mainTable.repaint();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(rootPane, "X or Y is not double!\n" + "(" + e + ")", "Error!", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_addPointButtonActionPerformed
private void tabulateAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tabulateAction
int res = mainFileChooser.showDialog(this, "Open file");
if (res == JFileChooser.APPROVE_OPTION) {
File tmp = mainFileChooser.getSelectedFile();
Object tmpObject = null;
if (creator.showDialog() == FunctionCreator.OK) {
try {
Class tmpClass = loader.loadClassFromFile(tmp.getAbsolutePath());
tmpObject = tmpClass.newInstance();
if (tmpObject instanceof Function) {
functionTable.tabulateFunction((Function) tmpObject, creator.getLeftDomainBorder(), creator.getRightDomainBorder(), creator.getPointsCount());
mainTable.revalidate();
mainTable.repaint();
} else {
JOptionPane.showMessageDialog(rootPane, "It is not supported class");
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(rootPane, "Something wrong with I/O");
} catch (InstantiationException ex) {
JOptionPane.showMessageDialog(rootPane, "Can not create instance of class (" + tmpObject + ")");
} catch (IllegalAccessException ex) {
JOptionPane.showMessageDialog(rootPane, "Can not access (" + tmpObject + ")");
}
}
}
}//GEN-LAST:event_tabulateAction
/**
* @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(FunctionEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FunctionEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FunctionEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FunctionEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FunctionEditor().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane MainTable;
private javax.swing.JButton addPointButton;
private javax.swing.JButton deletePointButton;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenu fileMenuBarElement;
private javax.swing.JMenuBar mainMenubar;
private javax.swing.JTable mainTable;
private javax.swing.JMenuItem newMenuItem;
private javax.swing.JTextField newPointXField;
private javax.swing.JLabel newPointXLabel;
private javax.swing.JTextField newPointYField;
private javax.swing.JLabel newPointYLabel;
private javax.swing.JMenuItem openMenuItem;
private javax.swing.JMenuItem saveAsMenuItem;
private javax.swing.JMenuItem saveMenuItem;
private javax.swing.JMenuItem tabulateFunctionItem;
private javax.swing.JMenu tabulateMenuBarElement;
// End of variables declaration//GEN-END:variables
}