FileDocCategorySizeDatePackage
MyTrackerView.javaAPI DocAzureus 3.0.3.416933Wed Aug 15 08:53:10 BST 2007org.gudy.azureus2.ui.swt.views

MyTrackerView

public class MyTrackerView extends org.gudy.azureus2.ui.swt.views.table.impl.TableViewTab implements org.gudy.azureus2.ui.swt.views.table.TableViewSWTMenuFillListener, org.gudy.azureus2.core3.category.CategoryManagerListener, TableSelectionListener, TableRefreshListener, TableLifeCycleListener, org.gudy.azureus2.core3.tracker.host.TRHostListener
author
parg
author
TuxPaper 2004/Apr/20: Remove need for tableItemToObject 2004/Apr/21: extends TableView instead of IAbstractView

Fields Summary
private static final TableColumnCore[]
basicItems
protected static final org.gudy.azureus2.plugins.torrent.TorrentAttribute
category_attribute
private com.aelitis.azureus.core.AzureusCore
azureus_core
private Menu
menuCategory
private org.gudy.azureus2.ui.swt.views.table.TableViewSWT
tv
private boolean
start
private boolean
stop
private boolean
remove
Constructors Summary
public MyTrackerView(com.aelitis.azureus.core.AzureusCore _azureus_core)


	   
		tv = new TableViewSWTImpl(TableManager.TABLE_MYTRACKER, "MyTrackerView",
				basicItems, "name", SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER
						| SWT.VIRTUAL);
		setTableView(tv);
		azureus_core = _azureus_core;
		tv.addLifeCycleListener(this);
		tv.addSelectionListener(this, false);
		tv.addMenuFillListener(this);
		tv.addRefreshListener(this, false);
	
Methods Summary
private voidaddCategory()

    CategoryAdderWindow adderWindow = new CategoryAdderWindow(SWTThread.getInstance().getDisplay());
    Category newCategory = adderWindow.getNewCategory();
    if (newCategory != null)
      assignSelectedToCategory(newCategory);
  
private voidaddCategorySubMenu()

    MenuItem[] items = menuCategory.getItems();
    int i;
    for (i = 0; i < items.length; i++) {
      items[i].dispose();
    }

    Category[] categories = CategoryManager.getCategories();
    Arrays.sort(categories);

    if (categories.length > 0) {
      Category catUncat = CategoryManager.getCategory(Category.TYPE_UNCATEGORIZED);
      if (catUncat != null) {
        final MenuItem itemCategory = new MenuItem(menuCategory, SWT.PUSH);
        Messages.setLanguageText(itemCategory, catUncat.getName());
        itemCategory.setData("Category", catUncat);
        itemCategory.addListener(SWT.Selection, new Listener() {
          public void handleEvent(Event event) {
            MenuItem item = (MenuItem)event.widget;
            assignSelectedToCategory((Category)item.getData("Category"));
          }
        });

        new MenuItem(menuCategory, SWT.SEPARATOR);
      }

      for (i = 0; i < categories.length; i++) {
        if (categories[i].getType() == Category.TYPE_USER) {
          final MenuItem itemCategory = new MenuItem(menuCategory, SWT.PUSH);
          itemCategory.setText(categories[i].getName());
          itemCategory.setData("Category", categories[i]);

          itemCategory.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
              MenuItem item = (MenuItem)event.widget;
              assignSelectedToCategory((Category)item.getData("Category"));
            }
          });
        }
      }

      new MenuItem(menuCategory, SWT.SEPARATOR);
    }

    final MenuItem itemAddCategory = new MenuItem(menuCategory, SWT.PUSH);
    Messages.setLanguageText(itemAddCategory,
                             "MyTorrentsView.menu.setCategory.add");

    itemAddCategory.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        addCategory();
      }
    });

  
