📄 gridview.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.communication.connections.ConnectionManager;import com.sun.spot.communication.connections.ConnectionWorker;import com.sun.spot.communication.connections.RadioConnectionManager;import com.sun.spot.communication.messages.SpotMessage;import com.sun.spot.spotworld.SpotWorld;import com.sun.spot.spotworld.common.LocaleUtil;import com.sun.spot.spotworld.gui.ISpotWorldViewer;import com.sun.spot.spotworld.participants.Application;import com.sun.spot.spotworld.participants.Group;import com.sun.spot.spotworld.virtualobjects.IVirtualObject;import com.sun.spot.spotworld.common.ObjectMap;import com.sun.spot.spotworld.gui.IUIObject;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Point;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.Vector;import javax.swing.JPanel;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;/** * So named because it draws a grid as a background * @author arshan */public class GridView extends JPanel implements ChangeListener,MouseListener,MouseMotionListener,ISpotWorldViewer { // some spotworld accounting, we might want to collapse all functionality into // some single class SpotWorld theWorld; ObjectMap objectMap; WorldGrid grid; private int centerX, centerY; private double zoom, zoomOrig; /** Creates a new instance of GridView */ public GridView(SpotWorld world) { super(); theWorld=world; zoom = 0.2; init(); } /** * Initialize GridView. */ private void init() { setBackground(Color.WHITE); setLayout(null); setPreferredSize(new Dimension(800,640)); centerX = 0; centerY = 0; grid = new WorldGrid(this); addMouseListener(this); // setup the mappings of participants to my known classes objectMap = new ObjectMap(); objectMap.addDefault(GVObject.class); objectMap.addMap(Application.class, GVApplication.class); } public void msg(String str){ System.out.println("[GridView] " + str); } public void addVirtualObjectMapping(Class vObj, Class uiObj) { if (uiObj.getPackage().equals(GVObject.class.getPackage())) { msg( "Mapping " + vObj + " to " + uiObj); objectMap.addMap(vObj, uiObj); } } public void addVirtualObject(IVirtualObject obj) { obj.msg(LocaleUtil.getString("registering with gridview ...")); Class cls = objectMap.findMapping(obj.getClass()); // No support in GridView for groups if (cls == null || obj instanceof Group) { obj.warning(LocaleUtil.getString("GridView: no mapping for " + obj)); } else { try { // create the instance IUIObject uiObj = (IUIObject) (cls.newInstance()); // register forward and back pointers between the two. // NOTE: may want to change this mechanism so that its up to the // objects themselves if they want uni/bi directional link or none at all obj.addUIObject((IUIObject)uiObj); uiObj.setVirtualObject(obj); // add it to this viewer setInitialPositionInView((GVObject)uiObj); if(uiObj instanceof GVObject){ ((GVObject)uiObj).addToView(this); } deselectAll(); uiObj.select(); } catch (InstantiationException ex) { obj.error(LocaleUtil.getString("messed up instantiation during register process")); ex.printStackTrace(); } catch (IllegalAccessException ex) { obj.error(LocaleUtil.getString("illegal access during registration in gridview")); ex.printStackTrace(); } } } public void paint(Graphics g){ paintComponent(g); grid.paint(g); paintChildren(g); } // Why doesn't this work?// public void paintComponent(Graphics g){// grid.paint(g);// super.paintComponent(g);// } public void stateChanged(ChangeEvent changeEvent) { } public void mouseReleased(MouseEvent mouseEvent) { } public void mousePressed(MouseEvent mouseEvent) { deselectAll(); } public void mouseMoved(MouseEvent mouseEvent) { } public void mouseExited(MouseEvent mouseEvent) { } public void mouseEntered(MouseEvent mouseEvent) { } public void mouseDragged(MouseEvent mouseEvent) { } ConnectionManager connectionMgr = RadioConnectionManager.getInstance(); ConnectionWorker connectionWkr; SpotMessage spotMssg = new SpotMessage("LIST_ISOLATES"); public Vector<GVObject> getGridObjects(){ Vector<GVObject> v = new Vector<GVObject>(); for(Component c : getComponents()){ if(c instanceof GVObject) v.add((GVObject) c); } return v; } public void mouseClicked(MouseEvent mouseEvent) { if(mouseEvent.isShiftDown()){ //PLESE LEAVE THIS TEST CODE IN PLACE -- Thanks, Randy.// try {// System.out.println("Sending message....");// Vector[] r = connectionWkr.send(spotMssg);// for (int i = 0; i < r.length; i++) {// System.out.println("Reply includes: " + r[i]);// }// } catch (IOException ex) {// ex.printStackTrace();// } } else { //PLESE LEAVE THIS TEST CODE IN PLACE -- Thanks, Randy.// System.out.println("[GridView] " + LocaleUtil.getString("Listing components") + ":");// Component[] cs = getComponents();// for (int i = 0; i < cs.length; i++) {// System.out.println(LocaleUtil.getString("Component") + " " + i + " = " + cs[i]);// } } } public void setInitialPositionInView(GVObject g){ g.setInitialPositionInView(this);// if(g instanceof GVApplication) {((GVApplication)g).setInitialPositionInView(this); return; }// // Comment out to allow class loading, as GVeBasestation is now "unmentionable.""//// if(g instanceof GVeBasestation) {g.setLocation(20,20); return;}// int xInit = (getWidth() - g.getWidth())/2;// int yInit = (getHeight() - g.getHeight())/2;// Rectangle r = new Rectangle(xInit, yInit, g.getWidth(), g.getHeight());// boolean foundSpace = true;// GVObject[] gos = getGridObjects().toArray(new GVObject[0]);// Comparator c = new Comparator<GVObject>(){// public int compare(GVObject g1, GVObject g2) {// if (g1.getY() < g2.getY()) return -1;// if (g1.getY() > g2.getY()) return +1;// if (g1.getX() < g2.getX()) return -1;// if (g1.getX() > g2.getX()) return +1;// return 0;// }// };// Arrays.sort(gos,c);// for(GVObject gc : gos){// if(gc != g){// if(gc.getBounds().intersects(r)){// xInit = xInit + g.getWidth() + 20;// if(xInit > getWidth()){// xInit = 20;// yInit = yInit + gc.getHeight() + 20;// }// }// }// } // g.setLocation(xInit, yInit); } public Point getPositionInWindow(int worldX, int worldY) { int windowWidthHalf = getWidth() / 2; int windowHeightHalf = getHeight() / 2; int windowPositionX = (int)((double)(worldX - centerX) * zoom) + windowWidthHalf; int windowPositionY = (int)((double)(worldY - centerY) * zoom) + windowHeightHalf; return new Point(windowPositionX , windowPositionY); } public void selectOnly(Object obj) { deselectAll(); if(obj instanceof GVObject){ ((GVObject)obj).select(); } } public void selectAlso(Object obj) { } public void deselect(Object obj) { } public void deselectAll() { for(GVObject g : getGridObjects()){ g.deselect(); } repaint(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -