/*
* Copyright (C) 2012 Miguel Angel Garcia <miguelangel.garcia@gmail.com>
*
* 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 org.magmax.master.project.admin.product;
import org.magmax.master.project.admin.BaseCrudPanel;
import org.magmax.master.project.admin.Persistence;
import org.magmax.master.project.persistence.dao.SectionDAO;
import org.magmax.master.project.persistence.pojo.Section;
/**
*
* @author Miguel Angel Garcia <miguelangel.garcia@gmail.com>
*/
public class ProductPanel extends BaseCrudPanel<ProductRow> {
public static final String TITLE_CREATION = "Product creation";
public static final String TITLE_DETAILS = "Product Details";
public static final String TITLE_UPDATE = "Product User";
/**
* Creates new form UserUI
*/
public ProductPanel() {
initComponents();
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
nameEntry = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
descriptionTextarea = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
sectionCombobox = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
prizeSpinner = new javax.swing.JSpinner();
jLabel1.setText("Nombre:");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Description"));
descriptionTextarea.setColumns(20);
descriptionTextarea.setRows(5);
jScrollPane1.setViewportView(descriptionTextarea);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addContainerGap())
);
jLabel2.setText("Sección:");
jLabel3.setText("Precio:");
prizeSpinner.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(0.0f), Float.valueOf(0.0f), null, Float.valueOf(1.0f)));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sectionCombobox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(nameEntry, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(prizeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(nameEntry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(sectionCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(prizeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea descriptionTextarea;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField nameEntry;
private javax.swing.JSpinner prizeSpinner;
private javax.swing.JComboBox sectionCombobox;
// End of variables declaration//GEN-END:variables
@Override
public String getTitleCreation() {
return TITLE_CREATION;
}
@Override
public String getTitleUpdate() {
return TITLE_UPDATE;
}
@Override
public String getTitleDetails() {
return TITLE_DETAILS;
}
@Override
public ProductRow getCrudObject() {
ProductRow result = new ProductRow();
updateCrudObject(result);
return result;
}
@Override
public void setCrudObject(ProductRow item) {
nameEntry.setText(item.getEntity().getName());
descriptionTextarea.setText(item.getEntity().getDescription());
prizeSpinner.setValue(item.getEntity().getPrize());
loadSections();
selectSection(item.getEntity().getSection());
}
@Override
public void updateCrudObject(ProductRow item) {
Number prize = (Number) prizeSpinner.getValue();
item.getEntity().setName(nameEntry.getText());
item.getEntity().setDescription(descriptionTextarea.getText());
item.getEntity().setPrize(prize.floatValue());
item.getEntity().setSection((Section) sectionCombobox.getSelectedItem());
}
@Override
public void setWidgetsEnabled(boolean enabled) {
nameEntry.setEnabled(enabled);
descriptionTextarea.setEnabled(enabled);
sectionCombobox.setEnabled(enabled);
prizeSpinner.setEnabled(enabled);
loadSections();
}
@Override
public void clearWidgets() {
nameEntry.setText("");
descriptionTextarea.setText("");
sectionCombobox.setSelectedIndex(-1);
prizeSpinner.setValue(0);
}
private void loadSections() {
sectionCombobox.removeAllItems();
SectionDAO sectiondao = Persistence.getInstance().getSectionDAO();
for (Section each : sectiondao.findAll()) {
sectionCombobox.addItem(each);
}
}
private void selectSection(Section section) {
if (section == null) {
return;
}
for (int i = 0; i < sectionCombobox.getItemCount(); ++i) {
Section current = (Section) sectionCombobox.getItemAt(i);
if (section.getId() == current.getId()) {
sectionCombobox.setSelectedIndex(i);
return;
}
}
}
}