/*
* SetConfig.java
*
* Created on July 10, 2007, 4:59 PM
*/
package editor;
import eug.parser.EUGFileIO;
import eug.parser.ParserSettings;
import eug.shared.GenericObject;
import eug.shared.Style;
import java.awt.event.ItemEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author Michael Myers
* @since 0.5pre2
*/
class SetConfig extends javax.swing.JFrame {
private static final String CONFIG_PATH = "config.txt";
private GenericObject config;
private boolean hadModDir;
/** Creates new form SetConfig */
public SetConfig() {
initComponents();
loadData();
setLocationRelativeTo(null);
}
private void loadData() {
config = EUGFileIO.load(CONFIG_PATH, ParserSettings.getDefaults().setPrintTimingInfo(false));
mainDirTextField.setText(config.getString("maindir").replaceAll("[/\\\\]", "/"));
modDirTextField.setText(config.getString("moddir").replaceAll("[/\\\\]", "/"));
hadModDir = config.hasString("moddir");
useModDirCheckBox.setSelected(hadModDir);
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
mainDirTextField = new javax.swing.JTextField();
javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
modDirTextField = new javax.swing.JTextField();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
useModDirCheckBox = new javax.swing.JCheckBox();
browseButton = new javax.swing.JButton();
FormListener formListener = new FormListener();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("Config");
addWindowListener(formListener);
jLabel1.setText("Main directory: ");
jLabel2.setText("Mod name (not full path):");
okButton.setText("OK");
okButton.addActionListener(formListener);
cancelButton.setText("Cancel");
cancelButton.addActionListener(formListener);
useModDirCheckBox.setText("Use mod directory");
useModDirCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
useModDirCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
useModDirCheckBox.addItemListener(formListener);
browseButton.setText("Browse");
browseButton.addActionListener(formListener);
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(useModDirCheckBox)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(modDirTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(mainDirTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(browseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(browseButton)
.addComponent(mainDirTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(modDirTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(useModDirCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(okButton))
.addContainerGap())
);
pack();
}
// Code for dispatching events from components to event handlers.
private class FormListener implements java.awt.event.ActionListener, java.awt.event.ItemListener, java.awt.event.WindowListener {
FormListener() {}
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == okButton) {
SetConfig.this.okButtonActionPerformed(evt);
}
else if (evt.getSource() == cancelButton) {
SetConfig.this.cancelButtonActionPerformed(evt);
}
else if (evt.getSource() == browseButton) {
SetConfig.this.browseButtonActionPerformed(evt);
}
}
public void itemStateChanged(java.awt.event.ItemEvent evt) {
if (evt.getSource() == useModDirCheckBox) {
SetConfig.this.useModDirCheckBoxItemStateChanged(evt);
}
}
public void windowActivated(java.awt.event.WindowEvent evt) {
}
public void windowClosed(java.awt.event.WindowEvent evt) {
}
public void windowClosing(java.awt.event.WindowEvent evt) {
if (evt.getSource() == SetConfig.this) {
SetConfig.this.formWindowClosing(evt);
}
}
public void windowDeactivated(java.awt.event.WindowEvent evt) {
}
public void windowDeiconified(java.awt.event.WindowEvent evt) {
}
public void windowIconified(java.awt.event.WindowEvent evt) {
}
public void windowOpened(java.awt.event.WindowEvent evt) {
}
}// </editor-fold>//GEN-END:initComponents
private void useModDirCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useModDirCheckBoxItemStateChanged
modDirTextField.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
}//GEN-LAST:event_useModDirCheckBoxItemStateChanged
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
JFileChooser chooser = new JFileChooser(mainDirTextField.getText());
chooser.setDialogTitle("Select the main EU3 directory");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int check = chooser.showDialog(this, "Select");
if (check == JFileChooser.APPROVE_OPTION) {
mainDirTextField.setText(chooser.getSelectedFile().getAbsolutePath().replaceAll("[/\\\\]", "/"));
}
}//GEN-LAST:event_browseButtonActionPerformed
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
close(true);
}//GEN-LAST:event_okButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
close(false);
}//GEN-LAST:event_cancelButtonActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
close(false);
}//GEN-LAST:event_formWindowClosing
private void close(boolean saveChanges) {
if (saveChanges) {
if (!setMainDir())
return;
if (!setModDir())
return;
saveConfig();
}
dispose();
}
private boolean setMainDir() {
File mainDir = new File(mainDirTextField.getText());
if (!mainDir.exists()) {
int check = JOptionPane.showConfirmDialog(this,
"The main directory does not exist. Do you want to continue?",
"Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (check == JOptionPane.NO_OPTION)
return false;
else {
config.setString("maindir", mainDirTextField.getText().replaceAll("[/\\\\]", "/"), true);
return true;
}
} else {
config.setString("maindir", mainDirTextField.getText().replaceAll("[/\\\\]", "/"), true);
return true;
}
}
private boolean setModDir() {
if (!useModDirCheckBox.isSelected()) {
if (hadModDir)
config.removeVariable("moddir");
return true;
} else {
StringBuilder filename = new StringBuilder(mainDirTextField.getText());
char last = filename.charAt(filename.length()-1);
if (last != '/' && last != '\\')
filename.append(File.separatorChar);
String mainDir = filename.toString(); // in case the next operation fails
File modDir = new File(filename.append(modDirTextField.getText()).toString());
if (!modDir.exists()) {
modDir = new File(mainDir + "mod" + File.separatorChar + modDirTextField.getText());
}
if (!modDir.exists()) {
int check = JOptionPane.showConfirmDialog(this,
"The mod directory does not exist. Do you want to continue?",
"Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (check == JOptionPane.NO_OPTION)
return false;
else {
config.setString("moddir", modDirTextField.getText().replaceAll("[/\\\\]", "/"), true);
return true;
}
} else {
config.setString("moddir", modDirTextField.getText().replaceAll("[/\\\\]", "/"), true);
return true;
}
}
}
private void saveConfig() {
EUGFileIO.save(config, CONFIG_PATH, EUGFileIO.NO_COMMENT, true, Style.AGCEEP);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SetConfig().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton browseButton;
private javax.swing.JButton cancelButton;
private javax.swing.JTextField mainDirTextField;
private javax.swing.JTextField modDirTextField;
private javax.swing.JButton okButton;
private javax.swing.JCheckBox useModDirCheckBox;
// End of variables declaration//GEN-END:variables
}