package eu.ggnet.saft.core.swing;
import java.awt.*;
import eu.ggnet.saft.api.ui.OnOk;
/**
*
*
* @author oliver.guenther
* @param <T> sub container
*/
public class OkCancelDialog<T extends Component> extends javax.swing.JDialog {
T subContainer;
boolean ok = false;
public OkCancelDialog(Window parent, T container) {
super(parent);
initComponents();
subContainer = container;
Dimension d = new Dimension(container.getPreferredSize());
templatePanel.add(container, BorderLayout.CENTER);
d.setSize(d.getWidth(), d.getHeight() + 70);
this.setPreferredSize(d);
Dimension d2 = new Dimension(container.getMinimumSize());
d2.setSize(d2.getWidth(), d2.getHeight() + 70);
this.setMinimumSize(d2);
this.pack();
}
public T getSubContainer() {
return subContainer;
}
public boolean isOk() {
return ok;
}
public boolean isCancel() {
return !ok;
}
private void closeWithOk(boolean ok) {
boolean close = true;
if ( ok && subContainer instanceof OnOk ) {
close = ((OnOk)subContainer).onOk();
}
if ( close ) {
this.ok = ok;
setVisible(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() {
java.awt.GridBagConstraints gridBagConstraints;
templatePanel = new javax.swing.JPanel();
jSeparator1 = new javax.swing.JSeparator();
buttonPanel = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
templatePanel.setLayout(new java.awt.BorderLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 100.0;
gridBagConstraints.weighty = 100.0;
getContentPane().add(templatePanel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(1, 0, 1, 0);
getContentPane().add(jSeparator1, gridBagConstraints);
buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
okButton.setText(" Ok ");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
buttonPanel.add(okButton);
cancelButton.setText("Abbrechen");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
buttonPanel.add(cancelButton);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
getContentPane().add(buttonPanel, gridBagConstraints);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
closeWithOk(true);
}//GEN-LAST:event_okButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
closeWithOk(false);
}//GEN-LAST:event_cancelButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel buttonPanel;
private javax.swing.JButton cancelButton;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JButton okButton;
private javax.swing.JPanel templatePanel;
// End of variables declaration//GEN-END:variables
}