operatorjdialog.java.netbeans-base

来自「Store Manager」· NETBEANS-BASE 代码 · 共 266 行

NETBEANS-BASE
266
字号
/* * OperatorJDialog.java * * Created on 2007年5月31日, 下午3:57 */package com.studio009.store.ui;import com.studio009.store.StoreApp;import com.studio009.store.entity.Operator;import java.util.List;import java.util.Vector;import javax.persistence.EntityManager;import javax.persistence.Query;import javax.swing.JOptionPane;import javax.swing.table.DefaultTableModel;/** * 用来管理操作员的对话框 * 显示所以的操作员列表 * <p> * "admin"为默认的管理员,不能删除,不能更改组权限,<br /> * 避免所以的管理员都成为普通操作员,丧失软件功能的弊端. * </p> * @see OperatorEditer * @author  wangs */public class OperatorJDialog extends javax.swing.JDialog {        /** Creates new form OperatorJDialog     * @param parent 父窗口     * @param modal 模式     */    public OperatorJDialog(java.awt.Window parent, boolean modal) {        super(parent, java.awt.Dialog.ModalityType.APPLICATION_MODAL);                initComponents();                // 设置对话框居中        this.setLocationRelativeTo(parent);    }        /**     * 用来显示操作员的表格模型     * @return 操作员的表格模型     */    private DefaultTableModel initialTableModel() {        if (em != null) em.close();        em = StoreApp.getEntityManagerFactory().createEntityManager();        em.getTransaction().begin();        query = em.createQuery("SELECT o FROM Operator o");        list = query.getResultList();        Vector columnName=new Vector();        columnName.add("操作员姓名");        columnName.add("权限分组");        columnName.add("职务");        columnName.add("联系电话");        columnName.add("联系地址");        columnName.add("操作员备注");        Vector content=new Vector();        for( Operator o : list) {            Vector row = new Vector();            row.add(o.getOperatorName());            row.add(o.getOperatorType());            row.add(o.getOperatorDuty());            row.add(o.getOperatorPhone());            row.add(o.getOperatorAddress());            row.add(o.getOperatorNote());            content.add(row);        }        DefaultTableModel operatorTableModel=new DefaultTableModel(content,columnName){            public boolean isCellEditable(int row, int col) {                return false;            }        };        return operatorTableModel;    }        /**     * 更新表格     */    private void updateTable(){        jTable1.setModel(initialTableModel());    }        /** 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.     */    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents    private void initComponents() {        jScrollPane1 = new javax.swing.JScrollPane();        jTable1 = new javax.swing.JTable();        jButton1 = new javax.swing.JButton();        jButton2 = new javax.swing.JButton();        jButton3 = new javax.swing.JButton();        jButton4 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);        setTitle("\u64cd\u4f5c\u5458\u7ba1\u7406");        jTable1.setModel(initialTableModel());        jScrollPane1.setViewportView(jTable1);        jButton1.setText("\u7f16\u8f91");        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton1ActionPerformed(evt);            }        });        jButton2.setText("\u65b0\u5efa");        jButton2.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton2ActionPerformed(evt);            }        });        jButton3.setText("\u5220\u9664");        jButton3.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton3ActionPerformed(evt);            }        });        jButton4.setText("\u9000\u51fa");        jButton4.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton4ActionPerformed(evt);            }        });        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createSequentialGroup()                        .addGap(26, 26, 26)                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addGap(27, 27, 27)                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addGap(26, 26, 26)                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addGap(26, 26, 26)                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))                    .addGroup(layout.createSequentialGroup()                        .addContainerGap()                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)))                .addContainerGap())        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))                .addGap(18, 18, 18)                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed    this.dispose();}//GEN-LAST:event_jButton4ActionPerformedprivate void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed    OperatorEditer oe = new OperatorEditer(this, null, true);    oe.setVisible(true);    updateTable();}//GEN-LAST:event_jButton2ActionPerformedprivate void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed        if (jTable1.getSelectedRow() == -1) {        JOptionPane.showMessageDialog(this, "请选择一个操作员!", "错误", JOptionPane.ERROR_MESSAGE);        return;    }    int [] selected = jTable1.getSelectedRows();        String Names = "";    for (int i = selected.length - 1; i>= 0; i--) {        Names += " \"" + list.get(selected[i]).getOperatorName() + "\"";    }    int result = JOptionPane.showConfirmDialog(this, "确定要删除" + Names + " 吗?", "", JOptionPane.YES_NO_OPTION);    if (result != JOptionPane.OK_OPTION) return;        if (list.size() == 1) {        JOptionPane.showMessageDialog(this, "不能删除,至少要保留一个操作员!");        return;    }        for (int i = selected.length - 1; i >= 0; i--) {        Operator o = list.get(selected[i]);        if (o.getAccountCollection() == null || o.getAccountCollection().isEmpty() ||                o.getSaleCollection() == null || o.getSaleCollection().isEmpty() ||                o.getStockCollection() == null || o.getStockCollection().isEmpty()                ){            if (o.getOperatorName().equals("admin")) {                JOptionPane.showMessageDialog(this, "admin为默认管理员用户,不能删除!");            } else {                em.remove(o);            }        } else {            JOptionPane.showMessageDialog(this, o.getOperatorName() + " 已经发生了业务,不能删除!");        }    }    try{        em.getTransaction().commit();    } catch (Exception e) {        JOptionPane.showMessageDialog(this, "错误!");        em.getTransaction().rollback();    }    em.getTransaction().begin();    updateTable();}//GEN-LAST:event_jButton3ActionPerformedprivate void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed    if (jTable1.getSelectedRow() == -1) {        JOptionPane.showMessageDialog(this, "请选择一个操作员!", "错误", JOptionPane.ERROR_MESSAGE);        return;    }    OperatorEditer oe = new OperatorEditer(this, (String)jTable1.getValueAt(jTable1.getSelectedRow(), 0), true);    oe.setVisible(true);    updateTable();}//GEN-LAST:event_jButton1ActionPerformed/** * @param args the command line arguments */public static void main(String args[]) {    java.awt.EventQueue.invokeLater(new Runnable() {        public void run() {            OperatorJDialog dialog = new OperatorJDialog(new javax.swing.JFrame(), true);            dialog.addWindowListener(new java.awt.event.WindowAdapter() {                public void windowClosed(java.awt.event.WindowEvent e) {                    System.exit(0);                }            });            dialog.setVisible(true);        }    });}    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JButton jButton3;    private javax.swing.JButton jButton4;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JTable jTable1;    // End of variables declaration//GEN-END:variables    private EntityManager em;    private Query query;    private List<Operator> list;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?