Java Code Examples for org.w3c.dom.svg.SVGDocument#getElementById()

The following examples show how to use org.w3c.dom.svg.SVGDocument#getElementById() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SVGMapMerger.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Merge map.
 *
 * @param srcMap
 *            the src map
 * @param dstMap
 *            the dst map
 * @param srcId
 *            the src id
 * @param dstId
 *            the dst id
 */
public static void mergeMap(SVGDocument srcMap, SVGDocument dstMap, String srcId, String dstId) {
	SVGElement srcMapRoot;
	Element srcElement;
	Element dstElement;

	srcMapRoot = srcMap.getRootElement();
	srcElement = (srcId == null ? srcMapRoot : srcMap.getElementById(srcId));

	dstElement = dstMap.getElementById(dstId);

	NodeList nodeList = srcElement.getChildNodes();
	for (int i = 0; i < nodeList.getLength(); i++) {
		Node node = nodeList.item(i);
		Node importedNode = dstMap.importNode(node, true);
		dstElement.appendChild(importedNode);
	}
}
 
Example 2
Source File: InteractiveMapRenderer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Include scripts.
 *
 * @param doc
 *            the doc
 */
private void includeScripts(SVGDocument doc) {
	Element scriptInit = doc.getElementById("included_scripts");
	Node scriptText = scriptInit.getFirstChild();
	StringBuffer buffer = new StringBuffer();
	includeScript(buffer, "helper_functions.js");
	includeScript(buffer, "timer.js");
	includeScript(buffer, "mapApp.js");
	includeScript(buffer, "timer.js");
	includeScript(buffer, "slider.js");
	includeScript(buffer, "button.js");
	includeScript(buffer, "Window.js");
	includeScript(buffer, "checkbox_and_radiobutton.js");
	includeScript(buffer, "navigation.js");
	includeScript(buffer, "tabgroup.js");
	includeScript(buffer, "colourPicker.js");

	includeScript(buffer, "custom/Utils.js");
	includeScript(buffer, "custom/BarChart.js");
	includeScript(buffer, "custom/NavigationWindow.js");
	includeScript(buffer, "custom/LayersWindow.js");
	includeScript(buffer, "custom/ThematicWindow.js");
	includeScript(buffer, "custom/DetailsWindow.js");
	includeScript(buffer, "custom/LegendWindow.js");
	includeScript(buffer, "custom/ColourPickerWindow.js");
	includeScript(buffer, "custom/ThresholdsFactory.js");
	includeScript(buffer, "custom/ColourRangesFactory.js");

	scriptText.setNodeValue(buffer.toString());
}
 
Example 3
Source File: InteractiveMapRenderer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void importScipt(SVGDocument map, String scriptName) {
	Element script = map.createElement("script");
	script.setAttribute("type", "text/ecmascript");
	script.setAttribute("xlink:href", (String) getEnv().get(SvgViewerEngineConstants.ENV_CONTEXT_URL) + "/js/lib/svg-widgets/" + scriptName);
	Element importsBlock = map.getElementById("imported_scripts");
	importsBlock.appendChild(script);
	Node lf = map.createTextNode("\n");
	importsBlock.appendChild(lf);
}
 
Example 4
Source File: InteractiveMapRenderer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Sets the main map bkg rect dimension.
 *
 * @param masterMap
 *            the master map
 * @param targetMap
 *            the target map
 */
public void setMainMapBkgRectDimension(SVGDocument masterMap, SVGDocument targetMap) {
	String viewBox = targetMap.getRootElement().getAttribute("viewBox");
	String[] chunks = viewBox.split(" ");
	String x = chunks[0];
	String y = chunks[1];
	String width = chunks[2];
	String height = chunks[3];
	Element mapBackgroundRect = masterMap.getElementById("mapBackgroundRect");
	mapBackgroundRect.setAttribute("x", x);
	mapBackgroundRect.setAttribute("y", y);
	mapBackgroundRect.setAttribute("width", width);
	mapBackgroundRect.setAttribute("height", height);
}
 
Example 5
Source File: InteractiveMapRenderer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Add label to the centroide
 *
 * @param masterMap
 *            the svg content
 * @param centroide
 *            the centroide svg element
 * @param labelGroup
 *            the svg label group element
 * @param aRecord
 *            the record with values
 * @param labelField
 *            the label field
 */