public voidaddThisColumnSubMenu(java.lang.String columnName, Menu menuThisColumn)

  
private voidassignSelectedToCategory(org.gudy.azureus2.core3.category.Category category)

    tv.runForSelectedRows(new TableGroupRowRunner() {
      public void run(TableRowCore row) {
      	
	    TRHostTorrent	tr_torrent = (TRHostTorrent)row.getDataSource(true);
		
		TOTorrent	torrent = tr_torrent.getTorrent();
		
		DownloadManager dm = azureus_core.getGlobalManager().getDownloadManager( torrent );

		if ( dm != null ){
			
			dm.getDownloadState().setCategory( category );
			
		}else{
			
	     	String cat_str;
	      	
	      	if ( category == null ){
	      		
				cat_str = null;
	      		
	      	}else if ( category == CategoryManager.getCategory(Category.TYPE_UNCATEGORIZED)){
	      		
				cat_str = null;
	      		
	      	}else{
	      		
				cat_str = category.getName();
	      	}
				// bit of a hack-alert here
			
			TorrentUtils.setPluginStringProperty( torrent, "azcoreplugins.category", cat_str );
			
			try{
				TorrentUtils.writeToFile( torrent );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
		}
      }
    });
  
public voidcategoryAdded(org.gudy.azureus2.core3.category.Category category)

  	Utils.execSWTThread(
	  		new AERunnable() 
			{
	  			public void 
				runSupport() 
	  			{
	  				addCategorySubMenu();
	  			}
			});
  
public voidcategoryRemoved(org.gudy.azureus2.core3.category.Category category)

  	Utils.execSWTThread(
  		new AERunnable() 
		{
  			public void 
			runSupport() 
  			{
  				addCategorySubMenu();
  			}
		});
  
private voidcomputePossibleActions()

    start = stop = remove = false;
    Object[] hostTorrents = tv.getSelectedDataSources();
    if (hostTorrents.length > 0) {
      remove = true;
      for (int i = 0; i < hostTorrents.length; i++) {
        TRHostTorrent	host_torrent = (TRHostTorrent)hostTorrents[i];
        
        int	status = host_torrent.getStatus();
        
        if ( status == TRHostTorrent.TS_STOPPED ){          
          start	= true;          
        }
        
        if ( status == TRHostTorrent.TS_STARTED ){          
          stop = true;
        }
        
        /*
        try{     	
        	host_torrent.canBeRemoved();
        	
        }catch( TRHostTorrentRemovalVetoException f ){
        	
        	remove = false;
        }
        */
      }
    }
  
public voiddefaultSelected(TableRowCore[] rows)

		TRHostTorrent torrent = (TRHostTorrent) tv.getFirstSelectedDataSource();
		if (torrent == null)
			return;
		DownloadManager dm = azureus_core.getGlobalManager().getDownloadManager(
				torrent.getTorrent());
		if (dm != null) {
			UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
			if (uiFunctions != null) {
				uiFunctions.openManagerView(dm);
			}
		}
	
public voiddeselected(TableRowCore[] rows)

	
public voidfillMenu(Menu menu)

	  
	    menuCategory = new Menu(getComposite().getShell(), SWT.DROP_DOWN);
	    final MenuItem itemCategory = new MenuItem(menu, SWT.CASCADE);
	    Messages.setLanguageText(itemCategory, "MyTorrentsView.menu.setCategory"); //$NON-NLS-1$
	    //itemCategory.setImage(ImageRepository.getImage("speed"));
	    itemCategory.setMenu(menuCategory);

	    addCategorySubMenu();
	    
	    new MenuItem(menu, SWT.SEPARATOR);

	   final MenuItem itemStart = new MenuItem(menu, SWT.PUSH);
	   Messages.setLanguageText(itemStart, "MyTorrentsView.menu.start"); //$NON-NLS-1$
	   Utils.setMenuItemImage(itemStart, "start");

	   final MenuItem itemStop = new MenuItem(menu, SWT.PUSH);
	   Messages.setLanguageText(itemStop, "MyTorrentsView.menu.stop"); //$NON-NLS-1$
	   Utils.setMenuItemImage(itemStop, "stop");

	   final MenuItem itemRemove = new MenuItem(menu, SWT.PUSH);
	   Messages.setLanguageText(itemRemove, "MyTorrentsView.menu.remove"); //$NON-NLS-1$
	   Utils.setMenuItemImage(itemRemove, "delete");

	   Object[] hostTorrents = tv.getSelectedDataSources();

	   itemStart.setEnabled(false);
	   itemStop.setEnabled(false);
	   itemRemove.setEnabled(false);

	   if (hostTorrents.length > 0) {
	   	
			boolean	start_ok 	= true;
			boolean	stop_ok		= true;
			boolean	remove_ok	= true;
			
			for (int i = 0; i < hostTorrents.length; i++) {
				
				TRHostTorrent	host_torrent = (TRHostTorrent)hostTorrents[i];
				
				int	status = host_torrent.getStatus();
				
				if ( status != TRHostTorrent.TS_STOPPED ){
					
					start_ok	= false;
					
				}
				
				if ( status != TRHostTorrent.TS_STARTED ){
					
					stop_ok = false;
				}
				
				/*
				try{
					
					host_torrent.canBeRemoved();
					
				}catch( TRHostTorrentRemovalVetoException f ){
					
					remove_ok = false;
				}
				*/
			}
	   		itemStart.setEnabled(start_ok);
		 	itemStop.setEnabled(stop_ok);
		 	itemRemove.setEnabled(remove_ok);
	   }

	   itemStart.addListener(SWT.Selection, new Listener() {
		 public void handleEvent(Event e) {
		   startSelectedTorrents();
		 }    
	   });
	   
	   itemStop.addListener(SWT.Selection, new Listener() {
		 public void handleEvent(Event e) {
		   stopSelectedTorrents();
		 }    
	   });
	   
	   itemRemove.addListener(SWT.Selection, new Listener() {
		 public void handleEvent(Event e) {
		   removeSelectedTorrents();
		 }   
	   });

    new MenuItem(menu, SWT.SEPARATOR);
  
public voidfocusChanged(TableRowCore focus)

	
public booleanhandleExternalRequest(java.net.InetSocketAddress client, java.lang.String user, java.lang.String url, java.net.URL absolute_url, java.lang.String header, java.io.InputStream is, java.io.OutputStream os)

		return( false );
	
public booleanisEnabled(java.lang.String itemKey)

    if(itemKey.equals("start"))
      return start;
    if(itemKey.equals("stop"))
      return stop;
    if(itemKey.equals("remove"))
      return remove;
    return false;
  
public voiditemActivated(java.lang.String itemKey)

    if(itemKey.equals("start")) {
      startSelectedTorrents();
      return;
    }
    if(itemKey.equals("stop")){
      stopSelectedTorrents();
      return;
    }
    if(itemKey.equals("remove")){
      removeSelectedTorrents();
      return;
    }
  
private voidremoveSelectedTorrents()

    tv.runForSelectedRows(new TableGroupRowRunner() {
      public void run(TableRowCore row) {
        TRHostTorrent	torrent = (TRHostTorrent)row.getDataSource(true);
      	try{
      		torrent.remove();
      		
      	}catch( TRHostTorrentRemovalVetoException f ){
      		
      		Alerts.showErrorMessageBoxUsingResourceString(
							new Object[] { torrent },
      				"globalmanager.download.remove.veto", f );
      	}
      }
    });
  
public voidselected(TableRowCore[] rows)

	
private voidstartSelectedTorrents()

    tv.runForSelectedRows(new TableGroupRowRunner() {
      public void run(TableRowCore row) {
        TRHostTorrent	torrent = (TRHostTorrent)row.getDataSource(true);
        if (torrent.getStatus() == TRHostTorrent.TS_STOPPED)
          torrent.start();
      }
    });
  
private voidstopSelectedTorrents()

    tv.runForSelectedRows(new TableGroupRowRunner() {
      public void run(TableRowCore row) {
        TRHostTorrent	torrent = (TRHostTorrent)row.getDataSource(true);
        if (torrent.getStatus() == TRHostTorrent.TS_STARTED)
          torrent.stop();
      }
    });
  
public voidtableRefresh()

		if (getComposite() == null || getComposite().isDisposed()){
	   
			return;
	   	}
		
		computePossibleActions();
  	UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
  	if (uiFunctions != null) {
  		uiFunctions.refreshIconBar();
  	}
		
		// Store values for columns that are calculate from peer information, so 
		// that we only have to do one loop.  (As opposed to each cell doing a loop)
		// Calculate code copied from TrackerTableItem
		TableRowCore[] rows = tv.getRows();
		for (int x = 0; x < rows.length; x++) {
		  TableRowSWT row = (TableRowSWT)rows[x];
		  
		  if (row == null){
		    continue;
		  }
		  
		  TRHostTorrent	host_torrent = (TRHostTorrent)rows[x].getDataSource(true);
		  
		  if (host_torrent == null){
			  continue;
		  }
		  
		  long	uploaded	= host_torrent.getTotalUploaded();
		  long	downloaded	= host_torrent.getTotalDownloaded();
		  long	left		= host_torrent.getTotalLeft();

		  int		seed_count	= host_torrent.getSeedCount();

		  host_torrent.setData("GUI_PeerCount", new Long(host_torrent.getLeecherCount()));
		  host_torrent.setData("GUI_SeedCount", new Long(seed_count));
		  host_torrent.setData("GUI_BadNATCount", new Long(host_torrent.getBadNATCount()));
		  host_torrent.setData("GUI_Uploaded", new Long(uploaded));
		  host_torrent.setData("GUI_Downloaded", new Long(downloaded));
		  host_torrent.setData("GUI_Left", new Long(left));

		  if ( seed_count != 0 ){
			  Color fg = row.getForeground();
			  
			  if (fg != null && fg.equals(Colors.blues[Colors.BLUES_MIDDARK])) {
				  row.setForeground(Colors.blues[Colors.BLUES_MIDDARK]);
			  }
		  }
		}
	
public voidtableViewDestroyed()

  	azureus_core.getTrackerHost().removeListener( this );
	
public voidtableViewInitialized()

		azureus_core.getTrackerHost().addListener( this );
	
public voidtorrentAdded(org.gudy.azureus2.core3.tracker.host.TRHostTorrent host_torrent)

	
	  tv.addDataSource(host_torrent);
	
public voidtorrentChanged(org.gudy.azureus2.core3.tracker.host.TRHostTorrent t)

 
public voidtorrentRemoved(org.gudy.azureus2.core3.tracker.host.TRHostTorrent host_torrent)

	  tv.removeDataSource(host_torrent);