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 net.ytbolg.mcxa; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import javax.swing.*; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.table.DefaultTableModel; import net.ytbolg.mcxa.Launcher.GameInfo; import static net.ytbolg.mcxa.Launcher.GameInfo.tpf; import static net.ytbolg.mcxa.MCLaucherXA.ReadFile; import org.json.JSONArray; import org.json.JSONObject; /** * * @author Ytong */ public class VersionCheck extends javax.swing.JFrame { /** * Creates new form VersionCheck */ public VersionCheck() { 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() { jTabbedPane1 = new javax.swing.JTabbedPane(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jScrollPane3 = new javax.swing.JScrollPane(); jTable2 = new javax.swing.JTable(); jScrollPane4 = new javax.swing.JScrollPane(); jTable3 = new javax.swing.JTable(); jScrollPane5 = new javax.swing.JScrollPane(); jTable4 = new javax.swing.JTable(); jProgressBar1 = new javax.swing.JProgressBar(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle(""); addWindowListener(new java.awt.event.WindowAdapter() { public void windowActivated(java.awt.event.WindowEvent evt) { formWindowActivated(evt); } public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { })); jTable1.setColumnSelectionAllowed(true); jTable1.getTableHeader().setReorderingAllowed(false); jScrollPane1.setViewportView(jTable1); jTable1.getColumnModel().getSelectionModel() .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jTabbedPane1.addTab("?", jScrollPane1); jTable2.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { })); jTable2.setColumnSelectionAllowed(true); jTable2.getTableHeader().setReorderingAllowed(false); jScrollPane3.setViewportView(jTable2); jTable2.getColumnModel().getSelectionModel() .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jTabbedPane1.addTab("", jScrollPane3); jTable3.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { })); jTable3.setColumnSelectionAllowed(true); jTable3.getTableHeader().setReorderingAllowed(false); jScrollPane4.setViewportView(jTable3); jTable3.getColumnModel().getSelectionModel() .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jTabbedPane1.addTab("", jScrollPane4); jTable4.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { })); jTable4.setColumnSelectionAllowed(true); jTable4.getTableHeader().setReorderingAllowed(false); jScrollPane5.setViewportView(jTable4); jTable4.getColumnModel().getSelectionModel() .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jTabbedPane1.addTab(" ", jScrollPane5); jButton1.setText(""); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTabbedPane1) .addGroup(layout.createSequentialGroup() .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1).addComponent(jLabel1)) .addGap(0, 21, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated }//GEN-LAST:event_formWindowActivated private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed DefaultTableModel zsbm = (DefaultTableModel) jTable1.getModel(); DefaultTableModel kzbm = (DefaultTableModel) jTable2.getModel(); DefaultTableModel jbbm = (DefaultTableModel) jTable3.getModel(); DefaultTableModel qbm = (DefaultTableModel) jTable4.getModel(); String version = ""; switch (jTabbedPane1.getSelectedIndex()) { case 0: System.out.println(jTable1.getSelectedRow()); version = zsbm.getValueAt(jTable1.getSelectedRow(), 0).toString(); break; case 1: System.out.println(jTable2.getSelectedRow()); version = kzbm.getValueAt(jTable2.getSelectedRow(), 0).toString(); break; case 2: System.out.println(jTable3.getSelectedRow()); version = jbbm.getValueAt(jTable3.getSelectedRow(), 0).toString(); break; case 3: System.out.println(jTable4.getSelectedRow()); version = qbm.getValueAt(jTable4.getSelectedRow(), 0).toString(); break; } DownVersionThread.version = version; DownVersionThread.j = jProgressBar1; DownVersionThread.l = jLabel1; new Thread(new DownVersionThread()).start(); }//GEN-LAST:event_jButton1ActionPerformed private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened setTitle(Lang.getLang("Version_Title")); DefaultTableModel zsbm = (DefaultTableModel) jTable1.getModel(); DefaultTableModel kzbm = (DefaultTableModel) jTable2.getModel(); DefaultTableModel jbbm = (DefaultTableModel) jTable3.getModel(); DefaultTableModel qbm = (DefaultTableModel) jTable4.getModel(); jButton1.setText(Lang.getLang("Version_Button_Download")); jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTable4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTable1.setColumnSelectionAllowed(false); jTable2.setColumnSelectionAllowed(false); jTable3.setColumnSelectionAllowed(false); jTable4.setColumnSelectionAllowed(false); jTable1.setRowSelectionAllowed(true); jTable2.setRowSelectionAllowed(true); jTable3.setRowSelectionAllowed(true); jTable4.setRowSelectionAllowed(true); zsbm.addColumn(Lang.getLang("Version_Table_McVersion")); zsbm.addColumn(Lang.getLang("Version_Table_RelTime")); zsbm.addColumn(Lang.getLang("Version_Table_Type")); kzbm.addColumn(Lang.getLang("Version_Table_McVersion")); kzbm.addColumn(Lang.getLang("Version_Table_RelTime")); kzbm.addColumn(Lang.getLang("Version_Table_Type")); jbbm.addColumn(Lang.getLang("Version_Table_McVersion")); jbbm.addColumn(Lang.getLang("Version_Table_RelTime")); jbbm.addColumn(Lang.getLang("Version_Table_Type")); qbm.addColumn(Lang.getLang("Version_Table_McVersion")); qbm.addColumn(Lang.getLang("Version_Table_RelTime")); qbm.addColumn(Lang.getLang("Version_Table_Type")); jTabbedPane1.setTitleAt(0, Lang.getLang("Version_TabledPanel_Zsb")); jTabbedPane1.setTitleAt(1, Lang.getLang("Version_TabledPanel_Kzb")); jTabbedPane1.setTitleAt(2, Lang.getLang("Version_TabledPanel_jbb")); jTabbedPane1.setTitleAt(3, Lang.getLang("Version_TabledPanel_qb")); try { JSONArray ja = (new JSONObject(downloadFile( DownLoadURL.getURL(DownLoadURL.VERSION_LIST, Integer.valueOf(Config.getConfig("DownSou"))))) .getJSONArray("versions")); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); if (jo.getString("type").equals("release")) { zsbm.addRow(new Object[] { jo.get("id"), jo.get("releaseTime"), jo.get("type") }); } if (jo.getString("type").equals("snapshot")) { kzbm.addRow(new Object[] { jo.get("id"), jo.get("releaseTime"), jo.get("type") }); } if (jo.getString("type").contains("old")) { jbbm.addRow(new Object[] { jo.get("id"), jo.get("releaseTime"), jo.get("type") }); } qbm.addRow(new Object[] { jo.get("id"), jo.get("releaseTime"), jo.get("type") }); } } catch (Exception e) { } // TODO add your handling code here: // TODO add your handling code here: }//GEN-LAST:event_formWindowOpened /** * @param args the command line arguments */ public String downloadFile(String remoteFilePath) throws IOException { URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(GameInfo.Rundir + GameInfo.tpf + "temp.tmp"); File xxxx = new File(f.getParent()); xxxx.mkdirs(); // f.mkdirs(); try { urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection) urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len = 2048; byte[] b = new byte[len]; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } bos.flush(); bis.close(); httpUrl.disconnect(); } catch (Exception e) { e.printStackTrace(); } finally { try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } String x = ReadFile(GameInfo.Rundir + GameInfo.tpf + "temp.tmp"); f.delete(); return x; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JProgressBar jProgressBar1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JScrollPane jScrollPane5; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTable jTable1; private javax.swing.JTable jTable2; private javax.swing.JTable jTable3; private javax.swing.JTable jTable4; // End of variables declaration//GEN-END:variables } class DownVersionThread implements Runnable { public static String version = ""; public static JProgressBar j; public static JLabel l; // public static String forgeversion = ""; //@Override @Override public void run() { System.out.println(""); downloadFile( DownLoadURL.getURL(DownLoadURL.VERSION, Integer.valueOf(Config.getConfig("DownSou"))) + "/" + version + "/" + version + ".jar", GameInfo.GameDir + tpf + "versions" + tpf + version + tpf + version + ".jar"); downloadFile( DownLoadURL.getURL(DownLoadURL.VERSION, Integer.valueOf(Config.getConfig("DownSou"))) + "/" + version + "/" + version + ".json", GameInfo.GameDir + tpf + "versions" + tpf + version + tpf + version + ".json"); JOptionPane.showMessageDialog(null, Lang.getLang("Version_DownSucc")); j.setValue(0); l.setText(""); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } void downloadFile(String remoteFilePath, String localFilePath) { URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(localFilePath); File xxxx = new File(f.getParent()); xxxx.mkdirs(); // f.mkdirs(); try { urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection) urlfile.openConnection(); httpUrl.connect(); j.setMaximum(httpUrl.getContentLength()); j.setValue(0); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len = 2048; byte[] b = new byte[len]; int i = 0; while ((len = bis.read(b)) != -1) { i = i + len; j.setValue(i); l.setText(i / 1024 + "/" + (httpUrl.getContentLength() / 1024) + "KB"); bos.write(b, 0, len); } bos.flush(); bis.close(); httpUrl.disconnect(); } catch (Exception e) { e.printStackTrace(); } finally { try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } } }