treesortdemo.java

来自「全面实现ilog地功能,没有使用第三方lib.」· Java 代码 · 共 162 行

JAVA
162
字号
/* * This source code is part of TWaver 1.3.1 * * SERVA Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * Copyright 2000-2005 SERVA Software, Inc. All rights reserved. */package demo.tree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import twaver.Element;
import twaver.Generator;
import twaver.TDataBox;
import twaver.tree.TTree;
import twaver.tree.TTreeCellIcon;
import demo.DemoPane;

class CustomTreeIcon extends TTreeCellIcon{
	public CustomTreeIcon(Element element) {
		super(element);
	}
	protected Icon getElementIcon(){	
		final String delta = (String)this.getElement().getClientProperty(DataFactory.Delta);
		final Icon deltaIcon = DataFactory.getIcon(delta);
		final Icon elementIcon = super.getElementIcon();
		if(deltaIcon == null){
			return elementIcon;
		}
		
		Icon icon = new Icon(){
			public int getIconHeight() {
				return Math.max(deltaIcon.getIconHeight(), elementIcon.getIconHeight());
			}
			public int getIconWidth() {
				if(DataFactory.twoIcon(delta)){
					return deltaIcon.getIconWidth() * 2 + elementIcon.getIconWidth(); 
				}else{
					return deltaIcon.getIconWidth() + elementIcon.getIconWidth(); 
				}
			}
			public void paintIcon(Component c, Graphics g, int x, int y) {
				elementIcon.paintIcon(c, g, x, y);
				x += elementIcon.getIconWidth();
				deltaIcon.paintIcon(c, g, x, y);
				x += deltaIcon.getIconWidth();
				if(DataFactory.twoIcon(delta)){
					deltaIcon.paintIcon(c, g, x, y);
				}
			}
		};
		return icon;
	}
}

public class TreeSortDemo extends DemoPane implements ActionListener{
	private TDataBox box = new TDataBox(this.getTitle());
	private TTree tree = new TTree(box);
	private ButtonGroup group = new ButtonGroup();
	private JPanel sortPanel = new JPanel(new FlowLayout());
	private JCheckBox ascend = new JCheckBox("ASCEND");
	private String sortKey = null;
	
	public TreeSortDemo(){
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(tree), BorderLayout.CENTER);
		this.add(sortPanel, BorderLayout.NORTH);
		
		DataFactory.createData(box);
		tree.setElementLabelGenerator(new Generator(){
			public Object generate(Object object) {
				Element element = (Element)object;
				return "<html>" +
						"<font color=blue>" + element.getClientProperty(DataFactory.Language) + "</font> " +
						"<font color=red>P2005:" + element.getClientProperty(DataFactory.PositionDec2005) + "</font> " +
						"<font color=green>P2004:" + element.getClientProperty(DataFactory.PositionDec2004) + "</font> " +
						"<font color=black>R2005:" + element.getClientProperty(DataFactory.RatingsDec2005) + "</font> " +
						"<font color=blue>D2004:" + element.getClientProperty(DataFactory.DeltaDec2004) + "</font> " +
						"<font color=red>Status:" + element.getClientProperty(DataFactory.Status) + "</font> " +
						"</html>";
			}
		});
		tree.setElementIconGenerator(new Generator(){
			public Object generate(Object object) {
				Element element = (Element)object;
				return new CustomTreeIcon(element);
			}
		});
		
		ascend.addActionListener(this);
		sortPanel.add(ascend);
		
		addRadioButton(DataFactory.Delta).setForeground(Color.BLACK);
		addRadioButton(DataFactory.Language).setForeground(Color.BLUE);
		addRadioButton(DataFactory.PositionDec2005).setForeground(Color.RED);
		addRadioButton(DataFactory.PositionDec2004).setForeground(Color.GREEN);
		addRadioButton(DataFactory.RatingsDec2005).setForeground(Color.BLACK);
		addRadioButton(DataFactory.DeltaDec2004).setForeground(Color.BLUE);
		addRadioButton(DataFactory.Status).setForeground(Color.red);
		
		tree.setSortComparator(new Comparator(){
			public int compare(Object o1, Object o2) {
				if(sortKey == null){
					return 0;
				}
				Element e1 = (Element)o1;
				Element e2 = (Element)o2;
				Object v1 = e1.getClientProperty(sortKey);
				Object v2 = e2.getClientProperty(sortKey);
				if(v1 instanceof Comparable && v2 instanceof Comparable){
					Comparable c1 = (Comparable)v1;
					Comparable c2 = (Comparable)v2;
					if(ascend.isSelected()){
						return c1.compareTo(c2);
					}else{
						return -c1.compareTo(c2);
					}
				}
				return 0;
			}
		});
	}
	
	private JRadioButton addRadioButton(String key){
		JRadioButton button = new JRadioButton(key);
		button.addActionListener(this);
		group.add(button);
		sortPanel.add(button);
		return button;
	}
	
	public String getTitle() {
		return "Tree Sort Demo";
	}

	public String getHelp() {
		return "This demo creates a sortable tree and you can change sort ruler.";
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() instanceof JRadioButton){
			JRadioButton button = (JRadioButton)e.getSource();
			this.sortKey = button.getText();
		}
		this.tree.updateTViewUI();
	}

}

⌨️ 快捷键说明

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