/** put all the needed things in the blackboards */
  private void commitToBlackboards(
      JGrassRegion jgR, CoordinateReferenceSystem crs, String windPath) {
    blackboard = getLayer().getMap().getBlackboard();

    style.north = (float) jgR.getNorth();
    style.south = (float) jgR.getSouth();
    style.east = (float) jgR.getEast();
    style.west = (float) jgR.getWest();
    style.rows = jgR.getRows();
    style.cols = jgR.getCols();

    style.windPath = windPath;

    try {

      try {
        Integer epsg = CRS.lookupEpsgCode(crs, true);
        style.crsString = "EPSG:" + epsg;
      } catch (Exception e) {
        // try non epsg
        style.crsString = CRS.lookupIdentifier(crs, true);
      }
    } catch (FactoryException e) {
      e.printStackTrace();
    }

    blackboard.put(ActiveregionStyleContent.ID, style);
    getLayer().setStatus(ILayer.DONE);
  }
 private void setWidgetsToWindow(JGrassRegion window) {
   northText.setText(String.valueOf(window.getNorth()));
   southText.setText(String.valueOf(window.getSouth()));
   westText.setText(String.valueOf(window.getWest()));
   eastText.setText(String.valueOf(window.getEast()));
   rowsText.setText(String.valueOf(window.getRows()));
   colsText.setText(String.valueOf(window.getCols()));
   xresText.setText(String.valueOf(window.getWEResolution()));
   yresText.setText(String.valueOf(window.getNSResolution()));
 }
  public void update(Object updatedObject) {
    if (updatedObject instanceof List) {
      String text = null;
      List layers = (List) updatedObject;
      for (Object layer : layers) {
        if (layer instanceof JGrassMapGeoResource) {
          JGrassMapGeoResource rasterMapResource = (JGrassMapGeoResource) layer;
          try {
            text = rasterMapResource.getInfo(null).getName();

            JGrassRegion fileWindow = rasterMapResource.getFileWindow();
            if (fileWindow != null) {
              setWidgetsToWindow(fileWindow);
            }
          } catch (IOException e1) {
            return;
          }
        } else if (layer instanceof DataStore || layer instanceof UDIGFeatureStore) {
          try {
            DataStore store = ((DataStore) layer);
            FeatureSource featureStore = store.getFeatureSource(store.getTypeNames()[0]);
            Envelope envelope = featureStore.getBounds();

            ActiveRegionStyle style = getActiveRegionStyle();
            JGrassRegion activeWindow =
                new JGrassRegion(
                    style.west, style.east, style.south, style.north, style.rows, style.cols);
            JGrassRegion newWindow =
                JGrassRegion.adaptActiveRegionToEnvelope(envelope, activeWindow);
            northText.setText(String.valueOf(newWindow.getNorth()));
            southText.setText(String.valueOf(newWindow.getSouth()));
            eastText.setText(String.valueOf(newWindow.getEast()));
            westText.setText(String.valueOf(newWindow.getWest()));
            textModified(bound_type);
          } catch (IOException e1) {
            e1.printStackTrace();
            return;
          }

        } else {
          return;
        }
      }
      if (text == null) {
        return;
      }
    }
  }
 /**
  * Reads a {@link GridCoverage2D} from the grass raster.
  *
  * @param jGrassMapEnvironment the {@link JGrassMapEnvironment}.
  * @param readRegion the region to read, or <code>null</code>.
  * @return the read coverage.
  * @throws Exception
  */
 public static GridCoverage2D getGridcoverageFromGrassraster(
     JGrassMapEnvironment jGrassMapEnvironment, JGrassRegion readRegion) throws Exception {
   CoordinateReferenceSystem crs = jGrassMapEnvironment.getCoordinateReferenceSystem();
   JGrassRegion jGrassRegion = readRegion;
   if (jGrassRegion == null) jGrassRegion = jGrassMapEnvironment.getActiveRegion();
   GeneralParameterValue[] readParams =
       JGrassCatalogUtilities.createGridGeometryGeneralParameter(
           jGrassRegion.getCols(),
           jGrassRegion.getRows(),
           jGrassRegion.getNorth(),
           jGrassRegion.getSouth(),
           jGrassRegion.getWest(),
           jGrassRegion.getEast(),
           crs);
   AbstractGridFormat format =
       (AbstractGridFormat) new GrassCoverageFormatFactory().createFormat();
   GridCoverageReader reader = format.getReader(jGrassMapEnvironment.getCELL());
   GridCoverage2D mapCoverage = ((GridCoverage2D) reader.read(readParams));
   return mapCoverage;
 }
  public static void writeGridCoverageFromGrassraster(
      File mapFile, JGrassRegion writeRegion, GridCoverage2D grassCoverage) throws Exception {
    JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(mapFile);
    GrassCoverageFormat format = new GrassCoverageFormatFactory().createFormat();
    GridCoverageWriter writer = format.getWriter(mapEnvironment.getCELL(), null);

    GeneralParameterValue[] readParams = null;
    if (writeRegion == null) {
      writeRegion = mapEnvironment.getActiveRegion();
    }
    readParams =
        JGrassCatalogUtilities.createGridGeometryGeneralParameter(
            writeRegion.getCols(),
            writeRegion.getRows(),
            writeRegion.getNorth(),
            writeRegion.getSouth(),
            writeRegion.getEast(),
            writeRegion.getWest(),
            mapEnvironment.getCoordinateReferenceSystem());

    writer.write(grassCoverage, readParams);
  }
  @Override
  protected void refresh() {
    try {
      IBlackboard blackboard = getLayer().getMap().getBlackboard();

      style = (ActiveRegionStyle) blackboard.get(ActiveregionStyleContent.ID);

      if (style == null) {
        style = ActiveregionStyleContent.createDefault();
        blackboard.put(ActiveregionStyleContent.ID, style);
        // ((StyleBlackboard) styleBlackboard).setSelected(new
        // String[]{ActiveregionStyleContent.ID});
      }

      // first time choose the mapset
      if (style.windPath == null) {
        CatalogJGrassMapsetTreeViewerDialog mapsetDialog =
            new CatalogJGrassMapsetTreeViewerDialog();
        mapsetDialog.open(parent.getShell());
        jGrassMapsetGeoResource = mapsetDialog.getSelectedLayers().get(0);

        JGrassRegion activeRegionWindow = jGrassMapsetGeoResource.getActiveRegionWindow();
        style.windPath = jGrassMapsetGeoResource.getActiveRegionWindowPath();
        style.north = (float) activeRegionWindow.getNorth();
        style.south = (float) activeRegionWindow.getSouth();
        style.west = (float) activeRegionWindow.getWest();
        style.east = (float) activeRegionWindow.getEast();
        style.rows = activeRegionWindow.getRows();
        style.cols = activeRegionWindow.getCols();

        CoordinateReferenceSystem jGrassCrs = jGrassMapsetGeoResource.getLocationCrs();
        try {
          Integer epsg = CRS.lookupEpsgCode(jGrassCrs, true);
          style.crsString = "EPSG:" + epsg;
        } catch (Exception e) {
          // try non epsg
          style.crsString = CRS.lookupIdentifier(jGrassCrs, true);
        }
      }
      windPathText.setText(style.windPath);

      JGrassRegion tmp =
          new JGrassRegion(
              style.west, style.east, style.south, style.north, style.rows, style.cols);
      // set initial values
      isWorking = true;
      northText.setText(String.valueOf(style.north));
      southText.setText(String.valueOf(style.south));
      westText.setText(String.valueOf(style.west));
      eastText.setText(String.valueOf(style.east));
      xresText.setText(String.valueOf(tmp.getWEResolution()));
      yresText.setText(String.valueOf(tmp.getNSResolution()));
      colsText.setText(String.valueOf(style.cols));
      rowsText.setText(String.valueOf(style.rows));
      isWorking = false;

      forgroundAlphaText.setText(Float.toString(style.fAlpha));
      backgroundAlphaText.setText(Float.toString(style.bAlpha));
      foregroundColor.setColorValue(
          new RGB(
              style.foregroundColor.getRed(),
              style.foregroundColor.getGreen(),
              style.foregroundColor.getBlue()));
      backgroundColour.setColorValue(
          new RGB(
              style.backgroundColor.getRed(),
              style.backgroundColor.getGreen(),
              style.backgroundColor.getBlue()));

      CoordinateReferenceSystem crs = CRS.decode(style.crsString);
      commitToBlackboards(tmp, crs, style.windPath);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }