Add configurator GUI

This commit is contained in:
Aelita4 2023-01-26 19:05:45 +01:00
parent 1d574b3ee1
commit a6728214b2
3 changed files with 400 additions and 6 deletions

View File

@ -1,31 +1,50 @@
package pl.mikorosa.dziecoin;
import pl.mikorosa.dziecoin.database.DatabaseConnection;
import pl.mikorosa.dziecoin.gui.ImportRuntime;
import pl.mikorosa.dziecoin.gui.MainFrame;
import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int difficulty;
public static Blockchain blockchain;
public static MainFrame mainFrame;
public static DatabaseConnection db;
public static MainFrame mainFrame;
public static void main(String[] args) {
db = new DatabaseConnection();
ImportRuntime importRuntime = new ImportRuntime();
importRuntime.setVisible(true);
}
public static void initImport(String blocksCSVPath, String transactionCSVPath, String NFTCSVPath, boolean autoImportToDatabase) {
blockchain = new Blockchain(blocksCSVPath, transactionCSVPath, NFTCSVPath, autoImportToDatabase);
mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
public static void initFromDatabase(boolean importToDatabase) {
blockchain = new Blockchain(importToDatabase);
mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
public static void initFromZero(boolean autoImportToDatabase) {
Wallet w1 = new Wallet();
Wallet w2 = new Wallet();
Wallet w3 = new Wallet();
difficulty = 4;
blockchain = new Blockchain(w1.getAddress(), false);
blockchain = new Blockchain(w1.getAddress(), autoImportToDatabase);
mainFrame = new MainFrame();
mainFrame.setVisible(true);
NFT n1 = new NFT(w1.getAddress(), "licencja na wozek widlowy");
NFT n2 = new NFT(w1.getAddress(), "legitymacja");
List<Transaction> t1 = new ArrayList<>();
t1.add(new Transaction(w1.getAddress(), w2.getAddress(), 5));
@ -33,13 +52,16 @@ public class Main {
Block b1 = new Block(blockchain.getLatestBlock().getHash(), blockchain.getLength() + 1, t1);
b1.addNFT(n1);
b1.addNFT(n2);
b1.mineBlock();
try {
blockchain.addBlock(b1);
} catch (BlockchainIntegrityException e) {
System.out.println("Blockchain Integrity Exception: " + e.getMessage());
JOptionPane.showMessageDialog(null, "Unable to add a block: " + e.getMessage(), "Fatal error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
List<Transaction> t2 = new ArrayList<>();
@ -52,7 +74,14 @@ public class Main {
try {
blockchain.addBlock(b2);
} catch (BlockchainIntegrityException e) {
System.out.println("Blockchain Integrity Exception: " + e.getMessage());
JOptionPane.showMessageDialog(null, "Unable to add a block: " + e.getMessage(), "Fatal error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
try {
blockchain.exportData(0b111);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,254 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="pl.mikorosa.dziecoin.gui.ImportRuntime">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="738" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="197a7" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1297" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Difficulty"/>
</properties>
</component>
<component id="f882a" class="javax.swing.JTextField" binding="difficulty">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="7e134" class="javax.swing.JButton" binding="runButton" default-binding="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Run"/>
</properties>
</component>
<component id="16627" class="javax.swing.JCheckBox" binding="updateDatabaseCheckBox" default-binding="true">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Update database?"/>
</properties>
</component>
</children>
</grid>
<grid id="7b5b3" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<tabbedpane id="38380" binding="tabbedPane1" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="f9e4d" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Create new"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="816e7" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="This will create:"/>
</properties>
</component>
<component id="b09ad" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="- 3 wallets"/>
</properties>
</component>
<component id="66f6f" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="- 2 NFTs (assigned to first wallet)"/>
</properties>
</component>
<component id="9d938" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="- 3 blocks consisting of 4 transactions"/>
</properties>
</component>
</children>
</grid>
<grid id="ebcc8" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Import from files"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="ad958" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="b7191" class="javax.swing.JButton" binding="browseBlock">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Browse for blocks.csv"/>
</properties>
</component>
<component id="58e66" class="javax.swing.JButton" binding="browseTransaction">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Browse for transactions.csv"/>
</properties>
</component>
<component id="a0011" class="javax.swing.JButton" binding="browseNFT">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Browse for nfts.csv"/>
</properties>
</component>
</children>
</grid>
<grid id="8fe77" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1be19" class="javax.swing.JLabel" binding="blockLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="No file selected"/>
</properties>
</component>
<component id="2a87d" class="javax.swing.JLabel" binding="transactionLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="No file selected"/>
</properties>
</component>
<component id="35e38" class="javax.swing.JLabel" binding="NFTLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="No file selected"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="975dd" layout-manager="GridLayoutManager" row-count="6" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Import from database"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7c5e2" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Database connection details:"/>
</properties>
</component>
<component id="1446d" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Type: MySQL"/>
</properties>
</component>
<component id="8786f" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Server: helium.mikorosa.pl"/>
</properties>
</component>
<component id="ac823" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Port: 3306"/>
</properties>
</component>
<component id="fe1f9" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Username: dziecoin"/>
</properties>
</component>
<component id="f43d3" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Database: dziecoin"/>
</properties>
</component>
</children>
</grid>
</children>
</tabbedpane>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,111 @@
package pl.mikorosa.dziecoin.gui;
import pl.mikorosa.dziecoin.Main;
import pl.mikorosa.dziecoin.database.DatabaseConnection;
import javax.swing.*;
import java.awt.*;
import static pl.mikorosa.dziecoin.Main.*;
public class ImportRuntime extends JFrame {
private JPanel panel;
private JTextField difficulty;
private JButton runButton;
private JTabbedPane tabbedPane1;
private JButton browseBlock;
private JButton browseTransaction;
private JButton browseNFT;
private JCheckBox updateDatabaseCheckBox;
private JLabel blockLabel;
private JLabel transactionLabel;
private JLabel NFTLabel;
private String blocksCSVPath;
private String transactionsCSVPath;
private String NFTsCSVPath;
public enum ImportType {
NEW,
FROM_FILE,
FROM_DATABASE
}
public ImportRuntime() {
super("DzieCoin Initial Configuration");
this.setContentPane(this.panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(640, 360));
this.pack();
runButton.addActionListener(e -> {
int diff = 0;
ImportType type = null;
String errorMessage = "";
String input;
try {
input = difficulty.getText();
if(input == null) throw new Exception();
diff = Integer.parseInt(input);
if(diff <= 0) throw new Exception();
} catch(Exception ex) {
errorMessage = "Input correct number";
}
if(tabbedPane1.getSelectedIndex() == 0) type = ImportType.NEW;
if(tabbedPane1.getSelectedIndex() == 1) type = ImportType.FROM_FILE;
if(tabbedPane1.getSelectedIndex() == 2) type = ImportType.FROM_DATABASE;
if(!errorMessage.isEmpty()) JOptionPane.showMessageDialog(null, errorMessage, "DzieCoin Initial Configuration", JOptionPane.ERROR_MESSAGE);
else if(type == ImportType.FROM_FILE) {
if(blocksCSVPath == null || transactionsCSVPath == null || NFTsCSVPath == null) JOptionPane.showMessageDialog(null, "Choose all three files to proceed", "DzieCoin Initial Configuration", JOptionPane.ERROR_MESSAGE);
else {
runButton.setEnabled(false);
Main.difficulty = diff;
Main.db = new DatabaseConnection();
this.setVisible(false);
this.dispose();
initImport(blocksCSVPath, transactionsCSVPath, NFTsCSVPath, updateDatabaseCheckBox.isSelected());
}
}
else {
runButton.setEnabled(false);
Main.difficulty = diff;
Main.db = new DatabaseConnection();
this.setVisible(false);
this.dispose();
if(type == ImportType.NEW) initFromZero(updateDatabaseCheckBox.isSelected());
if(type == ImportType.FROM_DATABASE) initFromDatabase(updateDatabaseCheckBox.isSelected());
}
});
browseBlock.addActionListener(e -> {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
blockLabel.setText(fc.getSelectedFile().getName());
blocksCSVPath = fc.getSelectedFile().getAbsolutePath();
});
browseTransaction.addActionListener(e -> {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
transactionLabel.setText(fc.getSelectedFile().getName());
transactionsCSVPath = fc.getSelectedFile().getAbsolutePath();
});
browseNFT.addActionListener(e -> {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
NFTLabel.setText(fc.getSelectedFile().getName());
NFTsCSVPath = fc.getSelectedFile().getAbsolutePath();
});
}
}