// ds MySQL logger private final void _logMaster(final String p_strInfo) { // ds get username and session id final String strUsername = m_cLearner.getUsername(); final int iSessionID = m_cLearner.getSessionID(); // ds if set if (null != strUsername) { try { // ds log m_cMySQLManager.logMaster(strUsername, iSessionID, p_strInfo); } catch (SQLException e) { System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(_logMaster) SQLException: " + e.getMessage() + " could not log to MySQL master"); } } else { System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(_logMaster) could not log to master because of empty username"); } }
// ds KeyListener - just map the keys to the buttons public void keyPressed(KeyEvent p_cKey) { // ds get the keycode final int iKeyCode = p_cKey.getKeyCode(); // ds if/else tree if (iKeyCode == KeyEvent.VK_RIGHT) { m_cButtonLike.doClick(); } else if (iKeyCode == KeyEvent.VK_DOWN) { m_cButtonDislike.doClick(); } else if (iKeyCode == KeyEvent.VK_LEFT) { m_cButtonPrevious.doClick(); } else if (iKeyCode == KeyEvent.VK_R) { /*m_cButtonReset.doClick( );*/ } else if (iKeyCode == KeyEvent.VK_ESCAPE) { // ds notify System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(keyPressed) Caught escape signal - shutting down GUI"); // ds escape gui m_cFrame.removeAll(); m_cFrame.dispose(); } }
// ds close GUI public void close() { // ds get username final String strUsername = m_cLearner.getUsername(); // ds if username is set if (null != strUsername) { // ds log _logMaster("<CGUI>(close) closed GUI application"); try { // ds remove user from active list m_cMySQLManager.removeActiveUser(strUsername); // ds log System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(close) Logout of: [" + strUsername + "] successful"); } catch (Exception e) { // ds could not remove System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(close) Could not remove user: [" + strUsername + "] from active users list - please check database"); } } // ds only if active if (isActive()) { // ds dispose frame completely m_cFrame.removeAll(); m_cFrame.dispose(); } // ds info System.out.println("[" + CLogger.getStamp() + "]<CGUI>(close) Closing of GUI complete"); }
// ds constructor public CGUI( final CLearnerBayes p_cLearner, final CMySQLManager p_cMySQLManager, final int p_iWindowWidth, final int p_iWindowHeight) { // ds set the learner m_cLearner = p_cLearner; // ds and MySQL manager m_cMySQLManager = p_cMySQLManager; // ds set constants m_iWindowWidth = p_iWindowWidth; m_iWindowHeight = p_iWindowHeight; // ds configure the frame m_cFrame.setSize(p_iWindowWidth, p_iWindowHeight); // m_cFrame.setResizable( false ); m_cFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // ds divide frame in two parts: information and image m_cFrame.setLayout(new BorderLayout()); // ds creating a border to highlight the JPanel areas Border cOutline = BorderFactory.createLineBorder(Color.black); // ds set borders // m_cPanelNorth.setBorder( cOutline ); // m_cPanelEast.setBorder( cOutline ); // m_cPanelSouth.setBorder( cOutline ); // m_cPanelWest.setBorder( cOutline ); m_cPanelCImage.setBorder(cOutline); // ds setup components (buttons, ..) _setComponents(); // ds add the panels to the frame m_cFrame.add(m_cPanelNorth, BorderLayout.NORTH); // m_cFrame.add( m_cPanelEast , BorderLayout.EAST ); m_cFrame.add(m_cPanelSouth, BorderLayout.SOUTH); // m_cFrame.add( m_cPanelWest , BorderLayout.WEST ); m_cFrame.add(m_cPanelCImage, BorderLayout.CENTER); System.out.println("[" + CLogger.getStamp() + "]<CGUI>(CGUI) Instance allocated"); }
// ds event invoked function public void actionPerformed(ActionEvent p_cEvent) { // ds get the event source final Object cSource = p_cEvent.getSource(); try { // ds determine event (big if/else tree) if (cSource == m_cButtonLike) { _displayImage( m_cLearner.getNextPattern(CLearnerBayes.ELearnerLabel.LIKE, m_cCurrentPattern)); } else if (cSource == m_cButtonDislike) { _displayImage( m_cLearner.getNextPattern(CLearnerBayes.ELearnerLabel.DISLIKE, m_cCurrentPattern)); } else if (cSource == m_cButtonPrevious) { _displayImage(m_cLearner.getPreviousPattern()); } else if (cSource == m_cButtonReset) { // ds reset the learning process m_cLearner.reset(); // ds get a new image _displayImage(m_cLearner.getFirstDataPoint()); } } catch (SQLException e) { _logMaster("<CGUI>(actionPerformed) SQLException: " + e.getMessage()); System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(actionPerformed) SQLException: " + e.getMessage()); // ds no previous image available JOptionPane.showMessageDialog(m_cFrame, "Could not load image from MySQL database"); } catch (CZEPnpIException e) { _logMaster("<CGUI>(actionPerformed) CZEPnpIException: " + e.getMessage()); System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(actionPerformed) CZEPnpIException: " + e.getMessage()); // ds no previous image available JOptionPane.showMessageDialog( m_cFrame, "No previous image available - please use Like/Dislike to classify the first image"); } catch (CZEPMySQLManagerException e) { _logMaster("<CGUI>(actionPerformed) CZEPMySQLManagerException: " + e.getMessage()); System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(actionPerformed) CZEPMySQLManagerException: " + e.getMessage()); // ds no previous image available JOptionPane.showMessageDialog(m_cFrame, "Could not load image from MySQL database"); } catch (MalformedURLException e) { _logMaster("<CGUI>(actionPerformed) MalformedURLException: " + e.getMessage()); System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(actionPerformed) MalformedURLException: " + e.getMessage()); // ds no previous image available JOptionPane.showMessageDialog(m_cFrame, "Could not load image from MySQL database"); } catch (CZEPEoIException e) { _logMaster("<CGUI>(actionPerformed) CZEPEoIException: " + e.getMessage()); System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(actionPerformed) CZEPEoIException: " + e.getMessage()); // ds no previous image available JOptionPane.showMessageDialog(m_cFrame, "Could not load image from MySQL database"); } }
// ds enable display public void launch() throws CZEPGUIException, HeadlessException, SQLException, CZEPMySQLManagerException { System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(launch) Loading initial application setup"); // ds allocate a dialog object to display independently final JDialog cDialogLoading = new JDialog(m_cFrame, "ZEP: Zero-Effort Procrastination", false); // ds set the option panel without any options cDialogLoading.setContentPane( new JOptionPane( "Loading image data please wait", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[] {}, null)); // ds display the dialog cDialogLoading.pack(); cDialogLoading.setVisible(true); cDialogLoading.setLocationRelativeTo(null); try { // ds fetch initial datapool m_cLearner.fetchInitialDataPool(); // ds try to get the image for first display _displayInitialImage(m_cLearner.getFirstDataPoint()); } catch (Exception e) { // ds info System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(launch) Exception: " + e.getMessage() + " - could not fetch database"); // ds dispose loading screen cDialogLoading.removeAll(); cDialogLoading.dispose(); // ds rethrow throw new CZEPGUIException("GUI aborted"); } // ds dispose loading screen cDialogLoading.removeAll(); cDialogLoading.dispose(); // ds register key listener m_cFrame.addKeyListener(this); // ds display frame for interaction m_cFrame.setVisible(true); m_cFrame.setLocationRelativeTo(null); // ds request focus for key strokes m_cFrame.requestFocus(); // ds initialize with empty string String strUsername = ""; // ds as long as it is not set while (strUsername.isEmpty()) { // ds show dialog to enter name strUsername = JOptionPane.showInputDialog( m_cFrame, "Please enter your desired username: "******"ZEP: Zero-Effort Procrastination", JOptionPane.PLAIN_MESSAGE); // ds check if null (cancelled by user) if (null == strUsername) { // ds escape throw new CZEPGUIException("cancelled username setting dialog"); } // ds check if already taken if (!strUsername.isEmpty() && !m_cMySQLManager.isUserAvailable(strUsername)) { // ds inform JOptionPane.showMessageDialog(m_cFrame, "Username already taken - please try again"); // ds keep looping strUsername = ""; } } // ds username is fine to set m_cLearner.setUsername(strUsername); // ds set user active m_cMySQLManager.setActiveUser(strUsername); // ds and log System.out.println( "[" + CLogger.getStamp() + "]<CGUI>(launch) Login of: [" + strUsername + "] successful"); // ds log successful launch _logMaster("<CGUI>(launch) launched GUI application"); // ds request focus for key strokes m_cFrame.requestFocus(); }