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

# src/cz/neubauer/tomas/hazardanalysis/viewcontroller/dangeranalysis/PersonSelectionFrame.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
    00012 import javax.swing.JButton;
    00013 import javax.swing.JFrame;
    00014 import javax.swing.JPanel;
    00015 import javax.swing.JScrollPane;
    00016 import javax.swing.JTable;
    00017 import javax.swing.table.AbstractTableModel;
    00018
    00022 public class PersonSelectionFrame extends JFrame {
    00026         private static final long serialVersionUID = 7050205720615355103L;
    00027
    00028         private DangerEstimationFrame owner;
    00029
    00030         private JPanel pnlTable, pnlButton;
    00031
    00032         private JTable tblPersons;
    00033
    00034         private PersonsTableModel personsTableModel;
    00035
    00036         private JButton btnOK;
    00037
    00038         public PersonSelectionFrame(DangerEstimationFrame owner) {
    00039                 this.owner = owner;
    00040                 initGUI();
    00041         }
    00042
    00046         private void initGUI() {
    00047                 setDefaultCloseOperation(HIDE_ON_CLOSE);
    00048                 setTitle("Ohrožené osoby");
    00049                 setLayout(new BorderLayout());
    00050
    00051                 pnlTable = new JPanel();
    00052                 personsTableModel = new PersonsTableModel();
    00053                 tblPersons = new JTable(personsTableModel);
    00054                 JScrollPane scroll = new JScrollPane(tblPersons);
    00055                 scroll.setViewportView(tblPersons);
    00056                 scroll.setPreferredSize(new Dimension(150, 85));
    00057                 pnlTable.add(scroll);
    00058
    00059                 pnlButton = new JPanel(new FlowLayout(FlowLayout.CENTER));
    00060                 btnOK = new JButton("OK");
    00061                 btnOK.addActionListener(new ActionListener() {
    00062                         @Override
    00063                         public void actionPerformed(ActionEvent e) {
    00064                                 PersonSelectionFrame.this.btnOKPerformed();
    00065                         }
    00066                 });
    00067                 pnlButton.add(btnOK);
    00068
    00069                 this.add(pnlTable, BorderLayout.CENTER);
    00070                 this.add(pnlButton, BorderLayout.SOUTH);
    00071
    00072                 pack();
    00073         }
    00074
    00078         protected void btnOKPerformed() {
    00079                 String text = "";
    00080                 for (int i = 0, j = 0; i < personsTableModel.getPersons()
    00081                                 .getPersonsCount(); i++) {
    00082                         if (personsTableModel.getPersons().isPersonSelected(i)) {
    00083                                 if (j > 0) {
    00084                                         text += ", ";
    00085                                 }
    00086                                 text += personsTableModel.getPersons().getPerson(i);
    00087                                 j++;
    00088                         }
    00089                 }
    00090                 owner.setPersons(text);
    00091                 // owner.btnSavePerformed();
    00092
    00093                 setVisible(false);
    00094         }
    00095
    00096         private class PersonsTableModel extends AbstractTableModel {
    00097
    00101                 private static final long serialVersionUID = -2583733951000179245L;
    00102
    00103                 private Persons persons;
    00104
    00105                 public PersonsTableModel() {
    00106                         persons = new Persons();
    00107                 }
    00108
    00109                 /*
    00110                  * (non-Javadoc)
    00111                  *
    00112                  * @see javax.swing.table.TableModel#getColumnCount()
    00113                  */
    00114                 @Override
    00115                 public int getColumnCount() {
    00116                         return 2;
    00117                 }
    00118
    00119                 /*
    00120                  * (non-Javadoc)
    00121                  *
    00122                  * @see javax.swing.table.TableModel#getRowCount()
    00123                  */
    00124                 @Override
    00125                 public int getRowCount() {
    00126                         return persons.getPersonsCount();
    00127                 }
    00128
    00129                 /*
    00130                  * (non-Javadoc)
    00131                  *
    00132                  * @see javax.swing.table.TableModel#getValueAt(int, int)
    00133                  */
    00134                 @Override
    00135                 public Object getValueAt(int rowIndex, int columnIndex) {
    00136                         switch (columnIndex) {
    00137                         case 0:
    00138                                 return persons.isPersonSelected(rowIndex);
    00139                         case 1:
    00140                                 return persons.getPerson(rowIndex);
    00141                         default:
    00142                                 return null;
    00143                         }
    00144                 }
    00145
    00146                 /*
    00147                  * (non-Javadoc)
    00148                  *
    00149                  * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object,
    00150                  *      int, int)
    00151                  */
    00152                 @Override
    00153                 public void setValueAt(Object value, int rowIndex, int columnIndex) {
    00154                         switch (columnIndex) {
    00155                         case 0:
    00156                                 persons.setPersonSelected(rowIndex, (Boolean) value);
    00157                                 break;
    00158                         default:
    00159                                 super.setValueAt(value, rowIndex, columnIndex);
    00160                         }
    00161                 }
    00162
    00163                 /*
    00164                  * (non-Javadoc)
    00165                  *
    00166                  * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
    00167                  */
    00168                 @Override
    00169                 public boolean isCellEditable(int rowIndex, int columnIndex) {
    00170                         return columnIndex == 0;
    00171                 }
    00172
    00173                 /*
    00174                  * (non-Javadoc)
    00175                  *
    00176                  * @see javax.swing.table.AbstractTableModel#getColumnName(int)
    00177                  */
    00178                 @Override
    00179                 public String getColumnName(int column) {
    00180                         return "";
    00181                 }
    00182
    00183                 /*
    00184                  * (non-Javadoc)
    00185                  *
    00186                  * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
    00187                  */
    00188                 @Override
    00189                 public Class<?> getColumnClass(int columnIndex) {
    00190                         switch (columnIndex) {
    00191                         case 0:
    00192                                 return Boolean.class;
    00193                         case 1:
    00194                                 return super.getColumnClass(columnIndex);
    00195                         default:
    00196                                 return null;
    00197                         }
    00198
    00199                 }
    00200
    00201                 public Persons getPersons() {
    00202                         return persons;
    00203                 }
    00204         }
    00205
    00206         protected class Persons {
    00207                 private String[] persons = new String[] { "dopravci", "seřizovač",
    00208                                 "údržbář", "obsluha", "třetí osoba" };
    00209
    00210                 private boolean[] personSelected = new boolean[persons.length];
    00211
    00212                 public String getPerson(int index) {
    00213                         if (index >= 0 && index < getPersonsCount()) {
    00214                                 return persons[index];
    00215                         } else {
    00216                                 return "";
    00217                         }
    00218                 }
    00219
    00220                 public int getPersonsCount() {
    00221                         return persons.length;
    00222                 }
    00223
    00224                 public void setPersonSelected(int personIndex, boolean selected) {
    00225                         if (personIndex >= 0 && personIndex < getPersonsCount()) {
    00226                                 personSelected[personIndex] = selected;
    00227                         }
    00228                 }
    00229
    00230                 public boolean isPersonSelected(int personIndex) {
    00231                         if (personIndex >= 0 && personIndex < getPersonsCount()) {
    00232                                 return personSelected[personIndex];
    00233                         } else {
    00234                                 return false;
    00235                         }
    00236                 }
    00237         }
    00238 }