package us.deans.zinctest; import us.deans.zinc.*; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import org.apache.log4j.Logger; import us.deans.panther.ppView; import us.deans.panther.ppCustomException; import us.deans.panther.ppExceptionChannel; /** Swing-based user interface */ public class ZnSwingView extends ppView { private ZnModel m_model; private JFrame frame; private JTable table; private Logger logger; public ZnSwingView(ZnModel model){ m_model = model; logger = Logger.getLogger("ZincLogger"); ZnMessageHandler msgHandler = new ZnMessageHandler(this); // create a message handler m_model.getMessenger().addObserver(msgHandler); // register the model's messenger String iconFile = System.getProperty("user.dir")+"\\zinc.jpg"; // logger.debug("Icon located at: " + iconFile + "\n"); //main frame frame = new JFrame("Hunting for Jobs"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exits application when window is closed frame.setIconImage(new ImageIcon(iconFile).getImage()); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // set frame dimensions via setBounds frame.setBounds(50, 50, screenSize.width-50*2,screenSize.height-50*2); // frame.setSize(1600, 900); // set frame dimensions via setSize // menu system JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("Program"); JMenu viewMenu = new JMenu("View"); JMenuItem miDisplayRSS = new JMenuItem("Display RSS"); JMenuItem miImportRSS = new JMenuItem("Import RSS"); JMenuItem miProcessRSS = new JMenuItem("Process RSS"); JMenuItem miImportDB = new JMenuItem("Import DB"); JMenuItem miExportDB = new JMenuItem("Export DB"); JMenuItem miShowSummary = new JMenuItem("Generate Summary"); JMenuItem miSortSummary = new JMenuItem("Sort Summary By Demand"); JMenuItem miExtractTerms = new JMenuItem("Extract Terms"); JMenuItem miLoadTable = new JMenuItem("Load Table (Refresh)"); JMenuItem miClearTable = new JMenuItem("Clear Table"); JMenuItem miExit = new JMenuItem("Exit"); JMenuItem miTest = new JMenuItem("Test"); fileMenu.add(miImportRSS); fileMenu.add(miProcessRSS); fileMenu.add(miDisplayRSS); fileMenu.add(miImportDB); fileMenu.add(miExportDB); fileMenu.add(miExtractTerms); fileMenu.add(miTest); fileMenu.add(miExit); menuBar.add(fileMenu); viewMenu.add(miShowSummary); viewMenu.add(miSortSummary); viewMenu.add(miLoadTable); viewMenu.add(miClearTable); menuBar.add(viewMenu); frame.setJMenuBar(menuBar); // Import RSS miDisplayRSS.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ displayRSSData(3); } }); miImportRSS.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ displayRSSData(1); } }); miProcessRSS.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ displayRSSData(2); } }); // Import DB miImportDB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ displayDBData(); } }); // Export DB miExportDB.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ exportDBData(); } }); // Show Summary miShowSummary.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ showSummary(1); } }); // Sort Summary miSortSummary.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ showSortedSummary(1); } }); // Extract Terms miExtractTerms.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ m_model.extractTerms(); } }); // Clear Swing Table miClearTable.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ clearTable(true); } }); // Load Swing Table miLoadTable.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ displayModel(); } }); // Test miTest.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a){ JOptionPane.showMessageDialog(frame, "Nothing Set up to Test", "Zinc Message", JOptionPane.INFORMATION_MESSAGE); } }); // Exit miExit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent a) { System.exit(0); } }); table = new JTable(); table.setModel(new ZnTableModel_001(new ZnJobList())); JScrollPane jsPane = new JScrollPane(table); frame.getContentPane().add(jsPane); frame.setVisible(true); logger.debug("view is loaded..."); } public void displaySummary(){ clearTable(false); ZnDemandList demandList = m_model.getDemandList(); logger.debug("demand records in current model = " + demandList.size()+ "\n"); table.setModel(new ZnTableModel_002(demandList)); JScrollPane jsPane = new JScrollPane(table); frame.getContentPane().add(jsPane); frame.setVisible(true); } public void displayModel(){ clearTable(false); ZnJobList jobList = m_model.getRssData(); logger.debug("records in current model = " + jobList.size()+ "\n"); table.setModel(new ZnTableModel_001(jobList)); JScrollPane jsPane = new JScrollPane(table); frame.getContentPane().add(jsPane); frame.setVisible(true); } public void clearTable(boolean opt){ frame.setVisible(false); frame.getContentPane().removeAll(); if (opt) { frame.setVisible(true); } } public void displayRSSData(int opt){ try { switch (opt){ case 1: m_model.importContentFromRSS(); break; case 2: m_model.processJobList(); break; case 3: m_model.importContentFromRSS(); m_model.processJobList(); break; } } catch(ZnNoDataException e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Error", JOptionPane.ERROR_MESSAGE); logger.debug(e.getMessage()); } catch(ppExceptionChannel e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Error", JOptionPane.ERROR_MESSAGE); logger.error(e.getMessage()); } displayModel(); } public void displayDBData(){ try { m_model.importContentFromDB(); displayModel(); } catch(ppExceptionChannel e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Error", JOptionPane.ERROR_MESSAGE); } } public void exportDBData(){ try { m_model.exportContentToDB(); } catch(ppExceptionChannel e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Error", JOptionPane.ERROR_MESSAGE); } } public void showSummary(int opt){ try{ m_model.generateSummaryList(1); displaySummary(); } catch(ppCustomException e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Message", JOptionPane.INFORMATION_MESSAGE); } catch(ppExceptionChannel e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Message", JOptionPane.ERROR_MESSAGE); } } public void showSortedSummary(int opt){ try{ m_model.sortSummaryList(); displaySummary(); } catch(ppCustomException e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Message", JOptionPane.INFORMATION_MESSAGE); } catch(ppExceptionChannel e){ JOptionPane.showMessageDialog(frame, e.getMessage(), "Zinc Message", JOptionPane.ERROR_MESSAGE); } } public void displayMessage(ZnMessage msgObject){ final int DEBUG = 1; final int INFO = 2; final int WARN = 3; switch (msgObject.level){ case DEBUG: logger.debug(msgObject.message); break; case INFO: logger.info(msgObject.message); break; case WARN: JOptionPane.showMessageDialog(frame, msgObject.message, "Zinc Message", JOptionPane.WARNING_MESSAGE); break; } } }