private void addLabels(SVGDocument masterMap, Element centroide, Element labelGroup, IRecord aRecord, IField labelField) {
	logger.debug("IN");
	labelGroup.setAttribute("transform", "translate(" + centroide.getAttribute("cx") + "," + centroide.getAttribute("cy") + ") scale(1)");
	labelGroup.setAttribute("display", "inherit");

	Element label = masterMap.createElement("text");
	label.setAttribute("x", "0");
	label.setAttribute("y", "0");

	label.setAttribute("font-family", "Arial,Helvetica");
	label.setAttribute("font-size", "12px");
	label.setAttribute("font-style", "normal");
	label.setAttribute("fill", "black");
	// label.setAttribute("text-anchor", "middle");
	// get text-anchor property:
	// 1. throught text-anchor property
	// 2. throught style property
	// 3. if it isn't found force 'middle' as default
	String anchor = "middle";
	String anchorProperty = centroide.getAttribute("text-anchor");
	if (anchorProperty != null && anchorProperty.equals("start") || anchorProperty.equals("middle") || anchorProperty.equals("end")) {
		anchor = anchorProperty;
	} else {
		String styleProperty = centroide.getAttribute("style");
		int anchorPropertyPosStart = styleProperty.indexOf("text-anchor:");
		int anchorPropertyPosEnd = styleProperty.indexOf(";", anchorPropertyPosStart);
		if (null != styleProperty && anchorPropertyPosStart >= 0) {
			anchorProperty = styleProperty.substring(anchorPropertyPosStart + 12, anchorPropertyPosEnd);
			anchor = anchorProperty;
			// clean the style from the anchor information
			styleProperty = styleProperty.replace(styleProperty.substring(anchorPropertyPosStart, anchorPropertyPosEnd + 1), "");
			centroide.setAttribute("style", styleProperty);
		}
	}
	label.setAttribute("text-anchor", anchor);
	Node labelText = masterMap.createTextNode((String) labelField.getValue());
	label.appendChild(labelText);
	labelGroup.appendChild(label);
	if (labelGroup != null) {
		// append labels to default layer "valori"
		Element valuesLayer = masterMap.getElementById("_labels_layer");
		valuesLayer.appendChild(labelGroup);
	}

}
 
Example 6
Source File: InteractiveMapRenderer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Show or hide elements through the specific column value
 *
 * @param masterMap
 *            the svg content
 *
 * @param geoIdField
 *            the element id field
 * @param visibilityIdField
 *            the visibility field
 */
private void showElements(SVGDocument masterMap, IField geoIdField, IField visibilityIdField) {
	logger.debug("IN");

	String id_element = (String) geoIdField.getValue();
	String elementVisibility = (String) visibilityIdField.getValue();
	try {
		Element element = masterMap.getElementById(id_element);
		if (element != null) {
			String displayStyle = "";
			String elementStyle = element.getAttribute("style");
			// clean style from ;;
			if (elementStyle.indexOf(";;") >= 0) {
				elementStyle = elementStyle.replaceAll(";;", ";");
			}
			// get original display option if present
			int displayStyleStart = elementStyle.indexOf("display:");
			String displayStyleValue = "";

			// if (displayStyleStart >= 0) {
			// int displayStyleEnd = -1;
			// try {
			// displayStyleEnd = elementStyle.indexOf(";", displayStyleStart);
			// displayStyleValue = elementStyle.substring(displayStyleStart, displayStyleEnd + 1);
			// } catch (StringIndexOutOfBoundsException se) {
			// logger.error("An error occured while getting style content of element with id [" + id_element
			// + "]. Please, check that ALL the style elements into the SVG have the final [;] char. Ex: [display:none;]");
			// throw se;
			// }
			// elementStyle = elementStyle.replace(displayStyleValue, ""); // clean old style
			// }

			// Manage 'display:none' or 'display:none;' properties
			if (displayStyleStart >= 0) {
				int displayStyleEnd = -1;
				String displayContent = "";
				try {
					// case with ; or other properties
					if (elementStyle.length() >= displayStyleStart + 13) {
						displayContent = elementStyle.substring(displayStyleStart, displayStyleStart + 13);
					} else {
						// case without ';'. Style value is : style="display:none"
						displayContent = elementStyle.substring(displayStyleStart, displayStyleStart + 12);
					}
					displayContent = displayContent.trim();
					if (displayContent.indexOf("none") >= 0) {
						displayStyleEnd = displayStyleStart + 12;
						if (displayContent.indexOf(";") > 0)
							displayStyleEnd = displayStyleEnd + 1;
					}
					displayStyleValue = elementStyle.substring(displayStyleStart, displayStyleEnd);
				} catch (StringIndexOutOfBoundsException se) {
					logger.error("An error occured while getting style content of element with id [" + id_element
							+ "]. Please, check that ALL the style elements into the SVG have the final [;] char. Ex: [display:none;]");
					throw se;
				}
				elementStyle = elementStyle.replace(displayStyleValue, ""); // clean old style
			}

			if (elementVisibility.equalsIgnoreCase("false")) {
				displayStyle = elementStyle + ";display:none;";
			} else {
				displayStyle = elementStyle;
			}
			// sets new visibility style for the element
			element.setAttribute("style", displayStyle);
		}
	} catch (Exception e) {
		logger.error("An error occured while managing show property for the element [" + id_element + "]");
		throw e;
	}
	logger.debug("OUT");
}