/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package View;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.LinkedList;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.swingbinding.JListBinding;
import org.jdesktop.swingbinding.SwingBindings;
/**
*
* @author tiph
*/
public class OptionView extends javax.swing.JDialog {
private LinkedList<OptionIterface> options;
/**
* Gere l'affichage des elements de la liste
*/
private class MyListRender extends JLabel implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setForeground(Color.BLACK);
if (isSelected) {
setBackground(Color.ORANGE);
} else {
setBackground(Color.WHITE);
}
setOpaque(true);
setText(((OptionIterface) value).getOptionName());
setHorizontalTextPosition(CENTER);
setVerticalAlignment(CENTER);
setPreferredSize(new Dimension(list.getWidth(), 35));
return this;
}
}
/**
* Creates new form OptionView
*/
public OptionView(java.awt.Frame parent, boolean modal, LinkedList<OptionIterface> op) {
super(parent, modal);
options = op;
initComponents();
initBinding();
}
private void initBinding() {
JListBinding option_biding = SwingBindings.createJListBinding(AutoBinding.UpdateStrategy.READ, this.options, l_option);
option_biding.bind();
}
/**
* 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() {
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
l_option = new javax.swing.JList();
panel = new javax.swing.JPanel();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
okButton.setMnemonic('e');
okButton.setText("Enregistrer");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
cancelButton.setMnemonic('a');
cancelButton.setText("Annuler");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
l_option.setCellRenderer(new MyListRender());
l_option.setSelectedIndex(0);
l_option.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
l_optionValueChanged(evt);
}
});
jScrollPane1.setViewportView(l_option);
panel.setLayout(new javax.swing.BoxLayout(panel, javax.swing.BoxLayout.LINE_AXIS));
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()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(okButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(okButton))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
for (OptionIterface option : options) {
// Sauvegarde en memoire toutes les options
option.saveParams();
// sauvegarde dans le XML
Config.Config.getInstance().save();
}
this.dispose();
}//GEN-LAST:event_okButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
this.dispose();
}//GEN-LAST:event_closeDialog
/**
* Gere l'affichage du paneau d'option en fonction de la selection
*
* @param evt
*/
private void l_optionValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_l_optionValueChanged
OptionIterface selectedOpt = (OptionIterface) l_option.getSelectedValue();
panel.removeAll();
panel.add((JPanel) selectedOpt);
panel.validate();
panel.repaint();
}//GEN-LAST:event_l_optionValueChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList l_option;
private javax.swing.JButton okButton;
private javax.swing.JPanel panel;
// End of variables declaration//GEN-END:variables
}