/* * Q2DataDialog.java * * Created on 17. September 2004, 20:13 */ package jake2.qcommon; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import javax.imageio.ImageIO; import javax.swing.*; /** * * @author hoz */ public class Q2DataDialog extends javax.swing.JDialog { /** Creates new form Q2DataDialog */ public Q2DataDialog() { super(); initComponents(); DisplayMode mode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode(); int x = (mode.getWidth() - getWidth()) / 2; int y = (mode.getHeight() - getHeight()) / 2; setLocation(x, y); dir = System.getProperty("user.home") + "/jake2"; jTextField1.setText(dir); } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; choosePanel = new javax.swing.JPanel(); statusPanel = new JPanel(); status = new JLabel("initializing Jake2..."); jTextField1 = new javax.swing.JTextField(); changeButton = new javax.swing.JButton(); installButton = new javax.swing.JButton(); exitButton = new javax.swing.JButton(); okButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setTitle("Jake2 - Bytonic Software"); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); choosePanel.setLayout(new java.awt.GridBagLayout()); choosePanel.setMaximumSize(new java.awt.Dimension(400, 100)); choosePanel.setMinimumSize(new java.awt.Dimension(400, 100)); choosePanel.setPreferredSize(new java.awt.Dimension(400, 100)); jTextField1.setPreferredSize(null); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); gridBagConstraints.weightx = 1.0; choosePanel.add(jTextField1, gridBagConstraints); changeButton.setText("change"); changeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { changeButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 0; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; choosePanel.add(changeButton, gridBagConstraints); installButton.setText("Install"); installButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { installButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; choosePanel.add(installButton, gridBagConstraints); exitButton.setText("Exit"); exitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); choosePanel.add(exitButton, gridBagConstraints); okButton.setText("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; choosePanel.add(okButton, gridBagConstraints); //getContentPane().add(choosePanel, java.awt.BorderLayout.SOUTH); Jake2Canvas c = new Jake2Canvas(); getContentPane().add(c, BorderLayout.CENTER); statusPanel.setLayout(new java.awt.GridBagLayout()); statusPanel.setMaximumSize(new java.awt.Dimension(400, 100)); statusPanel.setMinimumSize(new java.awt.Dimension(400, 100)); statusPanel.setPreferredSize(new java.awt.Dimension(400, 100)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); gridBagConstraints.weightx = 1.0; statusPanel.add(status, gridBagConstraints); getContentPane().add(statusPanel, java.awt.BorderLayout.SOUTH); progressPanel = new ProgressPanel(this); installPanel = new InstallPanel(this); pack(); }//GEN-END:initComponents private void installButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_installButtonActionPerformed showInstallPanel(); }//GEN-LAST:event_installButtonActionPerformed private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed System.exit(1); dispose(); }//GEN-LAST:event_exitButtonActionPerformed private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed if (dir != null) { Cvar.Set("cddir", dir); FS.setCDDir(); } synchronized(this) { notifyAll(); } }//GEN-LAST:event_okButtonActionPerformed private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); chooser.setMultiSelectionEnabled(false); chooser.setDialogTitle("choose a valid baseq2 directory"); chooser.showDialog(this, "OK"); dir = null; try { dir = chooser.getSelectedFile().getCanonicalPath(); } catch (Exception e) {} if (dir != null) jTextField1.setText(dir); else dir = jTextField1.getText(); }//GEN-LAST:event_changeButtonActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing System.exit(1); dispose(); }//GEN-LAST:event_formWindowClosing // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton changeButton; private javax.swing.JButton exitButton; private javax.swing.JButton installButton; private Jake2Canvas canvas; private javax.swing.JPanel choosePanel; private JPanel statusPanel; private ProgressPanel progressPanel; private InstallPanel installPanel; private JLabel status; private javax.swing.JTextField jTextField1; private javax.swing.JButton okButton; // End of variables declaration//GEN-END:variables private String dir; void showChooseDialog() { getContentPane().remove(statusPanel); getContentPane().remove(progressPanel); getContentPane().remove(installPanel); getContentPane().add(choosePanel, BorderLayout.SOUTH); validate(); repaint(); } void showStatus() { getContentPane().remove(choosePanel); getContentPane().remove(installPanel); getContentPane().add(statusPanel, BorderLayout.SOUTH); validate(); repaint(); } void showProgressPanel() { getContentPane().remove(choosePanel); getContentPane().remove(installPanel); getContentPane().add(progressPanel, BorderLayout.SOUTH); validate(); repaint(); } void showInstallPanel() { getContentPane().remove(choosePanel); getContentPane().remove(statusPanel); getContentPane().add(installPanel, BorderLayout.SOUTH); validate(); repaint(); } void setStatus(String text) { status.setText(text); } void testQ2Data() { while (FS.LoadFile("pics/colormap.pcx") == null) { showChooseDialog(); try { synchronized(this) { wait(); } } catch (InterruptedException e) {} } showStatus(); repaint(); } static class Jake2Canvas extends Canvas { private Image image; Jake2Canvas() { setSize(400, 200); try { image = ImageIO.read(getClass().getResource("/splash.png")); } catch (Exception e) {} } /* (non-Javadoc) * @see java.awt.Component#paint(java.awt.Graphics) */ public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } } static class InstallPanel extends JPanel { private static final String[][] mirrors = { {"LOC gate", "ftp://gate/q2-314-demo-x86.exe"}, // local test @home {"DE ftp.fu-berlin.de", "ftp://ftp.fu-berlin.de/pc/msdos/games/idgames/idstuff/quake2/q2-314-demo-x86.exe"}, {"UK ftp.demon.co.uk", "ftp://ftp.demon.co.uk/pub/mirrors/idsoftware/quake2/q2-314-demo-x86.exe"}, {"SE ftp.fragzone.se", "ftp://ftp.fragzone.se/pub/spel/quake2/q2-314-demo-x86.exe"}, {"US ftp.idsoftware.com", "ftp://ftp.idsoftware.com/idstuff/quake2/q2-314-demo-x86.exe"} }; private Q2DataDialog parent; private JComboBox mirrorBox; private JTextField destDir; private JButton cancel; private JButton exit; private JButton install; private JButton choose; public InstallPanel(Q2DataDialog d) { initComponents(); initMirrors(); parent = d; } private void initComponents() { GridBagConstraints constraints = new GridBagConstraints(); setLayout(new GridBagLayout()); Dimension d = new Dimension(400, 100); setMinimumSize(d); setMaximumSize(d); setPreferredSize(d); constraints.gridx = 0; constraints.gridy = 0; constraints.insets = new Insets(5, 5, 0, 5); constraints.anchor = GridBagConstraints.SOUTHWEST; add(new JLabel("download mirror"), constraints); constraints.gridx = 0; constraints.gridy = 1; constraints.insets = new Insets(5, 5, 5, 5); add(new JLabel("destination directory"), constraints); constraints.gridx = 1; constraints.gridy = 0; constraints.weightx = 1; constraints.gridwidth = 3; constraints.insets = new Insets(5, 5, 0, 5); constraints.fill = GridBagConstraints.HORIZONTAL; mirrorBox = new JComboBox(); add(mirrorBox, constraints); constraints.gridx = 1; constraints.gridy = 1; constraints.gridwidth = 2; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); destDir = new JTextField(); add(destDir, constraints); constraints.gridx = 3; constraints.gridy = 1; constraints.weightx = 0; constraints.gridwidth = 1; constraints.fill = GridBagConstraints.NONE; choose = new JButton("..."); choose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { choose(); }}); add(choose, constraints); constraints.gridx = 0; constraints.gridy = 2; constraints.gridwidth = 1; constraints.weighty = 1; constraints.fill = GridBagConstraints.NONE; cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancel(); }}); add(cancel, constraints); constraints.gridx = 0; constraints.gridy = 2; constraints.gridwidth = 4; constraints.anchor = GridBagConstraints.SOUTH; exit = new JButton("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exit(); }}); add(exit, constraints); constraints.gridx = 2; constraints.gridy = 2; constraints.gridwidth = 2; constraints.anchor = GridBagConstraints.SOUTHEAST; install = new JButton("Install"); install.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { install(); }}); add(install, constraints); } private void initMirrors() { for (int i = 0; i < mirrors.length; i++) { mirrorBox.addItem(mirrors[i][0]); } } private void cancel() { parent.showChooseDialog(); } private void install() { parent.progressPanel.destDir = destDir.getText(); parent.progressPanel.mirror = mirrors[mirrorBox.getSelectedIndex()][1]; parent.showProgressPanel(); new Thread(parent.progressPanel).start(); } private void exit() { System.exit(0); } private void choose() { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); chooser.setMultiSelectionEnabled(false); chooser.setDialogTitle("choose destination directory"); chooser.showDialog(this, "OK"); String dir = null; try { dir = chooser.getSelectedFile().getCanonicalPath(); } catch (IOException e) {} destDir.setText(dir); } } static class ProgressPanel extends JPanel implements Runnable { static byte[] buf = new byte[8192]; String destDir; String mirror; JProgressBar progress = new JProgressBar(); JLabel label = new JLabel("test"); Q2DataDialog parent; public ProgressPanel(Q2DataDialog d) { initComponents(); parent = d; } void initComponents() { progress.setMinimum(0); progress.setMaximum(100); setLayout(new GridBagLayout()); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10); gridBagConstraints.weightx = 1.0; gridBagConstraints.anchor = GridBagConstraints.SOUTH; add(label, gridBagConstraints); gridBagConstraints.gridy = 1; gridBagConstraints.anchor = GridBagConstraints.NORTH; add(progress, gridBagConstraints); Dimension d = new Dimension(400, 100); setMinimumSize(d); setMaximumSize(d); setPreferredSize(d); } public void run() { InputStream in = null; OutputStream out = null; File outFile = null; label.setText("downloading..."); File dir = null; try { dir = new File(destDir); dir.mkdirs(); } catch (Exception e) {} try { if (!dir.isDirectory() || !dir.canWrite()) { endInstall(); return; } } catch (Exception e) { endInstall(); return; } try { URL url = new URL(mirror); URLConnection conn = url.openConnection(); int length = conn.getContentLength(); progress.setMaximum(length / 1024); in = conn.getInputStream(); outFile = File.createTempFile("Jake2Data", ".zip"); outFile.deleteOnExit(); out = new FileOutputStream(outFile); copyStream(in, out); } catch (Exception e) { endInstall(); return; } finally { try { in.close(); } catch (Exception e) {} try { out.close(); } catch (Exception e) {} } try { installData(outFile.getCanonicalPath()); } catch (Exception e) { endInstall(); return; } try { if (outFile != null) outFile.delete(); } catch (Exception e) {} parent.dir = destDir + "/baseq2"; parent.showChooseDialog(); parent.okButtonActionPerformed(null); } void installData(String filename) throws Exception { InputStream in = null; OutputStream out = null; try { ZipFile f = new ZipFile(filename); Enumeration e = f.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry)e.nextElement(); String name = entry.getName(); int i; if ((i = name.indexOf("/baseq2")) > -1 && name.indexOf(".dll") == -1) { name = destDir + name.substring(i); File outFile = new File(name); if (entry.isDirectory()) { outFile.mkdirs(); } else { label.setText("installing " + outFile.getName()); progress.setMaximum((int)entry.getSize()/1024); progress.setValue(0); outFile.getParentFile().mkdirs(); out = new FileOutputStream(outFile); in = f.getInputStream(entry); copyStream(in, out); } } } } catch (Exception e) { throw new Exception(); } finally { try {in.close();} catch (Exception e1) {} try {out.close();} catch (Exception e1) {} } } void endInstall() { parent.dir = destDir + "/baseq2"; parent.showChooseDialog(); parent.okButtonActionPerformed(null); } void copyStream(InputStream in, OutputStream out) throws Exception { try { int c = 0; int l; while ((l = in.read(buf)) > 0) { out.write(buf, 0, l); c += l; progress.setValue(c / 1024); } } catch (Exception e) { throw new Exception(); } finally { try { in.close(); } catch (Exception e) {} try { out.close(); } catch (Exception e) {} } } } }