diff --git a/src/main/java/pl/mikorosa/dziecoin/Main.java b/src/main/java/pl/mikorosa/dziecoin/Main.java index 563d5e5..ad7a9d3 100644 --- a/src/main/java/pl/mikorosa/dziecoin/Main.java +++ b/src/main/java/pl/mikorosa/dziecoin/Main.java @@ -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 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 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); } } } \ No newline at end of file diff --git a/src/main/java/pl/mikorosa/dziecoin/gui/ImportRuntime.form b/src/main/java/pl/mikorosa/dziecoin/gui/ImportRuntime.form new file mode 100644 index 0000000..35d5c68 --- /dev/null +++ b/src/main/java/pl/mikorosa/dziecoin/gui/ImportRuntime.form @@ -0,0 +1,254 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/pl/mikorosa/dziecoin/gui/ImportRuntime.java b/src/main/java/pl/mikorosa/dziecoin/gui/ImportRuntime.java new file mode 100644 index 0000000..dfda3ce --- /dev/null +++ b/src/main/java/pl/mikorosa/dziecoin/gui/ImportRuntime.java @@ -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(); + }); + } +} \ No newline at end of file