- Hlavní stránka
- Balíky
- Třídy
- Soubory

# src/cz/neubauer/tomas/hazardanalysis/viewcontroller/administration/AdminFrame.java

Zobrazit dokumentaci tohoto souboru.

    00001
    00004 package cz.neubauer.tomas.hazardanalysis.viewcontroller.administration;
    00005
    00006 import java.awt.BorderLayout;
    00007 import java.awt.Dimension;
    00008 import java.awt.event.ActionEvent;
    00009 import java.awt.event.ActionListener;
    00010 import java.awt.event.MouseAdapter;
    00011 import java.awt.event.MouseEvent;
    00012 import java.awt.event.WindowEvent;
    00013 import java.awt.event.WindowListener;
    00014 import java.sql.Connection;
    00015 import java.sql.ResultSet;
    00016 import java.sql.SQLException;
    00017 import java.sql.Statement;
    00018 import java.util.ArrayList;
    00019
    00020 import javax.swing.JFrame;
    00021 import javax.swing.JMenuItem;
    00022 import javax.swing.JOptionPane;
    00023 import javax.swing.JPanel;
    00024 import javax.swing.JPopupMenu;
    00025 import javax.swing.JScrollPane;
    00026 import javax.swing.JTabbedPane;
    00027 import javax.swing.JTable;
    00028 import javax.swing.JTree;
    00029 import javax.swing.tree.DefaultMutableTreeNode;
    00030 import javax.swing.tree.DefaultTreeModel;
    00031
    00032 import cz.neubauer.tomas.hazardanalysis.model.components.Component;
    00033 import cz.neubauer.tomas.hazardanalysis.model.components.ComponentCatalog;
    00034 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerGroup;
    00035 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerType;
    00036 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
    00037 import cz.neubauer.tomas.hazardanalysis.model.tablemodels.ComponentTableModel;
    00038 import cz.neubauer.tomas.hazardanalysis.viewcontroller.MainFrame;
    00039 import cz.neubauer.tomas.hazardanalysis.viewcontroller.windowmanipulator.WindowManipulator;
    00040
    00044 public class AdminFrame extends JFrame {
    00045
    00049         private static final long serialVersionUID = -8831256305745180895L;
    00050
    00051         private MainFrame owner;
    00052
    00053         private JTabbedPane tpnTabs;
    00054
    00055         private JPanel pnlComponents, pnlDangers;
    00056
    00057         private JTable tblComponents;
    00058
    00059         private JTree treDangers;
    00060
    00061         private JPopupMenu pmnDangersPopup, pmnComponentsPopup;
    00062
    00063         private JMenuItem mniAddDanger, mniDeleteDanger, mniUpdateDanger;
    00064
    00065         private JMenuItem mniAddComponent, mniDeleteComponent, mniUpdateComponent;
    00066
    00067         private DangerGroupList dangerGroups;
    00068
    00069         public AdminFrame(MainFrame owner) {
    00070                 this.owner = owner;
    00071                 dangerGroups = new DangerGroupList();
    00072                 initGUI();
    00073                 // setVisible(true);
    00074         }
    00075
    00079         private void initGUI() {
    00080                 setDefaultCloseOperation(HIDE_ON_CLOSE);
    00081                 setTitle("Správa databáze");
    00082
    00083                 addWindowListener(new WindowListener() {
    00084
    00085                         @Override
    00086                         public void windowOpened(WindowEvent e) {
    00087                                 // nop
    00088                         }
    00089
    00090                         @Override
    00091                         public void windowIconified(WindowEvent e) {
    00092                                 // nop
    00093                         }
    00094
    00095                         @Override
    00096                         public void windowDeiconified(WindowEvent e) {
    00097                                 // nop
    00098                         }
    00099
    00100                         @Override
    00101                         public void windowDeactivated(WindowEvent e) {
    00102                                 // nop
    00103                         }
    00104
    00105                         @Override
    00106                         public void windowClosing(WindowEvent e) {
    00107                                 // nop
    00108                         }
    00109
    00110                         @Override
    00111                         public void windowClosed(WindowEvent e) {
    00112                                 owner.refresh();
    00113                         }
    00114
    00115                         @Override
    00116                         public void windowActivated(WindowEvent e) {
    00117                                 // nop
    00118                         }
    00119                 });
    00120
    00121                 initTabbedPanel();
    00122
    00123                 this.add(tpnTabs, BorderLayout.CENTER);
    00124                 pack();
    00125                 WindowManipulator.centerWindow(this);
    00126         }
    00127
    00131         private void initTabbedPanel() {
    00132                 tpnTabs = new JTabbedPane();
    00133
    00134                 initPanelComponents();
    00135                 tpnTabs.addTab("Komponenty", pnlComponents);
    00136
    00137                 initPanelDangers();
    00138                 tpnTabs.addTab("Definice nebezpečí", pnlDangers);
    00139
    00140                 initComponentPopupMenu();
    00141
    00142                 initDangersPopupMenu();
    00143
    00144         }
    00145
    00149         private void initComponentPopupMenu() {
    00150                 pmnComponentsPopup = new JPopupMenu("Možnosti komponenty");
    00151
    00152                 mniAddComponent = new JMenuItem("vložit");
    00153                 mniAddComponent.addActionListener(new ActionListener() {
    00154                         @Override
    00155                         public void actionPerformed(ActionEvent e) {
    00156                                 AdminFrame.this.mniAddComponentPerformed();
    00157                         }
    00158                 });
    00159                 pmnComponentsPopup.add(mniAddComponent);
    00160
    00161                 mniUpdateComponent = new JMenuItem("upravit");
    00162                 mniUpdateComponent.addActionListener(new ActionListener() {
    00163                         @Override
    00164                         public void actionPerformed(ActionEvent e) {
    00165                                 AdminFrame.this.mniUpdateComponentPerformed();
    00166                         }
    00167                 });
    00168                 pmnComponentsPopup.add(mniUpdateComponent);
    00169
    00170                 mniDeleteComponent = new JMenuItem("smazat");
    00171                 mniDeleteComponent.addActionListener(new ActionListener() {
    00172                         @Override
    00173                         public void actionPerformed(ActionEvent e) {
    00174                                 AdminFrame.this.mniDeleteComponentPerformed();
    00175                         }
    00176                 });
    00177                 pmnComponentsPopup.add(mniDeleteComponent);
    00178
    00179                 tblComponents.add(pmnComponentsPopup);
    00180         }
    00181
    00185         protected void mniAddComponentPerformed() {
    00186                 Component newComponent = new Component(0, "", "");
    00187                 new ComponentDangerVariantFrame(this, newComponent, dangerGroups);
    00188         }
    00189
    00193         protected void mniUpdateComponentPerformed() {
    00194                 int selectedComponentIndex = tblComponents.getSelectedRow();
    00195                 if (selectedComponentIndex >= 0) {
    00196                         ComponentTableModel componentTableModel = ((ComponentTableModel) tblComponents
    00197                                         .getModel());
    00198                         Component currentComponent = componentTableModel.getData().get(
    00199                                         selectedComponentIndex);
    00200
    00201                         new ComponentDangerVariantFrame(this, currentComponent,
    00202                                         dangerGroups);
    00203                 }
    00204         }
    00205
    00209         protected void mniDeleteComponentPerformed() {
    00210                 int selectedComponentIndex = tblComponents.getSelectedRow();
    00211                 if (selectedComponentIndex >= 0) {
    00212                         ComponentTableModel componentTableModel = ((ComponentTableModel) tblComponents
    00213                                         .getModel());
    00214                         Component currentComponent = componentTableModel.getData().get(
    00215                                         selectedComponentIndex);
    00216                         if (JOptionPane.showConfirmDialog(this,
    00217                                         "Oravdu si přejete odstranit komponentu \""
    00218                                                         + currentComponent.getName()
    00219                                                         + "\" jejích přiřazení k nebezpečím?") == JOptionPane.YES_OPTION) {
    00220                                 currentComponent.deleteFromDB(getConnection());
    00221                                 componentTableModel.getData().remove(selectedComponentIndex);
    00222                                 componentTableModel.fireTableDataChanged();
    00223                                 owner.refresh();
    00224                         }
    00225                 }
    00226         }
    00227
    00231         private void initDangersPopupMenu() {
    00232                 pmnDangersPopup = new JPopupMenu("Možnosti");
    00233
    00234                 mniAddDanger = new JMenuItem("vložit");
    00235                 mniAddDanger.addActionListener(new ActionListener() {
    00236                         @Override
    00237                         public void actionPerformed(ActionEvent e) {
    00238                                 AdminFrame.this.mniAddPerformed();
    00239                         }
    00240                 });
    00241                 pmnDangersPopup.add(mniAddDanger);
    00242
    00243                 mniUpdateDanger = new JMenuItem("upravit");
    00244                 mniUpdateDanger.addActionListener(new ActionListener() {
    00245                         @Override
    00246                         public void actionPerformed(ActionEvent e) {
    00247                                 AdminFrame.this.mniUpdatePerformed();
    00248                         }
    00249                 });
    00250                 pmnDangersPopup.add(mniUpdateDanger);
    00251
    00252                 mniDeleteDanger = new JMenuItem("smazat");
    00253                 mniDeleteDanger.addActionListener(new ActionListener() {
    00254                         @Override
    00255                         public void actionPerformed(ActionEvent e) {
    00256                                 AdminFrame.this.mniDeletePerformed();
    00257                         }
    00258                 });
    00259                 pmnDangersPopup.add(mniDeleteDanger);
    00260
    00261                 treDangers.add(pmnDangersPopup);
    00262         }
    00263
    00267         @SuppressWarnings("unchecked")
    00268         protected void mniAddPerformed() {
    00269                 DefaultMutableTreeNode selectedNode = ((DefaultMutableTreeNode) treDangers
    00270                                 .getLastSelectedPathComponent());
    00271                 if (selectedNode != null) {
    00272                         Object nodeObject = selectedNode.getUserObject();
    00273                         if (nodeObject instanceof DangerGroupList) {
    00274                                 // vložení nové skupiny
    00275                                 String name = JOptionPane
    00276                                                 .showInputDialog("Název skupiny nebezpečí");
    00277                                 if (name != null) {
    00278                                         DangerGroup dg = new DangerGroup(0, name);
    00279                                         dg.saveToDB(getConnection());
    00280                                         refreshTree();
    00281                                 }
    00282                         } else if (nodeObject instanceof DangerGroup) {
    00283                                 // vloženi noveho typu do skupiny
    00284
    00285                                 DangerGroup dg = (DangerGroup) nodeObject;
    00286                                 DangerType newType = new DangerType(dg, 0, "", "", "", "", "",
    00287                                                 "", "", "", "", "");
    00288                                 ArrayList<DangerGroup> groups = (ArrayList<DangerGroup>) ((DefaultMutableTreeNode) treDangers
    00289                                                 .getModel().getRoot()).getUserObject();
    00290                                 new AdminDangerTypeFrame(this, newType, groups);
    00291                         } else if (nodeObject instanceof DangerType) {
    00292                                 // vložení nové varianty do typu
    00293                                 DangerType dt = (DangerType) nodeObject;
    00294                                 DangerVariant dv = new DangerVariant(dt, 0, "");
    00295                                 new AdminDangerVariantFrame(this, dv);
    00296                         }
    00297                         // else if (nodeObject instanceof DangerVariant) {
    00298                         // DangerVariant dv = (DangerVariant) nodeObject;
    00299                         // new AdminDangerVariantFrame(this, new DangerVariant(dv
    00300                         // .getDangerType(), 0, ""));
    00301                         // }
    00302                 }
    00303         }
    00304
    00308         @SuppressWarnings("unchecked")
    00309         protected void mniUpdatePerformed() {
    00310                 DefaultMutableTreeNode selectedNode = ((DefaultMutableTreeNode) treDangers
    00311                                 .getLastSelectedPathComponent());
    00312                 if (selectedNode != null) {
    00313                         Object nodeObject = selectedNode.getUserObject();
    00314                         if (nodeObject instanceof DangerGroup) {
    00315                                 DangerGroup dg = (DangerGroup) nodeObject;
    00316                                 String newName = JOptionPane.showInputDialog("Název", dg
    00317                                                 .getName());
    00318                                 if (newName != null) {
    00319                                         dg.setName(newName);
    00320                                         dg.saveToDB(owner.getApplication().getConnection());
    00321                                         refreshTree();
    00322                                 }
    00323                         } else if (nodeObject instanceof DangerType) {
    00324
    00325                                 DangerType dt = (DangerType) nodeObject;
    00326                                 ArrayList<DangerGroup> groups = (ArrayList<DangerGroup>) ((DefaultMutableTreeNode) treDangers
    00327                                                 .getModel().getRoot()).getUserObject();
    00328                                 new AdminDangerTypeFrame(this, dt, groups);
    00329                         } else if (nodeObject instanceof DangerVariant) {
    00330                                 DangerVariant dv = (DangerVariant) nodeObject;
    00331                                 new AdminDangerVariantFrame(this, dv);
    00332                         }
    00333                 }
    00334         }
    00335
    00339         protected void mniDeletePerformed() {
    00340                 DefaultMutableTreeNode selectedNode = ((DefaultMutableTreeNode) treDangers
    00341                                 .getLastSelectedPathComponent());
    00342                 if (selectedNode != null) {
    00343                         Object nodeObject = selectedNode.getUserObject();
    00344                         if (nodeObject instanceof DangerVariant) {
    00345                                 DangerVariant dv = (DangerVariant) nodeObject;
    00346                                 if (JOptionPane.showConfirmDialog(this,
    00347                                                 "Opravdu si přejete smazat variantu \""
    00348                                                                 + dv.getDescription() + "\"?") == JOptionPane.YES_OPTION) {
    00349                                         dv.deleteFromDB(getConnection());
    00350                                         refreshTree();
    00351                                 }
    00352                         } else if (nodeObject instanceof DangerType) {
    00353                                 DangerType dt = (DangerType) nodeObject;
    00354                                 if (JOptionPane.showConfirmDialog(this,
    00355                                                 "Opravdu si přejete smazat typ \""
    00356                                                                 + dt.getDescription() + "\"?") == JOptionPane.YES_OPTION
    00357                                                 && JOptionPane.showConfirmDialog(this,
    00358                                                                 "Při smazání typu nebezpečí"
    00359                                                                                 + " dojde ke smazání všech jeho"
    00360                                                                                 + " variant včetně přiřazení"
    00361                                                                                 + " ke komponentám. Souhlasíte?") == JOptionPane.YES_OPTION) {
    00362                                         dt.deleteFromDB(getConnection());
    00363                                         refreshTree();
    00364                                 }
    00365                         } else if (nodeObject instanceof DangerGroup) {
    00366                                 DangerGroup dg = (DangerGroup) nodeObject;
    00367                                 if (JOptionPane.showConfirmDialog(this,
    00368                                                 "Opravdu si přejete smazat skupinu \"" + dg.getName()
    00369                                                                 + "\"?") == JOptionPane.YES_OPTION
    00370                                                 && JOptionPane.showConfirmDialog(this,
    00371                                                                 "Při smazání skupiny nebezpečí"
    00372                                                                                 + " dojde ke smazání všech jích"
    00373                                                                                 + " typů včetně přiřazení"
    00374                                                                                 + " ke komponentám a jejich"
    00375                                                                                 + " variant. Souhlasíte?") == JOptionPane.YES_OPTION) {
    00376                                         dg.deleteFromDB(getConnection());
    00377                                         refreshTree();
    00378                                 }
    00379                         }
    00380                 }
    00381         }
    00382
    00386         private void initPanelComponents() {
    00387                 pnlComponents = new JPanel();
    00388                 ComponentCatalog cs = owner.getApplication().getComponentCatalog();
    00389                 tblComponents = new JTable(new ComponentTableModel(this, cs
    00390                                 .getComponents()));
    00391                 tblComponents.addMouseListener(new ComponentsPopupListener());
    00392
    00393                 JScrollPane scroll = new JScrollPane(tblComponents);
    00394                 scroll.setViewportView(tblComponents);
    00395                 scroll.setPreferredSize(new Dimension(500, 300));
    00396
    00397                 pnlComponents.add(scroll, BorderLayout.CENTER);
    00398         }
    00399
    00403         private void initPanelDangers() {
    00404                 pnlDangers = new JPanel();
    00405
    00406                 treDangers = new JTree();
    00407                 treDangers.addMouseListener(new DangersPopupListener());
    00408                 refreshTree();
    00409
    00410                 JScrollPane scroll = new JScrollPane(treDangers);
    00411                 scroll.setViewportView(treDangers);
    00412                 scroll.setPreferredSize(new Dimension(500, 300));
    00413
    00414                 pnlDangers.add(scroll);
    00415         }
    00416
    00420         protected void refreshTree() {
    00421                 dangerGroups.clear();
    00422                 DefaultMutableTreeNode root = new DefaultMutableTreeNode(dangerGroups);
    00423
    00424                 try {
    00425                         Statement stmt_dg = owner.getApplication().getConnection()
    00426                                         .createStatement();
    00427                         Statement stmt_dt = owner.getApplication().getConnection()
    00428                                         .createStatement();
    00429                         Statement stmt_dv = owner.getApplication().getConnection()
    00430                                         .createStatement();
    00431
    00432                         stmt_dg.executeQuery("SELECT id, name FROM danger_group");
    00433                         ResultSet rs_dg = stmt_dg.getResultSet();
    00434                         DefaultMutableTreeNode dg_node;
    00435                         while (rs_dg.next()) {
    00436                                 DangerGroup dg = new DangerGroup(rs_dg.getInt("id"), rs_dg
    00437                                                 .getString("name"));
    00438                                 dg_node = new DefaultMutableTreeNode(dg);
    00439                                 stmt_dt
    00440                                                 .executeQuery("SELECT * FROM danger_type WHERE danger_group_id='"
    00441                                                                 + rs_dg.getInt("id") + "';");
    00442
    00443                                 ResultSet rs_dt = stmt_dt.getResultSet();
    00444                                 DefaultMutableTreeNode dt_node;
    00445                                 while (rs_dt.next()) {
    00446                                         DangerType dt = new DangerType(dg, rs_dt.getInt(1), rs_dt
    00447                                                         .getString(3), rs_dt.getString(4), rs_dt
    00448                                                         .getString(5), rs_dt.getString(6), rs_dt
    00449                                                         .getString(7), rs_dt.getString(8), rs_dt
    00450                                                         .getString(9), rs_dt.getString(10), rs_dt
    00451                                                         .getString(11), rs_dt.getString(12));
    00452                                         dt_node = new DefaultMutableTreeNode(dt);
    00453                                         stmt_dv
    00454                                                         .executeQuery("SELECT id, description FROM danger_variant WHERE danger_type_id='"
    00455                                                                         + rs_dt.getInt("id") + "';");
    00456
    00457                                         ResultSet rs_dv = stmt_dv.getResultSet();
    00458                                         DefaultMutableTreeNode dv_node;
    00459                                         while (rs_dv.next()) {
    00460                                                 DangerVariant dv = new DangerVariant(dt, rs_dv
    00461                                                                 .getInt("id"), rs_dv.getString("description"));
    00462                                                 dv_node = new DefaultMutableTreeNode(dv);
    00463                                                 dt_node.add(dv_node);
    00464                                                 dt.addDangerVariant(dv);
    00465                                         }
    00466                                         dg_node.add(dt_node);
    00467                                         dg.addDangerType(dt);
    00468                                 }
    00469                                 root.add(dg_node);
    00470                                 dangerGroups.add(dg);
    00471                         }
    00472
    00473                         stmt_dv.close();
    00474                         stmt_dt.close();
    00475                         stmt_dg.close();
    00476                 } catch (SQLException e) {
    00477                         e.printStackTrace();
    00478                 }
    00479                 ((DefaultTreeModel) treDangers.getModel()).setRoot(root);
    00480         }
    00481
    00482         public Connection getConnection() {
    00483                 return owner.getApplication().getConnection();
    00484         }
    00485
    00486         private class DangersPopupListener extends MouseAdapter {
    00487                 @Override
    00488                 public void mousePressed(MouseEvent e) {
    00489                         maybeShowPopup(e);
    00490                 }
    00491
    00492                 @Override
    00493                 public void mouseReleased(MouseEvent e) {
    00494                         maybeShowPopup(e);
    00495                 }
    00496
    00497                 private void maybeShowPopup(MouseEvent e) {
    00498                         if (e.isPopupTrigger()) {
    00499                                 DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) treDangers
    00500                                                 .getLastSelectedPathComponent();
    00501                                 if (selectedNode == null) {
    00502                                         return;
    00503                                 }
    00504                                 Object nodeObject = selectedNode.getUserObject();
    00505                                 if (nodeObject instanceof DangerGroupList) {
    00506                                         mniAddDanger.setVisible(true);
    00507                                         mniAddDanger.setText("vložit skupinu");
    00508                                         mniUpdateDanger.setVisible(false);
    00509                                         mniDeleteDanger.setVisible(false);
    00510                                 } else if (nodeObject instanceof DangerGroup) {
    00511                                         mniAddDanger.setVisible(true);
    00512                                         mniAddDanger.setText("vložit typ");
    00513                                         mniUpdateDanger.setVisible(true);
    00514                                         mniUpdateDanger.setText("upravit skupinu");
    00515                                         mniDeleteDanger.setVisible(true);
    00516                                         mniDeleteDanger.setText("smazat skupinu");
    00517                                 } else if (nodeObject instanceof DangerType) {
    00518                                         mniAddDanger.setVisible(true);
    00519                                         mniAddDanger.setText("vložit variantu");
    00520                                         mniUpdateDanger.setVisible(true);
    00521                                         mniUpdateDanger.setText("upravit typ");
    00522                                         mniDeleteDanger.setVisible(true);
    00523                                         mniDeleteDanger.setText("smazat typ");
    00524                                 } else if (nodeObject instanceof DangerVariant) {
    00525                                         mniAddDanger.setVisible(false);
    00526                                         mniUpdateDanger.setVisible(true);
    00527                                         mniUpdateDanger.setText("upravit variantu");
    00528                                         mniDeleteDanger.setVisible(true);
    00529                                         mniDeleteDanger.setText("smazat variantu");
    00530                                 } else {
    00531                                         return;
    00532                                 }
    00533                                 pmnDangersPopup.show(e.getComponent(), e.getX(), e.getY());
    00534                         }
    00535                 }
    00536         }
    00537
    00538         private class ComponentsPopupListener extends MouseAdapter {
    00539                 @Override
    00540                 public void mousePressed(MouseEvent e) {
    00541                         maybeShowPopup(e);
    00542                 }
    00543
    00544                 @Override
    00545                 public void mouseReleased(MouseEvent e) {
    00546                         maybeShowPopup(e);
    00547                 }
    00548
    00549                 private void maybeShowPopup(MouseEvent e) {
    00550                         if (e.isPopupTrigger()) {
    00551                                 pmnComponentsPopup.show(e.getComponent(), e.getX(), e.getY());
    00552                         }
    00553                 }
    00554         }
    00555
    00556         protected class DangerGroupList extends ArrayList<DangerGroup> {
    00560                 private static final long serialVersionUID = 6446228818721535648L;
    00561
    00562                 public DangerGroupList() {
    00563                         super(10);
    00564                 }
    00565
    00566                 /*
    00567                  * (non-Javadoc)
    00568                  *
    00569                  * @see java.util.AbstractCollection#toString()
    00570                  */
    00571                 @Override
    00572                 public String toString() {
    00573                         return "Rizika";
    00574                 }
    00575         }
    00576
    00577         @Override
    00578         public MainFrame getOwner() {
    00579                 return owner;
    00580         };
    00581 }