/*
* 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.customer;
import eu.ggnet.dwoss.util.IPreClose;
import eu.ggnet.dwoss.util.CloseType;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import eu.ggnet.dwoss.customer.api.UiCustomer;
import eu.ggnet.dwoss.customer.priv.OldCustomer;
/**
* View for create of a Customer, also inits an empty Model.
*
* @author pascal.perau, oliver.guenther
*/
public class CustomerCreateView extends javax.swing.JPanel implements IPreClose {
private CustomerUpdateViewCask customerDetails;
private CustomerCreateController controller;
/** Creates new form CreateOrEditCustomerView */
public CustomerCreateView() {
initComponents();
customerDetails = new CustomerUpdateViewCask();
customerDetailPanel.add(customerDetails);
searchResultList.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if ( value instanceof UiCustomer ) {
label.setText("<html>" + ((UiCustomer)value).getSimpleHtml() + "</html>");
label.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
}
return label;
}
});
initCustomer();
}
@Override
public boolean pre(CloseType type) {
return customerDetails.pre(type);
}
public OldCustomer getCustomer() {
return customerDetails.getCustomer();
}
public void setCustomer(OldCustomer customer) {
customerDetails.setCustomer(customer);
// Hint: Do not set it in the controller here, may create a loop.
}
public CustomerCreateController getController() {
return controller;
}
/**
* Sets the Controller and autowires a Model.
*
* @param controller the controller to be set.
*/
public void setController(CustomerCreateController controller) {
this.controller = controller;
if ( controller != null ) controller.setCustomer(customerDetails.getCustomer());
}
private void initCustomer() {
// FIXME: This is not perfect. Better use the CustomerCreateModel and set the customer there. Works for now.
OldCustomer customer = new OldCustomer();
customerDetails.setCustomer(customer);
if ( controller != null ) controller.setCustomer(customer);
}
/** 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() {
GridBagConstraints gridBagConstraints;
jSeparator1 = new JSeparator();
searchCommandField = new JTextField();
jScrollPane2 = new JScrollPane();
searchResultList = new JList();
customerDetailPanel = new JPanel();
jLabel1 = new JLabel();
clearFieldsButton = new JButton();
setLayout(new GridBagLayout());
jSeparator1.setOrientation(SwingConstants.VERTICAL);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 20;
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
add(jSeparator1, gridBagConstraints);
searchCommandField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
searchCommandFieldActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
add(searchCommandField, gridBagConstraints);
searchResultList.setMinimumSize(new Dimension(300, 125));
searchResultList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
searchResultListValueChanged(evt);
}
});
jScrollPane2.setViewportView(searchResultList);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridheight = 17;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.weighty = 0.1;
add(jScrollPane2, gridBagConstraints);
customerDetailPanel.setLayout(new GridBagLayout());
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 20;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weighty = 0.1;
add(customerDetailPanel, gridBagConstraints);
jLabel1.setText("Kundensuche:");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.WEST;
add(jLabel1, gridBagConstraints);
clearFieldsButton.setText("Felder leeren");
clearFieldsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
clearFieldsButtonActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 19;
gridBagConstraints.anchor = GridBagConstraints.WEST;
add(clearFieldsButton, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void clearFieldsButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_clearFieldsButtonActionPerformed
initCustomer();
}//GEN-LAST:event_clearFieldsButtonActionPerformed
private void searchResultListValueChanged(ListSelectionEvent evt) {//GEN-FIRST:event_searchResultListValueChanged
if ( evt.getValueIsAdjusting() || searchResultList.getSelectedIndex() == -1 ) return;
if ( controller == null ) return;
controller.selectCustomer(((UiCustomer)searchResultList.getSelectedValue()).getId());
}//GEN-LAST:event_searchResultListValueChanged
private void searchCommandFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchCommandFieldActionPerformed
if ( controller == null ) return;
controller.findCustomers(searchCommandField.getText());
}//GEN-LAST:event_searchCommandFieldActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
JButton clearFieldsButton;
JPanel customerDetailPanel;
JLabel jLabel1;
JScrollPane jScrollPane2;
JSeparator jSeparator1;
JTextField searchCommandField;
JList searchResultList;
// End of variables declaration//GEN-END:variables
}