Guest User

BachecaClasse

a guest
Feb 7th, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. /*
  2.  *
  3.  */
  4. package agc2.it.sm.UI;
  5.  
  6. import java.awt.BorderLayout;
  7. import java.awt.Cursor;
  8. import java.awt.EventQueue;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.border.EmptyBorder;
  12. import javax.swing.text.BadLocationException;
  13. import javax.swing.text.Document;
  14.  
  15. import org.springframework.context.ApplicationContext;
  16. import org.springframework.jdbc.core.JdbcTemplate;
  17. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  18.  
  19. import agc2.it.sm.account.AccountJpa;
  20. import agc2.it.sm.dao.BachecaDiClasseJpaDAO;
  21. import agc2.it.sm.dao.BachecaDipendentiJpaDAO;
  22. import agc2.it.sm.dao.PostJpaDAO;
  23. import agc2.it.sm.gestioneBacheca.BachecaDiClasseJpa;
  24. import agc2.it.sm.gestioneBacheca.BachecaDipendentiJpa;
  25. import agc2.it.sm.gestioneBacheca.Post;
  26.  
  27. import javax.swing.JTextPane;
  28. import javax.swing.ScrollPaneConstants;
  29. import javax.swing.JSeparator;
  30. import javax.swing.JTextArea;
  31. import javax.swing.Icon;
  32. import javax.swing.ImageIcon;
  33. import javax.swing.JButton;
  34. import javax.swing.JLabel;
  35. import javax.swing.JMenu;
  36. import javax.swing.JMenuBar;
  37. import javax.swing.JMenuItem;
  38.  
  39. import java.awt.event.ActionListener;
  40. import java.sql.Types;
  41. import java.util.ArrayList;
  42. import java.util.GregorianCalendar;
  43. import java.util.HashSet;
  44. import java.util.List;
  45. import java.util.Set;
  46. import java.awt.event.ActionEvent;
  47. import javax.swing.JScrollBar;
  48. import javax.swing.JScrollPane;
  49. import java.awt.ScrollPane;
  50. import java.awt.Panel;
  51. import java.awt.Color;
  52. import java.awt.Label;
  53.  
  54.  
  55. /**
  56.  * The Class BachecaClasse.
  57.  */
  58. public class BachecaClasse extends JFrame {
  59.  
  60.     /** The content pane. */
  61.     private JPanel contentPane;
  62.    
  63.     /** The testo. */
  64.     private String testo;
  65.    
  66.     /** The doc. */
  67.     private Document doc;
  68.    
  69.     /** The text area. */
  70.     private JTextArea textArea;
  71.    
  72.     /** The text pane. */
  73.     private JTextPane textPane;
  74.     private JTextPane textPane_1;
  75.    
  76.     /** The ap. */
  77.     private ApplicationContext ap;
  78.    
  79.     /** The post dao. */
  80.     private PostJpaDAO postDAO;
  81.    
  82.     /** The bacheca dao. */
  83.     private BachecaDiClasseJpaDAO bachecaDAO;
  84.    
  85.     /** The Constant DOCENTE. */
  86.     public static final String DOCENTE ="DOCENTE";
  87.    
  88.     /** The Constant STUDENTE. */
  89.     public static final String STUDENTE ="STUDENTE";
  90.  
  91.  
  92.     /** The lista post. */
  93.     private List<Post> listaPost = new ArrayList<Post>();
  94.    
  95.     /**
  96.      * Create the frame.
  97.      *
  98.      * @param account the account
  99.      * @param ap the ap
  100.      * @param idBacheca the id bacheca
  101.      * @param tipo the tipo
  102.      */
  103.     public BachecaClasse(final AccountJpa account, final ApplicationContext ap , final int idBacheca, final String tipo) {
  104.  
  105.         this.ap = ap;
  106.         bachecaDAO = (BachecaDiClasseJpaDAO) ap.getBean("bachecaClasseDAO");
  107.         postDAO = (PostJpaDAO) ap.getBean("postDAO");
  108.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109.         setBounds(100, 100, 1169, 521);
  110.         contentPane = new JPanel();
  111.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  112.         setTitle("Bacheca Classe");
  113.         setContentPane(contentPane);
  114.         contentPane.setLayout(null);
  115.  
  116.         JSeparator separator = new JSeparator();
  117.         separator.setBounds(193, 245, 729, -2);
  118.         contentPane.add(separator);
  119.  
  120.         listaPost = postDAO.getAll();
  121.         removePostNotMatching(idBacheca);
  122.  
  123.  
  124.          textArea = new JTextArea();
  125.          textPane = new JTextPane();
  126.         JScrollPane scrollPaneIN = new JScrollPane();
  127.         scrollPaneIN.setBounds(10, 248, 1130, 184);
  128.         contentPane.add(scrollPaneIN);
  129.         textArea = new JTextArea();
  130.         scrollPaneIN.setViewportView(textArea);
  131.  
  132.         JScrollPane scrollPaneOUT = new JScrollPane();
  133.         scrollPaneOUT.setBounds(10, 31, 1130, 186);
  134.         contentPane.add(scrollPaneOUT);
  135.         textPane_1 = new JTextPane();
  136.         textPane_1.setEditable(false);
  137.         scrollPaneOUT.setViewportView(textPane_1);
  138.         String bacheca = fillBacheca(listaPost);
  139.    
  140.         JButton btnScriviSuBacheca = new JButton("Scrivi su bacheca");
  141.         btnScriviSuBacheca.setForeground(Color.BLUE);
  142.         btnScriviSuBacheca.setBounds(969, 449, 171, 23);
  143.         btnScriviSuBacheca.addActionListener(new ActionListener() {
  144.             public void actionPerformed(ActionEvent e) {
  145.                 String post = "(" + account.getUsername() + ")" + textArea.getText();
  146.                 Post newPost = new Post();
  147.                 newPost.setData(new GregorianCalendar());
  148.                 BachecaDiClasseJpa bacheca = bachecaDAO.get(idBacheca);
  149.                 newPost.setBachecaClasse(bacheca);
  150.                 newPost.setContenuto(post);
  151.                 listaPost.add(newPost);
  152.                 Set<Post> collectionPost = new HashSet<Post>();
  153.                 collectionPost.addAll(listaPost);
  154.                 bacheca.setListaPost(collectionPost);
  155.                 JdbcTemplate jdbc = new JdbcTemplate(getDataSource()) ;
  156.                 String sql = "Insert into post("
  157.                  + "data,"
  158.                  + "contenuto,"
  159.                  + "id_bacheca_classe)"
  160.                  +"VALUES (?,?,?)" ;
  161.                
  162.                
  163.                  // define query arguments
  164.                
  165.                 Object[] params = new Object[] { newPost.getData(), newPost.getContenuto(),idBacheca};
  166.                
  167.                    // define SQL types of the arguments
  168.                 int[] types = new int[] { Types.DATE, Types.VARCHAR, Types.INTEGER};
  169.                        
  170.                 jdbc.update(sql, params, types);
  171.  
  172.                 textArea.setText(null);
  173.                 textPane.setText(null);
  174.                 textPane.setText(fillBacheca(listaPost));
  175.             }
  176.         });
  177.         contentPane.add(btnScriviSuBacheca);
  178.  
  179.         JButton btnIndietro = new JButton();
  180.         btnIndietro.setForeground(Color.BLUE);
  181.         btnIndietro.setBounds(10, 449, 59, 23);
  182.         btnIndietro.setText("<--");
  183.         btnIndietro.addActionListener(new ActionListener() {
  184.             public void actionPerformed(ActionEvent e) {
  185.                
  186.                 if(tipo.equals(DOCENTE)){
  187.                     AreaDocente x = new AreaDocente(account,ap);
  188.                     x.setVisible(true);
  189.                     dispose();
  190.                 }
  191.                 else{AreaStudente x = new AreaStudente(account, ap);
  192.                 x.setVisible(true);
  193.                 dispose();
  194.                 }
  195.             }
  196.         });
  197.         contentPane.add(btnIndietro);
  198.        
  199.        
  200.         JSeparator separator_1 = new JSeparator();
  201.         separator_1.setBounds(193, 24, 729, 8);
  202.         contentPane.add(separator_1);
  203.        
  204.         Label Scrittura = new Label("Scrittura");
  205.         Scrittura.setBounds(525, 221, 135, 14);
  206.         Scrittura.setForeground(Color.BLUE);;
  207.         contentPane.add(Scrittura);
  208.        
  209.         Label Visualizzazione = new Label("Visualizzazione");
  210.         Visualizzazione.setBounds(508, -2, 135, 14);
  211.         Visualizzazione.setForeground(Color.BLUE);
  212.         contentPane.add(Visualizzazione);
  213.        
  214.         JSeparator separator_2 = new JSeparator();
  215.         separator_2.setBounds(174, 235, 729, 8);
  216.         contentPane.add(separator_2);
  217.        
  218.         String immagine=new String("C:/Users/ale/Desktop/FL00002.jpg");
  219.     }
  220.  
  221.  
  222.     /**
  223.      * Removes the post not matching.
  224.      *
  225.      * @param idBacheca the id bacheca
  226.      */
  227.     private void removePostNotMatching(final int idBacheca) {
  228.         Set<Post> list = new HashSet<Post>();
  229.        
  230.         for (Post post : listaPost) {
  231.             if(post.getBachecaClasse() == null)
  232.                 list.add(post);
  233.         }
  234.         listaPost.removeAll(list);
  235.         list = new HashSet<Post>();
  236.        
  237.         for (Post post : listaPost) {
  238.             if(post.getBachecaClasse().getId_bacheca_classe() != idBacheca)
  239.                 list.add(post);
  240.         }
  241.         listaPost.removeAll(list);
  242.     }
  243.    
  244.    
  245.     /**
  246.      * Fill bacheca.
  247.      *
  248.      * @param listaPost the lista post
  249.      * @return the string
  250.      */
  251.     private String fillBacheca(List<Post> listaPost) {
  252.  
  253.         String bacheca = "";
  254.         if (listaPost != null) {
  255.             for (Post post : listaPost) {
  256.                
  257.                     bacheca += post.toStringViewBacheca(post);
  258.                
  259.             }
  260.         }
  261.         return bacheca;
  262.  
  263.     }
  264.    
  265.    
  266.    
  267.     /**
  268.      * Gets the data source.
  269.      *
  270.      * @return the data source
  271.      */
  272.     public static DriverManagerDataSource getDataSource() {
  273.          String driverClassName = "com.mysql.jdbc.Driver";
  274.          String url = "jdbc:mysql://localhost:3306/S_Manager?autoReconnect=true";
  275.          String dbUsername = "root";
  276.          String dbPassword = "root";
  277.          DriverManagerDataSource dataSource = new DriverManagerDataSource();
  278.          dataSource.setDriverClassName(driverClassName);
  279.          dataSource.setUrl(url);
  280.          dataSource.setUsername(dbUsername);
  281.          dataSource.setPassword(dbPassword);
  282.          return dataSource;
  283.     }
  284. }
Add Comment
Please, Sign In to add comment