package gui.models;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;

import qora.assets.Asset;
import utils.ObserverMessage;
import controller.Controller;

@SuppressWarnings("serial")
public class AssetsComboBoxModel extends DefaultComboBoxModel<Asset> implements Observer {

	public AssetsComboBoxModel()
	{
		Controller.getInstance().addWalletListener(this);
	}
	
	@Override
	public void update(Observable o, Object arg) 
	{
		try
		{
			this.syncUpdate(o, arg);
		}
		catch(Exception e)
		{
			//GUI ERROR
		}
	}
	
	@SuppressWarnings("unchecked")
	public synchronized void syncUpdate(Observable o, Object arg)
	{
		ObserverMessage message = (ObserverMessage) arg;
		
		//CHECK IF LIST UPDATED
		if(message.getType() == ObserverMessage.LIST_ASSET_FAVORITES_TYPE)
		{
			//GET SELECTED ITEM
			Asset selected = (Asset) this.getSelectedItem();
						
			//EMPTY LIST
			this.removeAllElements();
				
			//INSERT ALL ACCOUNTS
			Set<Long> keys = (Set<Long>) message.getValue();
			List<Asset> assets = new ArrayList<Asset>();
			for(Long key: keys)
			{				
				//GET ASSET
				Asset asset = Controller.getInstance().getAsset(key);
				assets.add(asset);
				
				//ADD
				this.addElement(asset);
			}
				
			//RESET SELECTED ITEM
			if(this.getIndexOf(selected) != -1)
			{
				for(Asset asset: assets)
				{
					if(asset.getKey() == selected.getKey())
					{
						this.setSelectedItem(asset);
						return;
					}
				}
			}
		}
	}
}

@@ -142,16 +142,105 @@
 til.Set;
+%0Aimport java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReentrantLock;
 %0A%0Aimport
@@ -481,17 +481,52 @@
 erver %7B%0A
+%09Lock lock = new ReentrantLock();%0A%09
 %0A
-
 %09public 
@@ -685,32 +685,124 @@
 %0A%09%09%09
-this.syncUpdate(o, arg);
+if (lock.tryLock()) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09this.syncUpdate(o, arg);%0A%09%09%09%09%7D%0A%09%09%09%09finally %7B%0A%09%09%09%09%09lock.unlock();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09
 %0A%09%09%7D

Fix a bug due to which often didn't load the GUI.