Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. /*
  2. * Adrian Gonzalez Leiva, 2014
  3. *
  4. * Siete Dobles Management App (bar).
  5. */
  6.  
  7. package gui;
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.sql.*;
  12.  
  13. import javax.swing.*;
  14.  
  15. import models.DBConnector;
  16. import dataStructures.tuple.Tuple2;
  17.  
  18. public class Panel extends JPanel implements View{
  19. private static final long serialVersionUID = 1L;
  20. private static final int BUTTONS_CAPACITY=20;
  21.  
  22. private JButton [] buttons; // array of buttons (tables)
  23. private boolean [] active; // table - active or not ?
  24. private int [] ids; // table ids
  25. private JPanel up1;
  26. private JPanel down1;
  27.  
  28. public Panel(){
  29.  
  30. setLayout(new BorderLayout()); // Main panel FlowLayout()
  31.  
  32. JPanel back=new JPanel();
  33. back.setLayout(new GridLayout(3,1));
  34.  
  35. JPanel up=new JPanel();
  36. up.setBorder(BorderFactory.createTitledBorder("Mesas inactivas"));
  37.  
  38. up1=new JPanel();
  39. up1.setLayout(new GridLayout(5,4));
  40.  
  41. up1.add(new JTextArea("Aqui van las mesas inactivas"));
  42.  
  43. up.add(up1);
  44.  
  45. JPanel down=new JPanel();
  46. down.setBorder(BorderFactory.createTitledBorder("Mesas activas"));
  47.  
  48. down1=new JPanel();
  49. down1.setLayout(new GridLayout(5,4));
  50.  
  51. down1.add(new JTextArea("Aqui van las mesas donde hay gente"));
  52.  
  53. down.add(down1);
  54.  
  55. JPanel log=new JPanel();
  56. log.add(new JTextArea(10,50));
  57.  
  58. back.add(up);
  59. back.add(down);
  60. back.add(log);
  61.  
  62. add(back,BorderLayout.CENTER);
  63. add(new JLabel("Hola :-)"),BorderLayout.SOUTH); // END main panel
  64.  
  65. // Initializing table's buttons
  66. initButtons();
  67. }
  68.  
  69. /*
  70. * Initialize the tables buttons checking if they're
  71. * active or not.
  72. */
  73. private void initButtons(int n){
  74. buttons=new JButton[n];
  75. active= new boolean[n];
  76. ids= new int[n];
  77. DBConnector db = new DBConnector("jdbc:mysql://localhost:3306/7dobles","root","");
  78. Tuple2<Connection,Statement> conn = db.createConnection();
  79. try {
  80. ResultSet rset=conn._2().executeQuery("SELECT table_id, table_name FROM Rest_Tables JOIN Bills USING (table_id)");
  81. int i=0;
  82. while(rset.next()){
  83. buttons[i]=new JButton(rset.getString(2));
  84. active[i]=true;
  85. ids[i]=rset.getInt(1);
  86. i++;
  87. }
  88. rset=conn._2().executeQuery("SELECT table_id, table_name FROM Rest_Tables WHERE table_id NOT IN (SELECT table_id FROM Rest_Tables JOIN Bills USING (table_id))");
  89. while(rset.next()){
  90. buttons[i]=new JButton(rset.getString(2));
  91. active[i]=false;
  92. ids[i]=rset.getInt(1);
  93. i++;
  94. }
  95. rset.close();
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99. db.closeConnection(conn);
  100.  
  101. for(int i=0;i<buttons.length && buttons[i]!=null;i++){
  102. if(active[i]){
  103. down1.add(buttons[i]);
  104. }
  105. else{
  106. up1.add(buttons[i]);
  107. }
  108. final int id=ids[i];
  109. final int num=i+1;
  110. // new window when you press
  111. buttons[i].addActionListener(new ActionListener() {
  112. public void actionPerformed(ActionEvent e) {
  113. JFrame window=new JFrame("Cuenta de la mesa "+num);
  114. window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  115. window.setContentPane((JPanel)new BillPanel(id));
  116. window.pack();
  117. window.setVisible(true);
  118. }
  119. });
  120. }
  121. }
  122. /*
  123. * If no number is passed as argument, the default number of tables is 20.
  124. */
  125. private void initButtons(){
  126. initButtons(BUTTONS_CAPACITY);
  127. }
  128.  
  129. public void controller(ActionListener ctr) {
  130. }
  131.  
  132. public void error(String m) {
  133.  
  134. }
  135.  
  136. public void ok(String m) {
  137.  
  138. }
  139.  
  140. public void addLogEntry(String message) {
  141.  
  142. }
  143.  
  144. public void clear() {
  145.  
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement