Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package rdfsystem; import java.awt.BorderLayout; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import rdfsystem.data.DataMining; import rdfsystem.data.RdfManager; import rdfsystem.entity.Paper; import weka.classifiers.trees.J48; import weka.clusterers.ClusterEvaluation; import weka.clusterers.EM; import weka.gui.graphvisualizer.GraphVisualizer; import weka.gui.treevisualizer.PlaceNode2; import weka.gui.treevisualizer.TreeVisualizer; /** * * @author Wizard */ public class MainFrame extends javax.swing.JFrame { /** * Creates new form MainFrame */ public MainFrame() { initComponents(); this.setLocationRelativeTo(null); startSpinner.setModel(new SpinnerNumberModel(1, 1, Integer.MAX_VALUE, 1)); endSpinner.setModel(new SpinnerNumberModel(1, 1, Integer.MAX_VALUE, 1)); } /** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); startSpinner = new javax.swing.JSpinner(); jLabel2 = new javax.swing.JLabel(); endSpinner = new javax.swing.JSpinner(); clearButton = new javax.swing.JButton(); accountButton = new javax.swing.JButton(); exportButton = new javax.swing.JButton(); importButton = new javax.swing.JButton(); classifyButton = new javax.swing.JButton(); clsButton = new javax.swing.JButton(); assocButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); paperTable = new javax.swing.JTable(); searchButton = new javax.swing.JButton(); sumLabel = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jLabel1.setText(" "); startSpinner.setToolTipText(""); jLabel2.setText(""); clearButton.setText(""); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearButtonActionPerformed(evt); } }); accountButton.setText(""); accountButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { accountButtonActionPerformed(evt); } }); exportButton.setText(""); exportButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exportButtonActionPerformed(evt); } }); importButton.setText(""); importButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { importButtonActionPerformed(evt); } }); classifyButton.setText(""); classifyButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { classifyButtonActionPerformed(evt); } }); clsButton.setText("?"); clsButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clsButtonActionPerformed(evt); } }); assocButton.setText("?"); assocButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { assocButtonActionPerformed(evt); } }); paperTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "ID", "" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class }; boolean[] canEdit = new boolean[] { false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); paperTable.setColumnSelectionAllowed(true); paperTable.getTableHeader().setReorderingAllowed(false); paperTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { paperTableMouseClicked(evt); } }); jScrollPane1.setViewportView(paperTable); paperTable.getColumnModel().getSelectionModel() .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); if (paperTable.getColumnModel().getColumnCount() > 0) { paperTable.getColumnModel().getColumn(0).setResizable(false); paperTable.getColumnModel().getColumn(1).setResizable(false); } searchButton.setText(""); searchButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchButtonActionPerformed(evt); } }); sumLabel.setText(" 0 "); 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(startSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(endSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(searchButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE) .addComponent(importButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(exportButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(accountButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(clearButton)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(sumLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(assocButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(clsButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(classifyButton))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(startSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2) .addComponent(endSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(clearButton).addComponent(accountButton).addComponent(exportButton) .addComponent(importButton).addComponent(searchButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(classifyButton).addComponent(clsButton).addComponent(assocButton) .addComponent(sumLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened try { manager.load(); int len = manager.size(); sumLabel.setText(" " + len + " "); if (len == 0) return; if (len > 1000) len = 1000; endSpinner.setValue(len); showPapers(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); System.exit(0); } }//GEN-LAST:event_formWindowOpened private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed int start = (Integer) startSpinner.getValue(); int end = (Integer) endSpinner.getValue(); if (start > manager.size()) { JOptionPane.showMessageDialog(null, "???"); return; } if (end > manager.size()) { JOptionPane.showMessageDialog(null, "???"); return; } if (start > end) { JOptionPane.showMessageDialog(null, "????"); return; } showPapers(); }//GEN-LAST:event_searchButtonActionPerformed private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed if (JOptionPane.showConfirmDialog(null, "??", "", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) return; manager.clear(); paperCache.clear(); DefaultTableModel model = (DefaultTableModel) paperTable.getModel(); for (int i = model.getRowCount() - 1; i >= 0; i--) model.removeRow(i); startSpinner.setValue(1); endSpinner.setValue(1); sumLabel.setText(" 0 "); }//GEN-LAST:event_clearButtonActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing try { manager.save(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } }//GEN-LAST:event_formWindowClosing private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importButtonActionPerformed try { JFileChooser dlg = new JFileChooser(); dlg.setFileFilter(new FileNameExtensionFilter("?(*.xml)", "xml")); if (dlg.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) return; String filename = dlg.getSelectedFile().getPath(); manager.importData(filename); JOptionPane.showMessageDialog(null, "??"); sumLabel.setText(" " + manager.size() + " "); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); ex.printStackTrace(); } }//GEN-LAST:event_importButtonActionPerformed private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed try { JFileChooser dlg = new JFileChooser(); dlg.setFileFilter(new FileNameExtensionFilter("?(*.xml)", "xml")); if (dlg.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) return; String filename = dlg.getSelectedFile().getPath(); manager.exportData(filename); JOptionPane.showMessageDialog(null, "??"); } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } }//GEN-LAST:event_exportButtonActionPerformed private void paperTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_paperTableMouseClicked if (evt.getClickCount() == 1) return; int row = paperTable.getSelectedRow(); Paper p = paperCache.get(row); paperFrame.setInfo(p); paperFrame.setVisible(true); }//GEN-LAST:event_paperTableMouseClicked private void accountButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accountButtonActionPerformed accountFrame.setAuthorData(manager.accountAuthor()); accountFrame.setYearData(manager.accountYear()); accountFrame.setJournalData(manager.accountJournal()); accountFrame.setVisible(true); }//GEN-LAST:event_accountButtonActionPerformed private void classifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_classifyButtonActionPerformed try { String tree = DataMining.classify(manager); TreeVisualizer tv = new TreeVisualizer(null, tree, new PlaceNode2()); JDialog jf = new JDialog(); jf.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); jf.setSize(800, 600); jf.setModal(true); jf.setLocationRelativeTo(null); jf.setResizable(false); jf.getContentPane().setLayout(new BorderLayout()); jf.getContentPane().add(tv, BorderLayout.CENTER); tv.fitToScreen(); jf.setVisible(true); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); ex.printStackTrace(); } }//GEN-LAST:event_classifyButtonActionPerformed private void clsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clsButtonActionPerformed try { String res = DataMining.cluster(manager); ResultFrame cf = new ResultFrame(); cf.setText(res); cf.setVisible(true); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); ex.printStackTrace(); } }//GEN-LAST:event_clsButtonActionPerformed private void assocButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_assocButtonActionPerformed try { DataMining.assoiate(manager); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); ex.printStackTrace(); } }//GEN-LAST:event_assocButtonActionPerformed private void showPapers() { DefaultTableModel model = (DefaultTableModel) paperTable.getModel(); for (int i = model.getRowCount() - 1; i >= 0; i--) model.removeRow(i); paperCache.clear(); int start = (Integer) startSpinner.getValue() - 1; int end = (Integer) endSpinner.getValue() - 1; Iterator<Map.Entry<String, Paper>> it = manager.iterator(); for (int i = 0; i < start; i++, it.next()) ; for (int i = start; i <= end; i++) { Paper p = it.next().getValue(); paperCache.add(p); model.addRow(new Object[] { p.getId(), p.getTitle() }); } } private RdfManager manager = new RdfManager(); private List<Paper> paperCache = new ArrayList<>(); private PaperFrame paperFrame = new PaperFrame(); private AccountFrame accountFrame = new AccountFrame(); // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton accountButton; private javax.swing.JButton assocButton; private javax.swing.JButton classifyButton; private javax.swing.JButton clearButton; private javax.swing.JButton clsButton; private javax.swing.JSpinner endSpinner; private javax.swing.JButton exportButton; private javax.swing.JButton importButton; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable paperTable; private javax.swing.JButton searchButton; private javax.swing.JSpinner startSpinner; private javax.swing.JLabel sumLabel; // End of variables declaration//GEN-END:variables }