📄 creercontrat.java
字号:
/* * CreerContrat.java * * Created on December 9, 2008, 5:27 PM */package view.swing;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.math.BigDecimal;import java.sql.Timestamp;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import javax.swing.BorderFactory;import javax.swing.DefaultCellEditor;import javax.swing.DefaultComboBoxModel;import javax.swing.GroupLayout;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.LayoutStyle;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.TableColumn;import service.IService;import service.ServiceImpl;import view.swing.model.CreeContratTableModel;import view.swing.model.DateColorTableCellRender;import dao.model.Categorie;import dao.model.Contrat;import dao.model.Passage;import dao.model.Plage;import dao.model.Spot;import exception.MetieException;/** * * @author alex */public class CreerContrat extends JPanel { protected IService service; protected Contrat contrat; protected Map<Categorie, List<Plage>> map=null; protected CreeContratTableModel myTableModel; protected DefaultComboBoxModel myComboBoxModel; protected final int COL_CHOIX=5; protected final int COL_TARIF=4; protected final int COL_DATE=1; protected final int COL_DEBUTSPOT=2; protected Map<Plage,Timestamp> mapPlage; /** Creates new form CreerContrat */ public CreerContrat(Contrat contrat) { this.contrat=contrat; mapPlage=new HashMap<Plage, Timestamp>();; initComponents(); updateContrat(contrat); } public CreerContrat(){ } public void updateContrat(Contrat contrat) { this.contrat=contrat; Spot s=contrat.getSpot(); this.service=new ServiceImpl(); try { map = service.suggestPlageBySpot(s); myComboBoxModel=new DefaultComboBoxModel(); jComboBox1.setModel(myComboBoxModel); Set<Categorie> set=map.keySet(); for(Categorie c:set){ myComboBoxModel.addElement(c); } jTextField1.setText(contrat.getNomClient()); validChoix.setEnabled(true); } catch (MetieException e) { if(e.getMessage().equals(ServiceImpl.PLAGES_INSUFFISSANT)){ JOptionPane.showMessageDialog(this, ServiceImpl.PLAGES_INSUFFISSANT, "error", JOptionPane.ERROR_MESSAGE); myTableModel.clear(); } } } /** 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 protected void initComponents() { jScrollPane1 = new JScrollPane(); jTable1 = new JTable(); jLabel1 = new JLabel(); jTextField1 = new JTextField(20); jLabel2 = new JLabel(); jButton1 = new JButton(); jLabel3 = new JLabel(); jComboBox1 = new JComboBox(); jLabel4 = new JLabel(); myComboBoxModel=new DefaultComboBoxModel(); myTableModel=new CreeContratTableModel(); coutTotal=new JLabel("0.0"); nbPassage=new JLabel(); jLabel4.setForeground(Color.RED); jLabel2.setForeground(Color.RED); coutTotal.setForeground(Color.RED); nbPassage.setForeground(Color.RED); validChoix=new JButton("Validé"); addAll=new JButton("Auto"); jTable1.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jTable1.setModel(myTableModel); TableColumn choixColumn=jTable1.getColumn("Choix"); JCheckBox checkbox=new JCheckBox(); checkbox.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { int row=jTable1.getSelectedRow(); int nb=Integer.valueOf(nbPassage.getText()); if(nb==0&& ((Boolean)jTable1.getValueAt(row, COL_CHOIX)).booleanValue()){ JOptionPane.showMessageDialog(null, "pas de passage restant" ); jTable1.setValueAt(Boolean.FALSE, row, COL_CHOIX); return; } BigDecimal cout=(BigDecimal) jTable1.getValueAt(row, COL_TARIF); double coutTl=Double.valueOf(coutTotal.getText()); if( ((JCheckBox)e.getSource()).isSelected()){ coutTotal.setText(cout.add(BigDecimal.valueOf(coutTl)).toString()); nb--; }else{ coutTotal.setText((BigDecimal.valueOf(coutTl).subtract(cout)).toString()); nb++; } nbPassage.setText(nb+""); } }); choixColumn.setCellEditor(new DefaultCellEditor(checkbox)); choixColumn.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class)); DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer(); colorColumnRenderer.setBackground(Color.LIGHT_GRAY); colorColumnRenderer.setToolTipText("Click pour chosir"); choixColumn.setPreferredWidth(50); TableColumn dateCol = jTable1.getColumn("Date"); DefaultTableCellRenderer dateColumnRenderer = new DateColorTableCellRender(); jTable1.getColumn("Date").setCellRenderer(dateColumnRenderer); jTable1.getColumn("Tarif").setCellRenderer(colorColumnRenderer); jTable1.getColumn("Catégorie").setPreferredWidth(80); jScrollPane1.setViewportView(jTable1); jLabel1.setText("Nom de client"); jTextField1.setText(""); jLabel2.setText("Coût Total: "); jButton1.setText("Créer"); jLabel3.setText("Plage Proposée"); jComboBox1.setModel(myComboBoxModel); jComboBox1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(jComboBox1.getSelectedItem()!=null) jComboBox1ItemStateChanged(e); } }); jLabel4.setText("Nombre de Passage restant: "); jButton1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { jButton1ActionPerformed(); } }); validChoix.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { validChoixActionPerformed(); } }); addAll.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { addAllAuto(); } }); this.setBorder(BorderFactory.createEtchedBorder()); GroupLayout layout = new GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel3) .addGap(19, 19, 19) .addComponent(jComboBox1, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(32, 32, 32) .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE) .addGap(96,96,96) .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addComponent(coutTotal,GroupLayout.PREFERRED_SIZE,50,GroupLayout.PREFERRED_SIZE) .addGap(35, 35, 35) .addComponent(jLabel4) .addComponent(nbPassage,GroupLayout.PREFERRED_SIZE,45,GroupLayout.PREFERRED_SIZE) .addGap(70,70,70) .addComponent(addAll) .addGap(10,10,10) .addComponent(validChoix)) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 529, GroupLayout.PREFERRED_SIZE) .addContainerGap()))) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jComboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE) .addGap(8, 8, 8) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(coutTotal) .addComponent(jLabel4) .addComponent(nbPassage) .addComponent(addAll) .addComponent(validChoix)) .addGap(18, 18, 18) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addGap(42, 42, 42)) ); }// </editor-fold>//GEN-END:initComponents protected void addAllAuto() { int nb=Integer.valueOf(nbPassage.getText()); double tarif = 0; for(int i=0;i<nb;i++){ jTable1.setValueAt(Boolean.TRUE, i, COL_CHOIX); tarif+=((BigDecimal) jTable1.getValueAt(i, COL_TARIF)).doubleValue(); } coutTotal.setText(tarif+""); nbPassage.setText("0"); } protected void validChoixActionPerformed() { if(Integer.valueOf(nbPassage.getText())!=0){ JOptionPane.showMessageDialog(null, "Passage n'ai pas finit"); return; } int rowCount=myTableModel.getRowCount(); for(;rowCount>0;rowCount--){ if(((Boolean)myTableModel.getValueAt(rowCount-1, COL_CHOIX)).booleanValue()){ String date=(String) myTableModel.getValueAt(rowCount-1, COL_DATE); String debut=(String) myTableModel.getValueAt(rowCount-1,COL_DEBUTSPOT); java.util.Date debutSpot; try { debutSpot = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").parse(date+" "+debut); mapPlage.put(myTableModel.getList().get(rowCount-1),new Timestamp(debutSpot.getTime())); } catch (ParseException e) { e.printStackTrace(); } } } myComboBoxModel.removeElement(myComboBoxModel.getSelectedItem()); if(myComboBoxModel.getSelectedItem()==null){ validChoix.setEnabled(false); } } protected void jComboBox1ItemStateChanged(ActionEvent e) {//GEN-FIRST:event_jComboBox1ItemStateChanged Categorie c=(Categorie)myComboBoxModel.getSelectedItem(); for(Passage p:contrat.getSpot().getLesPassages()){ if(p.getCategorie().equals(c.getNom())){ nbPassage.setText(String.valueOf(p.getNombre())); } } List<Plage> list=map.get(c); myTableModel.updateItems(list, c.getPrix()); }//GEN-LAST:event_jComboBox1ItemStateChanged protected void jButton1ActionPerformed() { String nomClient=jTextField1.getText(); if(nomClient.equals("")||nomClient==null){ JOptionPane.showMessageDialog(null, "Saisir le nom"); return; }else{ contrat.setNomClient(nomClient); } BigDecimal cout=BigDecimal.valueOf(Double.valueOf(coutTotal.getText())); contrat.setCout(cout); service.addContratComplete(contrat, mapPlage); coutTotal.setText("0.0"); mapPlage.clear(); //just for the initiation of contract if(jComboBox1.getSelectedIndex()==-1){ if(this.getParent() instanceof InitContrat){ ((InitContrat)this.getParent()).removeContrat(contrat); }else if(this.getParent() instanceof JTabbedPane){ ((JTabbedPane)this.getParent()).remove(this); JOptionPane.showMessageDialog(this, "Vous avaz créer un contrat"); } } } // Variables declaration - do not modify//GEN-BEGIN:variables private JButton jButton1; protected JComboBox jComboBox1; private JLabel jLabel1; private JLabel jLabel2; private JLabel jLabel3; private JLabel jLabel4; private JScrollPane jScrollPane1; protected JTable jTable1; protected JTextField jTextField1; // End of variables declaration//GEN-END:variables protected JLabel coutTotal; protected JLabel nbPassage; private JButton validChoix; private JButton addAll;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -