2.0
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package exopandora.worldhandler.installer;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import exopandora.worldhandler.main.Main;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ChangeFolderListener implements ActionListener
|
||||
{
|
||||
private final JTextField textField;
|
||||
|
||||
public ChangeFolderListener(JTextField textField)
|
||||
{
|
||||
this.textField = textField;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
File inital = new File(this.textField.getText());
|
||||
|
||||
if(inital.exists())
|
||||
{
|
||||
fileChooser.setCurrentDirectory(inital);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileChooser.setCurrentDirectory(Main.getInitialDirectory());
|
||||
}
|
||||
|
||||
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
fileChooser.showOpenDialog(null);
|
||||
|
||||
File file = fileChooser.getSelectedFile();
|
||||
|
||||
if(file != null)
|
||||
{
|
||||
this.textField.setText(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package exopandora.worldhandler.installer;
|
||||
|
||||
import java.awt.Desktop;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.net.URI;
|
||||
|
||||
import exopandora.worldhandler.main.Main;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ForumListener implements ActionListener
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
if(Desktop.isDesktopSupported())
|
||||
{
|
||||
try
|
||||
{
|
||||
Desktop.getDesktop().browse(new URI(Main.URL));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package exopandora.worldhandler.installer;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import exopandora.worldhandler.main.Main;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class InstallListener implements ActionListener
|
||||
{
|
||||
private final JFrame parent;
|
||||
private final JTextField textField;
|
||||
|
||||
public InstallListener(JFrame parent, JTextField textField)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.textField = textField;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event)
|
||||
{
|
||||
File directory = new File(this.textField.getText());
|
||||
|
||||
if(directory.isDirectory())
|
||||
{
|
||||
File mods = new File(directory, "mods");
|
||||
File versions = new File(directory, "versions");
|
||||
|
||||
if(!this.isPartialFileNameInFolder(versions, Main.MC_VERSION, "Forge"))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Please install Mineceaft Forge", null, JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(File file : mods.listFiles())
|
||||
{
|
||||
if(this.containsIgnoreCase(file.getName(), "World") && this.containsIgnoreCase(file.getName(), "Handler"))
|
||||
{
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Path path = new File(InstallListener.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).toPath();
|
||||
Files.copy(path, new File(mods, "WorldHandler-" + Main.FULL_VERSION + "-Universal.jar").toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
JOptionPane.showMessageDialog(null, Main.NAME_AND_VERSION + " has been successfully installed", null, JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Error:\n" + e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
this.parent.dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Invalid directory", null, JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containsIgnoreCase(String string, String sequence)
|
||||
{
|
||||
return string.toLowerCase().contains(sequence.toLowerCase());
|
||||
}
|
||||
|
||||
private boolean isPartialFileNameInFolder(File path, String... parts)
|
||||
{
|
||||
if(path.exists())
|
||||
{
|
||||
int contains = 0;
|
||||
|
||||
for(File file : path.listFiles())
|
||||
{
|
||||
for(String part : parts)
|
||||
{
|
||||
if(this.containsIgnoreCase(file.getName(), part))
|
||||
{
|
||||
contains++;
|
||||
}
|
||||
|
||||
if(contains == parts.length)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
194
src/main/java/exopandora/worldhandler/installer/Window.java
Normal file
194
src/main/java/exopandora/worldhandler/installer/Window.java
Normal file
@@ -0,0 +1,194 @@
|
||||
package exopandora.worldhandler.installer;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Container;
|
||||
import java.awt.FocusTraversalPolicy;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Image;
|
||||
import java.awt.Insets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import exopandora.worldhandler.main.Main;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class Window implements Runnable
|
||||
{
|
||||
private final String title = Main.NAME_AND_VERSION + " Installer";
|
||||
private final JFrame frame = new JFrame();
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
this.frame.setResizable(false);
|
||||
this.frame.setTitle(this.title);
|
||||
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
List<Image> icons = new ArrayList<Image>();
|
||||
|
||||
icons.add(new ImageIcon(this.getClass().getResource("/assets/worldhandler/installer/icon/icon16.png")).getImage());
|
||||
icons.add(new ImageIcon(this.getClass().getResource("/assets/worldhandler/installer/icon/icon32.png")).getImage());
|
||||
icons.add(new ImageIcon(this.getClass().getResource("/assets/worldhandler/installer/icon/icon64.png")).getImage());
|
||||
icons.add(new ImageIcon(this.getClass().getResource("/assets/worldhandler/installer/icon/icon128.png")).getImage());
|
||||
icons.add(new ImageIcon(this.getClass().getResource("/assets/worldhandler/installer/icon/icon256.png")).getImage());
|
||||
|
||||
this.frame.setIconImages(icons);
|
||||
this.frame.setLayout(new GridBagLayout());
|
||||
|
||||
JLabel logo = new JLabel();
|
||||
logo.setIcon(new ImageIcon(Window.class.getResource("/assets/worldhandler/installer/logo.png")));
|
||||
|
||||
GridBagConstraints gbcLogo = new GridBagConstraints();
|
||||
gbcLogo.insets = new Insets(15, 10, 15, 5);
|
||||
gbcLogo.gridx = 0;
|
||||
gbcLogo.gridy = 0;
|
||||
this.frame.add(logo, gbcLogo);
|
||||
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
|
||||
GridBagConstraints gbcPanel = new GridBagConstraints();
|
||||
gbcPanel.insets = new Insets(15, 5, 15, 10);
|
||||
gbcPanel.fill = GridBagConstraints.BOTH;
|
||||
gbcPanel.gridx = 1;
|
||||
gbcPanel.gridy = 0;
|
||||
this.frame.add(panel, gbcPanel);
|
||||
|
||||
JLabel title = new JLabel(this.title);
|
||||
panel.add(title, this.getButtonConstraints(0, 0));
|
||||
|
||||
JTextField textField = new JTextField(Main.getInitialDirectory().getAbsolutePath());
|
||||
textField.setCaretPosition(textField.getText().length());
|
||||
textField.setColumns(30);
|
||||
panel.add(textField, this.getButtonConstraints(0, 1));
|
||||
|
||||
JButton directory = new JButton("Change Minecraft Folder");
|
||||
directory.addActionListener(new ChangeFolderListener(textField));
|
||||
panel.add(directory, this.getButtonConstraints(0, 2));
|
||||
|
||||
JButton changelog = new JButton("Forum");
|
||||
changelog.addActionListener(new ForumListener());
|
||||
panel.add(changelog, this.getButtonConstraints(0, 3));
|
||||
|
||||
JButton install = new JButton("Install");
|
||||
install.addActionListener(new InstallListener(this.frame, textField));
|
||||
panel.add(install, this.getButtonConstraints(0, 4));
|
||||
|
||||
JButton exit = new JButton("Exit");
|
||||
exit.addActionListener(event -> this.frame.dispose());
|
||||
panel.add(exit, this.getButtonConstraints(0, 5));
|
||||
|
||||
this.frame.pack();
|
||||
this.frame.setLocationRelativeTo(null);
|
||||
this.frame.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{textField, directory, changelog, install, exit}));
|
||||
this.frame.setVisible(true);
|
||||
|
||||
install.requestFocusInWindow();
|
||||
}
|
||||
|
||||
private GridBagConstraints getButtonConstraints(int x, int y)
|
||||
{
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc.insets = new Insets(5, 5, 5, 5);
|
||||
gbc.gridx = x;
|
||||
gbc.gridy = y;
|
||||
|
||||
return gbc;
|
||||
}
|
||||
|
||||
private static final class FocusTraversalOnArray extends FocusTraversalPolicy
|
||||
{
|
||||
private final Component components[];
|
||||
|
||||
public FocusTraversalOnArray(Component components[])
|
||||
{
|
||||
this.components = components;
|
||||
}
|
||||
|
||||
private int indexCycle(int index, int delta)
|
||||
{
|
||||
int size = this.components.length;
|
||||
int next = (index + delta + size) % size;
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
private Component cycle(Component currentComponent, int delta)
|
||||
{
|
||||
int index = -1;
|
||||
|
||||
loop : for(int i = 0; i < this.components.length; i++)
|
||||
{
|
||||
Component component = this.components[i];
|
||||
|
||||
for(Component c = currentComponent; c != null; c = c.getParent())
|
||||
{
|
||||
if(component == c)
|
||||
{
|
||||
index = i;
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int initialIndex = index;
|
||||
|
||||
while(true)
|
||||
{
|
||||
int newIndex = this.indexCycle(index, delta);
|
||||
|
||||
if(newIndex == initialIndex)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
index = newIndex;
|
||||
|
||||
Component component = this.components[newIndex];
|
||||
|
||||
if(component.isEnabled() && component.isVisible() && component.isFocusable())
|
||||
{
|
||||
return component;
|
||||
}
|
||||
}
|
||||
|
||||
return currentComponent;
|
||||
}
|
||||
|
||||
public Component getComponentAfter(Container container, Component component)
|
||||
{
|
||||
return this.cycle(component, 1);
|
||||
}
|
||||
|
||||
public Component getComponentBefore(Container container, Component component)
|
||||
{
|
||||
return this.cycle(component, -1);
|
||||
}
|
||||
|
||||
public Component getFirstComponent(Container container)
|
||||
{
|
||||
return this.components[0];
|
||||
}
|
||||
|
||||
public Component getLastComponent(Container container)
|
||||
{
|
||||
return this.components[this.components.length - 1];
|
||||
}
|
||||
|
||||
public Component getDefaultComponent(Container container)
|
||||
{
|
||||
return this.getFirstComponent(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user