📄 gvtangibleobject.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.gridview;import com.sun.spot.spotworld.common.UICommand;import com.sun.spot.spotworld.common.LocaleUtil;import com.sun.spot.spotworld.gui.IUIObject;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.Vector;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;/** * A GVTangibleObject is a kind of GVObject that lives in GridView that can be manipulated. It can also be * animated with the Animator class. */public class GVTangibleObject extends GVObject implements MouseListener, MouseMotionListener, ActionListener { public void init() { super.init(); addMouseListener(this); } /* * * Mouse and keyboard events. * */ /* (non-Javadoc) * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) */ public void mouseClicked(MouseEvent e) { } /* (non-Javadoc) * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) */ public void mouseEntered(MouseEvent e) { } /* (non-Javadoc) * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) */ public void mouseExited(MouseEvent e) { } /* (non-Javadoc) * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) */ public void mousePressed(MouseEvent e) { // Tidied up this method to conform to conventions. -- Robert if(!( getLooseParent() instanceof GVTangibleObject)) return; // ctrl + click on a mac generates a isPopupTrigger event and a // mouse button 1 event. We need to supress the button 1 event if we are // doing a ctrl + click. -- Robert if ((e.getClickCount() == 1 && e.getButton() == e.BUTTON1) && !e.isPopupTrigger()) mouseButton1Pressed(e); if (e.getClickCount() == 1 && e.getButton() == e.BUTTON2) mouseButton2Pressed(e); if (e.getClickCount() == 1 && e.isPopupTrigger()) mouseButton3Pressed(e); } public void mouseReleased(MouseEvent e) { if (e.getButton() == e.BUTTON1 && !e.isPopupTrigger()) ((GVTangibleObject)getRoot()).mouseButton1Released(e); if (e.getButton() == e.BUTTON2) ((GVTangibleObject)getRoot()).mouseButton2Released(e); if (e.isPopupTrigger()) ((GVTangibleObject)getRoot()).mouseButton3Released(e); // Added fix for Windows. isPopupTrigger isn't true until mouseRelease // rather than mousePressed on a Mac. -- Robert if (e.getClickCount() == 1 && e.isPopupTrigger()) mouseButton3Pressed(e); } public synchronized void mouseButton1Pressed(MouseEvent e) { /* Try to pass the event to the loosePiece parent if * 1] there is one * 2] and it can handle mouse events. * NOTE: THIS DOES NOT WORK FOR SOME REASON. * The target does get the event, but it does not handle it * properly possibly because * * * Create a new InvisoHolderObject and add ourselves to it. * Then add it to the world. */ if(! e.isShiftDown() ) getView().deselectAll(); // If SPOT is already selected, deselect -- Robert if (e.isShiftDown() && isSelected()) deselect(); else select(); ShadowingHolder holder = new ShadowingHolder(); holder.setOriginator(this); GridView v = getView(); holder.addAndEncompass(this); holder.addToView(v); holder.setDragOffset(e.getX() ,e.getY()); addMouseMotionListener(holder); } public void mouseButton2Pressed(MouseEvent e) { } public void mouseButton3Pressed(MouseEvent e) { getPopupMenu().show(e.getComponent(), e.getX(), e.getY()); } public synchronized void mouseButton1Released(MouseEvent e) { removeMouseMotionListener(this); } public void mouseButton2Released(MouseEvent e) { } public void mouseButton3Released(MouseEvent e) { } public void mouseDragged(MouseEvent e) { ((GVTangibleObject)getRoot()).mouseDragged(e); } public void mouseMoved(MouseEvent e) { } /* * Subclasses should normally override getMenuItems, * and let this method be inherited as is. */ public JPopupMenu getPopupMenu(){ JPopupMenu menu = new JPopupMenu(); Vector<JMenuItem> v = getMenuItems(); for(JMenuItem item : v){ menu.add(item); } return menu; } public Vector<JMenuItem> getMenuItems(){ Vector<JMenuItem> v = new Vector<JMenuItem>(); return v; } // Is this the right place to put this? -- Robert protected Vector<JMenuItem> menuHelper(Vector<UICommand> commands) { Vector<JMenuItem> menuItems = new Vector<JMenuItem>(); for (UICommand command : commands) { final UICommand command2 = command; // check to see if the UICommand we have is a submenu if (command2.getSubUICommands().size() > 0) { JMenu submenu = new JMenu(command2.getName()); submenu.setToolTipText(command2.getToolTip()); // populate the submenu with UICommands Vector<JMenuItem> v = menuHelper(command2.getSubUICommands()); for (JMenuItem item : v) submenu.add(item); menuItems.add(submenu); } else { JMenuItem menuItem = new JMenuItem(command2.getName()); menuItem.setToolTipText(command2.getToolTip()); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { command2.run((IUIObject) GVTangibleObject.this); } }); menuItems.add(menuItem); } } return menuItems; } public void actionPerformed(ActionEvent actionEvent) { if (actionEvent.getActionCommand().equals(LocaleUtil.getString("Hide"))) { GridView gv = getView(); removeFromView(); gv.repaint(); } } /** * Animate from current position to final position. * * @param goalObject If null, xF, Yf indicate target position, else they are offsets from this object. * @param xF position to move to, x coordinate, or if goalObject is not null, offset from the goalObject. * @param yF position to move to, y coordinate, or if goalObject is not null, offset from the goalObject. * @param dur duration in seconds * @param dt step size of animation, in seconds * @param slowIn fraction of time (dur) spent in initial acceleration (slow in) portion of animation * @param slowOut fraction of time (dur)spent in deceleration. */ public synchronized void animateTo(GVObject goalObject, final int xF, final int yF, final double dur, final double dt, final double slowIn, final double slowOut) { killAllAnimations(); currentAnimator = new Animator(this, goalObject, xF, yF, dur, dt, slowIn, slowOut); currentAnimator.start(); } public synchronized void animateToAndBlock(GVObject goalObject, final int xF, final int yF, final double dur, final double dt, final double slowIn, final double slowOut) { killAllAnimations(); try { animateTo(goalObject, xF, yF, dur, dt, slowIn, slowOut); currentAnimator.join(); } catch (InterruptedException e) { /* * What can we do? Print a trace. * Animation has failed in some bizarre way. */ e.printStackTrace(); } } public void killAllAnimations() { if (currentAnimator != null) { currentAnimator.stopAllAnimations(); try { currentAnimator.join(); } catch (InterruptedException e) { /* * What to do? Again, something weird has happened in the Animator. * We simply print out a stack trace. */ e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -