⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listview.java

📁 这是我模仿window自带的小游戏扫雷编的,很简单,只实现了扫雷的基本功能,现拿出来与大家分享!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		if (child == null || child.isDisposed()) {
			return;
		}

		Rectangle bounds = child.getBounds();
		Rectangle area = sc.getClientArea();
		Point origin = sc.getOrigin();
		if (origin.x > bounds.x) {
			origin.x = Math.max(0, bounds.x);
		}
		if (origin.y > bounds.y) {
			origin.y = Math.max(0, bounds.y);
		}
		if (origin.x + area.width < bounds.x + bounds.width) {
			origin.x = Math.max(0, bounds.x + bounds.width - area.width);
		}
		if (origin.y + area.height < bounds.y + bounds.height) {
			origin.y = Math.max(0, bounds.y + bounds.height - area.height);
		}
		sc.setOrigin(origin);
	}

	public boolean isFocused() {
		return listComposite.isFocusControl();
	}

	// @see com.aelitis.azureus.ui.swt.utils.UIUpdatable#getUpdateUIName()
	public String getUpdateUIName() {
		return "ListView";
	}

	// @see com.aelitis.azureus.ui.swt.utils.UIUpdatable#updateUI()
	public void updateUI() {
		if (listComposite.isDisposed()) {
			return;
		}
		processDataSourceQueue();

		iGraphicRefresh++;
		boolean bDoGraphics = (iGraphicRefresh % graphicsUpdate) == 0;
		refreshAll(bDoGraphics);

		sortTable();
	}

	// XXX This gets called a lot.  Could store location and size on 
	//     resize/scroll of sc
	public Rectangle getBounds() {
		Point location = sc.getContent().getLocation();
		Point size = sc.getSize();
		return new Rectangle(-location.x, -location.y, size.x, size.y);
	}

	public Rectangle getClientArea() {
		return sc.getClientArea();
	}

	// @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
	public void handleEvent(Event event) {
		if (event.type == SWT.FocusIn || event.type == SWT.FocusOut) {
			selectedRows_mon.enter();
			try {
				for (Iterator iter = selectedRows.iterator(); iter.hasNext();) {
					ListRow row = (ListRow) iter.next();
					if (row != null) {
						row.repaint();
					}
				}
			} finally {
				selectedRows_mon.exit();
			}
		} else if (event.type == SWT.Traverse) {
			event.doit = true;

			switch (event.detail) {
				case SWT.TRAVERSE_ARROW_NEXT:
					if ((event.stateMask & SWT.MOD2) > 0) { // shift
						ListRow focusedRow = getRowFocused();
						if (focusedRow != null) {
							int index = focusedRow.getIndex();
							index++;
							ListRow nextRow = getRow(index);
							if (nextRow != null) {
								if (nextRow.isSelected()) {
									focusedRow.setSelected(false);
								}

								nextRow.setSelected(true);
								nextRow.setFocused(true);
							}
						}
					} else if ((event.stateMask & SWT.MOD1) > 0) { // control
						ListRow focusedRow = getRowFocused();
						if (focusedRow != null) {
							int index = focusedRow.getIndex();
							index++;
							ListRow nextRow = getRow(index);
							if (nextRow != null) {
								nextRow.setFocused(true);
							}
						}
					} else {
						moveFocus(1, false);
					}
					break;

				case SWT.TRAVERSE_ARROW_PREVIOUS:
					if ((event.stateMask & SWT.MOD2) > 0) {
						ListRow activeRow = getRowFocused();
						if (activeRow != null) {
							int index = activeRow.getIndex();
							index--;
							ListRow previousRow = getRow(index);
							if (previousRow != null) {
								if (previousRow.isSelected()) {
									activeRow.setSelected(false);
								}
								previousRow.setSelected(true);
								previousRow.setFocused(true);
							}
						}
					} else if ((event.stateMask & SWT.MOD1) > 0) { // control
						ListRow focusedRow = getRowFocused();
						if (focusedRow != null) {
							int index = focusedRow.getIndex();
							index--;
							ListRow nextRow = getRow(index);
							if (nextRow != null) {
								nextRow.setFocused(true);
							}
						}
					} else {
						moveFocus(-1, false);
					}
					break;

				case SWT.TRAVERSE_RETURN:
					_runDefaultAction();
					break;

				default:
					System.out.println("TR" + event.detail);

			}
		} else if (event.type == SWT.KeyDown) {
			int key = event.character;
			if (key <= 26 && key > 0)
				key += 'a' - 1;

			if (event.stateMask == SWT.MOD1) { // Control/Command
				switch (event.keyCode) {
					case 'a': // select all
						setSelectedRows(getRowsUnsorted());
						break;

					case ' ':
						event.doit = false;
						ListRow focusedRow = getRowFocused();
						if (focusedRow != null) {
							focusedRow.setSelected(!focusedRow.isSelected());
						}
						break;
				}

			} else {
				switch (event.keyCode) {
					case SWT.PAGE_UP:
						moveFocus(sc.getSize().y / -30, false); // TODO: Use real height
						break;

					case SWT.PAGE_DOWN:
						moveFocus(sc.getSize().y / 30, false); // TODO: Use real height
						break;

					case SWT.HOME:
						if (event.stateMask == SWT.CONTROL) {
							setSelectedRows(new ListRow[] { (ListRow) rows.get(0)
							});
						}
						break;

					case SWT.END:
						if (event.stateMask == SWT.CONTROL) {
							int i = rows.size();
							if (i > 0) {
								setSelectedRows(new ListRow[] { (ListRow) rows.get(i - 1)
								});
							}
						}
						break;
				}
			}
		} else if (event.type == SWT.DefaultSelection
				|| event.type == SWT.MouseDoubleClick) {
			_runDefaultAction();
		}

	}

	private void _runDefaultAction() {
		// plugin may have cancelled the default action

		if (lCancelSelectionTriggeredOn > 0
				&& System.currentTimeMillis() - lCancelSelectionTriggeredOn < 200) {
			lCancelSelectionTriggeredOn = -1;
		} else {
			ListRow[] selectedRows = getSelectedRows();
			for (Iterator iter = listenersSelection.iterator(); iter.hasNext();) {
				ListSelectionAdapter l = (ListSelectionAdapter) iter.next();
				l.defaultSelected(selectedRows);
			}
		}
	}

	public void setMouseClickIsDefaultSelection(boolean b) {
		bMouseClickIsDefaultSelection = b;
	}

	public void addSelectionListener(ListSelectionAdapter listener,
			boolean bFireSelection) {
		listenersSelection.add(listener);
		if (bFireSelection) {
			ListRow[] rows = getSelectedRows();
			for (int i = 0; i < rows.length; i++) {
				listener.selected(rows[i]);
			}

			listener.focusChanged(getRowFocused());
		}
	}

	public void addCountChangeListener(ListCountChangeAdapter listener) {
		listenersCountChange.add(listener);
	}

	protected void triggerListenerRowAdded(ListRow row) {
		for (Iterator iter = listenersCountChange.iterator(); iter.hasNext();) {
			ListCountChangeAdapter l = (ListCountChangeAdapter) iter.next();
			l.rowAdded(row);
		}
	}

	protected void triggerListenerRowRemoved(ListRow row) {
		for (Iterator iter = listenersCountChange.iterator(); iter.hasNext();) {
			ListCountChangeAdapter l = (ListCountChangeAdapter) iter.next();
			l.rowRemoved(row);
		}
	}

	/**
	 * Retrieve the control that the rows are added to.
	 * 
	 * @return
	 */
	public Control getControl() {
		return listComposite;
	}

	public ScrolledComposite getScrolledComposite() {
		return sc;
	}

	public String getTableID() {
		return sTableID;
	}

	/** Get all the rows for this table
	 *
	 * @return a list of TableRowCore objects
	 */
	public ListRow[] getRowsUnsorted() {
		try {
			row_mon.enter();

			return (ListRow[]) rows.toArray(new ListRow[0]);

		} finally {
			row_mon.exit();
		}
	}

	/** For every row source, run the code provided by the specified 
	 * parameter.
	 *
	 * @param runner Code to run for each row/datasource
	 */
	public void runForAllRows(GroupTableRowRunner runner) {
		// put to array instead of synchronised iterator, so that runner can remove
		TableRowCore[] rows = getRowsUnsorted();

		for (int i = 0; i < rows.length; i++) {
			runner.run(rows[i]);
		}
	}

	public void columnInvalidate(TableColumnCore tableColumn) {
		// TODO Auto-generated method stub

	}

	public void columnOrderChanged(int[] iPositions) {
		// TODO Auto-generated method stub

	}

	public void columnSizeChanged(TableColumnCore tableColumn) {
		if (tableColumn.getPosition() < 0) {
			return;
		}

		final String id = tableColumn.getName();
		final int width = tableColumn.getWidth();

		final int position = tableColumn.getPosition();
		final int numColumns = visibleColumns.length;

		runForAllRows(new GroupTableRowRunner() {
			public void run(TableRowCore row) {
				TableCellCore cell = row.getTableCellCore(id);
				if (cell != null) {
					Rectangle bounds = cell.getBounds();
					int diff = width - bounds.width;
					if (diff != 0) {
						bounds.width = width;
						((ListCell) cell.getBufferedTableItem()).setBounds(bounds);
						cell.refresh(true);

						for (int i = position + 1; i < numColumns; i++) {
							TableColumnCore nextColumn = visibleColumns[i];
							TableCellCore nextCell = row.getTableCellCore(nextColumn.getName());
							if (nextCell != null) {
								Rectangle nextBounds = nextCell.getBounds();
								nextBounds.x += diff;
								((ListCell) nextCell.getBufferedTableItem()).setBounds(nextBounds);
								nextCell.refresh(true);
							}
						}
					}
				}
			}
		});
	}

	public void tableStructureChanged() {
	}

	public TableColumnCore getSortColumn() {
		return sortColumn;
	}

	public void setSortColumn(TableColumnCore sorter) {
		boolean bSameColumn = sortColumn.equals(sorter);
		if (!bSameColumn) {
			sortColumn = sorter;
			int iSortDirection = configMan.getIntParameter(CFG_SORTDIRECTION);
			if (iSortDirection == 0)
				sortColumn.setSortAscending(true);
			else if (iSortDirection == 1)
				sortColumn.setSortAscending(false);
			else
				sortColumn.setSortAscending(!sortColumn.isSortAscending());

			configMan.setParameter(sTableID + ".sortAsc",
					sortColumn.isSortAscending());
			configMan.setParameter(sTableID + ".sortColumn", sortColumn.getName());
		} else {
			sortColumn.setSortAscending(!sortColumn.isSortAscending());
			configMan.setParameter(sTableID + ".sortAsc",
					sortColumn.isSortAscending());
		}

		sortColumn.setLastSortValueChange(SystemTime.getCurrentTime());

		changeColumnIndicator();
		sortTable();
	}

	public void sortTable() {
		long lTimeStart;
		if (DEBUG_SORTER) {
			//System.out.println(">>> Sort.. ");
			lTimeStart = System.currentTimeMillis();
		}

		try {
			row_mon.enter();

			if (sortColumn != null
					&& sortColumn.getLastSortValueChange() > lLastSortedOn) {
				lLastSortedOn = SystemTime.getCurrentTime();

				// 1) Copy rows to array and sort
				// 2) check if any have changed position
				// 3) make row sort permanent (move sorted array back into rows field)
				// 4) tell each row affected to fix itself up

				if (sortColumn != null) {
					String sColumnID = sortColumn.getName();
					for (Iterator iter = rows.iterator(); iter.hasNext();) {
						TableRowCore row = (TableRowCore) iter.next();
						TableCellCore cell = row.getTableCellCore(sColumnID);
						if (cell != null) {
							cell.refresh(true, true, true);
						}
					}
				}

				// Since rows alternate in color, all rows including and after the
				// first changed row need to be notified to change.

				Object[] rowsArray = rows.toArray();
				Arrays.sort(rowsArray, sortColumn);

				//Collections.sort(rows, sortColumn);
				if (DEBUG_SORTER) {
					long lTimeDiff = (System.currentTimeMillis() - lTimeStart);
					if (lTimeDiff > 100)
						System.out.println("--- Build & Sort took " + lTimeDiff + "ms");
					lTimeStart = System.currentTimeMillis();
				}

				int iNumChanged = 0;
				int iFirstChange = -1;
				for (int i = 0; i < rowsArray.length; i++) {
					ListRow row = (ListRow) rowsArray[i];
					if (row != rows.get(i)) {
						iNumChanged++;
						if (iFirstChange < 0) {
							iFirstChange = i;
						}
					}
				}

				List list = Arrays.asList(rowsArray);
				if (list instanceof ArrayList) {
					rows = (ArrayList) list;
				} else {
					rows = new ArrayList(list);
				}

				if (iFirstChange >= 0) {
					for (int i = iFirstChange; i < rows.size(); i++) {
						ListRow row = (ListRow) rows.get(i);
						row.fixupPosition();
					}

					if (DEBUG_SORTER) {
						long lTimeDiff = (System.currentTimeMillis() - lTimeStart);
						System.out.println("Sort made " + iNumChanged + " rows move in "
								+ lTimeDiff + "ms");
					}

					listComposite.layout();
				}
			}

		} finally {
			row_mon.exit();
		}

		// Selection should be okay still.  May need to be moved into view
		// if we want that behaviour
	}

	public TableColumnCore[] getAllColumns() {
		return allColumns;
	}
}

⌨️ 快捷键说明

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