📄 spotworld.java
字号:
} } } } } } catch (TimeoutException tex) { // timeout - printing no packet received can flood your output screen. // System.out.println("[SPOT World] No Packet Recieved"); } } catch (IOException ex) { // ignore } Utils.sleep(1000); } } }; Thread rxThread = new Thread(rxRunnable); rxThread.start(); } /* put test code in here */ public void dummy() { } public Vector<ESpotBasestation> getBasestations(){ //Filter on the list of objects in this world. Vector<ESpotBasestation> result = new Vector<ESpotBasestation>(); for(IVirtualObject vo : getVirtualObjectsCopy()){ if(vo instanceof ESpotBasestation){ result.add((ESpotBasestation) vo); } } return result; } public Vector<Group> getGroups(){ Vector<Group> r = new Vector<Group>(); for(IVirtualObject vo : getVirtualObjectsCopy()){ if(vo instanceof Group){ r.add((Group)vo); } } return r; } public Group getGroupNamed(String nm){ for(Group g : getGroups()){ if(g.getName().equals(nm)){ return g; } } return null; } public void removeVirtualObject(IVirtualObject obj) { super.removeVirtualObject(obj); } public void addVirtualObject(IVirtualObject obj) { msg(LocaleUtil.getString("adding new virtual object") + " : " + obj + " and notifying all viewers."); super.addVirtualObject(obj); // also add the appropriate object to each viewer Iterator<ISpotWorldViewer> iterator = viewers.iterator(); ISpotWorldViewer viewer; while ( iterator.hasNext()) { viewer = (ISpotWorldViewer) iterator.next(); viewer.addVirtualObject(obj); } if(obj instanceof SquawkHost){ getGroupNamed("All").addElement(obj); } } public void startSPOTCastTxThread() { if(SPOTCastTxing){ msg("Tried to start transmitting SPOTCasts, but it appears we already are."); return; } try { if(SPOTCastTxConn == null) SPOTCastTxConn = (DatagramConnection) Connector.open("radiogram://broadcast:47"); } catch (IOException ex) { System.out.println("Could not open radiogram broadcast connection"); ex.printStackTrace(); return; } SPOTCastTxing = true; Runnable r = new Runnable(){ public void run() { Random rand = new Random(); while(SPOTCastTxing) { try { Datagram dg = SPOTCastTxConn.newDatagram(SPOTCastTxConn.getMaximumLength()); dg.writeByte(SPOTWORLD_BEACON_PACKET); dg.writeLong(spotWorldID); SPOTCastTxConn.send(dg); } catch (IOException ex) { ex.printStackTrace(); } int extraTime = rand.nextInt(2000); Utils.sleep(6000 + extraTime); } } }; (new Thread(r)).start(); } public boolean isReadyToAddVirtualObjects() { return readyToAddVirtualObjects; } public void setReadyToAddVirtualObjects(boolean readyToAddVirtualObjects) { this.readyToAddVirtualObjects = readyToAddVirtualObjects; } private Map<String, String> views = null; /** * Looks in SPOTWorld/views directory for available views to use * loads the jars into SPOTWorld (but not run them) and adds their * names into a data structure. */ private void findAvailableViews() { File dir = new File(System.getProperty("user.dir") + "/views"); File[] files = dir.listFiles(); FilenameFilter filter = new FilenameFilter() { public boolean accept(File file, String name) { return name.endsWith("jar"); } }; files = dir.listFiles(filter); views = new HashMap<String, String>(); // Generate an array of urls for the class viewLoader URL[] urls = new URL[files.length]; for (int i = 0; i < urls.length; i++) { try { urls[i] = new URL("jar:file:" + files[i] + "!/"); } catch (MalformedURLException ex) { ex.printStackTrace(); } } loader.addURLs(urls); // Get the class paths and short names (used for menus) for all the loaded views for (URL url : urls) { try { JarURLConnection conn = (JarURLConnection) url.openConnection(); String name = conn.getManifest().getMainAttributes().getValue("View-Name"); String shortName = conn.getManifest().getMainAttributes().getValue("SPOTWorld-Name"); views.put(shortName, name); } catch (IOException ex) { ex.printStackTrace(); } } } public Vector<Application> getApplications(){ Vector<Application> reply = new Vector<Application>(); for(IVirtualObject vo : getVirtualObjectsCopy()){ if(vo instanceof Application){ reply.add((Application)vo); } }; return reply; } /** * Returns the available views. The available views are the jar files in * the SPOTWOrld/view dir and are loaded at run-time. */ public Vector<String> getAvailableViews() { if (views == null) findAvailableViews(); return new Vector(views.keySet()); } private HashMap<String, Component> viewCache = new HashMap<String, Component>(); /** * Returns requested view as a component. * * @param viewName name of the view. */ public Component getView(String viewName) { if (views == null) findAvailableViews(); try { if (viewCache.get(viewName) == null) { // Create a new view object msg("About to load the following class: " + views.get(viewName) + " associated with the name " + viewName); Class c = loader.loadClass(views.get(viewName)); Constructor constructor = c.getConstructor(SpotWorld.class); ISpotWorldViewer view = (ISpotWorldViewer) constructor.newInstance(this); // Tell our view what objects it can draw for (Vector<String> classNViews : getVOClassesWithViews()) { for (String voItem : classNViews) { view.addVirtualObjectMapping(loader.loadClass(classNViews.firstElement()), loader.loadClass(voItem)); } } // Add the view to our view data structure viewers.add(view); // Tell the view what we know for(IVirtualObject vo : getVirtualObjectsCopy()) { view.addVirtualObject(vo); } // Add the view to a scroll pane. JScrollPane scrollPane = new JScrollPane((Component) view); scrollPane.setName(viewName); viewCache.put(viewName, scrollPane); return scrollPane; } else { return viewCache.get(viewName); } } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (SecurityException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.getCause().getMessage(); // Robert, I am not sure why you added this -- Randy ex.printStackTrace(); } catch (NoSuchMethodException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } return null; } /** * Finds all available virtual object jars * */ private void initVOClassesWithViews() { // look in /virtualObjects directory for jar files. File dir = new File(System.getProperty("user.dir") + "/virtualObjects"); FilenameFilter filter = new FilenameFilter() { public boolean accept(File file, String name) { return name.endsWith("jar"); } }; File[] files = dir.listFiles(filter); voClassesWithViews = new Vector<Vector<String>>(); // Convert all the jar files found into in an array of URLs which will // be passed to the virtual object class loader. URL[] urls = new URL[files.length]; for (int i = 0; i < urls.length; i++) { try { urls[i] = new URL("jar:file:" + files[i] + "!/"); } catch (MalformedURLException ex) { ex.printStackTrace(); } } loader.addURLs(urls); // Get the class paths and short names (used for menus) for all the loaded views for (URL url : urls) { try { JarURLConnection conn = (JarURLConnection) url.openConnection(); Vector<String> vo = new Vector<String>(); vo.addElement(conn.getManifest().getMainAttributes().getValue("VO-Name")); String supportedViews = conn.getManifest().getMainAttributes().getValue("Supported-Views"); String[] viewNames = supportedViews.split(","); for (String viewName : viewNames) vo.addElement(viewName.trim()); voClassesWithViews.addElement(vo); } catch (IOException ex) { ex.printStackTrace(); } } } public Vector<Vector<String>> getVOClassesWithViews() { if (voClassesWithViews == null) initVOClassesWithViews(); return voClassesWithViews; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -