예제 #1
0
  /**
   * Adds the layer with the information in an XML entity and the specified name, to this collection
   * of layers.
   *
   * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect
   * </code>, <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>
   * FLayers</code>), or another kind of layer (<code>FLayer</code>)), and the driver in the layer.
   *
   * @param xml tree-node structure with information about layers
   * @param name name of the layer to add
   * @throws LoadLayerException if fails loading this layer.
   */
  private void addLayerFromXML(XMLEntity xml, String name) throws LoadLayerException {
    FLayer layer = null;

    try {
      if (name == null) {
        name = xml.getName();
      }

      String className = xml.getStringProperty("className");
      Class clase = Class.forName(className);
      layer = (FLayer) clase.newInstance();
      if (FLayers.class.isAssignableFrom(clase)) {
        ((FLayers) layer).setMapContext(getMapContext());
        ((FLayers) layer).setParentLayer(this);
        //			layer = new FLayers(getMapContext(),this);
        layer.setXMLEntity(xml);
      } else {
        // Capas Nuevas (externas)
        layer.setName(name);
        layer.setXMLEntity(xml);
        layer.load();
      }

      //		//TODO VCN FLyrAnnotation es un parche para no tener que duplicar todo el código de aqí y
      // de los diferentes métodos de LayerFactory,
      //		//ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver
      // Vectorial.
      //		if (className.equals(FLyrVect.class.getName())){// ||
      // className.equals(FLyrAnnotation.class.getName())) {
      //			String type = xml.getStringProperty("type");
      //			if ("vectorial".equals(type)){
      //				//String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
      //				IProjection proj = null;
      //				if (xml.contains("proj")) {
      //					proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
      //				}
      //				else
      //				{
      //					proj = this.getMapContext().getViewPort().getProjection();
      //				}
      //				if (xml.contains("file")) {
      //					Driver d;
      //					try {
      //						d = LayerFactory.getDM().getDriver(xml.getStringProperty("driverName"));
      //					} catch (DriverLoadException e1) {
      //						throw new DriverLayerException(name,e1);
      //					}
      //					layer = LayerFactory.createLayer(name, (VectorialFileDriver) d,
      //							new File(xml.getStringProperty("file")),
      //							proj);
      //
      //
      //				}
      //				if (xml.contains("db")) {
      //
      //					String driverName = xml.getStringProperty("db");
      //					IVectorialDatabaseDriver driver;
      //					try {
      //						driver = (IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
      //						//Hay que separar la carga de los datos del XMLEntity del load.
      //						driver.setXMLEntity(xml.getChild(2));
      //
      //						boolean loadOk = false;
      //						((DefaultJDBCDriver)driver).load();
      //						if (((DefaultJDBCDriver)driver).getConnection() != null) {
      //							loadOk = true;
      //						}
      //						layer = LayerFactory.createDBLayer(driver, name, proj);
      //						if (!loadOk) {
      //							layer.setAvailable(false);
      //						}
      //
      //					} catch (DriverLoadException e) {
      //						throw new DriverLayerException(name,e);
      //					} catch (XMLException e) {
      //						throw new DriverLayerException(name,e);
      //					} catch (ReadException e) {
      //						throw new DriverLayerException(name,e);
      //					}
      //
      //				}
      //				// Clases con algun driver genérico creado por otro
      //				// programador
      //				if (xml.contains("other")) {
      //
      //					String driverName = xml.getStringProperty("other");
      //					VectorialDriver driver = null;
      //					try {
      //						driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
      //					} catch (DriverLoadException e) {
      //						// Si no existe ese driver, no pasa nada.
      //						// Puede que el desarrollador no quiera que
      //						// aparezca en el cuadro de diálogo y ha metido
      //						// el jar con sus clases en nuestro directorio lib.
      //						// Intentamos cargar esa clase "a pelo".
      //						if (xml.getChild(2).contains("className"))
      //						{
      //							String className2 = xml.getChild(2).getStringProperty("className");
      //							try {
      //								driver = (VectorialDriver) Class.forName(className2).newInstance();
      //							} catch (Exception e1) {
      //								throw new DriverLayerException(name,e);
      //							}
      //						}
      //					} catch (NullPointerException npe) {
      //						// Si no existe ese driver, no pasa nada.
      //						// Puede que el desarrollador no quiera que
      //						// aparezca en el cuadro de diálogo y ha metido
      //						// el jar con sus clases en nuestro directorio lib.
      //						// Intentamos cargar esa clase "a pelo".
      //						if (xml.getChild(2).contains("className"))
      //						{
      //							String className2 = xml.getChild(2).getStringProperty("className");
      //							try {
      //								driver = (VectorialDriver) Class.forName(className2).newInstance();
      //							} catch (Exception e1) {
      //								throw new DriverLayerException(name,e1);
      //							}
      //						}
      //					}
      //					if (driver instanceof IPersistence)
      //					{
      //						IPersistence persist = (IPersistence) driver;
      //						persist.setXMLEntity(xml.getChild(2));
      //					}
      //					layer = LayerFactory.createLayer(name, driver, proj);
      //				}
      //
      //			}
      //
      //			//TODO VCN FLyrAnnotation es un parche para no tener que duplicar todo el código de aqí y
      // de los diferentes métodos de LayerFactory,
      //			//ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver
      // Vectorial.
      //			if (className.equals(FLyrAnnotation.class.getName())){
      //				layer=FLyrAnnotation.createLayerFromVect((FLyrVect)layer);
      //			}
      //
      //
      //			layer.setXMLEntity(xml);
      //
      //		} else {
      //			Class clase = LayerFactory.getLayerClassForLayerClassName(className);
      //			layer = (FLayer) clase.newInstance();
      //			if (clase.isAssignableFrom(FLayers.class)) {
      //				((FLayers)layer).setMapContext(getMapContext());
      //				((FLayers)layer).setParentLayer(this);
      ////				layer = new FLayers(getMapContext(),this);
      //				layer.setXMLEntity(xml);
      //			} else {
      //				// Capas Nuevas (externas)
      //				layer.setName(name);
      //				layer.setXMLEntity(xml);
      //				layer.load();
      //			}
      //		}
      this.addLayer(layer);
      logger.debug("layer: " + layer.getName() + " loaded");
      // Comprobar que la proyección es la misma que la de FMap
      // Si no lo es, es una capa que está reproyectada al vuelo
      IProjection proj = layer.getProjection();
      if ((proj != null)) {
        if (!proj.getFullCode().equals(getMapContext().getProjection().getFullCode())) {
          ICoordTrans ct = proj.getCT(getMapContext().getProjection());
          // TODO: REVISAR CON LUIS
          // Se lo fijamos a todas, luego cada una que se reproyecte
          // si puede, o que no haga nada

          layer.setCoordTrans(ct);
        }
      }
    } catch (XMLException e) {
      fmap.addLayerError(xml.getStringProperty("name"));
      throw new LoadLayerException(name, e);
    } catch (ClassNotFoundException e) {
      fmap.addLayerError(xml.getStringProperty("name"));
      throw new LoadLayerException(name, e);
    } catch (InstantiationException e) {
      fmap.addLayerError(xml.getStringProperty("name"));
      throw new LoadLayerException(name, e);
    } catch (IllegalAccessException e) {
      fmap.addLayerError(xml.getStringProperty("name"));
      throw new LoadLayerException(name, e);
    } catch (LoadLayerException e) {
      fmap.addLayerError(xml.getStringProperty("name"));
      throw e;
    }
  }
예제 #2
0
  /**
   * Checks all layers (each one as a sub-node of this node <i>collection of layers</i>) of this
   * collection and draws their requested properties. If a node is a group of layers (<code>
   * ComposedLayer</code>), executes it's drawn.
   *
   * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn
   * together. And once the <code>
   * group</code> is drawn, will be set to <code>null</code> if hasn't a parent layer.
   *
   * <p>The particular implementation depends on the kind of each layer and composed layer. And this
   * process can be cancelled at any time by the shared object <code>cancel</code>.
   *
   * <p>According the print quality, labels will be printed in different resolution:
   *
   * <ul>
   *   <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).
   *   <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).
   *   <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).
   * </ul>
   *
   * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
   * @param viewPort the information for drawing the layers
   * @param cancel shared object that determines if this layer can continue being drawn
   * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and
   *     {@linkplain FLayer#getMaxScale()}.
   * @param properties properties that will be print
   * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the
   *     composed layer
   * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null
   *     </code>; otherwise, the <code>group</code>
   * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double, PrintAttributes)
   * @throws ReadDriverException if fails the driver reading the data.
   */
  public ComposedLayer print_old(
      Graphics2D g,
      ViewPort viewPort,
      Cancellable cancel,
      double scale,
      PrintAttributes properties,
      ComposedLayer group)
      throws ReadException {
    double dpi = 72;

    int resolution = properties.getPrintQuality();
    if (resolution == PrintAttributes.PRINT_QUALITY_NORMAL) {
      dpi = 300;
    } else if (resolution == PrintAttributes.PRINT_QUALITY_HIGH) {
      dpi = 600;
    } else if (resolution == PrintAttributes.PRINT_QUALITY_DRAFT) {
      dpi = 72;
    }

    // TODO: A la hora de imprimir, isWithinScale falla, porque está
    // calculando la escala en pantalla, no para el layout.
    // Revisar esto.

    // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer
    // group.
    for (int i = 0; i < layers.size(); i++) {
      FLayer lyr = (FLayer) layers.get(i);
      if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
        continue;
      }

      try {

        ///// CHEMA ComposedLayer
        // Checks for draw group (ComposedLayer)
        if (group != null) {
          if (lyr instanceof FLayers) {
            group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
          } else {
            // If layer can be added to the group, does it
            if (lyr instanceof ILabelable
                && ((ILabelable) lyr).isLabeled()
                && ((ILabelable) lyr).getLabelingStrategy() != null
                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
              group.add(lyr);
            } else {
              // draw the 'pending to draw' layer group
              group.print(g, viewPort, cancel, scale, properties);

              // gets a new group instance
              if (lyr instanceof ILabelable
                  && ((ILabelable) lyr).isLabeled()
                  && ((ILabelable) lyr).getLabelingStrategy() != null
                  && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
                group = lyr.newComposedLayer();
              } else {
                group = null;
              }
              // if layer hasn't group, draws it inmediately
              if (group == null) {
                if (lyr instanceof FLayers) {
                  group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
                } else {
                  lyr.print(g, viewPort, cancel, scale, properties);
                  if (lyr instanceof ILabelable
                      && ((ILabelable) lyr).isLabeled()
                      && ((ILabelable) lyr).getLabelingStrategy() != null
                      && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
                    ILabelable lLayer = (ILabelable) lyr;
                    lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
                  }
                }
              } else {
                // add the layer to the group
                group.setMapContext(fmap);
                group.add(lyr);
              }
            }
          }
        } else {
          // gets a new group instance
          group = lyr.newComposedLayer();
          // if layer hasn't group, draws it inmediately
          if (group == null) {
            if (lyr instanceof FLayers) {
              group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
            } else {
              lyr.print(g, viewPort, cancel, scale, properties);
              if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
                ILabelable lLayer = (ILabelable) lyr;

                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
              }
            }
          } else {
            // add the layer to the group
            group.setMapContext(fmap);
            group.add(lyr);
          }
        }
        ///// CHEMA ComposedLayer

      } catch (Exception e) {
        String mesg =
            Messages.getString("error_printing_layer")
                + " "
                + lyr.getName()
                + ": "
                + e.getMessage();
        fmap.addLayerError(mesg);
        logger.error(mesg, e);
      }
    }

    ///// CHEMA ComposedLayer
    if (group != null && this.getParentLayer() == null) {
      // si tenemos un grupo pendiente de pintar, pintamos
      group.print(g, viewPort, cancel, scale, properties);
      group = null;
    }
    ///// CHEMA ComposedLayer

    //		if (getVirtualLayers() != null) {
    //			getVirtualLayers().print( g, viewPort, cancel, scale, properties);
    //		}

    ///// CHEMA ComposedLayer
    return group;
    ///// CHEMA ComposedLayer
  }