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

# src/cz/neubauer/tomas/hazardanalysis/viewcontroller/dangeranalysis/CriticalDangerAnalysisFrame.java

Zobrazit dokumentaci tohoto souboru.

    00001
    00004 package cz.neubauer.tomas.hazardanalysis.viewcontroller.dangeranalysis;
    00005
    00006 import java.awt.BorderLayout;
    00007 import java.awt.FlowLayout;
    00008 import java.awt.event.ActionEvent;
    00009 import java.awt.event.ActionListener;
    00010 import java.io.BufferedWriter;
    00011 import java.io.File;
    00012 import java.io.FileWriter;
    00013 import java.io.IOException;
    00014 import java.sql.ResultSet;
    00015 import java.sql.SQLException;
    00016 import java.sql.Statement;
    00017 import java.util.ArrayList;
    00018 import java.util.Collections;
    00019 import java.util.HashMap;
    00020
    00021 import javax.swing.JButton;
    00022 import javax.swing.JFrame;
    00023 import javax.swing.JOptionPane;
    00024 import javax.swing.JPanel;
    00025 import javax.swing.JTabbedPane;
    00026
    00027 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
    00028 import cz.neubauer.tomas.hazardanalysis.model.stages.Stage;
    00029 import cz.neubauer.tomas.hazardanalysis.model.stages.StageManager;
    00030 import cz.neubauer.tomas.hazardanalysis.model.stages.VariantStage;
    00031 import cz.neubauer.tomas.hazardanalysis.viewcontroller.MainFrame;
    00032
    00036 public class CriticalDangerAnalysisFrame extends JFrame {
    00037
    00041         private static final long serialVersionUID = -8160786472107551281L;
    00042
    00043         private MainFrame owner;
    00044
    00045         private JTabbedPane tpnZalozky;
    00046
    00047         private JPanel pnlButtons;
    00048
    00049         private JButton btnForms, btnExport;
    00050
    00051         private StagePanel spnVyroba, spnPrepravaMontazInstalace,
    00052                         spnUvedeniDoProvozu, spnPouzivani, spnVyrazeniZProvozu;
    00053
    00054         private ArrayList<DangerVariant> criticalVariants;
    00055
    00056         private StageManager stageManager;
    00057
    00058         private DangerEstimationFrame dangerEstimationFrame;
    00059
    00060         public CriticalDangerAnalysisFrame(MainFrame owner,
    00061                         ArrayList<DangerVariant> criticalVariants) {
    00062                 this.owner = owner;
    00063                 this.criticalVariants = criticalVariants;
    00064                 Collections.sort(this.criticalVariants);
    00065
    00066                 stageManager = new StageManager();
    00067                 stageManager.loadFromDB(owner.getApplication().getConnection());
    00068
    00069                 initGUI();
    00070                 setVisible(true);
    00071         }
    00072
    00076         private void initGUI() {
    00077                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    00078                 setTitle("Analýza významných nebezpečí");
    00079
    00080                 initTabbedPanel();
    00081                 this.add(tpnZalozky, BorderLayout.CENTER);
    00082
    00083                 initButtonsPanel();
    00084                 this.add(pnlButtons, BorderLayout.SOUTH);
    00085
    00086                 pack();
    00087         }
    00088
    00092         private void initTabbedPanel() {
    00093                 tpnZalozky = new JTabbedPane();
    00094
    00095                 spnVyroba = new StagePanel(this, stageManager.getStage(0),
    00096                                 criticalVariants);
    00097                 tpnZalozky.addTab("Výroba", spnVyroba);
    00098
    00099                 spnPrepravaMontazInstalace = new StagePanel(this, stageManager
    00100                                 .getStage(1), criticalVariants);
    00101                 tpnZalozky.addTab("Přeprava, montáž a instalace",
    00102                                 spnPrepravaMontazInstalace);
    00103
    00104                 spnUvedeniDoProvozu = new StagePanel(this, stageManager.getStage(2),
    00105                                 criticalVariants);
    00106                 tpnZalozky.addTab("Uvedení do provozu", spnUvedeniDoProvozu);
    00107
    00108                 spnPouzivani = new StagePanel(this, stageManager.getStage(3),
    00109                                 criticalVariants);
    00110                 tpnZalozky.addTab("Používání", spnPouzivani);
    00111
    00112                 spnVyrazeniZProvozu = new StagePanel(this, stageManager.getStage(4),
    00113                                 criticalVariants);
    00114                 tpnZalozky.addTab("Vyřazení z provozu", spnVyrazeniZProvozu);
    00115
    00116         }
    00117
    00121         private void initButtonsPanel() {
    00122                 pnlButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));
    00123
    00124                 btnForms = new JButton("Formuláře pro odhad rizika");
    00125                 btnForms.addActionListener(new ActionListener() {
    00126                         @Override
    00127                         public void actionPerformed(ActionEvent e) {
    00128                                 btnFormsPerformed();
    00129                         }
    00130                 });
    00131                 pnlButtons.add(btnForms);
    00132
    00133                 btnExport = new JButton("export");
    00134                 btnExport.addActionListener(new ActionListener() {
    00135                         @Override
    00136                         public void actionPerformed(ActionEvent e) {
    00137                                 btnExportPerformed();
    00138                         }
    00139                 });
    00140                 pnlButtons.add(btnExport);
    00141         }
    00142
    00146         protected void btnExportPerformed() {
    00147                 spnPouzivani.exportData(4, "etapa-pouzivani.txt");
    00148                 spnPrepravaMontazInstalace.exportData(2,
    00149                                 "etapa-preprava-montaz-instalace.txt");
    00150                 spnUvedeniDoProvozu.exportData(3, "etapa-uvedeni-do-provozu.txt");
    00151                 spnVyrazeniZProvozu.exportData(5, "etapa-vyrazeni-z-provozu.txt");
    00152                 spnVyroba.exportData(1, "etapa-vyroba.txt");
    00153
    00154                 exportCriticalDangers("prehled-zavaznych-nebezpeci.txt",
    00155                                 "seznam-zavaznych-nebezpeci.txt");
    00156                 if (dangerEstimationFrame != null) {
    00157                         dangerEstimationFrame.export("odhad");
    00158                 }
    00159         }
    00160
    00164         private void exportCriticalDangers(String viewFile,
    00165                         String criticalDangersListFile) {
    00166                 BufferedWriter bw = null;
    00167                 BufferedWriter bw2 = null;
    00168                 Statement stmt = null;
    00169
    00170                 HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>(10);
    00171                 try {
    00172                         String conditions = " where ";
    00173                         int dt_id = 0;
    00174                         for (int i = 0; i < criticalVariants.size(); i++) {
    00175                                 dt_id = criticalVariants.get(i).getDangerType().getId();
    00176                                 if (counts.get(dt_id) == null) {
    00177                                         counts.put(dt_id, 1);
    00178                                 } else {
    00179                                         counts.put(dt_id, counts.get(dt_id) + 1);
    00180                                 }
    00181                                 if (i > 0) {
    00182                                         conditions += " or ";
    00183                                 }
    00184                                 conditions += "dv.id=" + criticalVariants.get(i).getId();
    00185                         }
    00186
    00187                         stmt = owner.getApplication().getConnection().createStatement();
    00188                         stmt
    00189                                         .executeQuery("select dg.id as dg_id, dg.name as dg_name, dt.id as dt_id, dt.code as dt_code,"
    00190                                                         + " dt.description as dt_description,"
    00191                                                         + " dt.column_cast2_a1_1995 as dt_column_cast2_a1_1995,"
    00192                                                         + " dt.column_cast1_r1991 as dt_column_cast1_R1991,"
    00193                                                         + " dt.column_cast2_r1991 as dt_column_cast2_R1991,"
    00194                                                         + " dt.column_csn_en_iso_12100_1_2003 as dt_column_csn_en_iso_12100_1_2003,"
    00195                                                         + " dt.column_csn_en_iso_12100_2_2003 as dt_column_csn_en_iso_12100_2_2003,"
    00196                                                         + " dt.column_normy_typu_b as dt_column_normy_typu_b,"
    00197                                                         + " dt.column_clanky_csn_en_12415_2001 as dt_column_clanky_csn_en_12415_2001,"
    00198                                                         + " dt.situation_description as dt_situation_description, dv.description as dv_description from ((danger_variant dv) join (danger_type dt) on (dv.danger_type_id = dt.id)) join (danger_group dg) on (dt.danger_group_id = dg.id)"
    00199                                                         + conditions + " order by dv.id asc");
    00200                         ResultSet rs = stmt.getResultSet();
    00201                         bw = new BufferedWriter(new FileWriter(new File(viewFile)));
    00202                         bw2 = new BufferedWriter(new FileWriter(new File(
    00203                                         criticalDangersListFile)));
    00204                         int oldDGID = 0, newDGID;
    00205                         int oldDTID = 0, newDTID;
    00206                         int i = 1;
    00207                         while (rs.next()) {
    00208                                 // přehled identifikovanych záv. nebezp.
    00209                                 newDGID = rs.getInt("dg_id");
    00210                                 if (newDGID != oldDGID) {
    00211                                         bw.write(rs.getInt("dg_id") + " " + rs.getString("dg_name")
    00212                                                         + "\n");
    00213                                         oldDGID = newDGID;
    00214                                 }
    00215
    00216                                 newDTID = rs.getInt("dt_id");
    00217                                 if (newDTID != oldDTID) {
    00218                                         bw.write("\t" + rs.getString("dt_code") + " "
    00219                                                         + rs.getString("dt_description") + "\n");
    00220
    00221                                         // seznam záv. nebezp.
    00222                                         bw2.write(rs.getString("dt_code") + ";");
    00223                                         bw2.write(rs.getString("dt_description") + ";");
    00224                                         bw2.write(rs.getString("dt_column_cast2_a1_1995") + ";");
    00225                                         bw2.write(rs.getString("dt_column_cast1_R1991") + ";");
    00226                                         bw2.write(rs.getString("dt_column_cast2_R1991") + ";");
    00227                                         bw2.write(rs.getString("dt_column_csn_en_iso_12100_1_2003")
    00228                                                         + ";");
    00229                                         bw2.write(rs.getString("dt_column_csn_en_iso_12100_2_2003")
    00230                                                         + ";");
    00231                                         bw2.write(rs.getString("dt_situation_description") + ";");
    00232                                         bw2.write(rs.getString("dt_column_normy_typu_b") + ";");
    00233                                         bw2.write(rs
    00234                                                         .getString("dt_column_clanky_csn_en_12415_2001")
    00235                                                         + ";");
    00236                                         Integer count = null;
    00237                                         if ((count = counts.get(newDTID)) == null) {
    00238                                                 count = new Integer(0);
    00239                                         }
    00240                                         bw2.write(String.valueOf(count));
    00241                                         bw2.newLine();
    00242
    00243                                         oldDTID = newDTID;
    00244                                         i = 1;
    00245                                 }
    00246
    00247                                 bw.write("\t\t" + rs.getString("dt_code") + "-" + (i++) + " "
    00248                                                 + rs.getString("dv_description") + "\n");
    00249
    00250                         }
    00251                 } catch (SQLException e) {
    00252                         e.printStackTrace();
    00253                 } catch (IOException e) {
    00254                         e.printStackTrace();
    00255                 } finally {
    00256                         if (stmt != null) {
    00257                                 try {
    00258                                         stmt.close();
    00259                                 } catch (SQLException e) {
    00260                                         e.printStackTrace();
    00261                                 }
    00262                         }
    00263
    00264                         if (bw != null) {
    00265                                 try {
    00266                                         bw.close();
    00267                                 } catch (IOException e) {
    00268                                         e.printStackTrace();
    00269                                 } finally {
    00270                                         if (bw2 != null) {
    00271                                                 try {
    00272                                                         bw2.close();
    00273                                                 } catch (IOException e) {
    00274                                                         e.printStackTrace();
    00275                                                 }
    00276                                         }
    00277                                 }
    00278                         }
    00279
    00280                 }
    00281         }
    00282
    00286         protected void btnFormsPerformed() {
    00287                 if (criticalVariants.size() > 0) {
    00288                         ArrayList<VariantStage> data = new ArrayList<VariantStage>(3);
    00289                         for (int i = 0; i < 5; i++) {
    00290                                 Stage s = stageManager.getStage(i);
    00291                                 ArrayList<DangerVariant> variants = s.getDangerVariants();
    00292                                 for (int j = 0; j < variants.size(); j++) {
    00293                                         data.add(new VariantStage(variants.get(j), j, s));
    00294                                 }
    00295                         }
    00296
    00297                         if (data.size() > 0) {
    00298                                 if (dangerEstimationFrame == null) {
    00299                                         dangerEstimationFrame = new DangerEstimationFrame(owner
    00300                                                         .getApplication(), this, data);
    00301                                 } else {
    00302                                         dangerEstimationFrame.setVariantStagesData(data);
    00303                                         dangerEstimationFrame.setVisible(true);
    00304                                 }
    00305                         }
    00306                 } else {
    00307                         JOptionPane.showMessageDialog(this,
    00308                                         "Není stanoveno žádné závažné nebezpečí!", "Chyba!",
    00309                                         JOptionPane.ERROR_MESSAGE);
    00310                 }
    00311
    00312         }
    00313
    00314         public void refresh() {
    00315                 spnVyroba.refresh();
    00316                 spnPrepravaMontazInstalace.refresh();
    00317                 spnUvedeniDoProvozu.refresh();
    00318                 spnVyrazeniZProvozu.refresh();
    00319                 spnPouzivani.refresh();
    00320         }
    00321
    00322         public ArrayList<DangerVariant> getCriticalVarians() {
    00323                 return criticalVariants;
    00324         }
    00325 }