From 700eb34baecbda81bbe0ed0f2a2aff6df9b9b6dd Mon Sep 17 00:00:00 2001 From: agran Date: Thu, 28 May 2015 01:34:32 +0300 Subject: [PATCH] fix bug - AccountsComboBoxModel.java Now only updated when the wallet is fully synced. - The sound on each transaction now plays only once. - During synchronization, the list of messages is updated at the end. So right displays the number of confirmations. - Some optimizations. --- .../src/gui/models/AccountsComboBoxModel.java | 6 +- Qora/src/gui/models/MessagesTableModel.java | 87 ++++++++++--------- .../models/WalletTransactionsTableModel.java | 13 +-- Qora/src/utils/PlaySound.java | 86 ++++++++++++------ 4 files changed, 116 insertions(+), 76 deletions(-) diff --git a/Qora/src/gui/models/AccountsComboBoxModel.java b/Qora/src/gui/models/AccountsComboBoxModel.java index e858d622..2166b6d6 100644 --- a/Qora/src/gui/models/AccountsComboBoxModel.java +++ b/Qora/src/gui/models/AccountsComboBoxModel.java @@ -45,8 +45,9 @@ public void update(Observable o, Object arg) public synchronized void syncUpdate(Observable o, Object arg) { ObserverMessage message = (ObserverMessage) arg; - - if(message.getType() == ObserverMessage.ADD_BALANCE_TYPE || message.getType() == ObserverMessage.REMOVE_BALANCE_TYPE || message.getType() == ObserverMessage.ADD_ACCOUNT_TYPE || message.getType() == ObserverMessage.REMOVE_ACCOUNT_TYPE) + + if((message.getType() == ObserverMessage.NETWORK_STATUS && (int) message.getValue() == Controller.STATUS_OKE) + ||((Controller.getInstance().getStatus() == Controller.STATUS_OKE) && (message.getType() == ObserverMessage.ADD_BALANCE_TYPE || message.getType() == ObserverMessage.REMOVE_BALANCE_TYPE || message.getType() == ObserverMessage.ADD_ACCOUNT_TYPE || message.getType() == ObserverMessage.REMOVE_ACCOUNT_TYPE))) { //GET SELECTED ITEM Account selected = (Account) this.getSelectedItem(); @@ -66,6 +67,7 @@ public synchronized void syncUpdate(Observable o, Object arg) { this.setSelectedItem(selected); } + } } diff --git a/Qora/src/gui/models/MessagesTableModel.java b/Qora/src/gui/models/MessagesTableModel.java index b2a55995..8ff90080 100644 --- a/Qora/src/gui/models/MessagesTableModel.java +++ b/Qora/src/gui/models/MessagesTableModel.java @@ -41,6 +41,7 @@ import qora.account.Account; import qora.account.PrivateKeyAccount; +import qora.block.Block; import qora.crypto.AEScrypto; import qora.transaction.MessageTransaction; import qora.transaction.Transaction; @@ -81,23 +82,23 @@ public MessagesTableModel() topRenderer.setVerticalAlignment(DefaultTableCellRenderer.TOP); this.getColumn("").setCellRenderer( topRenderer ); - List> transaction = Controller.getInstance().getLastTransactions(10000); + List> transaction = Controller.getInstance().getLastTransactions(30000); - for (int i = 0; i < transaction.size(); i++) { - if(transaction.get(i).getB().getType() == Transaction.MESSAGE_TRANSACTION) + for (Pair pair : transaction) + { + if(pair.getB().getType() == Transaction.MESSAGE_TRANSACTION) { boolean is = false; - - for ( int j = messageBufs.size()-1; j >= 0; j-- ) - { - if(Arrays.equals(transaction.get(i).getB().getSignature(),messageBufs.get(j).getSignature())) + for (MessageBuf message : messageBufs) { + if(Arrays.equals(pair.getB().getSignature(), message.getSignature())) { is = true; + break; } } if(!is) { - addMessage(messageBufs.size(),(MessageTransaction)transaction.get(i).getB()); + addMessage(messageBufs.size(),(MessageTransaction)pair.getB()); } } } @@ -327,6 +328,7 @@ public synchronized void syncUpdate(Observable o, Object arg) { ObserverMessage message = (ObserverMessage) arg; + if(message.getType() == ObserverMessage.WALLET_STATUS) { int status = (int) message.getValue(); @@ -342,19 +344,14 @@ public synchronized void syncUpdate(Observable o, Object arg) { if(Controller.getInstance().getStatus() == Controller.STATUS_OKE) { - new Thread() - { - public void run() - { - try { - sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - updateBlock(); - } - }.start(); + this.repaint(); + } + else if (message.getType() == ObserverMessage.ADD_BLOCK_TYPE && + ((Block)message.getValue()).getHeight() == Controller.getInstance().getMaxPeerHeight()) + { + this.repaint(); } + } if(message.getType() == ObserverMessage.ADD_TRANSACTION_TYPE) @@ -364,10 +361,11 @@ public void run() { is = false; for ( int i = messageBufs.size()-1; i >= 0; i-- ) - { - if(Arrays.equals(((MessageTransaction) message.getValue()).getSignature(), messageBufs.get(i).getSignature())) + for (MessageBuf messageBuf : messageBufs) { + if(Arrays.equals(((MessageTransaction) message.getValue()).getSignature(), messageBuf.getSignature())) { is = true; + break; } } if(!is) @@ -405,7 +403,6 @@ private void addMessage(int pos, MessageTransaction transaction) transaction.getFee(), transaction.getSignature(), transaction.getCreator().getPublicKey(), - transaction.getConfirmations(), transaction.isText() )); } @@ -532,7 +529,7 @@ private void setHeight(int row) this.setRowHeight(row, textHeight); } - +/* private void updateBlock() { for (int j = 0; j < messageBufs.size(); j++) @@ -553,7 +550,7 @@ private void updateBlock() } this.repaint(); } - +*/ int lineCount( String text ) { int lineCount = 1; @@ -583,9 +580,8 @@ public class MessageBuf private BigDecimal amount; private BigDecimal fee; private byte[] signature; - private int confirmations; - public MessageBuf( byte[] rawMessage, boolean encrypted, String sender, String recipient, long timestamp, BigDecimal amount, BigDecimal fee, byte[] signature, byte[] senderPublicKey, int confirmations, boolean isText ) + public MessageBuf( byte[] rawMessage, boolean encrypted, String sender, String recipient, long timestamp, BigDecimal amount, BigDecimal fee, byte[] signature, byte[] senderPublicKey, boolean isText ) { this.rawMessage = rawMessage; this.encrypted = encrypted; @@ -599,7 +595,6 @@ public MessageBuf( byte[] rawMessage, boolean encrypted, String sender, String r this.senderPublicKey = senderPublicKey; this.recipientPublicKey = null; this.signature = signature; - this.confirmations = confirmations; this.isText = isText; } @@ -676,12 +671,16 @@ public void setDecryptedMessage(String decryptedMessage) } public int getConfirmations() { - return this.confirmations; - } - public void setConfirmations(int confirmations) - { - this.confirmations = confirmations; + if( DBSet.getInstance().getTransactionMap().contains(this.signature) ) + { + return 0; + } + else + { + return Controller.getInstance().getTransaction(this.signature).getConfirmations(); + } } + public boolean isText() { return isText; @@ -721,11 +720,13 @@ public String getDecrMessageHtml(int width, boolean selected, boolean images) imglock = ""; } - String confirmations = Integer.toString( this.confirmations ); + int confirmations = getConfirmations(); + + String strconfirmations = Integer.toString( confirmations ); - if( this.confirmations < 1 ) + if( confirmations < 1 ) { - confirmations = "" + confirmations +""; + strconfirmations = "" + strconfirmations +""; } String colorHeader = "F0F0F0"; @@ -764,7 +765,7 @@ public String getDecrMessageHtml(int width, boolean selected, boolean images) + "\n
\nTo:" + recipient+"\n\n" + "\n" - + "\n" + confirmations + " . " + + "\n" + strconfirmations + " . " + format.format(date) + "\n
\n" + "Amount: " + amount.toPlainString()+" Fee: " + fee.toPlainString() @@ -813,12 +814,14 @@ public String getDecrMessageTXT() { imglock = "Unencrypted"; } - - String confirmations = Integer.toString( this.confirmations ); - if( this.confirmations < 1 ) + int confirmations = getConfirmations(); + + String strconfirmations = Integer.toString( confirmations ); + + if( confirmations < 1 ) { - confirmations = confirmations + " !"; + strconfirmations = strconfirmations + " !"; } return "Date: " + format.format(date) + "\n" @@ -826,7 +829,7 @@ public String getDecrMessageTXT() + "Recipient: " + recipient + "\n" + "Amount: " + amount.toPlainString() + " Fee: " + fee.toPlainString() + "\n" + "Type: " + imginout + ". " + imglock + "\n" - + "Confirmations: " + confirmations + "\n" + + "Confirmations: " + strconfirmations + "\n" + "[MESSAGE START]\n" + getDecrMessage() + "\n" + "[MESSAGE END]\n"; diff --git a/Qora/src/gui/models/WalletTransactionsTableModel.java b/Qora/src/gui/models/WalletTransactionsTableModel.java index a4124d63..c4226553 100644 --- a/Qora/src/gui/models/WalletTransactionsTableModel.java +++ b/Qora/src/gui/models/WalletTransactionsTableModel.java @@ -8,6 +8,7 @@ import org.mapdb.Fun.Tuple2; import controller.Controller; +import database.DBSet; import database.SortableList; import database.wallet.TransactionMap; import qora.account.Account; @@ -151,7 +152,7 @@ public synchronized void syncUpdate(Observable o, Object arg) if(Controller.getInstance().getStatus() == Controller.STATUS_OKE && message.getType() == ObserverMessage.ADD_TRANSACTION_TYPE) { - if(((Transaction) message.getValue()).getParent() == null) + if(DBSet.getInstance().getTransactionMap().contains(((Transaction) message.getValue()).getSignature())) { if(((Transaction) message.getValue()).getType() == Transaction.PAYMENT_TRANSACTION) { @@ -160,12 +161,12 @@ public synchronized void syncUpdate(Observable o, Object arg) { if(Settings.getInstance().isSoundReceivePaymentEnabled()) { - PlaySound.playSound("receivepayment.wav"); + PlaySound.getInstance().playSound("receivepayment.wav", ((Transaction) message.getValue()).getSignature()); } } else if(Settings.getInstance().isSoundNewTransactionEnabled()) { - PlaySound.playSound("newtransaction.wav"); + PlaySound.getInstance().playSound("newtransaction.wav", ((Transaction) message.getValue()).getSignature()); } } else if(((Transaction) message.getValue()).getType() == Transaction.MESSAGE_TRANSACTION) @@ -175,17 +176,17 @@ else if(((Transaction) message.getValue()).getType() == Transaction.MESSAGE_TRAN { if(Settings.getInstance().isSoundReceiveMessageEnabled()) { - PlaySound.playSound("receivemessage.wav"); + PlaySound.getInstance().playSound("receivemessage.wav", ((Transaction) message.getValue()).getSignature()) ; } } else if(Settings.getInstance().isSoundNewTransactionEnabled()) { - PlaySound.playSound("newtransaction.wav"); + PlaySound.getInstance().playSound("newtransaction.wav", ((Transaction) message.getValue()).getSignature()); } } else if(Settings.getInstance().isSoundNewTransactionEnabled()) { - PlaySound.playSound("newtransaction.wav"); + PlaySound.getInstance().playSound("newtransaction.wav", ((Transaction) message.getValue()).getSignature()); } } } diff --git a/Qora/src/utils/PlaySound.java b/Qora/src/utils/PlaySound.java index 73126638..6c4d6f3c 100644 --- a/Qora/src/utils/PlaySound.java +++ b/Qora/src/utils/PlaySound.java @@ -2,6 +2,8 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; @@ -10,32 +12,64 @@ import javax.sound.sampled.UnsupportedAudioFileException; public class PlaySound { - public static void playSound(final String url) { - new Thread(new Runnable() { - - public void run() { - - try { - File soundFile = new File( "sounds/" + url ); - - AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); - - Clip clip = AudioSystem.getClip(); - - clip.open(ais); - - clip.setFramePosition(0); - clip.start(); - - Thread.sleep(clip.getMicrosecondLength()/1000); - clip.stop(); - clip.close(); - } catch(IOException | UnsupportedAudioFileException | LineUnavailableException exc) { - exc.printStackTrace(); - } catch (InterruptedException exc) {} - - } - }).start(); + private static PlaySound instance; + private ArrayList transactionsAlreadyPlayed; + + public static PlaySound getInstance() + { + if(instance == null) + { + instance = new PlaySound(); + } + + return instance; + } + + public PlaySound() + { + transactionsAlreadyPlayed = new ArrayList(); + } + + public void playSound(final String url, byte[] signature) { + + boolean is = false; + + for (byte[] transactionSign : transactionsAlreadyPlayed) { + if(Arrays.equals(transactionSign, signature)) + { + is = true; + break; + } + } + + if(!is) + { + transactionsAlreadyPlayed.add(0, signature); + + new Thread(new Runnable() { + public void run() { + try { + File soundFile = new File( "sounds/" + url ); + + AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); + + Clip clip = AudioSystem.getClip(); + + clip.open(ais); + + clip.setFramePosition(0); + clip.start(); + + Thread.sleep(clip.getMicrosecondLength()/1000); + clip.stop(); + clip.close(); + } catch(IOException | UnsupportedAudioFileException | LineUnavailableException exc) { + exc.printStackTrace(); + } catch (InterruptedException exc) {} + + } + }).start(); } + } }