Java tutorial
package math.tools; /* * 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. */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; import java.nio.file.Paths; import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.logging.Level; import java.util.logging.Logger; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** * * @author Ilgaz */ public class Browse extends javax.swing.JFrame { Object[][] table = new Object[Short.MAX_VALUE][5]; /** * Creates new form jsontable */ public Browse() { BufferedReader in; FileOutputStream fos; new File(System.getProperty("user.home") + "//MathHelper//tools//").mkdirs(); try { URL website = new URL("https://dl.dropboxusercontent.com/u/67622419/mathTools/toolInfo.json"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(System.getProperty("user.home") + "//MathHelper//toolInfo.json"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (IOException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } try { in = new BufferedReader( new FileReader(System.getProperty("user.home") + "//MathHelper//toolInfo.json")); String inputLine; int i = -1; JSONObject json; while ((inputLine = in.readLine()) != null) { i++; json = (JSONObject) new JSONParser().parse(inputLine); table[i][0] = json.get("name"); table[i][1] = json.get("author"); table[i][2] = json.get("rating"); File f = new File( System.getProperty("user.home") + "//MathHelper//tools//" + json.get("name") + ".jar"); if (!f.exists()) { table[i][3] = "Not Downloaded Yet"; } else { table[i][3] = "Downloaded"; } table[i][4] = json.get("adress"); } in.close(); } catch (IOException | ParseException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTable1.setModel(new javax.swing.table.DefaultTableModel(table, new String[] { "Name", "Author", "Rating", "Local Status" }) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class }; boolean[] canEdit = new boolean[] { false, false, false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(jTable1); jButton1.setText("Run"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Force Update"); 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(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed if (table[jTable1.getSelectedRow()][3] == "Not Downloaded Yet") { System.out.println("downloading"); FileOutputStream fos; try { URL website = new URL("" + table[jTable1.getSelectedRow()][4]); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(System.getProperty("user.home") + "//MathHelper//tools//" + table[jTable1.getSelectedRow()][0] + ".jar"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (IOException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } } try { Runtime.getRuntime().exec("java -jar " + System.getProperty("user.home") + "//MathHelper//tools//" + table[jTable1.getSelectedRow()][0] + ".jar"); } catch (IOException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed FileOutputStream fos; new File(System.getProperty("user.home") + "//MathHelper//tools//" + table[jTable1.getSelectedRow()][0] + ".jar").delete(); try { URL website = new URL("" + table[jTable1.getSelectedRow()][4]); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(System.getProperty("user.home") + "//MathHelper//tools//" + table[jTable1.getSelectedRow()][0] + ".jar"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (IOException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } try { Runtime.getRuntime().exec("java -jar " + System.getProperty("user.home") + "//MathHelper//tools//" + table[jTable1.getSelectedRow()][0] + ".jar"); } catch (IOException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton2ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* 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(Browse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Browse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Browse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Browse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Browse().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }