/* * Copyright (C) 2014 GG-Net GmbH - Oliver Günther * * 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 eu.ggnet.dwoss.util; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import static eu.ggnet.dwoss.util.CloseType.CANCEL; import static eu.ggnet.dwoss.util.CloseType.OK; /** * * * @author oliver.guenther */ // TODO: Add the case if someone presses the X, to call pre closing public class OkCancelDialog<T extends Component> extends javax.swing.JDialog { T subContainer; CloseType type = CloseType.CANCEL; /** Creates new form OkCancelDialog with Window parent */ public OkCancelDialog(java.awt.Window parent, ModalityType modalityType, String title, T container) { super(parent); initComponents(); if ( modalityType == null ) setModalityType(ModalityType.APPLICATION_MODAL); else setModalityType(modalityType); if ( container instanceof IView ) ((IView)container).setParent(this); 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.setTitle(title); this.pack(); if ( parent != null ) setLocationRelativeTo(parent); } /** Creates new form OkCancelDialog with no parent */ public OkCancelDialog(String title, T container) { this(null, null, title, container); } public OkCancelDialog(java.awt.Window parent, String title, T container) { this(parent, null, title, container); } public T getSubContainer() { return subContainer; } /** * Returns true if the Ok Button was pressed to close the window * * @return true if the Ok Button was pressed to close the window */ public CloseType getCloseType() { return type; } public boolean isOk() { return type == CloseType.OK; } public boolean isCancel() { return type == CloseType.CANCEL; } /** 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 close(OK); }//GEN-LAST:event_okButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed close(CANCEL); }//GEN-LAST:event_cancelButtonActionPerformed private void close(CloseType type) { boolean close = true; if ( subContainer instanceof IPreClose ) { close = ((IPreClose)subContainer).pre(type); } if ( close ) { this.type = type; setVisible(false); } } /** * @param args the command line arguments */ public static void main(String args[]) { final JFrame f1 = new JFrame("Blub"); f1.setSize(100, 100); f1.getContentPane().setLayout(new BorderLayout()); JButton b = new JButton("Open"); f1.getContentPane().add(b, BorderLayout.CENTER); f1.setVisible(true); f1.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { System.exit(0); } }); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JLabel text = new JLabel("Ein toller Text"); text.setPreferredSize(new Dimension(200, 30)); OkCancelDialog dialog = new OkCancelDialog(f1, "Test Dialog", text); dialog.setVisible(true); if ( dialog.getCloseType() == OK ) { System.out.println("Ok was pressed"); } else { System.out.println("Cancel was pressed"); } } }); } // 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 }