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

# src/cz/neubauer/tomas/hazardanalysis/viewcontroller/administration/ComponentDangerVariantFrame.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.FlowLayout;
    00009 import java.awt.GridLayout;
    00010 import java.awt.event.ActionEvent;
    00011 import java.awt.event.ActionListener;
    00012 import java.awt.event.MouseAdapter;
    00013 import java.awt.event.MouseEvent;
    00014 import java.sql.ResultSet;
    00015 import java.sql.SQLException;
    00016 import java.sql.Statement;
    00017 import java.util.ArrayList;
    00018
    00019 import javax.swing.JButton;
    00020 import javax.swing.JComboBox;
    00021 import javax.swing.JFrame;
    00022 import javax.swing.JLabel;
    00023 import javax.swing.JOptionPane;
    00024 import javax.swing.JPanel;
    00025 import javax.swing.JScrollPane;
    00026 import javax.swing.JTable;
    00027 import javax.swing.JTextField;
    00028
    00029 import cz.neubauer.tomas.hazardanalysis.model.components.Component;
    00030 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerGroup;
    00031 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerType;
    00032 import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
    00033 import cz.neubauer.tomas.hazardanalysis.model.tablemodels.ComponentDangerVariantTableModel;
    00034 import cz.neubauer.tomas.hazardanalysis.viewcontroller.windowmanipulator.WindowManipulator;
    00035
    00039 public class ComponentDangerVariantFrame extends JFrame {
    00040
    00041         private boolean saved;
    00042
    00046         private static final long serialVersionUID = -2009393442557761987L;
    00047
    00048         private AdminFrame owner;
    00049
    00050         private JPanel pnlForm, pnlTable, pnlButtons;
    00051
    00052         private JButton btnSave, btnClose;
    00053
    00054         private JTextField txtComponentName;
    00055
    00056         private JTextField txtComponentLocation;
    00057
    00058         private JTable tableVariants;
    00059
    00060         private ComponentDangerVariantTableModel componentDangerVariantTableModel;
    00061
    00062         private ArrayList<Integer> assignedDangerVariantIDs;
    00063
    00064         private JComboBox cmbGroups;
    00065
    00066         private JComboBox cmbTypes;
    00067
    00068         private ArrayList<DangerGroup> dangerGroups;
    00069
    00070         private DangerGroup currentDangerGroup;
    00071
    00072         private DangerType currentDangerType;
    00073
    00074         private Component component;
    00075
    00076         public ComponentDangerVariantFrame(AdminFrame owner, Component component,
    00077                         ArrayList<DangerGroup> dangerGroups) {
    00078                 this.owner = owner;
    00079                 this.component = component;
    00080                 this.dangerGroups = dangerGroups;
    00081                 assignedDangerVariantIDs = new ArrayList<Integer>(10);
    00082                 saved = true;
    00083                 initGUI();
    00084                 // updateSelectedDangerVariants();
    00085                 refreshDangerVariants();
    00086         }
    00087
    00088         // /**
    00089         // *
    00090         // */
    00091         // private void updateSelectedDangerVariants() {
    00092         // assignedDangerVariantIDs.clear();
    00093         // int dangerGroupCount = component.getDangerGroupCount();
    00094         // DangerGroup dg;
    00095         // for (int i = 0; i < dangerGroupCount; i++) {
    00096         // dg = component.getDangerGroup(i);
    00097         //
    00098         // int dangerTypeCount = dg.getDangerTypeCount();
    00099         // DangerType dt;
    00100         // for (int j = 0; j < dangerTypeCount; j++) {
    00101         // dt = dg.getDangerType(j);
    00102         //
    00103         // int dangerVariantCount = dt.getDangerVariantCount();
    00104         // for (int k = 0; k < dangerVariantCount; k++) {
    00105         // assignedDangerVariantIDs.add(new Integer(dt
    00106         // .getDangerVariant(k).getId()));
    00107         // }
    00108         // }
    00109         // }
    00110         // }
    00111
    00115         private void initGUI() {
    00116                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    00117                 setTitle("Přiřazení nebezpečí komponentě");
    00118
    00119                 initPanelTable();
    00120                 this.add(pnlTable, BorderLayout.CENTER);
    00121
    00122                 initPanelForm();
    00123                 this.add(pnlForm, BorderLayout.NORTH);
    00124
    00125                 initPanelButtons();
    00126                 this.add(pnlButtons, BorderLayout.SOUTH);
    00127
    00128                 pack();
    00129                 WindowManipulator.centerWindow(this);
    00130                 setVisible(true);
    00131         }
    00132
    00136         private void initPanelButtons() {
    00137                 pnlButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));
    00138
    00139                 btnSave = new JButton("Uložit");
    00140                 btnSave.addActionListener(new ActionListener() {
    00141                         @Override
    00142                         public void actionPerformed(ActionEvent e) {
    00143                                 ComponentDangerVariantFrame.this.btnSavePerformed();
    00144                         }
    00145                 });
    00146                 pnlButtons.add(btnSave);
    00147
    00148                 btnClose = new JButton("Zavřít");
    00149                 btnClose.addActionListener(new ActionListener() {
    00150                         @Override
    00151                         public void actionPerformed(ActionEvent e) {
    00152                                 ComponentDangerVariantFrame.this.btnClosePerformed();
    00153                         }
    00154                 });
    00155                 pnlButtons.add(btnClose);
    00156         }
    00157
    00161         protected void btnClosePerformed() {
    00162                 dispose();
    00163         }
    00164
    00168         protected void btnSavePerformed() {
    00169                 if (txtComponentName.getText().equals("")) {
    00170                         JOptionPane
    00171                                         .showMessageDialog(this, "Není vyplněn název komponenty");
    00172                 } else if (txtComponentLocation.getText().equals("")) {
    00173                         JOptionPane.showMessageDialog(this,
    00174                                         "Není vyplněno umístění komponenty");
    00175                 } else {
    00176                         component.setName(txtComponentName.getText());
    00177                         component.setLocation(txtComponentLocation.getText());
    00178                         component.saveToDB(owner.getConnection());
    00179
    00180                         try {
    00181                                 Statement stmt = owner.getConnection().createStatement();
    00182                                 Statement stmt2 = owner.getConnection().createStatement();
    00183                                 // stmt
    00184                                 // .executeUpdate("DELETE FROM component_danger_variant cdv
    00185                                 // WHERE cdv.component_id = '"
    00186                                 // + component.getId()
    00187                                 // + "' AND (SELECT danger_type_id FROM danger_variant dv WHERE
    00188                                 // dv.id = cdv.danger_variant_id) = '"
    00189                                 // + currentDangerType.getId() + "';");
    00190                                 ResultSet rs = stmt2
    00191                                                 .executeQuery("SELECT id FROM danger_variant WHERE danger_type_id = '"
    00192                                                                 + currentDangerType.getId() + "';");
    00193                                 while (rs.next()) {
    00194                                         stmt
    00195                                                         .executeUpdate("DELETE FROM component_danger_variant WHERE component_id = '"
    00196                                                                         + component.getId()
    00197                                                                         + "' AND danger_variant_id = '"
    00198                                                                         + rs.getInt("id") + "';");
    00199                                 }
    00200
    00201                                 // omponent.setDangerGroups(new ArrayList<DangerGroup>(100));
    00202
    00203                                 DangerType dt = currentDangerType;
    00204
    00205                                 int dangerVariantCount = dt.getDangerVariantCount();
    00206                                 DangerVariant dv;
    00207                                 for (int k = 0; k < dangerVariantCount; k++) {
    00208                                         dv = dt.getDangerVariant(k);
    00209                                         if (dv.isSelected()) {
    00210                                                 stmt
    00211                                                                 .executeUpdate("INSERT INTO component_danger_variant VALUES ('"
    00212                                                                                 + component.getId()
    00213                                                                                 + "', '"
    00214                                                                                 + dv.getId() + "');");
    00215                                         }
    00216                                 }
    00217
    00218                                 owner.refreshTree();
    00219                                 owner.getOwner().refresh();
    00220                                 component = owner.getOwner().getApplication()
    00221                                                 .getComponentCatalog().getComponentByName(
    00222                                                                 component.getName());
    00223                                 componentDangerVariantTableModel.setComponent(component);
    00224                                 componentDangerVariantTableModel.fireTableDataChanged();
    00225                                 stmt.close();
    00226                                 JOptionPane.showMessageDialog(this, "Komponenta byla uložena",
    00227                                                 "Informace", JOptionPane.INFORMATION_MESSAGE);
    00228                                 saved = true;
    00229                                 // dispose();
    00230                         } catch (SQLException e) {
    00231                                 e.printStackTrace();
    00232                         }
    00233
    00234                 }
    00235         }
    00236
    00240         private void initPanelForm() {
    00241                 pnlForm = new JPanel(new GridLayout(8, 1));
    00242
    00243                 pnlForm.add(new JLabel("Název komponenty"));
    00244
    00245                 txtComponentName = new JTextField(component.getName(), 10);
    00246                 pnlForm.add(txtComponentName);
    00247
    00248                 pnlForm.add(new JLabel("Umístění komponenty"));
    00249
    00250                 txtComponentLocation = new JTextField(component.getLocation(), 10);
    00251                 pnlForm.add(txtComponentLocation);
    00252
    00253                 initCombos();
    00254                 pnlForm.add(new JLabel("Skupina nebezpečí"));
    00255                 pnlForm.add(cmbGroups);
    00256                 pnlForm.add(new JLabel("Typ nebezpečí"));
    00257                 pnlForm.add(cmbTypes);
    00258         }
    00259
    00263         private void initPanelTable() {
    00264                 pnlTable = new JPanel();
    00265                 componentDangerVariantTableModel = new ComponentDangerVariantTableModel(
    00266                                 component, new ArrayList<DangerVariant>(10));
    00267
    00268                 tableVariants = new JTable(componentDangerVariantTableModel);
    00269
    00270                 tableVariants.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    00271                 tableVariants.getColumnModel().getColumn(0).setPreferredWidth(400);
    00272                 tableVariants.getColumnModel().getColumn(1).setPreferredWidth(50);
    00273
    00274                 tableVariants.addMouseListener(new MouseAdapter() {
    00275                         @Override
    00276                         public void mouseClicked(MouseEvent e) {
    00277                                 ComponentDangerVariantFrame.this.tableClicked();
    00278                         }
    00279                 });
    00280                 JScrollPane spn = new JScrollPane(tableVariants);
    00281                 spn.setViewportView(tableVariants);
    00282                 spn.setPreferredSize(new Dimension(453, 200));
    00283
    00284                 pnlTable.add(spn, BorderLayout.CENTER);
    00285         }
    00286
    00291         private void refreshComboTypes(int dangerGroupIndex) {
    00292                 cmbTypes.removeAllItems();
    00293                 currentDangerGroup = dangerGroups.get(dangerGroupIndex);
    00294                 ArrayList<DangerType> types = currentDangerGroup.getTypes();
    00295                 int dangerTypeCount = types.size();
    00296                 for (int i = 0; i < dangerTypeCount; i++) {
    00297                         cmbTypes.addItem(types.get(i));
    00298                 }
    00299                 if (types.size() > 0) {
    00300                         currentDangerType = types.get(0);
    00301                 } else {
    00302                         currentDangerType = null;
    00303                         cmbTypes.addItem("Tato skupina neobsahuje žádné typy");
    00304                 }
    00305
    00306         }
    00307
    00311         private void initCombos() {
    00312                 cmbGroups = new JComboBox();
    00313                 cmbTypes = new JComboBox();
    00314
    00315                 cmbGroups.addActionListener(new ActionListener() {
    00316                         @Override
    00317                         public void actionPerformed(ActionEvent e) {
    00318                                 ComponentDangerVariantFrame.this.cmbGroupsChanged();
    00319                         }
    00320                 });
    00321                 int dangerGroupCount = dangerGroups.size();
    00322                 for (int i = 0; i < dangerGroupCount; i++) {
    00323                         cmbGroups.addItem(dangerGroups.get(i));
    00324                 }
    00325
    00326                 cmbTypes = new JComboBox();
    00327                 cmbTypes.addActionListener(new ActionListener() {
    00328                         @Override
    00329                         public void actionPerformed(ActionEvent e) {
    00330                                 ComponentDangerVariantFrame.this.cmbTypesChanged();
    00331                         }
    00332                 });
    00333                 refreshComboTypes(0);
    00334         }
    00335
    00339         protected void tableClicked() {
    00340                 int selectedRowIndex = tableVariants.getSelectedRow();
    00341                 int selectedColumnIndex = tableVariants.getSelectedColumn();
    00342                 if (selectedColumnIndex == 1 && selectedRowIndex >= 0) {
    00343                         saved = false;
    00344                         DangerVariant selectedDangerVariant = currentDangerType
    00345                                         .getVariants().get(selectedRowIndex);
    00346                         selectedDangerVariant.setSelected(!selectedDangerVariant
    00347                                         .isSelected());
    00348                         componentDangerVariantTableModel.fireTableDataChanged();
    00349                 }
    00350         }
    00351
    00355         protected void cmbTypesChanged() {
    00356                 int selectedIndex = cmbTypes.getSelectedIndex();
    00357                 if (selectedIndex >= 0) {
    00358                         if (!saved) {
    00359                                 if (JOptionPane.showConfirmDialog(this,
    00360                                                 "Byly provedeny změny. Uložit?") == JOptionPane.YES_OPTION) {
    00361                                         btnSavePerformed();
    00362                                         // refreshDangerVariants();
    00363                                 }
    00364                         }
    00365                         saved = true;
    00366                         if (currentDangerGroup.getDangerTypeCount() > 0) {
    00367                                 currentDangerType = currentDangerGroup
    00368                                                 .getDangerType(selectedIndex);
    00369                         } else {
    00370                                 currentDangerType = null;
    00371                         }
    00372                         refreshDangerVariants();
    00373                 }
    00374         }
    00375
    00379         protected void cmbGroupsChanged() {
    00380                 currentDangerGroup = dangerGroups.get(cmbGroups.getSelectedIndex());
    00381                 refreshComboTypes(cmbGroups.getSelectedIndex());
    00382         }
    00383
    00387         private void refreshDangerVariants() {
    00388                 if (currentDangerType != null) {
    00389                         int dangerVariantCount = currentDangerType.getDangerVariantCount();
    00390                         ArrayList<DangerVariant> variants = currentDangerType.getVariants();
    00391                         DangerVariant currentDangerVariant;
    00392                         for (int i = 0; i < dangerVariantCount; i++) {
    00393                                 currentDangerVariant = variants.get(i);
    00394                                 if (component.hasDangerVariant(currentDangerVariant)) {
    00395                                         currentDangerVariant.setSelected(true);
    00396                                 } else {
    00397                                         currentDangerVariant.setSelected(false);
    00398                                 }
    00399                         }
    00400                         componentDangerVariantTableModel
    00401                                         .setDangerVariants(currentDangerType.getVariants());
    00402                 } else {
    00403                         componentDangerVariantTableModel.getDangerVariants().clear();
    00404                 }
    00405                 // this.tableVariants.setModel(dangerVariantTableModel);
    00406         }
    00407
    00408         public ArrayList<Integer> getSelectedDangerVariantIDs() {
    00409                 return assignedDangerVariantIDs;
    00410         }
    00411
    00412         public void setSelectedDangerVariantIDs(
    00413                         ArrayList<Integer> selectedDangerVariantIDs) {
    00414                 assignedDangerVariantIDs = selectedDangerVariantIDs;
    00415         }
    00416 }