📄 treeview.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.treeview;import com.sun.spot.spotworld.participants.Application;import com.sun.spot.spotworld.participants.Group;import java.awt.Insets;import java.awt.Rectangle;import java.awt.dnd.Autoscroll;import java.awt.dnd.DnDConstants;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseListener;import java.awt.event.MouseEvent;import java.awt.Point;import java.util.Enumeration;import java.util.Vector;import javax.swing.JPopupMenu;import javax.swing.JMenuItem;import javax.swing.JTree;import javax.swing.SwingUtilities;import javax.swing.event.TreeModelEvent;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.TreeNode;import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;import com.sun.spot.spotworld.SpotWorld;import com.sun.spot.spotworld.common.ObjectMap;import com.sun.spot.spotworld.common.IStateListener;import com.sun.spot.spotworld.gui.IUIObject;import com.sun.spot.spotworld.gui.ISpotWorldViewer;import com.sun.spot.spotworld.virtualobjects.IVirtualObject;/** * represents SPOTs, grops of SPOTs (not quite yet) and their applications as elemnts in a heirarchical list. * @author taylorr */public class TreeView extends JTree implements MouseListener, ISpotWorldViewer, IStateListener, Autoscroll { private SpotWorld world = null; private DefaultTreeModel model = null; private ObjectMap objectMap = null; private TVObject rootNode = null; public TreeView(SpotWorld world) { super(); this.world = world; init(); } public void init() { objectMap = new ObjectMap(); // These need to be removed if at some point groups and // applications are class loaded. -- Roibert objectMap.addDefault(TVObject.class); objectMap.addMap(Group.class, TVGroup.class); objectMap.addMap(Application.class, TVApplication.class); // The root of the tree is a representation of SPOTWorld TVSPOTWorld tvsw = new TVSPOTWorld(); tvsw.setVirtualObject(world); tvsw.registerStateChangeListener(this); world.addUIObject(tvsw); // Tree stuff setDragEnabled(true); model = new DefaultTreeModel(tvsw); setModel(model); getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); TreeViewDragAndDrop tvDND = new TreeViewDragAndDrop(this, DnDConstants.ACTION_MOVE); addMouseListener(this); setShowsRootHandles(true); setCellRenderer(new TreeViewRenderer()); } public void msg(String str){ System.out.println("[TreeView] " + str); } // MouseListener Events public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} // isPopupTrigger() is false on mousePressed but true on mouseReleased // in Windows. On a mac it's opposite, so process the menu event in both // handlers -- Robert public void mouseReleased(MouseEvent e) { processTreeViewMouseEvent(e); } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { processTreeViewMouseEvent(e); } private void processTreeViewMouseEvent(MouseEvent e) { int selRow = getRowForLocation(e.getX(), e.getY()); TreePath selPath = getPathForLocation(e.getX(), e.getY()); if(selRow != -1) { // If we have clicked on a node // Right click on a node if(e.getClickCount() == 1 && e.isPopupTrigger()) { ((TVObject) selPath.getLastPathComponent()).getPopupMenu().show(e.getComponent(), e.getX(), e.getY()); } // left click; reset selection and select this node. if(e.getClickCount() == 1 && e.getButton() == e.BUTTON1 && !e.isShiftDown()) { deselectAll(); ((TVObject) selPath.getLastPathComponent()).select(); } // shift and click; add/remove clicked node to selection. if(e.getClickCount() == 1 && e.getButton() == e.BUTTON1 && e.isShiftDown()) { // if node is already selected deselect if (((TVObject) selPath.getLastPathComponent()).isSelected()) ((TVObject) selPath.getLastPathComponent()).deselect(); // else add node to current selection else ((TVObject) selPath.getLastPathComponent()).select(); } } else { // we never clicked on a node // Popup TreeView menu if(e.getClickCount() == 1 && e.isPopupTrigger()) { JMenuItem group = new JMenuItem("Group"); group.setActionCommand("Group"); group.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { world.addVirtualObject(new Group("New Group")); } }); JPopupMenu menu = new JPopupMenu(); menu.add(group); menu.show(e.getComponent(), e.getX(), e.getY()); } else deselectAll(); } } // ISpotWorldViewer methods public void selectOnly(Object obj) {} public void selectAlso(Object obj) {} public void deselect(Object obj) {} // TreeModelListenerEvents public void treeNodesChanged(TreeModelEvent treeModelEvent) { } public void treeNodesInserted(TreeModelEvent treeModelEvent) { } public void treeNodesRemoved(TreeModelEvent treeModelEvent) { } public void treeStructureChanged(TreeModelEvent treeModelEvent) { } // Maybe we should move these two methods into a subclass of TreeModel... public void deselectAll() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -