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

📄 tableviewersorter.java

📁 SWT_designer安装软件
💻 JAVA
字号:
package com.swtdesigner.table;

import java.util.ArrayList;
import java.util.Comparator;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IMemento;

/**
 * Sorter for the TableViewer based upon FavoritesViewSorter
 * from the "Eclipse: Building Commercial Quality Plug-ins" book.
 * 
 * This class may be freely distributed as part of any application or plugin.
 * <p>
 * Copyright (c) 2003, Instantiations, Inc. <br>All Rights Reserved
 * @author Dan Rubel
 * @version $Revision$
 */
public class TableViewerSorter extends ViewerSorter
{
	// Simple data structure for grouping
	// sort information by column
	private class SortInfo
	{
		int columnIndex;
		Comparator<Object> comparator;
		boolean descending;
	}

	private TableViewer viewer;
	private SortInfo[] infos;

	/**
	 * Create the table viewer sorter
	 * @param viewer
	 * @param columns
	 * @param comparators
	 */
	public TableViewerSorter(TableViewer viewer, TableColumn[] columns, Comparator<Object>[] comparators) {
		this.viewer = viewer;
		this.infos = new SortInfo[columns.length];
		for (int i = 0; i < columns.length; i++) {
			SortInfo info = new SortInfo();
			info.columnIndex = i;
			info.comparator = comparators[i];
			info.descending = false;
			createSelectionListener(columns[i], info);
			this.infos[i] = info;
		}
	}

	@Override
	public int compare(Viewer sortViewer, Object favorite1, Object favorite2) {
		for (int i = 0; i < this.infos.length; i++) {
			int result = this.infos[i].comparator.compare(favorite1, favorite2);
			if (result != 0) {
				if (this.infos[i].descending)
					return -result;
				return result;
			}
		}
		return 0;
	}

	private void createSelectionListener(final TableColumn column, final SortInfo info) {
		column.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				sortUsing(info);
			}
		});
	}

	protected void sortUsing(SortInfo info) {
		if (info == this.infos[0])
			info.descending = !info.descending;
		else {
			for (int i = 0; i < this.infos.length; i++) {
				if (info == this.infos[i]) {
					System.arraycopy(this.infos, 0, this.infos, 1, i);
					this.infos[0] = info;
					info.descending = false;
					break;
				}
			}
		}
		this.viewer.refresh();
	}

	////////////////////////////////////////////////////////////////////////////
	//
	// Loading And Saving State
	//
	////////////////////////////////////////////////////////////////////////////

	private static final String TAG_DESCENDING = "descending"; //$NON-NLS-1$

	private static final String TAG_COLUMN_INDEX = "columnIndex"; //$NON-NLS-1$

	private static final String TAG_TYPE = "SortInfo"; //$NON-NLS-1$

	private static final String TAG_TRUE = "true"; //$NON-NLS-1$

	/**
	 * Save the sort state
	 * @param memento
	 */
	public void saveState(IMemento memento) {
		for (int i = 0; i < this.infos.length; i++) {
			SortInfo info = this.infos[i];
			IMemento mem = memento.createChild(TAG_TYPE);
			mem.putInteger(TAG_COLUMN_INDEX, info.columnIndex);
			if (info.descending)
				mem.putString(TAG_DESCENDING, TAG_TRUE);
		}
	}

	/**
	 * Initialize the sort state
	 * @param memento
	 */
	public void init(IMemento memento) {
		ArrayList<SortInfo> newInfos = new ArrayList<SortInfo>(this.infos.length);
		IMemento[] mems = memento.getChildren(TAG_TYPE);
		for (int i = 0; i < mems.length; i++) {
			IMemento mem = mems[i];
			Integer value = mem.getInteger(TAG_COLUMN_INDEX);
			if (value == null)
				continue;
			int index = value.intValue();
			if (index < 0 || index >= this.infos.length)
				continue;
			SortInfo info = this.infos[index];
			if (newInfos.contains(info))
				continue;
			info.descending = TAG_TRUE.equals(mem.getString(TAG_DESCENDING));
			newInfos.add(info);
		}
		for (int i = 0; i < this.infos.length; i++)
			if (!newInfos.contains(this.infos[i]))
				newInfos.add(this.infos[i]);
		this.infos = newInfos.toArray(new SortInfo[newInfos.size()]);
	}
}

⌨️ 快捷键说明

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