From 2fb4289202b6a9ff4d24a74cdee3aa156f619f4a Mon Sep 17 00:00:00 2001 From: Grzegorz Jaszewski <> Date: Wed, 30 Jan 2013 22:23:14 +0000 Subject: [PATCH] Crawler GUI almost working --- nbactions.xml | 6 +- src/main/java/sem2/iwi/App.java | 6 ++ .../sem2/iwi/crawler/MainTwellowCrawler.java | 24 ++++- src/main/java/sem2/iwi/gui/MainPanel.form | 10 +- src/main/java/sem2/iwi/gui/MainPanel.java | 36 ++++++- src/main/java/sem2/iwi/gui/MainWindow.form | 68 +++++++++++++ src/main/java/sem2/iwi/gui/MainWindow.java | 97 +++++++++++++++++++ .../java/sem2/iwi/gui/TwellowCateryModel.java | 53 ++++++++++ .../java/sem2/iwi/gui/TwellowCrawler.form | 53 +++++----- .../java/sem2/iwi/gui/TwellowCrawler.java | 74 ++++++++++---- 10 files changed, 374 insertions(+), 53 deletions(-) create mode 100644 src/main/java/sem2/iwi/gui/MainWindow.form create mode 100644 src/main/java/sem2/iwi/gui/MainWindow.java create mode 100644 src/main/java/sem2/iwi/gui/TwellowCateryModel.java diff --git a/nbactions.xml b/nbactions.xml index b2ea63e..a1584ca 100644 --- a/nbactions.xml +++ b/nbactions.xml @@ -7,7 +7,7 @@ org.codehaus.mojo:exec-maven-plugin:1.2:exec - -classpath %classpath sem2.iwi.crawler.MainTwellowCrawler + -classpath %classpath sem2.iwi.App java runtime @@ -19,7 +19,7 @@ org.codehaus.mojo:exec-maven-plugin:1.2:exec - -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath sem2.iwi.crawler.MainTwellowCrawler + -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath sem2.iwi.App java runtime true @@ -32,7 +32,7 @@ org.codehaus.mojo:exec-maven-plugin:1.2:exec - ${profiler.args} -classpath %classpath sem2.iwi.crawler.MainTwellowCrawler + ${profiler.args} -classpath %classpath sem2.iwi.App ${profiler.java} diff --git a/src/main/java/sem2/iwi/App.java b/src/main/java/sem2/iwi/App.java index 1602b9d..8b81817 100644 --- a/src/main/java/sem2/iwi/App.java +++ b/src/main/java/sem2/iwi/App.java @@ -7,6 +7,8 @@ import sem2.iwi.bayes.NBAttributeValue; import java.util.ArrayList; import java.util.HashMap; +import javax.swing.JFrame; +import sem2.iwi.gui.MainWindow; /** * Hello world! @@ -15,6 +17,10 @@ public class App { public static void main(String[] args) { + JFrame f = new MainWindow(); + f.setVisible(true); + } + public static void testRun(){ try { HashMap hm = InOut.getInputStreamAsOutputDictionary(Thread.currentThread().getContextClassLoader().getResourceAsStream("input2_test.dict")); diff --git a/src/main/java/sem2/iwi/crawler/MainTwellowCrawler.java b/src/main/java/sem2/iwi/crawler/MainTwellowCrawler.java index 9e146cf..3a67d7e 100644 --- a/src/main/java/sem2/iwi/crawler/MainTwellowCrawler.java +++ b/src/main/java/sem2/iwi/crawler/MainTwellowCrawler.java @@ -82,13 +82,35 @@ public static void main(String[] args) { } } - ArrayList nicks = getNicksForCategory(atc.iterator().next()); + /*for (String nick : nicks) { System.out.println(nick); }*/ } + public static String getNicksForCategoryNames(ArrayList categories) { + + HashSet atc = getMainCategoryList(); + StringBuilder output = new StringBuilder(); + + for (TwellowCategory tc : atc) { + if (!categories.contains(tc.getName())) { + continue; + } + //System.out.println(String.format("%s\t%d\t http://twellow.com%s ", tc.getName(), tc.getUsers(), tc.getUrl())); + + ArrayList nicks = getNicksForCategory(tc); + for (String nick : nicks) { + output.append(tc.getName()) + .append('\t') + .append(nick) + .append('\n'); + } + } + return output.toString(); + } + private static String getUrlForCategory(TwellowCategory tc) { return TWELLOW_BASE + tc.getUrl(); } diff --git a/src/main/java/sem2/iwi/gui/MainPanel.form b/src/main/java/sem2/iwi/gui/MainPanel.form index 738a007..c175772 100644 --- a/src/main/java/sem2/iwi/gui/MainPanel.form +++ b/src/main/java/sem2/iwi/gui/MainPanel.form @@ -87,8 +87,8 @@ - - + + @@ -106,6 +106,9 @@ + + + @@ -118,6 +121,9 @@ + + + diff --git a/src/main/java/sem2/iwi/gui/MainPanel.java b/src/main/java/sem2/iwi/gui/MainPanel.java index 96cebdd..dd46687 100644 --- a/src/main/java/sem2/iwi/gui/MainPanel.java +++ b/src/main/java/sem2/iwi/gui/MainPanel.java @@ -4,6 +4,8 @@ */ package sem2.iwi.gui; +import javax.swing.JFileChooser; + /** * * @author jjroman @@ -42,10 +44,20 @@ private void initComponents() { jLabel1.setText("Zbiór uczący"); jButton1.setText("przeglądaj"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); jLabel2.setText("Zbiór testujacy"); jButton2.setText("przeglądaj"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); jTextArea1.setColumns(20); jTextArea1.setRows(5); @@ -115,11 +127,31 @@ private void initComponents() { .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton3)) - .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) + .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE) .addContainerGap()) ); }// //GEN-END:initComponents + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + // TODO add your handling code here: + final JFileChooser fc = new JFileChooser(); + int returnVal = fc.showOpenDialog(this); + if(returnVal == JFileChooser.APPROVE_OPTION){ + jTextField1.setText(fc.getSelectedFile().getAbsolutePath()); + } + + }//GEN-LAST:event_jButton1ActionPerformed + + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + // TODO add your handling code here: + final JFileChooser fc = new JFileChooser(); + int returnVal = fc.showOpenDialog(this); + if(returnVal == JFileChooser.APPROVE_OPTION){ + jTextField2.setText(fc.getSelectedFile().getAbsolutePath()); + } + }//GEN-LAST:event_jButton2ActionPerformed + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; diff --git a/src/main/java/sem2/iwi/gui/MainWindow.form b/src/main/java/sem2/iwi/gui/MainWindow.form new file mode 100644 index 0000000..dbfb85c --- /dev/null +++ b/src/main/java/sem2/iwi/gui/MainWindow.form @@ -0,0 +1,68 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/sem2/iwi/gui/MainWindow.java b/src/main/java/sem2/iwi/gui/MainWindow.java new file mode 100644 index 0000000..95f4dd9 --- /dev/null +++ b/src/main/java/sem2/iwi/gui/MainWindow.java @@ -0,0 +1,97 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package sem2.iwi.gui; + +/** + * + * @author jjroman + */ +public class MainWindow extends javax.swing.JFrame { + + /** + * Creates new form MainWindow + */ + public MainWindow() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jTabbedPane1 = new javax.swing.JTabbedPane(); + mainPanel1 = new sem2.iwi.gui.MainPanel(); + twellowCrawler1 = new sem2.iwi.gui.TwellowCrawler(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jTabbedPane1.addTab("Klasyfikator", mainPanel1); + jTabbedPane1.addTab("Kategorie Twittów", twellowCrawler1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(jTabbedPane1) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new MainWindow().setVisible(true); + } + }); + } + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JTabbedPane jTabbedPane1; + private sem2.iwi.gui.MainPanel mainPanel1; + private sem2.iwi.gui.TwellowCrawler twellowCrawler1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/main/java/sem2/iwi/gui/TwellowCateryModel.java b/src/main/java/sem2/iwi/gui/TwellowCateryModel.java new file mode 100644 index 0000000..ef2125b --- /dev/null +++ b/src/main/java/sem2/iwi/gui/TwellowCateryModel.java @@ -0,0 +1,53 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package sem2.iwi.gui; + +import java.util.HashSet; +import java.util.Iterator; +import javax.swing.table.DefaultTableModel; +import sem2.iwi.crawler.MainTwellowCrawler; +import sem2.iwi.crawler.TwellowCategory; + +/** + * + * @author jjroman + */ +class TwellowCateryModel extends DefaultTableModel { + + + + public TwellowCateryModel(Object[][] data, String[] columnNames) { + super(data,columnNames); + } + + public static String[] getColumnNames(){ + return new String[]{"Nazwa", "Liczba użytkowników", "URL"}; + } + public static Object[][] getData(){ + HashSet hstc = MainTwellowCrawler.getMainCategoryList(); + Object[][] data = new Object[hstc.size()][3]; + int i = 0; + for (Iterator it = hstc.iterator(); it.hasNext(); i++) { + TwellowCategory tc = it.next(); + data[i][0] = tc.getName(); + data[i][1] = tc.getUsers(); + data[i][2] = tc.getUrl(); + } + return data; + } + + @Override + public boolean isCellEditable(int y, int x){ + return false; + } + + @Override + public Class getColumnClass(int columnIndex) { + if (columnIndex == 1) { + return Integer.class; + } + return String.class; + } +} diff --git a/src/main/java/sem2/iwi/gui/TwellowCrawler.form b/src/main/java/sem2/iwi/gui/TwellowCrawler.form index dd3d847..880481b 100644 --- a/src/main/java/sem2/iwi/gui/TwellowCrawler.form +++ b/src/main/java/sem2/iwi/gui/TwellowCrawler.form @@ -1,6 +1,6 @@ -
+ @@ -16,21 +16,19 @@ - - + + - - + - - - - - + + + + @@ -40,13 +38,12 @@ - + + + - - - @@ -60,11 +57,6 @@ - - - - - @@ -88,13 +80,11 @@ - - - - - - + + + + @@ -102,6 +92,11 @@ + + + + + @@ -118,5 +113,13 @@ + + + + + + + + diff --git a/src/main/java/sem2/iwi/gui/TwellowCrawler.java b/src/main/java/sem2/iwi/gui/TwellowCrawler.java index bd841b0..c9a2fba 100644 --- a/src/main/java/sem2/iwi/gui/TwellowCrawler.java +++ b/src/main/java/sem2/iwi/gui/TwellowCrawler.java @@ -4,6 +4,10 @@ */ package sem2.iwi.gui; +import java.util.ArrayList; +import javax.swing.ListSelectionModel; +import sem2.iwi.crawler.MainTwellowCrawler; + /** * * @author jjroman @@ -15,6 +19,9 @@ public class TwellowCrawler extends javax.swing.JPanel { */ public TwellowCrawler() { initComponents(); + + loadMainCategories(); + } /** @@ -26,17 +33,14 @@ public TwellowCrawler() { // //GEN-BEGIN:initComponents private void initComponents() { - jToggleButton1 = new javax.swing.JToggleButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton1 = new javax.swing.JButton(); - jSlider1 = new javax.swing.JSlider(); jLabel1 = new javax.swing.JLabel(); jSpinner1 = new javax.swing.JSpinner(); jScrollPane2 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); - - jToggleButton1.setText("Odświerz"); + jButton2 = new javax.swing.JButton(); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { @@ -51,16 +55,28 @@ private void initComponents() { )); jScrollPane1.setViewportView(jTable1); - jButton1.setText("jButton1"); - - jSlider1.setValue(0); + jButton1.setText("Generuj listę kont"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); jLabel1.setText("liczba kont na kategorię:"); + jSpinner1.setModel(new javax.swing.SpinnerNumberModel(20, 20, 100, 20)); + jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane2.setViewportView(jTextArea1); + jButton2.setText("Odświerz"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( @@ -68,30 +84,27 @@ private void initComponents() { .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 802, Short.MAX_VALUE) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(31, 31, 31) - .addComponent(jToggleButton1) + .addComponent(jButton2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) @@ -100,15 +113,36 @@ private void initComponents() { .addContainerGap()) ); }// //GEN-END:initComponents + + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + // TODO add your handling code here: + loadMainCategories(); + }//GEN-LAST:event_jButton2ActionPerformed + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + // TODO add your handling code here: + TwellowCateryModel tcm = (TwellowCateryModel) jTable1.getModel(); + ListSelectionModel selectionModel = jTable1.getSelectionModel(); + ArrayList retVal = new ArrayList<>(); + for (int i = 0; i < jTable1.getRowCount(); i++) { + if (selectionModel.isSelectedIndex(i)) { + retVal.add((String) tcm.getValueAt(i, 0)); + } + } + jTextArea1.setText(MainTwellowCrawler.getNicksForCategoryNames(retVal)); + }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; + private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; - private javax.swing.JSlider jSlider1; private javax.swing.JSpinner jSpinner1; private javax.swing.JTable jTable1; private javax.swing.JTextArea jTextArea1; - private javax.swing.JToggleButton jToggleButton1; // End of variables declaration//GEN-END:variables + + private void loadMainCategories() { + jTable1.setModel(new TwellowCateryModel(TwellowCateryModel.getData(), TwellowCateryModel.getColumnNames())); + } }