Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- */
- package agc2.it.sm.UI;
- import java.awt.BorderLayout;
- import java.awt.Cursor;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Document;
- import org.springframework.context.ApplicationContext;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import agc2.it.sm.account.AccountJpa;
- import agc2.it.sm.dao.BachecaDiClasseJpaDAO;
- import agc2.it.sm.dao.BachecaDipendentiJpaDAO;
- import agc2.it.sm.dao.PostJpaDAO;
- import agc2.it.sm.gestioneBacheca.BachecaDiClasseJpa;
- import agc2.it.sm.gestioneBacheca.BachecaDipendentiJpa;
- import agc2.it.sm.gestioneBacheca.Post;
- import javax.swing.JTextPane;
- import javax.swing.ScrollPaneConstants;
- import javax.swing.JSeparator;
- import javax.swing.JTextArea;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import java.awt.event.ActionListener;
- import java.sql.Types;
- import java.util.ArrayList;
- import java.util.GregorianCalendar;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.awt.event.ActionEvent;
- import javax.swing.JScrollBar;
- import javax.swing.JScrollPane;
- import java.awt.ScrollPane;
- import java.awt.Panel;
- import java.awt.Color;
- import java.awt.Label;
- /**
- * The Class BachecaClasse.
- */
- public class BachecaClasse extends JFrame {
- /** The content pane. */
- private JPanel contentPane;
- /** The testo. */
- private String testo;
- /** The doc. */
- private Document doc;
- /** The text area. */
- private JTextArea textArea;
- /** The text pane. */
- private JTextPane textPane;
- private JTextPane textPane_1;
- /** The ap. */
- private ApplicationContext ap;
- /** The post dao. */
- private PostJpaDAO postDAO;
- /** The bacheca dao. */
- private BachecaDiClasseJpaDAO bachecaDAO;
- /** The Constant DOCENTE. */
- public static final String DOCENTE ="DOCENTE";
- /** The Constant STUDENTE. */
- public static final String STUDENTE ="STUDENTE";
- /** The lista post. */
- private List<Post> listaPost = new ArrayList<Post>();
- /**
- * Create the frame.
- *
- * @param account the account
- * @param ap the ap
- * @param idBacheca the id bacheca
- * @param tipo the tipo
- */
- public BachecaClasse(final AccountJpa account, final ApplicationContext ap , final int idBacheca, final String tipo) {
- this.ap = ap;
- bachecaDAO = (BachecaDiClasseJpaDAO) ap.getBean("bachecaClasseDAO");
- postDAO = (PostJpaDAO) ap.getBean("postDAO");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 1169, 521);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setTitle("Bacheca Classe");
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JSeparator separator = new JSeparator();
- separator.setBounds(193, 245, 729, -2);
- contentPane.add(separator);
- listaPost = postDAO.getAll();
- removePostNotMatching(idBacheca);
- textArea = new JTextArea();
- textPane = new JTextPane();
- JScrollPane scrollPaneIN = new JScrollPane();
- scrollPaneIN.setBounds(10, 248, 1130, 184);
- contentPane.add(scrollPaneIN);
- textArea = new JTextArea();
- scrollPaneIN.setViewportView(textArea);
- JScrollPane scrollPaneOUT = new JScrollPane();
- scrollPaneOUT.setBounds(10, 31, 1130, 186);
- contentPane.add(scrollPaneOUT);
- textPane_1 = new JTextPane();
- textPane_1.setEditable(false);
- scrollPaneOUT.setViewportView(textPane_1);
- String bacheca = fillBacheca(listaPost);
- JButton btnScriviSuBacheca = new JButton("Scrivi su bacheca");
- btnScriviSuBacheca.setForeground(Color.BLUE);
- btnScriviSuBacheca.setBounds(969, 449, 171, 23);
- btnScriviSuBacheca.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String post = "(" + account.getUsername() + ")" + textArea.getText();
- Post newPost = new Post();
- newPost.setData(new GregorianCalendar());
- BachecaDiClasseJpa bacheca = bachecaDAO.get(idBacheca);
- newPost.setBachecaClasse(bacheca);
- newPost.setContenuto(post);
- listaPost.add(newPost);
- Set<Post> collectionPost = new HashSet<Post>();
- collectionPost.addAll(listaPost);
- bacheca.setListaPost(collectionPost);
- JdbcTemplate jdbc = new JdbcTemplate(getDataSource()) ;
- String sql = "Insert into post("
- + "data,"
- + "contenuto,"
- + "id_bacheca_classe)"
- +"VALUES (?,?,?)" ;
- // define query arguments
- Object[] params = new Object[] { newPost.getData(), newPost.getContenuto(),idBacheca};
- // define SQL types of the arguments
- int[] types = new int[] { Types.DATE, Types.VARCHAR, Types.INTEGER};
- jdbc.update(sql, params, types);
- textArea.setText(null);
- textPane.setText(null);
- textPane.setText(fillBacheca(listaPost));
- }
- });
- contentPane.add(btnScriviSuBacheca);
- JButton btnIndietro = new JButton();
- btnIndietro.setForeground(Color.BLUE);
- btnIndietro.setBounds(10, 449, 59, 23);
- btnIndietro.setText("<--");
- btnIndietro.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(tipo.equals(DOCENTE)){
- AreaDocente x = new AreaDocente(account,ap);
- x.setVisible(true);
- dispose();
- }
- else{AreaStudente x = new AreaStudente(account, ap);
- x.setVisible(true);
- dispose();
- }
- }
- });
- contentPane.add(btnIndietro);
- JSeparator separator_1 = new JSeparator();
- separator_1.setBounds(193, 24, 729, 8);
- contentPane.add(separator_1);
- Label Scrittura = new Label("Scrittura");
- Scrittura.setBounds(525, 221, 135, 14);
- Scrittura.setForeground(Color.BLUE);;
- contentPane.add(Scrittura);
- Label Visualizzazione = new Label("Visualizzazione");
- Visualizzazione.setBounds(508, -2, 135, 14);
- Visualizzazione.setForeground(Color.BLUE);
- contentPane.add(Visualizzazione);
- JSeparator separator_2 = new JSeparator();
- separator_2.setBounds(174, 235, 729, 8);
- contentPane.add(separator_2);
- String immagine=new String("C:/Users/ale/Desktop/FL00002.jpg");
- }
- /**
- * Removes the post not matching.
- *
- * @param idBacheca the id bacheca
- */
- private void removePostNotMatching(final int idBacheca) {
- Set<Post> list = new HashSet<Post>();
- for (Post post : listaPost) {
- if(post.getBachecaClasse() == null)
- list.add(post);
- }
- listaPost.removeAll(list);
- list = new HashSet<Post>();
- for (Post post : listaPost) {
- if(post.getBachecaClasse().getId_bacheca_classe() != idBacheca)
- list.add(post);
- }
- listaPost.removeAll(list);
- }
- /**
- * Fill bacheca.
- *
- * @param listaPost the lista post
- * @return the string
- */
- private String fillBacheca(List<Post> listaPost) {
- String bacheca = "";
- if (listaPost != null) {
- for (Post post : listaPost) {
- bacheca += post.toStringViewBacheca(post);
- }
- }
- return bacheca;
- }
- /**
- * Gets the data source.
- *
- * @return the data source
- */
- public static DriverManagerDataSource getDataSource() {
- String driverClassName = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/S_Manager?autoReconnect=true";
- String dbUsername = "root";
- String dbPassword = "root";
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName(driverClassName);
- dataSource.setUrl(url);
- dataSource.setUsername(dbUsername);
- dataSource.setPassword(dbPassword);
- return dataSource;
- }
- }
Add Comment
Please, Sign In to add comment