Java tutorial
/* * Copyright 2010 Nikita Kuklev. * * This file is part of LPServer2 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * AddGameDialog.java * * Created on 10-Nov-2010, 1:57:55 AM */ package com.nuclearrussian.lp.server.main; import com.nuclearrussian.lp.persistence.Game; import com.nuclearrussian.lp.persistence.GamelistStorage; import com.nuclearrussian.lp.shared.Shared; import java.io.File; import javax.swing.JOptionPane; import javax.swing.UIManager; import org.apache.commons.io.FilenameUtils; import org.apache.log4j.Logger; /** * * @author Nikita Kuklev */ public class AddGameDialog extends javax.swing.JDialog { static Logger l = Logger.getLogger(AddGameDialog.class.getName()); Game game = new Game(); AddGameDialog dialog; boolean isEditDialog = false; /** Creates new form AddGameDialog */ public AddGameDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); 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() { jFileChooser1 = new javax.swing.JFileChooser(); jFileChooser2 = new javax.swing.JFileChooser(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jScrollPane2 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jTextField4 = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); jSeparator3 = new javax.swing.JSeparator(); jLabel8 = new javax.swing.JLabel(); jTextField5 = new javax.swing.JTextField(); jLabel9 = new javax.swing.JLabel(); jTextField6 = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); jTextField7 = new javax.swing.JTextField(); jFileChooser1.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setTitle("Set game options"); setModal(true); setResizable(false); jPanel1.setBackground(new java.awt.Color(51, 51, 51)); jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Game name"); jLabel2.setForeground(new java.awt.Color(255, 255, 255)); jLabel2.setText("Game root folder"); jLabel3.setForeground(new java.awt.Color(255, 255, 255)); jLabel3.setText("Game executable to run"); jLabel4.setForeground(new java.awt.Color(255, 255, 255)); jLabel4.setText("Flags on launch"); jLabel5.setForeground(new java.awt.Color(255, 255, 255)); jLabel5.setText( ".bat files to execute after download done (1 per line, absolute paths, executed sequentially) "); jButton1.setBackground(new java.awt.Color(102, 102, 102)); jButton1.setText("..."); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setBackground(new java.awt.Color(102, 102, 102)); jButton2.setText("..."); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N jTextArea1.setRows(5); jScrollPane2.setViewportView(jTextArea1); jButton3.setBackground(new java.awt.Color(51, 51, 51)); jButton3.setText("Cancel"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setBackground(new java.awt.Color(51, 51, 51)); jButton4.setText("OK"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jLabel7.setForeground(new java.awt.Color(255, 255, 255)); jLabel7.setText("Process fingerprint (as seen by tasklist.exe)"); jLabel8.setForeground(new java.awt.Color(255, 255, 255)); jLabel8.setText("Process name:"); jLabel9.setForeground(new java.awt.Color(255, 255, 255)); jLabel9.setText("Window name:"); jLabel10.setForeground(new java.awt.Color(255, 255, 255)); jLabel10.setText("Running under:"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(19, 19, 19) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE) .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE) .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2)))) .addComponent(jButton1))) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 773, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(20, 20, 20).addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(434, 434, 434) .addComponent(jButton4) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel9) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26).addComponent(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton1) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3).addComponent(jButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel5).addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel7).addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel10)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3).addComponent(jButton4)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed int result = jFileChooser1.showOpenDialog(this); if (result == 0) { jTextField2.setText(jFileChooser1.getSelectedFile().getAbsolutePath() + "\\"); l.trace("FC 1: " + result + ", chosen file: " + jFileChooser1.getSelectedFile().getAbsolutePath() + "\\"); } else if (result == 1) { l.trace("FC 1: " + result + ", choosing cancelled"); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed int result = jFileChooser2.showOpenDialog(this); if (result == 0) { jTextField3.setText(jFileChooser2.getSelectedFile().getAbsolutePath()); l.trace("FC 2: " + result + ", chosen file: " + jFileChooser2.getSelectedFile().getAbsolutePath()); } else if (result == 1) { l.trace("FC 2: " + result + ", choosing cancelled"); } }//GEN-LAST:event_jButton2ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed Object[] params = new Object[9]; params[0] = jTextField1.getText(); params[1] = jTextField2.getText(); params[2] = jTextField3.getText(); params[3] = jTextField4.getText(); params[4] = jTextArea1.getText(); params[5] = jTextField5.getText(); params[6] = jTextField6.getText(); params[7] = jTextField7.getText(); int verification = GamelistStorage.verifyConfig(params, isEditDialog); switch (verification) { case (-1): { break; } case (0): { JOptionPane.showMessageDialog(this, "Game name incorrect"); break; } case (1): { JOptionPane.showMessageDialog(this, "Game folder path incorrect"); break; } case (2): { JOptionPane.showMessageDialog(this, "Game executable path incorrect"); break; } case (3): { JOptionPane.showMessageDialog(this, "Game flags incorrect"); break; } case (4): { JOptionPane.showMessageDialog(this, "Bat execution list incorrect"); break; } } if (verification == -1) { game.gameName = jTextField1.getText(); game.gameAbsoluteFolderPath = jTextField2.getText(); game.gameAbsoluteExecPath = jTextField3.getText(); game.gameRuntimeFlags = jTextField4.getText(); game.gameBatCommands = jTextArea1.getText().split("\\r?\\n"); String relative = new File(jTextField2.getText()).toURI() .relativize(new File(jTextField3.getText()).toURI()).getPath(); game.gameRelativeExecPath = FilenameUtils.separatorsToWindows(relative); game.gamePName = jTextField5.getText(); game.gamePWName = jTextField6.getText(); game.gamePUser = jTextField7.getText(); /* String recreated = FilenameUtils.concat(g.gameAbsoluteFolderPath, FilenameUtils.separatorsToWindows(relative)); File f = new File(recreated); l.trace(recreated); l.trace(f.exists()); l.trace(f.getAbsolutePath()); * */ game.gameStatus = 0; l.trace("Game " + game.gameName + " created"); l.trace("Dir " + game.gameAbsoluteFolderPath); Shared.lastCreatedGame = game; l.trace(game.toString()); this.setVisible(false); /* this.removeAll(); this.dispose(); * */ } }//GEN-LAST:event_jButton4ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed l.trace("Game add cancelled"); Shared.lastCreatedGame = null; this.removeAll(); this.dispose(); }//GEN-LAST:event_jButton3ActionPerformed public Game popupCreateDialog() { isEditDialog = false; l.trace("Creating game add dialog"); /* dialog = new AddGameDialog(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { //System.exit(0); } }); * */ jTextField1.setText("tf2"); jTextField2.setText("C:\\_Team Fortress 2\\"); jTextField3.setText("C:\\_Team Fortress 2\\tf2.exe"); jTextField5.setText("hl2.exe"); jTextField6.setText("Team Fortress 2"); jTextField7.setText(""); l.trace("Setting game add dialog visible"); this.setVisible(true); l.trace("Dialog progress, (setVisible(true)) passed"); Game temp = null; if (Shared.lastCreatedGame != null) { temp = new Game(); Shared.lastCreatedGame.copyTo(temp); GamelistStorage.addGame(temp); } l.trace("Add game dialog done"); l.trace(temp); Shared.lastCreatedGame = null; return temp; } public Game popupEditDialog(Game original) { isEditDialog = true; l.trace("Creating game edit dialog"); /* dialog = new AddGameDialog(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { //System.exit(0); } }); * */ l.trace("Importing old game values"); //old game->form l.trace(original); jTextField1.setText(original.gameName); jTextField2.setText(original.gameAbsoluteFolderPath); jTextField3.setText(original.gameAbsoluteExecPath); jTextField4.setText(original.gameRuntimeFlags); jTextField5.setText(original.gamePName); jTextField6.setText(original.gamePWName); jTextField7.setText(original.gamePUser); StringBuilder sb = new StringBuilder(); for (String s : original.gameBatCommands) { sb.append(s); sb.append("\n"); } jTextArea1.setText(sb.toString()); //form->new game /* game.gameName = jTextField1.getText(); game.gameAbsoluteFolderPath = jTextField2.getText(); game.gameAbsoluteExecPath = jTextField3.getText(); game.gameRuntimeFlags = jTextField4.getText(); game.gameBatCommands = jTextArea1.getText().split("\r?\n"); game.gamePName = jTextField5.getText(); game.gamePWName = jTextField6.getText(); game.gamePUser = jTextField7.getText(); * */ original.copyTo(game); l.trace("Setting game edit dialog visible"); this.setVisible(true); l.trace("Dialog progress, (setVisible(true)) passed"); Game temp = null; if (Shared.lastCreatedGame != null) { temp = new Game(); Shared.lastCreatedGame.copyTo(temp); GamelistStorage.removeGame(temp.gameName); GamelistStorage.addGame(temp); } l.trace("Edit game dialog done"); l.trace(temp); Shared.lastCreatedGame = null; return temp; } /** * @param args the command line arguments */ public static void main(String args[]) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { // } java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { AddGameDialog dialog = new AddGameDialog(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JFileChooser jFileChooser1; private javax.swing.JFileChooser jFileChooser2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator3; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField6; private javax.swing.JTextField jTextField7; // End of variables declaration//GEN-END:variables }