private boolean applyCurrentStyle() {
try {
if (editorSupported) {
StyleLayer layer = getSelectedLayer();
// IGeoResource resource = layer.getGeoResource();
// GridCoverage2D coverage2d = resource.resolve(GridCoverage2D.class,
// new NullProgressMonitor());
// int numBands = coverage2d.getNumSampleDimensions();
// if (numBands > 1) {
// System.out.println("BANDS: " + numBands);
// // return false;
// }
StyleBuilder sB = new StyleBuilder(sf);
RasterSymbolizer rasterSym = sf.createRasterSymbolizer();
ColorMap colorMap = sf.createColorMap();
ArrayList<CoverageRule> rulesList = colorRulesEditor.getRulesList();
for( int i = 0; i < rulesList.size(); i++ ) {
CoverageRule coverageRule = rulesList.get(i);
if (!coverageRule.isActive()) {
continue;
}
Color fromColor = coverageRule.getFromColor();
Color toColor = coverageRule.getToColor();
double[] values = coverageRule.getFromToValues();
double opacity = coverageRule.getOpacity();
Expression fromColorExpr = sB.colorExpression(new java.awt.Color(fromColor.getRed(), fromColor.getGreen(),
fromColor.getBlue(), 255));
Expression toColorExpr = sB.colorExpression(new java.awt.Color(toColor.getRed(), toColor.getGreen(), toColor
.getBlue(), 255));
Expression fromExpr = sB.literalExpression(values[0]);
Expression toExpr = sB.literalExpression(values[1]);
Expression opacityExpr = sB.literalExpression(opacity);
ColorMapEntry entry = sf.createColorMapEntry();
entry.setQuantity(fromExpr);
entry.setColor(fromColorExpr);
entry.setOpacity(opacityExpr);
colorMap.addColorMapEntry(entry);
entry = sf.createColorMapEntry();
entry.setQuantity(toExpr);
entry.setOpacity(opacityExpr);
entry.setColor(toColorExpr);
colorMap.addColorMapEntry(entry);
}
rasterSym.setColorMap(colorMap);
/*
* set global transparency for the map
*/
rasterSym.setOpacity(sB.literalExpression(colorRulesEditor.getAlphaVAlue() / 100.0));
Style newStyle = SLD.wrapSymbolizers(rasterSym);
Layer selLayer = getSelectedLayer();
newStyle.setName(selLayer.getName());
StyleBlackboard styleBlackboard = layer.getStyleBlackboard();
// put style back on blackboard
styleBlackboard.put(SLDContent.ID, newStyle);
styleBlackboard.setSelected(new String[]{SLDContent.ID});
}