📄 locationlayer.java
字号:
// If there isn't a worker thread working on this already, // create a thread that will do the real work. If there is // a thread working on this, then set the cancelled flag // in the layer. doPrepare(); } /** * The LocationWorker calls this method on the layer when it is done * working. If the calling worker is not the same as the "current" worker, * then a new worker is created. * * @param worker the worker that has the graphics. */ protected synchronized void workerComplete(LocationWorker worker) { if (!isCancelled()) { currentWorker = null; setGraphicList((Vector) worker.get()); repaint(); } else { setCancelled(false); currentWorker = new LocationWorker(); currentWorker.execute(); } } /** * A method that will launch a Worker to fetch the data. This is the method * to call if you want the layer to refresh it's graphics from the location * handlers. The layer will repaint itself automatically. */ public void doPrepare() { // If there isn't a worker thread working on a projection // changed or other doPrepare call, then create a thread that // will do the real work. If there is a thread working on // this, then set the cancelled flag in the layer. if (currentWorker == null) { currentWorker = new LocationWorker(); currentWorker.execute(); } else setCancelled(true); } /** * Prepares the graphics for the layer. This is where the getRectangle() * method call is made on the location. * <p> * Occasionally it is necessary to abort a prepare call. When this happens, * the map will set the cancel bit in the LayerThread, (the thread that is * running the prepare). If this Layer needs to do any cleanups during the * abort, it should do so, but return out of the prepare asap. */ public synchronized Vector prepare() { if (isCancelled()) { if (Debug.debugging("location")) { Debug.output(getName() + "|LocationLayer.prepare(): aborted."); } return null; } Vector omGraphicList = new Vector(); Projection projection = getProjection(); if (projection == null) { if (Debug.debugging("location")) { Debug.output(getName() + "|LocationLayer.prepare(): null projection, layer not ready."); } return omGraphicList; } if (Debug.debugging("location")) { Debug.output(getName() + "|LocationLayer.prepare(): doing it"); } if (useDeclutterMatrix && declutterMatrix != null) { declutterMatrix.setWidth(projection.getWidth()); declutterMatrix.setHeight(projection.getHeight()); declutterMatrix.create(); } // Setting the OMGraphicsList for this layer. Remember, the // Vector is made up of OMGraphics, which are generated // (projected) when the graphics are added to the list. So, // after this call, the list is ready for painting. // call getRectangle(); if (Debug.debugging("location")) { Debug.output(getName() + "|LocationLayer.prepare(): " + "calling prepare with projection: " + projection + " ul = " + projection.getUpperLeft() + " lr = " + projection.getLowerRight()); } LatLonPoint ul = projection.getUpperLeft(); LatLonPoint lr = projection.getLowerRight(); if (Debug.debugging("location")) { float delta = lr.getLongitude() - ul.getLongitude(); Debug.output(getName() + "|LocationLayer.prepare(): " + " ul.lon =" + ul.getLongitude() + " lr.lon = " + lr.getLongitude() + " delta = " + delta); } if (dataHandlers != null) { for (int i = 0; i < dataHandlers.length; i++) { ((LocationHandler) dataHandlers[i]).get(ul.getLatitude(), ul.getLongitude(), lr.getLatitude(), lr.getLongitude(), omGraphicList); } } // /////////////////// // safe quit int size = 0; if (omGraphicList != null) { size = omGraphicList.size(); if (Debug.debugging("basic")) { Debug.output(getName() + "|LocationLayer.prepare(): finished with " + size + " graphics"); } // Don't forget to project them. Since they are only // being recalled if the projection hase changed, then // we need to force a reprojection of all of them // because the screen position has changed. Enumeration things = omGraphicList.elements(); while (things.hasMoreElements()) { OMGraphic thingy = (OMGraphic) things.nextElement(); if (useDeclutterMatrix && thingy instanceof Location) { Location loc = (Location) thingy; loc.generate(projection, declutterMatrix); } else { thingy.generate(projection); } } } else if (Debug.debugging("basic")) { Debug.output(getName() + "|LocationLayer.prepare(): finished with null graphics list"); } return omGraphicList; } /** * Paints the layer. * * @param g the Graphics context for painting */ public void paint(java.awt.Graphics g) { if (Debug.debugging("location")) { Debug.output(getName() + "|LocationLayer.paint()"); } Vector vlist = getGraphicList(); Object[] list = null; if (vlist != null) list = vlist.toArray(); if (list != null) { int i; OMGraphic loc; // Draw from the bottom up, so it matches the palette, and // the order in which the handlers were loaded - the first // in the list is on top. // We need to go through list twice. The first time, draw // all the regular OMGraphics, and also draw all of the // graphics for the locations. The second time through, // draw the labels. This way, the labels won't be covered // up by graphics. for (int j = 0; j < 2; j++) { for (i = list.length - 1; i >= 0; i--) { loc = (OMGraphic) list[i]; if (j == 0) { if (loc instanceof Location) { ((Location) loc).renderLocation(g); } else { loc.render(g); } } else if (loc instanceof Location) { ((Location) loc).renderName(g); } } } } else { if (Debug.debugging("location")) { Debug.error(getName() + "|LocationLayer: paint(): Null list..."); } } } /** * Parse the properties and set up the location handlers. The prefix will * should be null, or a prefix string with a period at the end, for scoping * purposes. */ protected void setLocationHandlers(String prefix, Properties p) { String handlersValue = p.getProperty(prefix + LocationHandlerListProperty); if (Debug.debugging("location")) { Debug.output(getName() + "| handlers = \"" + handlersValue + "\""); } if (handlersValue == null) { if (Debug.debugging("location")) { Debug.output("No property \"" + prefix + LocationHandlerListProperty + "\" found in application properties."); } return; } // Divide up the names ... StringTokenizer tokens = new StringTokenizer(handlersValue, " "); Vector handlerNames = new Vector(); while (tokens.hasMoreTokens()) { handlerNames.addElement(tokens.nextToken()); } if (Debug.debugging("location")) { Debug.output("OpenMap.getLocationHandlers(): " + handlerNames); } int nHandlerNames = handlerNames.size(); Vector handlers = new Vector(nHandlerNames); Vector goodNames = new Vector(nHandlerNames); for (int i = 0; i < nHandlerNames; i++) { String handlerName = (String) handlerNames.elementAt(i); String classProperty = handlerName + ".class"; String className = p.getProperty(classProperty); String nameProperty = handlerName + ".prettyName"; String prettyName = p.getProperty(nameProperty); if (className == null) { Debug.error("Failed to locate property \"" + classProperty + "\"\nSkipping handler \"" + handlerName + "\""); continue; } try { if (Debug.debugging("location")) { Debug.output("OpenMap.getHandlers():instantiating handler \"" + className + "\""); } Object obj = Class.forName(className).newInstance();// Works // for // applet! if (obj instanceof LocationHandler) { LocationHandler lh = (LocationHandler) obj; lh.setProperties(handlerName, p); lh.setLayer(this); handlers.addElement(lh); goodNames.addElement(prettyName != null ? prettyName : ""); } if (false) throw new java.io.IOException();// fool javac // compiler } catch (java.lang.ClassNotFoundException e) { Debug.error("Handler class not found: \"" + className + "\"\nSkipping handler \"" + handlerName + "\""); } catch (java.io.IOException e) { Debug.error("IO Exception instantiating class \"" + className + "\"\nSkipping handler \"" + handlerName + "\""); } catch (Exception e) { Debug.error("Exception instantiating class \"" + className + "\": " + e); } } int nHandlers = handlers.size(); dataHandlers = new LocationHandler[nHandlers]; dataHandlerNames = new String[nHandlers]; if (nHandlers != 0) { handlers.copyInto(dataHandlers); goodNames.copyInto(dataHandlerNames); } } /** * Let the LocationHandlers know that the layer has been removed. */ public void removed(java.awt.Container cont) { if (dataHandlers != null) { for (int i = 0; i < dataHandlers.length; i++) { ((LocationHandler) dataHandlers[i]).removed(cont); } } } /** * Set the LocationHandlers for the layer. Make sure you update the * LocationHandler names, too, so the names coorespond to these. * * @param handlers an array of LocationHandlers. */ public void setLocationHandlers(LocationHandler[] handlers) { dataHandlers = handlers; // Need to set the layer on the handlers. for (int i = 0; i < handlers.length; i++) { handlers[i].setLayer(this); } resetPalette(); } /** * Get the LocationHandlers for this layer. */ public LocationHandler[] getLocationHandlers() { return dataHandlers; } /** * Set the LocationHandler names suitable for a GUI. Make sure these end up * cooresponding to the LocationHandlers. * * @param handlerNames an array of Strings. */ public void setLocationHandlerNames(String[] handlerNames) { dataHandlerNames = handlerNames; resetPalette(); } /** * Get the LocationHandlers for this layer. */ public String[] getLocationHandlerNames() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -