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

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

Zobrazit dokumentaci tohoto souboru.

    00001
    00004 package cz.neubauer.tomas.hazardanalysis.viewcontroller.dangeranalysis;
    00005
    00006 import java.awt.BorderLayout;
    00007 import java.awt.Dimension;
    00008 import java.awt.FlowLayout;
    00009 import java.awt.event.ActionEvent;
    00010 import java.awt.event.ActionListener;
    00011 import java.awt.event.MouseAdapter;
    00012 import java.awt.event.MouseEvent;
    00013 import java.io.BufferedWriter;
    00014 import java.io.File;
    00015 import java.io.FileWriter;
    00016 import java.io.IOException;
    00017 import java.util.ArrayList;
    00018
    00019 import javax.swing.JButton;
    00020 import javax.swing.JOptionPane;
    00021 import javax.swing.JPanel;
    00022 import javax.swing.JScrollPane;
    00023 import javax.swing.JTable;
    00024
    00025 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
    00026 import cz.neubauer.tomas.hazardanalysis.model.stages.Phase;
    00027 import cz.neubauer.tomas.hazardanalysis.model.stages.Stage;
    00028 import cz.neubauer.tomas.hazardanalysis.model.tablemodels.StageTableModel;
    00029
    00033 public class StagePanel extends JPanel {
    00037         private static final long serialVersionUID = 121459743732488051L;
    00038
    00039         private CriticalDangerAnalysisFrame owner;
    00040
    00041         private JPanel pnlTable, pnlButtons;
    00042
    00043         private JButton btnAdd, btnDelete;
    00044
    00045         private Stage stage;
    00046
    00047         private JTable tblPhases;
    00048
    00049         private ArrayList<DangerVariant> criticalVarians;
    00050
    00051         private StageTableModel stageTableModel;
    00052
    00056         public StagePanel(CriticalDangerAnalysisFrame owner, Stage stage,
    00057                         ArrayList<DangerVariant> criticalVarians) {
    00058                 setLayout(new BorderLayout());
    00059                 this.owner = owner;
    00060                 this.stage = stage;
    00061                 this.criticalVarians = criticalVarians;
    00062                 initGUI();
    00063         }
    00064
    00068         private void initGUI() {
    00069                 initTablePanel();
    00070                 this.add(pnlTable, BorderLayout.CENTER);
    00071
    00072                 initBottonsPanel();
    00073                 this.add(pnlButtons, BorderLayout.SOUTH);
    00074         }
    00075
    00079         private void initBottonsPanel() {
    00080                 pnlButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));
    00081
    00082                 btnAdd = new JButton("Přidat fázi");
    00083                 btnAdd.addActionListener(new ActionListener() {
    00084                         @Override
    00085                         public void actionPerformed(ActionEvent e) {
    00086                                 StagePanel.this.btnAddPerformed();
    00087                         }
    00088                 });
    00089                 pnlButtons.add(btnAdd);
    00090
    00091                 btnDelete = new JButton("Smazat fázi");
    00092                 btnDelete.addActionListener(new ActionListener() {
    00093                         @Override
    00094                         public void actionPerformed(ActionEvent e) {
    00095                                 StagePanel.this.btnDeletePerformed();
    00096                         }
    00097                 });
    00098                 pnlButtons.add(btnDelete);
    00099         }
    00100
    00104         protected void btnAddPerformed() {
    00105                 Phase p = new Phase(stage, "");
    00106                 stage.addPhase(p);
    00107                 new PhaseDangerVariantFrame(this, p, criticalVarians);
    00108         }
    00109
    00113         protected void btnDeletePerformed() {
    00114                 int selectedRow = tblPhases.getSelectedRow();
    00115                 if (selectedRow >= 0
    00116                                 && JOptionPane.showConfirmDialog(this,
    00117                                                 "Opravdu si přejete smazat tuto fázi?") == JOptionPane.YES_OPTION) {
    00118
    00119                         stage.removePhase(selectedRow);
    00120                         stageTableModel.fireTableDataChanged();
    00121                 }
    00122         }
    00123
    00127         private void initTablePanel() {
    00128                 pnlTable = new JPanel();
    00129                 stageTableModel = new StageTableModel(stage);
    00130                 tblPhases = new JTable(stageTableModel);
    00131                 tblPhases.addMouseListener(new MouseAdapter() {
    00132                         @Override
    00133                         public void mouseClicked(MouseEvent e) {
    00134                                 StagePanel.this.tblPhasesClicked();
    00135                         }
    00136                 });
    00137
    00138                 JScrollPane scroll = new JScrollPane(tblPhases);
    00139                 scroll.setViewportView(tblPhases);
    00140                 scroll.setPreferredSize(new Dimension(600, 300));
    00141
    00142                 pnlTable.add(scroll);
    00143         }
    00144
    00148         protected void tblPhasesClicked() {
    00149                 int selectedRowIndex = tblPhases.getSelectedRow();
    00150                 int selectedColumnIndex = tblPhases.getSelectedColumn();
    00151                 if (selectedColumnIndex == 4 && selectedRowIndex >= 0) {
    00152                         new PhaseDangerVariantFrame(this, stage.getPhase(selectedRowIndex),
    00153                                         criticalVarians);
    00154                 }
    00155         }
    00156
    00157         public Stage getStage() {
    00158                 return stage;
    00159         }
    00160
    00161         public void setStage(Stage stage) {
    00162                 this.stage = stage;
    00163         }
    00164
    00165         public void refresh() {
    00166                 stageTableModel.fireTableDataChanged();
    00167                 // ((StageTableModel) tblPhases.getModel()).fireTableDataChanged();
    00168         }
    00169
    00170         public CriticalDangerAnalysisFrame getOwner() {
    00171                 return owner;
    00172         }
    00173
    00174         public void exportData(int no, String file) {
    00175                 File f = new File(file);
    00176                 try {
    00177                         BufferedWriter bw = new BufferedWriter(new FileWriter(f));
    00178
    00179                         int rowCount = stageTableModel.getRowCount();
    00180                         int columnCount = stageTableModel.getColumnCount();
    00181
    00182                         String radek;
    00183                         for (int i = 0; i < rowCount; i++) {
    00184                                 radek = String.valueOf(no) + "." + (i + 1);
    00185                                 for (int j = 0; j < columnCount - 1; j++) {
    00186                                         radek += ";" + stageTableModel.getValueAt(i, j);
    00187                                 }
    00188                                 bw.write(radek);
    00189                                 bw.newLine();
    00190                         }
    00191
    00192                         bw.close();
    00193                 } catch (IOException e) {
    00194                         e.printStackTrace();
    00195                 }
    00196
    00197         }
    00198
    00199 }