diff --git a/src/com/elezeta/jarspliceplus/ConfigParser.java b/src/com/elezeta/jarspliceplus/ConfigParser.java new file mode 100644 index 0000000..879dcf2 --- /dev/null +++ b/src/com/elezeta/jarspliceplus/ConfigParser.java @@ -0,0 +1,142 @@ +package com.elezeta.jarspliceplus; + +import java.io.File; + +import javax.swing.JOptionPane; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.ninjacave.jarsplice.gui.JarSpliceFrame; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +public class ConfigParser { + + public static final String extension = ".xml"; + protected static final String el_root = "JarSplicePlusConfig"; + protected static final String el_jar = "Jar"; + protected static final String el_native = "Native"; + protected static final String at_path = "Path"; + protected static final String at_main = "MainClass"; + protected static final String at_vmArgs = "VM_Arguments"; + + protected final JarSpliceFrame jarSpliceFrame; + + public ConfigParser(JarSpliceFrame frame) { + this.jarSpliceFrame = frame; + } + + public void saveXMLFile(File file) { + try { + // create document : + DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); + + // define root elements : + Document document = documentBuilder.newDocument(); + Element rootElement = document.createElement(el_root); + document.appendChild(rootElement); + + // Save jars : + String[] jarFiles = jarSpliceFrame.getJarsList(); + if(jarFiles != null) { + int nbJars = jarFiles.length; + for(int i=0; i
%s
", new Object[] { - Integer.valueOf(300), - "Enter your applications main class file below, complete with any packages that it maybe in.

e.g. mypackage.someotherpackage.MainClass
" })); - - centerPanel1.add(label); - mainClassBox.add(centerPanel1, "First"); - - JPanel centerPanel2 = new JPanel(); - this.classTextField = new JTextField(); - this.classTextField.setPreferredSize(new Dimension(400, 30)); - this.classTextField.setMinimumSize(new Dimension(400, 30)); - this.classTextField.setMaximumSize(new Dimension(400, 30)); - this.classTextField.setText(""); - centerPanel2.add(this.classTextField); - - mainClassBox.add(centerPanel2, "Center"); - - JPanel centerPanel3 = new JPanel(); - centerPanel3.setLayout(new FlowLayout(2)); - centerPanel3.add(this.optionsButton); - - mainClassBox.add(centerPanel3, "Last"); - - return mainClassBox; - } - - protected JPanel getVmArgBox() { - JPanel vmArgBox = new JPanel(); - vmArgBox.setLayout(new BorderLayout(5, 5)); - TitledBorder border2 = BorderFactory.createTitledBorder("Set VM Arguments"); - vmArgBox.setBorder(border2); - - JPanel centerPanel3 = new JPanel(); - JLabel label2 = new JLabel(); - label2.setText( - String.format("
%s
", new Object[] { - Integer.valueOf(300), - "Enter any java VM arguments that you would like to start the java virtual machine with. Leave blank if you are unsure whether you need to enter something here.

e.g. -Xms128m -Xmx512m
" })); - - centerPanel3.add(label2); - vmArgBox.add(centerPanel3, "First"); - - JPanel centerVmArgPanel = new JPanel(); - centerVmArgPanel.setLayout(new BorderLayout(5, 5)); - - JPanel centerPanel4 = new JPanel(); - this.vmTextField = new JTextField(); - this.vmTextField.setPreferredSize(new Dimension(400, 30)); - this.vmTextField.setMinimumSize(new Dimension(400, 30)); - this.vmTextField.setMaximumSize(new Dimension(400, 30)); - this.vmTextField.setText(""); - centerPanel4.add(this.vmTextField); - - centerVmArgPanel.add(centerPanel4, "First"); - - JPanel centerPanel5 = new JPanel(); - JLabel label3 = new JLabel(); - label3.setText( - String.format("
%s
", new Object[] { - Integer.valueOf(300), - "Note: Do not use the -cp and -Djava.library.path VM arguments as JarSplice uses them internally and adds them automatically.
" })); - - centerPanel5.add(label3); - centerVmArgPanel.add(centerPanel5, "Center"); - - vmArgBox.add(centerVmArgPanel, "Center"); - - return vmArgBox; - } - - public String getMainClass() { - return this.classTextField.getText(); - } - - public String getVmArgs() { - return this.vmTextField.getText(); - } - - public void actionPerformed(ActionEvent e) { - if (e.getSource() == this.optionsButton) - if (this.vmArgBox.isVisible()) { - this.vmArgBox.setVisible(false); - this.optionsButton.setText("Show Options"); - } - else { - this.vmArgBox.setVisible(true); - this.optionsButton.setText("Hide Options"); - } - } + protected JPanel getMainClassBox() { + JPanel mainClassBox = new JPanel(); + mainClassBox.setLayout(new BorderLayout(5, 5)); + TitledBorder border1 = BorderFactory.createTitledBorder("Enter Main Class"); + mainClassBox.setBorder(border1); + + JPanel centerPanel1 = new JPanel(); + JLabel label = new JLabel(); + label.setText( + String.format("
%s
", new Object[] { + Integer.valueOf(300), + "Enter your applications main class file below, complete with any packages that it maybe in.

e.g. mypackage.someotherpackage.MainClass
" })); + + centerPanel1.add(label); + mainClassBox.add(centerPanel1, "First"); + + JPanel centerPanel2 = new JPanel(); + this.classTextField = new JTextField(); + this.classTextField.setPreferredSize(new Dimension(400, 30)); + this.classTextField.setMinimumSize(new Dimension(400, 30)); + this.classTextField.setMaximumSize(new Dimension(400, 30)); + this.classTextField.setText(""); + centerPanel2.add(this.classTextField); + + mainClassBox.add(centerPanel2, "Center"); + + JPanel centerPanel3 = new JPanel(); + centerPanel3.setLayout(new FlowLayout(2)); + centerPanel3.add(this.optionsButton); + + mainClassBox.add(centerPanel3, "Last"); + + return mainClassBox; + } + + protected JPanel getVmArgBox() { + JPanel vmArgBox = new JPanel(); + vmArgBox.setLayout(new BorderLayout(5, 5)); + TitledBorder border2 = BorderFactory.createTitledBorder("Set VM Arguments"); + vmArgBox.setBorder(border2); + + JPanel centerPanel3 = new JPanel(); + JLabel label2 = new JLabel(); + label2.setText( + String.format("
%s
", new Object[] { + Integer.valueOf(300), + "Enter any java VM arguments that you would like to start the java virtual machine with. Leave blank if you are unsure whether you need to enter something here.

e.g. -Xms128m -Xmx512m
" })); + + centerPanel3.add(label2); + vmArgBox.add(centerPanel3, "First"); + + JPanel centerVmArgPanel = new JPanel(); + centerVmArgPanel.setLayout(new BorderLayout(5, 5)); + + JPanel centerPanel4 = new JPanel(); + this.vmTextField = new JTextField(); + this.vmTextField.setPreferredSize(new Dimension(400, 30)); + this.vmTextField.setMinimumSize(new Dimension(400, 30)); + this.vmTextField.setMaximumSize(new Dimension(400, 30)); + this.vmTextField.setText(""); + centerPanel4.add(this.vmTextField); + + centerVmArgPanel.add(centerPanel4, "First"); + + JPanel centerPanel5 = new JPanel(); + JLabel label3 = new JLabel(); + label3.setText( + String.format("
%s
", new Object[] { + Integer.valueOf(300), + "Note: Do not use the -cp and -Djava.library.path VM arguments as JarSplice uses them internally and adds them automatically.
" })); + + centerPanel5.add(label3); + centerVmArgPanel.add(centerPanel5, "Center"); + + vmArgBox.add(centerVmArgPanel, "Center"); + + return vmArgBox; + } + + public String getMainClass() { + return this.classTextField.getText(); + } + + public void setMainClass(String mainClass) { + this.classTextField.setText(mainClass); + } + + public String getVmArgs() { + return this.vmTextField.getText(); + } + + public void setVmArgs(String vmArgs) { + this.vmTextField.setText(vmArgs); + } + + public void actionPerformed(ActionEvent e) { + if (e.getSource() == this.optionsButton) + setVMBoxVisible(!vmArgBox.isVisible()); + } + + public void setVMBoxVisible(boolean visible) { + if (!visible) { + this.vmArgBox.setVisible(false); + this.optionsButton.setText("Show Options"); + } + else { + this.vmArgBox.setVisible(true); + this.optionsButton.setText("Hide Options"); + } + } } diff --git a/src/org/ninjacave/jarsplice/gui/JarSpliceFrame.java b/src/org/ninjacave/jarsplice/gui/JarSpliceFrame.java index 663eed6..a7cb125 100644 --- a/src/org/ninjacave/jarsplice/gui/JarSpliceFrame.java +++ b/src/org/ninjacave/jarsplice/gui/JarSpliceFrame.java @@ -1,59 +1,183 @@ package org.ninjacave.jarsplice.gui; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.io.File; + +import javax.swing.JFileChooser; import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; + +import com.elezeta.jarspliceplus.ConfigParser; public class JarSpliceFrame { - IntroductionPanel introPanel = new IntroductionPanel(); - JarsPanel jarsPanel = new JarsPanel(this); - NativesPanel nativesPanel = new NativesPanel(this); - ClassPanel classPanel = new ClassPanel(this); - CreatePanel createPanel = new CreatePanel(this); - ShellScriptPanel shellScriptPanel = new ShellScriptPanel(this); - MacAppPanel macAppPanel = new MacAppPanel(this); - WinExePanel exePanel = new WinExePanel(this); - public File lastDirectory; - - public JarSpliceFrame() - { - - //TITLE MODIFIED AS TO STATE THAT JARSPLICEPLUS IS AN EXTENSION TO JARSPLICE - - JFrame frame = new JFrame("JarSplicePlus - An Extension to JarSplice"); - - TabPane tabPane = new TabPane(this); - - tabPane.addTab("INTRODUCTION", this.introPanel, true); - tabPane.addTab("1) ADD JARS", this.jarsPanel, true); - tabPane.addTab("2) ADD NATIVES", this.nativesPanel, true); - tabPane.addTab("3) MAIN CLASS", this.classPanel, true); - tabPane.addTab("4) CREATE FAT JAR", this.createPanel, true); - - tabPane.addTab("EXTRA (LINUX .SH)", this.shellScriptPanel, false); - tabPane.addTab("EXTRA (MAC .APP)", this.macAppPanel, false); - tabPane.addTab("EXTRA (WINDOWS .EXE)", this.exePanel, false); - - frame.add(tabPane, "Center"); - - frame.setSize(640, 480); - frame.setVisible(true); - frame.setDefaultCloseOperation(3); - } - - public String[] getJarsList() { - return this.jarsPanel.getSelectedFiles(); - } - - public String[] getNativesList() { - return this.nativesPanel.getSelectedFiles(); - } - - public String getMainClass() { - return this.classPanel.getMainClass(); - } - - public String getVmArgs() { - return this.classPanel.getVmArgs(); - } + final JFrame frame; + IntroductionPanel introPanel = new IntroductionPanel(); + JarsPanel jarsPanel = new JarsPanel(this); + NativesPanel nativesPanel = new NativesPanel(this); + ClassPanel classPanel = new ClassPanel(this); + CreatePanel createPanel = new CreatePanel(this); + ShellScriptPanel shellScriptPanel = new ShellScriptPanel(this); + MacAppPanel macAppPanel = new MacAppPanel(this); + WinExePanel exePanel = new WinExePanel(this); + public File lastDirectory; + + public JarSpliceFrame() + { + // TITLE MODIFIED AS TO STATE THAT JARSPLICEPLUS IS AN EXTENSION TO JARSPLICE : + frame = new JFrame("JarSplicePlus - An Extension to JarSplice"); + + TabPane tabPane = new TabPane(this); + + tabPane.addTab("INTRODUCTION", this.introPanel, true); + tabPane.addTab("1) ADD JARS", this.jarsPanel, true); + tabPane.addTab("2) ADD NATIVES", this.nativesPanel, true); + tabPane.addTab("3) MAIN CLASS", this.classPanel, true); + tabPane.addTab("4) CREATE FAT JAR", this.createPanel, true); + + tabPane.addTab("EXTRA (LINUX .SH)", this.shellScriptPanel, false); + tabPane.addTab("EXTRA (MAC .APP)", this.macAppPanel, false); + tabPane.addTab("EXTRA (WINDOWS .EXE)", this.exePanel, false); + + frame.add(tabPane, "Center"); + + // Menu : + createMenu(); + + frame.setSize(680, 490); + frame.setVisible(true); + frame.setLocationRelativeTo(null); // center frame on screen + frame.setDefaultCloseOperation(3); + } + + public JFrame getFrame() { + return frame; + } + + protected void createMenu() { + JMenuBar menuBar = new JMenuBar(); + frame.setJMenuBar(menuBar); + + JMenu fileMenu = new JMenu("File"); + menuBar.add(fileMenu); + JMenuItem loadItem = new JMenuItem("Load configuration"); + JMenuItem saveItem = new JMenuItem("Save configuration"); + fileMenu.add(loadItem); + fileMenu.add(saveItem); + + loadItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + loadConfig(); + } + }); + + saveItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + saveConfig(); + } + }); + } + + public String[] getJarsList() { + return this.jarsPanel.getSelectedFiles(); + } + + public String[] getNativesList() { + return this.nativesPanel.getSelectedFiles(); + } + + public String getMainClass() { + return this.classPanel.getMainClass(); + } + + public String getVmArgs() { + return this.classPanel.getVmArgs(); + } + + public void setMainClass(String mainClass) { + this.classPanel.setMainClass(mainClass); + } + + public void setVmArgs(String vmArgs) { + this.classPanel.setVmArgs(vmArgs); + } + + public void setSelectedJars(String[] files) { + jarsPanel.setSelectedFiles(files); + } + + public void setSelectedNatives(String[] files) { + nativesPanel.setSelectedFiles(files); + } + + protected void loadConfig() { + JFileChooser chooser = new JFileChooser(); + chooser.setCurrentDirectory(lastDirectory); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setDialogTitle("Select the file to load"); + chooser.addChoosableFileFilter(getConfigFileFilter()); + + if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { + File selectedFile = chooser.getSelectedFile(); + if(selectedFile.exists()) { + new ConfigParser(this).loadXMLFile(selectedFile); + } + } + + // save directory : + lastDirectory = chooser.getCurrentDirectory(); + } + + protected void saveConfig() { + JFileChooser chooser = new JFileChooser(); + chooser.setCurrentDirectory(lastDirectory); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setDialogTitle("Select the file to save"); + chooser.setSelectedFile(new File("JarSpliceConfig" + ConfigParser.extension)); + chooser.addChoosableFileFilter(getConfigFileFilter()); + + if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) { + File selectedFile = chooser.getSelectedFile(); + if(selectedFile.exists()) { + int option = JOptionPane.showConfirmDialog(frame, "File already exists, overwrite it ?", "", JOptionPane.YES_NO_OPTION ); + if(option != JOptionPane.YES_OPTION) + return; + if(!selectedFile.delete()) { + JOptionPane.showMessageDialog(frame, "ERROR, could not overwrite file !", "", JOptionPane.ERROR_MESSAGE); + } + } + + // add extension if needed : + if(selectedFile.getName().toLowerCase().endsWith(ConfigParser.extension) == false) + selectedFile = new File(selectedFile.getAbsolutePath() + ConfigParser.extension); + + new ConfigParser(this).saveXMLFile(selectedFile); + } + + // save directory : + lastDirectory = chooser.getCurrentDirectory(); + } + + protected FileFilter getConfigFileFilter() { + return new FileFilter() { + @Override + public String getDescription() { + return ConfigParser.extension; + } + + @Override + public boolean accept(File file) { + return file != null && file.getName().toLowerCase().endsWith(ConfigParser.extension); + } + }; + } } diff --git a/src/org/ninjacave/jarsplice/gui/JarsPanel.java b/src/org/ninjacave/jarsplice/gui/JarsPanel.java index 25f84de..dc3b4cf 100644 --- a/src/org/ninjacave/jarsplice/gui/JarsPanel.java +++ b/src/org/ninjacave/jarsplice/gui/JarsPanel.java @@ -77,6 +77,20 @@ public String[] getSelectedFiles() { return files; } + + public void setSelectedFiles(String[] files) { + listModel.removeAllElements(); + selectedFiles = null; + + if(files != null) { + int nbFiles = files.length; + selectedFiles = new File[nbFiles]; + for(int i=0; i