qmod.generator.MainWindow.java Source code

Java tutorial

Introduction

Here is the source code for qmod.generator.MainWindow.java

Source

package qmod.generator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
import javax.swing.JEditorPane;

public class MainWindow extends JFrame {
    @SuppressWarnings("rawtypes")
    private JComboBox authCombo1 = new JComboBox<Object>();
    @SuppressWarnings("rawtypes")
    private JComboBox authCombo2 = new JComboBox<Object>();
    @SuppressWarnings("rawtypes")
    private JComboBox authCombo3 = new JComboBox<Object>();

    @SuppressWarnings("unchecked")
    public MainWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Quick Mod Generator");

        JLabel lblAuthors = new JLabel("Authors:");

        authorField1 = new JTextField();
        authorField1.setColumns(25);

        JButton btnGenerateFile = new JButton("Generate File");
        btnGenerateFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                writeFiles();
            }
        });

        authCombo1.setFont(new Font("Dialog", Font.BOLD, 8));
        authCombo1.setModel(
                (ComboBoxModel<Object>) new DefaultComboBoxModel<Object>(new String[] { "Author", "Maintainer" }));
        authCombo1.setSelectedIndex(0);

        authCombo2.setFont(new Font("Dialog", Font.BOLD, 8));
        authCombo2.setModel(
                (ComboBoxModel<Object>) new DefaultComboBoxModel<Object>(new String[] { "Author", "Maintainer" }));
        authCombo2.setSelectedIndex(0);

        authorField2 = new JTextField();
        authorField2.setColumns(25);

        authCombo3.setFont(new Font("Dialog", Font.BOLD, 8));
        authCombo3.setModel(
                (ComboBoxModel<Object>) new DefaultComboBoxModel<Object>(new String[] { "Author", "Maintainer" }));
        authCombo3.setSelectedIndex(0);

        authorField3 = new JTextField();
        authorField3.setColumns(25);

        JLabel lblCategories = new JLabel("Categories:");

        categoriesField = new JTextField();
        categoriesField.setColumns(10);

        JLabel lblDescription = new JLabel("Description:");

        JEditorPane descriptionField = new JEditorPane();

        JLabel lblLicense = new JLabel("License:");

        licenseField = new JTextField();
        licenseField.setColumns(10);
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup().addContainerGap()
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
                                .createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
                                        .createParallelGroup(Alignment.TRAILING).addComponent(btnGenerateFile)
                                        .addGroup(groupLayout.createSequentialGroup().addComponent(lblAuthors)
                                                .addPreferredGap(ComponentPlacement.RELATED).addGroup(groupLayout
                                                        .createParallelGroup(Alignment.LEADING).addGroup(groupLayout
                                                                .createSequentialGroup()
                                                                .addComponent(
                                                                        authCombo2, GroupLayout.PREFERRED_SIZE, 81,
                                                                        GroupLayout.PREFERRED_SIZE)
                                                                .addGap(6).addComponent(authorField2,
                                                                        GroupLayout.PREFERRED_SIZE, 192,
                                                                        GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup().addComponent(
                                                                authCombo1, GroupLayout.PREFERRED_SIZE, 81,
                                                                GroupLayout.PREFERRED_SIZE).addPreferredGap(
                                                                        ComponentPlacement.RELATED)
                                                                .addComponent(
                                                                        authorField1, GroupLayout.DEFAULT_SIZE, 264,
                                                                        Short.MAX_VALUE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(authCombo3,
                                                                        GroupLayout.PREFERRED_SIZE, 81,
                                                                        GroupLayout.PREFERRED_SIZE)
                                                                .addGap(6).addGroup(groupLayout.createParallelGroup(
                                                                        Alignment.LEADING).addComponent(
                                                                                descriptionField)
                                                                        .addGroup(groupLayout
                                                                                .createParallelGroup(
                                                                                        Alignment.LEADING, false)
                                                                                .addComponent(categoriesField)
                                                                                .addComponent(
                                                                                        authorField3,
                                                                                        GroupLayout.DEFAULT_SIZE,
                                                                                        192, Short.MAX_VALUE))
                                                                        .addGroup(groupLayout
                                                                                .createSequentialGroup()
                                                                                .addComponent(licenseField,
                                                                                        GroupLayout.DEFAULT_SIZE,
                                                                                        192, Short.MAX_VALUE)
                                                                                .addPreferredGap(
                                                                                        ComponentPlacement.RELATED)))))))
                                        .addComponent(lblCategories).addComponent(lblDescription))
                                .addGap(84))
                                .addGroup(groupLayout.createSequentialGroup().addComponent(lblLicense)
                                        .addContainerGap(377, Short.MAX_VALUE)))));
        groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
                .createSequentialGroup().addContainerGap()
                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblAuthors)
                        .addComponent(authorField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                        .addComponent(authCombo1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(authCombo2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createSequentialGroup().addGap(3).addComponent(authorField2,
                                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(authCombo3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                        .addGroup(groupLayout.createSequentialGroup().addGap(3).addComponent(authorField3,
                                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(lblCategories)
                        .addComponent(categoriesField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(lblDescription)
                        .addComponent(descriptionField, GroupLayout.PREFERRED_SIZE, 100,
                                GroupLayout.PREFERRED_SIZE))
                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup().addGap(9).addComponent(lblLicense))
                        .addGroup(groupLayout.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(licenseField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                        GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(ComponentPlacement.RELATED, 322, Short.MAX_VALUE).addComponent(btnGenerateFile)
                .addContainerGap()));
        getContentPane().setLayout(groupLayout);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        mnFile.setMnemonic('F');
        menuBar.add(mnFile);

        JMenuItem mntmNew = new JMenuItem("New");
        mntmNew.setMnemonic(KeyEvent.VK_N);
        mnFile.add(mntmNew);

        JMenuItem mntmOpen = new JMenuItem("Open");
        mnFile.add(mntmOpen);

        JMenuItem mntmSave = new JMenuItem("Save");
        mnFile.add(mntmSave);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        mntmExit.setMnemonic(KeyEvent.VK_X);
        mnFile.add(mntmExit);
    }

    private static final long serialVersionUID = 1L;
    private JTextField authorField1;
    private JTextField authorField2;
    private JTextField authorField3;
    private JTextField categoriesField;
    private JTextField licenseField;

    protected void writeFiles() {
        QModFile myFile = new QModFile();
        ObjectMapper mapper = new ObjectMapper();

        loadQModData(myFile);

        try {
            mapper.writerWithDefaultPrettyPrinter().writeValue(new File("/home/lewile1/Documents/QMOD.json"),
                    myFile.getFile());

            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(myFile.getFile()));
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void loadQModData(QModFile myFile) {
        if (authCombo1.getSelectedItem().equals("Author")) {
            if (!authorField1.getText().isEmpty()) {
                myFile.addAuthor(authorField1.getText());
            }
        } else {
            if (!authorField1.getText().isEmpty()) {
                myFile.addMaintainer(authorField1.getText());
            }
        }
        if (authCombo2.getSelectedItem().equals("Author")) {
            if (!authorField2.getText().isEmpty()) {
                myFile.addAuthor(authorField2.getText());
            }
        } else {
            if (!authorField2.getText().isEmpty()) {
                myFile.addMaintainer(authorField2.getText());
            }
        }
        if (authCombo2.getSelectedItem().equals("Author")) {
            if (!authorField3.getText().isEmpty()) {
                myFile.addAuthor(authorField3.getText());
            }
        } else {
            if (!authorField3.getText().isEmpty()) {
                myFile.addMaintainer(authorField3.getText());
            }
        }

    }
}