package easysm.boundaries.components.stateobserver;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import easysm.boundaries.UpdateStateObservers;
import easysm.boundaries.components.FullViewer;
import easysm.boundaries.components.utils.BooleanTableCellRender;
import easysm.boundaries.components.utils.CellRender;
import easysm.datatypes.visualization.StateObserverVis;
import easysm.stores.StateObserver;
/**
* @author Artur Tolstenco
*/
public class SOPanel extends javax.swing.JPanel
{
private static final long serialVersionUID = 1L;
private JScrollPane jScrollPane1;
private JButton button_remove;
private JButton button_add;
private JButton button_edit;
private JPanel jPanel1;
private JTable table_stateobservers;
private DefaultTableModel table_model;
private UpdateStateObservers updateStateObservers;
private StateObserverVis[] stateObservers;
private int stateObserversCount;
private JButton jbutton_generateSo;
private JSeparator jSeparator2;
private JButton jbutton_fullViewer;
private JSeparator jSeparator1;
private boolean error = false;
private FullViewer fullViewer;
public SOPanel(UpdateStateObservers updateStateObservers)
{
super();
initGUI();
this.updateStateObservers = updateStateObservers;
fullViewer = new FullViewer("State Observers");
}
private void fillTable()
{
table_model.getDataVector().removeAllElements();
if (stateObservers == null)
return;
for (int i = 0; i < stateObserversCount; ++i) {
String inv = "";
if (stateObservers[i].stateObserver().invariant() != null)
inv = stateObservers[i].stateObserver().invariant().toString();
table_model.addRow(new String[] { stateObservers[i].toString(), inv });
table_model.setValueAt(!stateObservers[i].stateObserver().isInvalid(), i, 2);
}
}
private void getStateObservers()
{
if (updateStateObservers.currentSO() == null) {
stateObserversCount = 0;
stateObservers = null;
return;
}
stateObservers = updateStateObservers.currentSO().stateObserversVis().toArray(new StateObserverVis[0]);
stateObserversCount = updateStateObservers.currentSO().stateObserversVis().size();
}
private void refreshColWidth() {
TableColumnModel colModel = table_stateobservers.getColumnModel();
colModel.getColumn(0).setWidth(colModel.getColumn(0).getWidth() + 1);
colModel.getColumn(0).setWidth(colModel.getColumn(0).getWidth() - 1);
table_model.fireTableDataChanged();
}
private void initGUI()
{
try {
BorderLayout thisLayout = new BorderLayout();
this.setLayout(thisLayout);
{
jPanel1 = new JPanel();
this.add(jPanel1, BorderLayout.EAST);
jPanel1.setPreferredSize(new java.awt.Dimension(123, 418));
{
button_add = new JButton();
jPanel1.add(button_add);
button_add.setText("<html><u>A</u>dd<br/>State<br/>Observer</html>");
button_add.setPreferredSize(new java.awt.Dimension(117, 53));
button_add.setIcon(new ImageIcon(getClass().getClassLoader().getResource(
"easysm/icons/x16/list-add.png")));
button_add.setHorizontalAlignment(SwingConstants.LEADING);
button_add.setMnemonic(java.awt.event.KeyEvent.VK_A);
button_add.setToolTipText("<html>Mnemonic: <b>ALT+A</b></html>");
button_add.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent evt)
{
button_addActionPerformed(evt);
}
});
}
{
button_edit = new JButton();
jPanel1.add(button_edit);
button_edit.setText("<html><u>E</u>dit<br/>State<br/>Observer</html>");
button_edit.setPreferredSize(new java.awt.Dimension(117, 53));
button_edit.setIcon(new ImageIcon(getClass().getClassLoader().getResource(
"easysm/icons/x16/edit-rename.png")));
button_edit.setHorizontalAlignment(SwingConstants.LEADING);
button_edit.setMnemonic(java.awt.event.KeyEvent.VK_E);
button_edit.setEnabled(false);
button_edit.setToolTipText("<html>Edit the selected State Observer"
+ "<br/>Mnemonic: <b>ALT+E</b></html>");
button_edit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
button_editActionPerformed(evt);
}
});
}
{
button_remove = new JButton();
jPanel1.add(button_remove);
button_remove.setText("<html><u>R</u>emove<br/>State<br/>Observer</html>");
button_remove.setPreferredSize(new java.awt.Dimension(117, 53));
button_remove.setIcon(new ImageIcon(getClass().getClassLoader().getResource(
"easysm/icons/x16/list-remove.png")));
button_remove.setHorizontalAlignment(SwingConstants.LEADING);
button_remove.setMnemonic(java.awt.event.KeyEvent.VK_R);
button_remove.setEnabled(false);
button_remove.setToolTipText("<html>Removes the selected State Observer"
+ "<br/>Mnemonic: <b>ALT+R</b></html>");
button_remove.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
button_removeActionPerformed(evt);
}
});
}
{
jSeparator2 = new JSeparator();
jPanel1.add(jSeparator2);
jSeparator2.setPreferredSize(new java.awt.Dimension(115,2));
}
{
jbutton_generateSo = new JButton();
jPanel1.add(jbutton_generateSo);
jbutton_generateSo.setText("<html><u>G</u>enerate<br/>basic State<br/>Observers</html>");
jbutton_generateSo.setIcon(new ImageIcon(getClass().getClassLoader().
getResource("easysm/icons/x16/generateBasicSO.png")));
jbutton_generateSo.setHorizontalAlignment(SwingConstants.LEADING);
jbutton_generateSo.setMnemonic(java.awt.event.KeyEvent.VK_G);
jbutton_generateSo.setToolTipText("<html>Generate basic State Observers considering Context Class"
+ "<br/>Mnemonic: <b>ALT+G</b></html>");
jbutton_generateSo.setPreferredSize(new java.awt.Dimension(117, 53));
jbutton_generateSo.setFont(new java.awt.Font("Dialog",1,10));
jbutton_generateSo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jbutton_generateSoActionPerformed(evt);
}
});
}
{
jSeparator1 = new JSeparator();
jPanel1.add(jSeparator1);
jSeparator1.setPreferredSize(new java.awt.Dimension(115, 2));
}
{
jbutton_fullViewer = new JButton();
jPanel1.add(jbutton_fullViewer);
jbutton_fullViewer.setText("<html><u>F</u>ull<br/>Viewer</html>");
jbutton_fullViewer.setHorizontalAlignment(SwingConstants.LEADING);
jbutton_fullViewer.setMnemonic(java.awt.event.KeyEvent.VK_F);
jbutton_fullViewer.setIcon(new ImageIcon(getClass().getClassLoader().getResource(
"easysm/icons/x16/document-preview.png")));
jbutton_fullViewer.setToolTipText("<html>Full view of all State Observers"
+ "<br/>Mnemonic: <b>ALT+F</b></html>");
jbutton_fullViewer.setPreferredSize(new java.awt.Dimension(117, 34));
jbutton_fullViewer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
jbutton_fullViewerActionPerformed(evt);
}
});
}
}
{
jScrollPane1 = new JScrollPane();
this.add(jScrollPane1, BorderLayout.CENTER);
{
String[] colNames = new String[] { "Name and Type", "Invariant", "Is Valid" };
table_model = new DefaultTableModel(null, colNames)
{
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row,
int col)
{
if (col == 0)
return false;
return true;
}
};
table_stateobservers = new JTable();
jScrollPane1.setViewportView(table_stateobservers);
table_stateobservers.setModel(table_model);
TableColumnModel colModel = table_stateobservers.getColumnModel();
colModel.getColumn(0).setMinWidth(110);
colModel.getColumn(0).setMaxWidth(200);
TableColumn col = colModel.getColumn(1);
col.setCellRenderer(new CellRender());
col.setCellEditor(new CellRender());
table_stateobservers.setRowHeight(50);
col = colModel.getColumn(2);
col.setMinWidth(50);
col.setMaxWidth(60);
BooleanTableCellRender booleanTableCellRender = new BooleanTableCellRender();
booleanTableCellRender.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
BooleanTableCellRender source = (BooleanTableCellRender)e.getSource();
if (source.isSelected()){
updateStateObservers.validate(stateObservers[source.getRow()].stateObserver());
table_model.setValueAt(!stateObservers[source.getRow()].stateObserver().isInvalid(), source.getRow(), 2);
}
refreshColWidth();
}
});
col.setCellEditor(booleanTableCellRender);
col.setCellRenderer(booleanTableCellRender);
table_stateobservers.setPreferredScrollableViewportSize(table_stateobservers.getPreferredSize());
table_stateobservers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSelectionModel = table_stateobservers.getSelectionModel();
rowSelectionModel.addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent arg0)
{
int index = table_stateobservers.getSelectedRow();
if (index > table_model.getRowCount() - 1 || index == -1) {
if (button_edit.isEnabled()) {
button_edit.setEnabled(false);
button_remove.setEnabled(false);
}
} else {
if (button_edit.isEnabled() == false) {
button_edit.setEnabled(true);
button_remove.setEnabled(true);
}
// se è un state observer final allora remove è
// disabilitato
if (stateObservers[index].stateObserver().name().equals(StateObserver.FINAL.name()))
button_remove.setEnabled(false);
}
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Events Handler
*/
private void button_addActionPerformed(ActionEvent evt)
{
new SO_addEdit(this, updateStateObservers, null).setVisible(true);
this.requestFocus();
}
private void button_editActionPerformed(ActionEvent evt)
{
new SO_addEdit(this, updateStateObservers, stateObservers[table_stateobservers.getSelectedRow()])
.setVisible(true);
this.requestFocus();
}
private void button_removeActionPerformed(ActionEvent evt)
{
updateStateObservers.deleteStateObserver(stateObservers[table_stateobservers.getSelectedRow()].stateObserver());
getError();
this.requestFocus();
}
private void jbutton_fullViewerActionPerformed(ActionEvent evt)
{
fullViewer.setVisible(true);
}
private void jbutton_generateSoActionPerformed(ActionEvent evt) {
updateStateObservers.generateBasicSO();
if (!getError())
updateTable();
}
/*
* public methods
*/
public void updateTable()
{
getStateObservers();
fillTable();
fullViewer.setText(updateStateObservers.currentSO().toString(),
updateStateObservers.currentSO().isInvalid());
refreshColWidth();
}
public void setError()
{
if (error == false)
error = true;
}
public boolean getError()
{
boolean tmp = error;
if (error == true)
error = false;
return tmp;
}
public void hideFullViewer()
{
if (fullViewer.isVisible())
fullViewer.setVisible(false);
}
}