PageRenderTime 23ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/fin_val/src/production/artem/finance/gui/OrgCatalog.java

http://finval.googlecode.com/
Java | 529 lines | 339 code | 59 blank | 131 comment | 29 complexity | 6017d7eb7c69b5b72c201c113a990edc MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, IPL-1.0, Apache-2.0, LGPL-2.1, LGPL-2.0, BSD-3-Clause
  1. package artem.finance.gui;
  2. import javax.swing.JPanel;
  3. import java.awt.Dimension;
  4. import java.awt.Rectangle;
  5. import javax.swing.JButton;
  6. import javax.swing.JInternalFrame;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JTextField;
  9. import javax.swing.JScrollPane;
  10. import javax.swing.JTable;
  11. import javax.swing.ListSelectionModel;
  12. import javax.swing.event.ListSelectionEvent;
  13. import javax.swing.event.ListSelectionListener;
  14. import javax.swing.table.DefaultTableColumnModel;
  15. import javax.swing.table.TableColumn;
  16. import java.rmi.RemoteException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Properties;
  20. import javax.swing.JLabel;
  21. import artem.finance.gui.db.BeansFactory;
  22. import artem.finance.server.persist.Organization;
  23. import artem.finance.server.persist.beans.OrganizationBean;
  24. /**
  25. *
  26. * @author Burtsev Ivan
  27. *
  28. */
  29. public class OrgCatalog extends JInternalFrame {
  30. private static final long serialVersionUID = 1L;
  31. private JPanel jContentPane = null;
  32. private JButton addButton = null;
  33. private JTextField orgNameTextField = null;
  34. private JTextField orgDescriptionTextField = null;
  35. private JButton delButton = null;
  36. private JScrollPane jScrollPane = null;
  37. private JTable jTable = null;
  38. private JButton cancelButton = null;
  39. private JLabel orgLabel = null;
  40. private JLabel orgDescriptionLabel = null;
  41. private JButton editButton = null;
  42. private BeansFactory factory;
  43. private Organization selectedOrganization;
  44. private Properties properties;
  45. /**
  46. * This is the default constructor
  47. */
  48. public OrgCatalog(Properties properties) {
  49. super("",
  50. false, //resizable
  51. true, //closable
  52. false, //maximizable
  53. true);//iconifiable
  54. this.properties = properties;
  55. this.factory = BeansFactory.getInstance();
  56. initialize();
  57. }
  58. /**
  59. * This method initializes this
  60. *
  61. * @return void
  62. */
  63. private void initialize() {
  64. this.setSize(490, 530);
  65. this.setContentPane(getJContentPane());
  66. this.setTitle(properties.getProperty("organizations"));
  67. }
  68. /**
  69. * This method initializes jContentPane
  70. *
  71. * @return javax.swing.JPanel
  72. */
  73. private JPanel getJContentPane()
  74. {
  75. if (jContentPane == null)
  76. {
  77. orgDescriptionLabel = new JLabel();
  78. orgDescriptionLabel.setBounds(new Rectangle(195, 499, 225, 16));
  79. orgDescriptionLabel.setText(properties.getProperty("orgFullName"));
  80. orgLabel = new JLabel();
  81. orgLabel.setBounds(new Rectangle(15, 499, 131, 16));
  82. orgLabel.setText(properties.getProperty("organization"));
  83. jContentPane = new JPanel();
  84. jContentPane.setLayout(null);
  85. jContentPane.setSize(new Dimension(690, 230));
  86. jContentPane.add(getAddButton(), null);
  87. jContentPane.add(getOrgNameTextField(), null);
  88. jContentPane.add(getOrgDescriptionTextField(), null);
  89. jContentPane.add(getDelButton(), null);
  90. jContentPane.add(getJScrollPane(), null);
  91. jContentPane.setOpaque(true);
  92. jContentPane.add(getCancelButton(), null);
  93. jContentPane.add(orgLabel, null);
  94. jContentPane.add(orgDescriptionLabel, null);
  95. jContentPane.add(getEditButton(), null);
  96. }
  97. return jContentPane;
  98. }
  99. /**
  100. * This method initializes addButton
  101. *
  102. * @return javax.swing.JButton
  103. */
  104. private JButton getAddButton()
  105. {
  106. if (addButton == null)
  107. {
  108. addButton = new JButton();
  109. addButton.setBounds(new Rectangle(15, 466, 140, 26));
  110. addButton.setText(properties.getProperty("add"));
  111. addButton.setEnabled(false);
  112. addButton.setVisible(false);
  113. addButton.addActionListener(new java.awt.event.ActionListener()
  114. {
  115. public void actionPerformed(java.awt.event.ActionEvent e)
  116. {
  117. //add new field or update current
  118. if(orgNameTextField.getText().equals("") || orgNameTextField.getText() == null){
  119. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("orgNameMissing"));
  120. return;
  121. }
  122. if(orgDescriptionTextField.getText().equals("") || orgDescriptionTextField.getText() == null){
  123. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("orgFullNameMissing"));
  124. return;
  125. }
  126. if(selectedOrganization == null )
  127. {
  128. try{
  129. selectedOrganization = new Organization();
  130. selectedOrganization.setName(orgNameTextField.getText());
  131. selectedOrganization.setDescription(orgDescriptionTextField.getText());
  132. factory.getOrganizationServiceSLSB().saveOrUpdate( new OrganizationBean(selectedOrganization));
  133. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+selectedOrganization.getDescription()+properties.getProperty("savedSuccessfully1"));
  134. cleanTextFields();
  135. //update jTable
  136. getDatabaseTableModel().setData(getDataFromDb());
  137. getDatabaseTableModel().fireTableRowsInserted(getJTable().getRowCount()-1, getJTable().getRowCount()-1);
  138. //Select the inserted row
  139. getJTable().getSelectionModel().setLeadSelectionIndex(getJTable().getRowCount());
  140. getJTable().repaint();
  141. selectedOrganization = null;
  142. }catch(Exception ex){
  143. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+properties.getProperty("notSaved1")+ex.getMessage());
  144. selectedOrganization = null;
  145. }
  146. }else{
  147. try
  148. {
  149. selectedOrganization.setName(orgNameTextField.getText());
  150. selectedOrganization.setDescription(orgDescriptionTextField.getText());
  151. factory.getOrganizationServiceSLSB().saveOrUpdate( new OrganizationBean(selectedOrganization));
  152. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+selectedOrganization.getDescription()+properties.getProperty("updatedSuccessfully1"));
  153. cleanTextFields();
  154. getDatabaseTableModel().setData(getDataFromDb());
  155. int selected = getJTable().getSelectionModel().getLeadSelectionIndex();
  156. getDatabaseTableModel().fireTableRowsUpdated(selected, selected);//.fireTableRowsInserted(getJTable().getRowCount()-1, getJTable().getRowCount());
  157. getJTable().repaint();
  158. selectedOrganization = null;
  159. }catch(Exception ex){
  160. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+properties.getProperty("notUpdated1")+ex.getMessage());
  161. selectedOrganization = null;
  162. }
  163. }
  164. }
  165. });
  166. }
  167. return addButton;
  168. }
  169. private void cleanTextFields()
  170. {
  171. orgNameTextField.setText("");
  172. orgDescriptionTextField.setText("");
  173. }
  174. /**
  175. * This method initializes orgNameTextField
  176. *
  177. * @return javax.swing.JTextField
  178. */
  179. private JTextField getOrgNameTextField()
  180. {
  181. if (orgNameTextField == null)
  182. {
  183. orgNameTextField = new JTextField();
  184. orgNameTextField.setBounds(new Rectangle(15, 524, 165, 20));
  185. }
  186. return orgNameTextField;
  187. }
  188. /**
  189. * This method initializes orgDescriptionTextField
  190. *
  191. * @return javax.swing.JTextField
  192. */
  193. private JTextField getOrgDescriptionTextField()
  194. {
  195. if (orgDescriptionTextField == null)
  196. {
  197. orgDescriptionTextField = new JTextField();
  198. orgDescriptionTextField.setBounds(new Rectangle(196, 525, 275, 20));
  199. }
  200. return orgDescriptionTextField;
  201. }
  202. /**
  203. * This method initializes updButton
  204. *
  205. * @return javax.swing.JButton
  206. */
  207. private JButton getDelButton()
  208. {
  209. if (delButton == null)
  210. {
  211. delButton = new JButton();
  212. delButton.setBounds(new Rectangle(165, 466, 140, 26));
  213. delButton.setText(properties.getProperty("delete"));
  214. delButton.setEnabled(false);
  215. delButton.setVisible(false);
  216. delButton.addActionListener(new java.awt.event.ActionListener()
  217. {
  218. public void actionPerformed(java.awt.event.ActionEvent e) {
  219. getOrgNameTextField().setText( "" );
  220. getOrgDescriptionTextField().setText( "" );
  221. if(selectedOrganization == null )
  222. {
  223. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("orgNotSelectedToDelete"));
  224. }
  225. else
  226. {
  227. try
  228. {
  229. factory.getOrganizationServiceSLSB().delete(new OrganizationBean(selectedOrganization));
  230. JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+selectedOrganization.getDescription()+properties.getProperty("deleted1"));
  231. selectedOrganization = null;
  232. //update jTable
  233. getDatabaseTableModel().setData(getDataFromDb());
  234. int selectedRow = getJTable().getSelectionModel().getLeadSelectionIndex();
  235. getDatabaseTableModel().fireTableRowsDeleted(selectedRow, selectedRow);
  236. }
  237. catch(Exception ex)
  238. {
  239. JOptionPane.showMessageDialog(getJTable(), selectedOrganization.getDescription()+" "+properties.getProperty("notDeleted"));
  240. }
  241. }
  242. }
  243. });
  244. }
  245. return delButton;
  246. }
  247. /**
  248. * This method initializes jScrollPane
  249. *
  250. * @return javax.swing.JScrollPane
  251. */
  252. private JScrollPane getJScrollPane()
  253. {
  254. if (jScrollPane == null)
  255. {
  256. jScrollPane = new JScrollPane();
  257. jScrollPane.setBounds(new Rectangle(13, 15, 455, 446));
  258. jScrollPane.setViewportView(getJTable());
  259. }
  260. return jScrollPane;
  261. }
  262. private List<Object> getDataFromDb()
  263. {
  264. List<Object> data = new ArrayList<Object>();
  265. List<OrganizationBean> organizations;
  266. try
  267. {
  268. organizations = factory.getOrganizationServiceSLSB().findAll();
  269. for(int i = 0; i< organizations.size(); i++){
  270. OrganizationBean organization = organizations.get(i);
  271. List<Object> raw = new ArrayList<Object>();
  272. raw.add(organization.getOrganization().getId());
  273. raw.add(organization.getOrganization().getName());
  274. raw.add(organization.getOrganization().getDescription());
  275. //raw.add(new JRadioButton());
  276. data.add(raw);
  277. }
  278. } catch (RemoteException e)
  279. {
  280. e.printStackTrace();
  281. }
  282. return data;
  283. }
  284. /**
  285. * Used for lazy data loading to the table. Fills the data only after first frame shoving
  286. */
  287. public void fillOrgsTable()
  288. {
  289. getDatabaseTableModel().setData(getDataFromDb());
  290. getDatabaseTableModel().fireTableDataChanged();
  291. }
  292. /**
  293. * This method initializes jTable
  294. *
  295. * @return javax.swing.JTable
  296. */
  297. private JTable getJTable()
  298. {
  299. if (jTable == null)
  300. {
  301. DatabaseTableModel tableModel = new DatabaseTableModel(true);
  302. final List<String> names = new ArrayList<String>();
  303. names.add("");
  304. names.add(properties.getProperty("organization"));
  305. names.add(properties.getProperty("desciption"));
  306. tableModel.setColumnNames(names);
  307. final List<Class> types = new ArrayList<Class>(0);
  308. types.add(Long.class);
  309. types.add(String.class);
  310. types.add(String.class);
  311. //types.add(JRadioButton.class);
  312. tableModel.setColumnTypes(types);
  313. //Gets all the organizations during loading!
  314. //tableModel.setData(getDataFromDb());
  315. jTable = new JTable(tableModel);
  316. DefaultTableColumnModel columns = (DefaultTableColumnModel) jTable.getColumnModel();
  317. TableColumn tc = columns.getColumn(0);
  318. tc.setPreferredWidth(0);
  319. tc = columns.getColumn(1);
  320. tc.setPreferredWidth(315);
  321. tc = columns.getColumn(2);
  322. tc.setPreferredWidth(815);
  323. jTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
  324. jTable.setBounds(new Rectangle(15, 15, 455, 420));
  325. jTable.setFillsViewportHeight(true);
  326. //User can select only one row.
  327. jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  328. jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
  329. @Override
  330. public void valueChanged(ListSelectionEvent e) {
  331. int selectedRow = getJTable().getSelectionModel().getLeadSelectionIndex();
  332. Long id = (Long)getJTable().getModel().getValueAt(selectedRow, 0);
  333. String name = (String)getJTable().getModel().getValueAt(selectedRow, 1);
  334. String description = (String)getJTable().getModel().getValueAt(selectedRow, 2);
  335. selectedOrganization = new Organization();
  336. selectedOrganization.setId(id);
  337. selectedOrganization.setName(name);
  338. selectedOrganization.setDescription(description);
  339. getOrgNameTextField().setText( name );
  340. getOrgDescriptionTextField().setText( description );
  341. }
  342. });
  343. }
  344. return jTable;
  345. }
  346. private DatabaseTableModel getDatabaseTableModel(){
  347. DatabaseTableModel model = (DatabaseTableModel)jTable.getModel();
  348. return model;
  349. }
  350. /**
  351. * This method initializes cancelButton
  352. *
  353. * @return javax.swing.JButton
  354. */
  355. private JButton getCancelButton() {
  356. if (cancelButton == null) {
  357. cancelButton = new JButton();
  358. cancelButton.setBounds(new Rectangle(315, 466, 145, 26));
  359. cancelButton.setText(properties.getProperty("cancel"));
  360. cancelButton.setEnabled(false);
  361. cancelButton.setVisible(false);
  362. cancelButton.addActionListener(new java.awt.event.ActionListener() {
  363. public void actionPerformed(java.awt.event.ActionEvent e) {
  364. editButton.setEnabled(true);
  365. editButton.setVisible(true);
  366. addButton.setVisible(false);
  367. addButton.setEnabled(false);
  368. addButton.setName(properties.getProperty("add"));
  369. delButton.setVisible(false);
  370. delButton.setEnabled(false);
  371. cancelButton.setVisible(false);
  372. cancelButton.setEnabled(false);
  373. setSize(490, 535);
  374. orgNameTextField.setText(null);
  375. orgDescriptionTextField.setText(null);
  376. //Clear highlighting the selected row in the table.
  377. int leadSelectionInd = getJTable().getSelectionModel().getLeadSelectionIndex();
  378. getJTable().getSelectionModel().removeSelectionInterval(leadSelectionInd, leadSelectionInd);
  379. }
  380. });
  381. }
  382. return cancelButton;
  383. }
  384. /**
  385. * This method initializes editButton1
  386. *
  387. * @return javax.swing.JButton
  388. */
  389. private JButton getEditButton() {
  390. if (editButton == null) {
  391. editButton = new JButton();
  392. editButton.setBounds(new Rectangle(15, 466, 140, 26));
  393. editButton.setText(properties.getProperty("edit"));
  394. editButton.addActionListener(new java.awt.event.ActionListener() {
  395. public void actionPerformed(java.awt.event.ActionEvent e) {
  396. editButton.setEnabled(false);
  397. editButton.setVisible(false);
  398. addButton.setVisible(true);
  399. addButton.setEnabled(true);
  400. delButton.setVisible(true);
  401. delButton.setEnabled(true);
  402. cancelButton.setVisible(true);
  403. cancelButton.setEnabled(true);
  404. setSize(490, 585);
  405. }
  406. });
  407. }
  408. return editButton;
  409. }
  410. // /**
  411. // * This class used to render RadioButtons in the cells of jTable
  412. // *
  413. // * @author Andrey Sirak
  414. // */
  415. //class RadioButtonRenderer implements TableCellRenderer {
  416. // public Component getTableCellRendererComponent(JTable table, Object value,
  417. // boolean isSelected, boolean hasFocus, int row, int column) {
  418. // if (value == null)
  419. // return null;
  420. // if (hasFocus) {
  421. // //?????? ?????? ? ???? ??? ?????????????? ? ???????? ?????? ???????
  422. // orgNameTextField.setText(table.getValueAt(row, 0).toString());
  423. // orgDescriptionTextField.setText(table.getValueAt(row, 1).toString());
  424. // delButton.setEnabled(true);
  425. // addButton.setName("????????");
  426. // };
  427. // return (Component) value;
  428. // }
  429. // }
  430. ///**
  431. // * This class used to edit RadioButtons in the cells of jTable
  432. // *
  433. // * @author Andrey Sirak
  434. // */
  435. // class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
  436. // /**
  437. // *
  438. // */
  439. // private static final long serialVersionUID = 1L;
  440. // private JRadioButton button;
  441. //
  442. //
  443. // public RadioButtonEditor(JCheckBox checkBox) {
  444. // super(checkBox);
  445. // }
  446. //
  447. // public Component getTableCellEditorComponent(JTable table, Object value,
  448. // boolean isSelected, int row, int column) {
  449. // if (value == null)
  450. // return null;
  451. // button = (JRadioButton) value;
  452. // button.addItemListener(this);
  453. //
  454. // return (Component) value;
  455. // }
  456. //
  457. // public Object getCellEditorValue() {
  458. // button.removeItemListener(this);
  459. //
  460. // return button;
  461. // }
  462. //
  463. // public void itemStateChanged(ItemEvent e) {
  464. // if (button.isSelected()) button.setSelected(false);
  465. // else button.setSelected(true);
  466. // super.fireEditingStopped();
  467. //
  468. // }
  469. // }
  470. } // @jve:decl-index=0:visual-constraint="8,-87"