Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dialog; import bean.Floor; import bean.Picture; import bean.Room; import common.Constant; import controller.ControllerRoom; import java.awt.Frame; import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import model.ModelFloor; import model.ModelPicture; import model.ModelRoom; import org.apache.commons.io.FilenameUtils; import renderer.RendererComboBoxFloor; /** * * @author GloomySunday */ public class DialogFunctionRoom extends javax.swing.JDialog { private List<URI> mListUriPhotos; private ControllerRoom mControllerRoom; private Room mObjRoom; private int mRow; private int mType; RendererComboBoxFloor mRendererComboBoxFloor; private List<Picture> mListPictures; private Frame mParent; public DialogFunctionRoom(Frame owner, boolean modal) { super(owner, modal); initComponents(); mListUriPhotos = new ArrayList<>(); mListPictures = new ArrayList<>(); mParent = owner; mRendererComboBoxFloor = new RendererComboBoxFloor(); cbFloor.setModel(mRendererComboBoxFloor); } /** * Creates new form DialogFunctionRoom * * @param parent * @param modal * @param controllerRoom */ public DialogFunctionRoom(java.awt.Frame parent, boolean modal, ControllerRoom controllerRoom) { this(parent, modal); this.mControllerRoom = controllerRoom; mType = Constant.TYPE_ADD; btnFunction.setText("Add"); btnFunction.setIcon(new ImageIcon(getClass().getResource("/images/ic_add.png"))); lbCondition.setVisible(false); cbCondition.setVisible(false); } public DialogFunctionRoom(java.awt.Frame parent, boolean modal, Floor objFloor) { this(parent, modal); mType = Constant.TYPE_ADD; btnFunction.setText("Add"); btnFunction.setIcon(new ImageIcon(getClass().getResource("/images/ic_add.png"))); lbCondition.setVisible(false); cbCondition.setVisible(false); mRendererComboBoxFloor.setSelectedItemById(objFloor.getIdFloor()); } public Room setVisibleReturnRoom() { this.setVisible(true); return mObjRoom; } public DialogFunctionRoom(Frame owner, boolean modal, ControllerRoom controllerRoom, int row, Room objRoom) { this(owner, modal); this.mObjRoom = objRoom; this.mRow = row; this.mControllerRoom = controllerRoom; mType = Constant.TYPE_EDIT; btnFunction.setText("Edit"); btnFunction.setIcon(new ImageIcon(getClass().getResource("/images/ic_edit.png"))); tfRoomName.setText(objRoom.getRoomName()); tfPrice.setText(objRoom.getPrice() + ""); cbRoomType.setSelectedIndex(objRoom.getType() - 1); mRendererComboBoxFloor.setSelectedItemById(objRoom.getIdFloor()); lbNumberPicture.setText("C " + mObjRoom.getListPicture().size() + " nh c ch?n"); mListPictures = mObjRoom.getListPicture(); tfMax.setText(mObjRoom.getMax() + ""); } /** * 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() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); tfRoomName = new javax.swing.JTextField(); tfPrice = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); cbRoomType = new javax.swing.JComboBox<>(); btnFunction = new javax.swing.JButton(); btnRefill = new javax.swing.JButton(); btnExit = new javax.swing.JButton(); btnChoosePicture = new javax.swing.JButton(); lbCondition = new javax.swing.JLabel(); cbCondition = new javax.swing.JComboBox<>(); lbNumberPicture = new javax.swing.JLabel(); cbFloor = new javax.swing.JComboBox<>(); jLabel6 = new javax.swing.JLabel(); tfMax = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(251, 250, 202)); jLabel1.setText("Tn phng"); jLabel2.setText("Tng"); jLabel3.setText("Gi"); jLabel4.setText("Loi"); jLabel5.setText("/ ng"); cbRoomType.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Phng n VIP", "Phng kp VIP", "Phng n ph thng", "Phng kp ph thng" })); btnFunction.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ic_add.png"))); // NOI18N btnFunction.setText("Thm"); btnFunction.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFunctionActionPerformed(evt); } }); btnRefill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ic_refill.png"))); // NOI18N btnRefill.setText("Nhp li"); btnRefill.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnRefillActionPerformed(evt); } }); btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ic_del.png"))); // NOI18N btnExit.setText("Thot"); btnExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnExitActionPerformed(evt); } }); btnChoosePicture.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ic_photo.png"))); // NOI18N btnChoosePicture.setText("Ch?n nh"); btnChoosePicture.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnChoosePictureActionPerformed(evt); } }); lbCondition.setText("Tnh trng"); cbCondition.setModel( new javax.swing.DefaultComboBoxModel<>(new String[] { "Bnh th?ng", "Sa cha" })); lbNumberPicture.setText("C 0 nh c ch?n"); cbFloor.setModel( new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jLabel6.setText("S ng?i"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 305, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(btnFunction) .addGap(18, 18, 18).addComponent(btnRefill).addGap(10, 10, 10) .addComponent(btnExit)) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3) .addComponent(jLabel4).addComponent(lbCondition).addComponent(jLabel6)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(tfPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5)) .addComponent(tfRoomName, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cbFloor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cbCondition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cbRoomType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup().addGap(36, 36, 36) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(tfMax, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lbNumberPicture)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnChoosePicture))) .addContainerGap()))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 317, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(tfRoomName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(cbFloor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(tfPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(cbRoomType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lbCondition) .addComponent(cbCondition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6).addComponent(tfMax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnChoosePicture).addComponent(lbNumberPicture)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnFunction).addComponent(btnRefill).addComponent(btnExit)) .addContainerGap()))); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold>//GEN-END:initComponents private void btnRefillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefillActionPerformed tfRoomName.setText(""); tfPrice.setText(""); cbRoomType.setSelectedIndex(0); cbFloor.setSelectedIndex(0); }//GEN-LAST:event_btnRefillActionPerformed private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed this.dispose(); }//GEN-LAST:event_btnExitActionPerformed private void btnFunctionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFunctionActionPerformed String roomName = tfRoomName.getText(); if (roomName.isEmpty()) { JOptionPane.showMessageDialog(null, "Tn phng khng c trng", "Error", JOptionPane.ERROR_MESSAGE); tfRoomName.requestFocus(); return; } String priceString = tfPrice.getText(); if (priceString.isEmpty()) { JOptionPane.showMessageDialog(null, "Gi phng khng c trng", "Error", JOptionPane.ERROR_MESSAGE); tfPrice.requestFocus(); return; } double price; try { price = Double.parseDouble(priceString); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "Gi phng khng ng nh dng", "Error", JOptionPane.ERROR_MESSAGE); return; } int max; try { max = Integer.parseInt(priceString); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "Gi phng khng ng nh dng", "Error", JOptionPane.ERROR_MESSAGE); return; } Floor objFloor = (Floor) cbFloor.getSelectedItem(); int roomType = cbRoomType.getSelectedIndex() + 1; ModelPicture modelPicture = new ModelPicture(); if (mType == Constant.TYPE_ADD) { if (mControllerRoom == null) { actionAddNoController(); return; } String idRoom = UUID.randomUUID().toString(); if (!mControllerRoom.isExistRoomInFloor(roomName, objFloor.getIdFloor())) { mListUriPhotos.stream().forEach((uriPicture) -> { File file = new File(uriPicture); String fileName = FilenameUtils.getBaseName(file.getName()) + "-" + System.nanoTime() + "." + FilenameUtils.getExtension(file.getName()); Path source = Paths.get(uriPicture); Path destination = Paths.get("files/" + fileName); try { Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { Logger.getLogger(DialogFunctionRoom.class.getName()).log(Level.SEVERE, null, ex); } Picture objPicture = new Picture(UUID.randomUUID().toString(), idRoom, fileName); modelPicture.addItem(objPicture); mListPictures.add(objPicture); }); Room objRoom = new Room(idRoom, objFloor.getIdFloor(), roomName, roomType, price, Constant.ROOM_CONDITION_AVAILABLE_TYPE, mListPictures, null, objFloor.getFloorName(), max); mObjRoom = objRoom; if (mControllerRoom.addItem(objRoom)) { objFloor.setMaxRoom(objFloor.getMaxRoom() + 1); new ModelFloor().editItem(objFloor); ImageIcon icon = new ImageIcon(getClass().getResource("/images/ic_success.png").getPath()); JOptionPane.showMessageDialog(this, "Thm thnh cng!", "Success", JOptionPane.INFORMATION_MESSAGE, icon); this.dispose(); } } else { JOptionPane.showMessageDialog(this, "Phng tn ti!", "Error", JOptionPane.ERROR_MESSAGE); } } else if (mType == Constant.TYPE_EDIT) { mObjRoom.setFloorName(objFloor.getFloorName()); mObjRoom.setRoomName(roomName); mObjRoom.setPrice(price); mObjRoom.setIdFloor(objFloor.getIdFloor()); mObjRoom.setMax(max); mObjRoom.setType(cbRoomType.getSelectedIndex() + 1); mListUriPhotos.stream().forEach((uriPicture) -> { File file = new File(uriPicture); String fileName = FilenameUtils.getBaseName(file.getName()) + "-" + System.nanoTime() + "." + FilenameUtils.getExtension(file.getName()); Path source = Paths.get(uriPicture); Path destination = Paths.get("files/" + fileName); try { Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { Logger.getLogger(DialogFunctionRoom.class.getName()).log(Level.SEVERE, null, ex); } Picture objPicture = new Picture(UUID.randomUUID().toString(), mObjRoom.getIdRoom(), fileName); modelPicture.addItem(objPicture); mListPictures.add(objPicture); }); mObjRoom.setListPicture(mListPictures); if (mControllerRoom.editItem(mObjRoom, mRow)) { ImageIcon icon = new ImageIcon(getClass().getResource("/images/ic_success.png").getPath()); JOptionPane.showMessageDialog(this, "Sa thnh cng!", "Success", JOptionPane.INFORMATION_MESSAGE, icon); this.dispose(); } } }//GEN-LAST:event_btnFunctionActionPerformed private void actionAddNoController() { String roomName = tfRoomName.getText(); if (roomName.isEmpty()) { JOptionPane.showMessageDialog(null, "Tn phng khng c trng", "Error", JOptionPane.ERROR_MESSAGE); tfRoomName.requestFocus(); return; } String priceString = tfPrice.getText(); if (priceString.isEmpty()) { JOptionPane.showMessageDialog(null, "Gi phng khng c trng", "Error", JOptionPane.ERROR_MESSAGE); tfPrice.requestFocus(); return; } double price; try { price = Double.parseDouble(priceString); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "Gi phng khng ng nh dng", "Error", JOptionPane.ERROR_MESSAGE); return; } int max; try { max = Integer.parseInt(priceString); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "Gi phng khng ng nh dng", "Error", JOptionPane.ERROR_MESSAGE); return; } Floor objFloor = (Floor) cbFloor.getSelectedItem(); int roomType = cbRoomType.getSelectedIndex() + 1; ModelPicture modelPicture = new ModelPicture(); String idRoom = UUID.randomUUID().toString(); if (!(new ModelRoom()).isExistRoomInFloor(roomName, objFloor.getIdFloor())) { mListUriPhotos.stream().forEach((uriPicture) -> { File file = new File(uriPicture); String fileName = FilenameUtils.getBaseName(file.getName()) + "-" + System.nanoTime() + "." + FilenameUtils.getExtension(file.getName()); Path source = Paths.get(uriPicture); Path destination = Paths.get("files/" + fileName); try { Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { Logger.getLogger(DialogFunctionRoom.class.getName()).log(Level.SEVERE, null, ex); } Picture objPicture = new Picture(UUID.randomUUID().toString(), idRoom, fileName); modelPicture.addItem(objPicture); mListPictures.add(objPicture); }); Room objRoom = new Room(idRoom, objFloor.getIdFloor(), roomName, roomType, price, Constant.ROOM_CONDITION_AVAILABLE_TYPE, mListPictures, null, objFloor.getFloorName(), max); mObjRoom = objRoom; if (new ModelRoom().addItem(objRoom)) { objFloor.setMaxRoom(objFloor.getMaxRoom() + 1); new ModelFloor().editItem(objFloor); ImageIcon icon = new ImageIcon(getClass().getResource("/images/ic_success.png").getPath()); JOptionPane.showMessageDialog(this, "Thm thnh cng!", "Success", JOptionPane.INFORMATION_MESSAGE, icon); this.dispose(); } } else { JOptionPane.showMessageDialog(this, "Phng tn ti!", "Error", JOptionPane.ERROR_MESSAGE); } } private void btnChoosePictureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChoosePictureActionPerformed DialogPickPicture dialogPickPicture = new DialogPickPicture(mParent, true, mListPictures); dialogPickPicture.setLocationRelativeTo(mParent); mListUriPhotos = dialogPickPicture.showDialog(); if (mListUriPhotos.size() > 4) { for (int i = 0; i < mListUriPhotos.size() - 4; i++) { mListUriPhotos.remove(i); } } lbNumberPicture.setText("C " + (mListUriPhotos.size() + mListPictures.size()) + " nh c ch?n"); }//GEN-LAST:event_btnChoosePictureActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnChoosePicture; private javax.swing.JButton btnExit; private javax.swing.JButton btnFunction; private javax.swing.JButton btnRefill; private javax.swing.JComboBox<String> cbCondition; private javax.swing.JComboBox<String> cbFloor; private javax.swing.JComboBox<String> cbRoomType; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JLabel lbCondition; private javax.swing.JLabel lbNumberPicture; private javax.swing.JTextField tfMax; private javax.swing.JTextField tfPrice; private javax.swing.JTextField tfRoomName; // End of variables declaration//GEN-END:variables }