tableview.java

来自「Azureus is a powerful, full-featured, cr」· Java 代码 · 共 1,316 行 · 第 1/3 页

JAVA
1,316
字号
	          if (ptIconSize != null) {
	            // set row height by setting image
	            Image image = new Image(display, ptIconSize.x, ptIconSize.y);
	            row.setImage(0, image);
	            row.setImage(0, null);
	            image.dispose();
	          } else if (iCellHeight > 0)
	            row.setHeight(iCellHeight);
	
	          if (objectToSortableItem.containsKey(dataSource)) {
	            try{
	            	objectToSortableItem_mon.enter();
	            
	            	objectToSortableItem.put(dataSource, row);
	            }finally{
	            	
	            	objectToSortableItem_mon.exit();
	            }
	            TableCellCore cell = row.getTableCellCore(sorter.getLastField());
	            if (cell != null)
	              cell.refresh();
	          } else {
	            row.delete();
	          }
	          bSortScheduled = true;
	        }
	      });
	    } catch (Exception e) {
	      System.out.println("Error adding row to " + sTableID + " table");
	      Debug.printStackTrace( e );
	    }
  	}finally{
  		
  		this_mon.exit();
  	}
  }

  /** Remove the specified dataSource from the table.
   *
   * @param dataSource data source to be removed
   */
  public void removeDataSource(Object dataSource) {
    TableRowCore item;
    try{
    	objectToSortableItem_mon.enter();
    	
    	item = (TableRowCore)objectToSortableItem.remove(dataSource);
    }finally{
    	
    	objectToSortableItem_mon.exit();
    }
    
    if (item == null)
      return;
    item.delete();
  }

  /** Remove all the data sources (table rows) from the table.
   */
  public void removeAllTableRows() {
    // clear all table items first, so that TableRowCore.delete() doesn't remove
    // them one by one (slow)
    if (table != null && !table.isDisposed())
      table.removeAll();

    runForAllRows(new GroupTableRowRunner() {
      public void run(TableRowCore row) {
        row.delete();
      }
    });
    objectToSortableItem.clear();
/* Old Way.  DELME after new way is verified working :)
    Iterator iter = objectToSortableItem.values().iterator();
    while(iter.hasNext()) {
      TableRowCore row = (TableRowCore) iter.next();
      if (row != null) row.delete();
      iter.remove();
    }
*/
  }
    
  public Table getTable() {
    return table;
  }
  
  public String getTableID() {
    return sTableID;
  }

  /* ParameterListener Implementation */

  public void parameterChanged(String parameterName) {
    if (parameterName.equals("Graphics Update")) {
      graphicsUpdate = COConfigurationManager.getIntParameter("Graphics Update");
      return;
    }
    if (parameterName.startsWith("Color")) {
      tableInvalidate();
    }
  }
  
  /* ITableStructureModificationListener implementation */

  public void tableStructureChanged() {
    //2. Clear everything
    removeAllTableRows();
    
    //3. Dispose the old table
    if (table != null && !table.isDisposed()) {
      table.dispose();
    }
    menu.dispose();
    
    //4. Re-create the table
    menu = createMenu();
    fillMenu(menu);
    table = createTable();
    initializeTable(table);
    
    panel.layout();
  }
  
  /** The Columns width changed
   *
   * @param columnNumber # of column which size has changed for
   * @param newWidth New width of column
   */
  public void columnSizeChanged(TableColumnCore tableColumn) {
    int newWidth = tableColumn.getWidth();
    if (table == null || table.isDisposed())
      return;
    
    TableColumn column = null;
    TableColumn[] tableColumnsSWT = table.getColumns();
    for (int i = 0; i < tableColumnsSWT.length; i++) {
      if (tableColumnsSWT[i].getData("TableColumnCore") == tableColumn) {
        column = tableColumnsSWT[i];
        break;
      }
    }
    if (column == null || column.isDisposed() || (column.getWidth() == newWidth))
      return;

    column.setWidth(newWidth);
  }
  
  public void columnInvalidate(TableColumnCore tableColumn) {
    final String sColumnName = tableColumn.getName();
    runForAllRows(new GroupTableRowRunner() {
      public void run(TableRowCore row) {
        TableCellCore cell = row.getTableCellCore(sColumnName);
        if (cell != null)
          cell.setValid(false);
      }
    });
  }

  public void tableInvalidate() {
    runForAllRows(new GroupTableRowRunner() {
      public void run(TableRowCore row) {
        row.setValid(false);
        row.refresh(true);
      }
    });
  }

  /* End ITableStructureModificationListener implementation */

  /** Get all the cells for one Column, in the order they are displayed */
  /* SortableTable implementation */
  public List getColumnCoreCells(String sColumnName) {
    ArrayList l = new ArrayList();
    if (table != null && !table.isDisposed()) {
      TableItem[] tis = table.getItems();
      for (int i = 0; i < tis.length; i++) {
        TableRowCore row = (TableRowCore)tis[i].getData("TableRow");
        if (row == null)
          continue;
        TableCellCore cell = row.getTableCellCore(sColumnName);
        if (cell != null)
          l.add(cell);
      }
    }
    return l;
  }

  /** Get all the rows for this table, in the order they are displayed
   *
   * @return a list of TableRowCore objects in the order the user sees them
   */
  public List getRowsOrdered() {
    ArrayList l = new ArrayList();
    if (table != null && !table.isDisposed()) {
      TableItem[] tis = table.getItems();
      for (int i = 0; i < tis.length; i++) {
        TableRowCore row = (TableRowCore)tis[i].getData("TableRow");
        if (row != null)
          l.add(row);
      }
    }
    return l;
  }
  
  /** Get all the rows for this table, in no particular order.  Faster than
   * {@link #getRowsOrdered}
   *
   * @return TableRowCore objects.  May contain null entries.
   */
  public TableRowCore[] getRowsUnordered() {
    return (TableRowCore[])objectToSortableItem.values().toArray(new TableRowCore[0]);
  }
  
  /** Return all the TableColumnCore objects that belong to this TableView
   *
   * @return All the TableColumnCore objects
   */
  public TableColumnCore[] getAllTableColumnCore() {
    return tableColumns;
  }
  
  /** Return the specified TableColumnCore object
   *
   * @return TableColumnCore requested
   */
  /* SortableTable implementation */
  public TableColumnCore getTableColumnCore(String sColumnName) {
    TableColumnManager tcManager = TableColumnManager.getInstance();
    return tcManager.getTableColumnCore(sTableID, sColumnName);
  }
    
  /* various selected rows functions */
  /***********************************/

  /** Returns an array of all selected Data Sources.  Null data sources are
   * ommitted.
   *
   * @return an array containing the selected data sources
   */
  public List getSelectedDataSourcesList() {
    ArrayList l = new ArrayList();
    if (table != null && !table.isDisposed()) {
      TableItem[] tis = table.getSelection();
      for (int i = 0; i < tis.length; i++) {
        TableRowCore row = (TableRowCore)tis[i].getData("TableRow");
        if (row != null && row.getDataSource(true) != null)
          l.add(row.getDataSource(true));
      }
    }
    return l;
  }

  /** Returns an array of all selected Data Sources.  Null data sources are
   * ommitted.
   *
   * @param a the array into which the selected data sources are to be stored, 
   *          if the size is the big enough; otherwise, a new array of the same 
   *          runtime type is allocated for this purpose.
   *
   * @return an array containing the selected data sources
   */
  public Object[] getSelectedDataSources(Object[] a) {
    return getSelectedDataSourcesList().toArray(a);
  }

  /** Returns an array of all selected Data Sources.  Null data sources are
   * ommitted.
   *
   * @return an array containing the selected data sources
   */
  public Object[] getSelectedDataSources() {
    return getSelectedDataSourcesList().toArray();
  }

  /** Returns an array of all selected TableRowCore.  Null data sources are
   * ommitted.
   *
   * @return an array containing the selected data sources
   */
  public TableRowCore[] getSelectedRows() {
    return (TableRowCore[])getSelectedRowsList().toArray(new TableRowCore[0]);
  }

  /** Returns an list of all selected TableRowCore objects.  Null data sources are
   * ommitted.
   *
   * @return an list containing the selected TableRowCore objects
   */
  public List getSelectedRowsList() {
    ArrayList l = new ArrayList();
    if (table != null && !table.isDisposed()) {
      TableItem[] tis = table.getSelection();
      for (int i = 0; i < tis.length; i++) {
        TableRowCore row = (TableRowCore)tis[i].getData("TableRow");
        if (row != null && row.getDataSource(true) != null)
          l.add(row);
      }
    }
    return l;
  }
  
  /** Returns the first selected data sources.
   *
   * @return the first selected data source, or null if no data source is 
   *         selected
   */
  public Object getFirstSelectedDataSource() {
    if (table == null || table.isDisposed() || table.getSelectionCount() == 0)
      return null;

    TableRowCore row = (TableRowCore)table.getSelection()[0].getData("TableRow");
    if (row == null)
      return null;
    return row.getDataSource(true);
  }
  
  /** For each row source that the user has selected, run the code 
   * provided by the specified parameter.
   *
   * @param runner Code to run for each selected row/datasource
   */
  public void runForSelectedRows(GroupTableRowRunner runner) {
    if (table == null || table.isDisposed())
      return;

    TableItem[] tis = table.getSelection();
    for (int i = 0; i < tis.length; i++) {
      TableRowCore row = (TableRowCore)tis[i].getData("TableRow");
      if (row != null)
        runner.run(row);
    }
  }

  public void runForAllRows(GroupTableRowRunner runner) {
    // put to array instead of synchronised iterator, so that runner can remove
    TableRowCore[] rows = 
      (TableRowCore[])objectToSortableItem.values().toArray(new TableRowCore[0]);
    for (int i = 0; i < rows.length; i++) {
      if (rows[i] != null)
        runner.run(rows[i]);
    }
  }
  
  /** Send Selected rows to the clipboard in a SpreadSheet friendly format 
   * (tab/cr delimited)
   */
  public void clipboardSelected() {
    String sToClipboard = "";
    for (int j = 0; j < getTable().getColumnCount(); j++) {
      if (j != 0) sToClipboard += "\t";
      sToClipboard += getTable().getColumn(j).getText();
    }

    TableItem[] tis = getTable().getSelection();
    for (int i = 0; i < tis.length; i++) {
      sToClipboard += "\n";
      for (int j = 0; j < getTable().getColumnCount(); j++) {
        if (j != 0) sToClipboard += "\t";
        sToClipboard += tis[i].getText(j);
      }
    }
    new Clipboard(getComposite().getDisplay()).setContents(
                                  new Object[] { sToClipboard },
                                  new Transfer[] {TextTransfer.getInstance()});
  }
    
  
  /** Used with {@link #runForSelectedRows}
   */
  public abstract class GroupTableRowRunner {
    /** Code to run 
     * @param row TableRowCore to run code against
     */
    public abstract void run(TableRowCore row);
  }
  
  /** Listener primarily for Menu Selection.  Implement run(TableRowCore) and it
   * will get called for each row the user has selected.
   */
  public abstract class SelectedTableRowsListener 
         extends GroupTableRowRunner
         implements Listener 
 {
    /** Event information passed in via the Listener.  Accessible in 
     * run(TableRowCore).
     */
    protected Event event;
    /** Process the trapped event.  This function does not need to be overidden.
     * @param e event information
     */
    public void handleEvent(Event e) {
      event = e;
      runForSelectedRows(this);
    }
    
    public abstract void run(TableRowCore row);
    
  }
  
  /** Handle sorting of a column based on clicking the Table Header */
  private class ColumnListener implements Listener {
    private TableColumnCore tableColumn;

    /** Initialize ColumnListener
     * @param tc TableColumnCore that will be sorted when header is clicked
     */
    public ColumnListener(TableColumnCore tc) {
      tableColumn = tc;
    }

    /** Process a Table Header click
     * @param e event information
     */
    public void handleEvent(Event e) {
      sorter.sortColumnReverse(tableColumn);
    }
  }

  private int getColumnNo(int iMouseX) {
    int iColumn = -1;
    if (table.getItemCount() > 0) {
      //Using  table.getTopIndex() instead of 0, cause
      //the first row has no bounds when it's not visible under OS X.
      TableItem ti = table.getItem(table.getTopIndex());
      for (int i = bSkipFirstColumn ?  1 : 0; i < table.getColumnCount(); i++) {
        // M8 Fixes SWT GTK Bug 51777:
        //  "TableItem.getBounds(int) returns the wrong values when table scrolled"
        Rectangle cellBounds = ti.getBounds(i);
        //System.out.println("i="+i+";Mouse.x="+iMouseX+";cellbounds="+cellBounds);
        if (iMouseX >= cellBounds.x && iMouseX < cellBounds.x + cellBounds.width && cellBounds.width > 0) {
          iColumn = i;
          break;
        }
      }
    }
    return iColumn;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?