Skip to content

Commit

Permalink
GEOS-8663 Easy insert images into style (fixed+docs)
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Apr 19, 2018
1 parent 630888c commit a50d836
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
Binary file modified doc/en/user/source/styling/webadmin/img/styles_editor.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions doc/en/user/source/styling/webadmin/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The editor page provides several options for submitting a new style:

Uploading an file from the local system


When creating a style, only the :guilabel:`Data` tab will be available. Click :guilabel:`Apply` on the new style to stay on the Style Editor page and gain access to all tabs.

.. _styling_webadmin_remove:
Expand Down Expand Up @@ -143,6 +144,8 @@ The style editor supports line numbering, automatic indentation, and real-time s
- Auto-format the editor contents
* - .. image:: img/styles_editor_fontsize.png
- Change the font size in the editor
* - .. image:: img/styles_editor_image.png
- Insert image into style (choose existing or upload)

During editing and especially after editing is complete, you will want to check validation of the syntax. This can be done by clicking the :guilabel:`Validate` button at the bottom.

Expand Down
14 changes: 13 additions & 1 deletion src/main/src/main/java/org/geoserver/catalog/SLDHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;
import java.util.logging.Logger;

import javax.activation.MimetypesFileTypeMap;
import javax.xml.transform.TransformerException;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -68,7 +69,7 @@ public class SLDHandler extends StyleHandler {

public static final String MIMETYPE_10 = "application/vnd.ogc.sld+xml";
public static final String MIMETYPE_11 = "application/vnd.ogc.se+xml";

static final Map<StyleType, String> TEMPLATES = new HashMap<StyleType, String>();
static {
try {
Expand Down Expand Up @@ -355,4 +356,15 @@ Object[] getVersionAndReader(Object input) throws IOException {

return new Object[]{new Version(version), reader};
}

@Override
public String insertImageCode(String imageFileName) {
return new StringBuffer("<ExternalGraphic>\\n")
.append("<OnlineResource xlink:type=\"simple\" xlink:href=\"")
.append(imageFileName).append("\" />\\n")
.append("<Format>")
.append(IMAGE_TYPES.getContentType(imageFileName))
.append("</Format>\\n")
.append("</ExternalGraphic>\\n").toString();
}
}
21 changes: 21 additions & 0 deletions src/main/src/main/java/org/geoserver/catalog/StyleHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;

import org.geoserver.platform.resource.Resource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.styling.ResourceLocator;
Expand All @@ -35,6 +38,16 @@
public abstract class StyleHandler {

protected static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);

protected static String[] IMAGE_EXTENSIONS = new String[] {"png", "jpg", "jpeg", "gif", "svg"};

protected static MimetypesFileTypeMap IMAGE_TYPES = new MimetypesFileTypeMap();
static {
IMAGE_TYPES.addMimeTypes("image/png png");
IMAGE_TYPES.addMimeTypes("image/jpg jpg jpeg");
IMAGE_TYPES.addMimeTypes("image/gif gif");
IMAGE_TYPES.addMimeTypes("image/svg+xml svg");
}

String name;
String format;
Expand Down Expand Up @@ -200,4 +213,12 @@ protected Reader toReader(Object input) throws IOException {

throw new IllegalArgumentException("Unable to turn " + input + " into reader");
}

public String[] imageExtensions() {
return IMAGE_EXTENSIONS;
}

public String insertImageCode(String imageFileName) {
return imageFileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,6 @@
@SuppressWarnings("serial")
public abstract class AbstractStylePage extends GeoServerSecuredPage {

private static String[] IMAGE_EXTENSIONS = new String[] {"png", "jpg", "jpeg", "gif", "svg"};
private static MimetypesFileTypeMap IMAGE_TYPES = new MimetypesFileTypeMap();
static {
IMAGE_TYPES.addMimeTypes("image/png png");
IMAGE_TYPES.addMimeTypes("image/jpg jpg jpeg");
IMAGE_TYPES.addMimeTypes("image/gif gif");
IMAGE_TYPES.addMimeTypes("image/svg+xml svg");
}

class ChooseImagePanel extends Panel {
private WorkspaceInfo ws;

Expand All @@ -115,7 +106,7 @@ protected void onInitialize() {
GeoServerDataDirectory dd =
GeoServerApplication.get().getBeanOfType(GeoServerDataDirectory.class);
for (Resource r : dd.getStyles(ws).list()) {
if (ArrayUtils.contains(IMAGE_EXTENSIONS,
if (ArrayUtils.contains(styleHandler().imageExtensions(),
FilenameUtils.getExtension(r.name()).toLowerCase())) {
imageSet.add(r.name());
}
Expand Down Expand Up @@ -462,7 +453,7 @@ public void onClick(AjaxRequestTarget target) {
@Override
protected Component getContents(String id) {
return imagePanel = new ChooseImagePanel(id,
style.getWorkspace());
styleModel.getObject().getWorkspace());
}

@Override
Expand Down Expand Up @@ -491,16 +482,10 @@ protected boolean onSubmit(AjaxRequestTarget target, Component contents) {
return false;
}
}

StringBuffer sb = new StringBuffer("<ExternalGraphic>\\n")
.append("<OnlineResource xlink:type=\"simple\" xlink:href=\"")
.append(imageFileName).append("\" />\\n")
.append("<Format>")
.append(IMAGE_TYPES.getContentType(imageFileName))
.append("</Format>\\n")
.append("</ExternalGraphic>\\n");
target.appendJavaScript(
"replaceSelection('" + sb.toString() + "');");
"replaceSelection('" +
styleHandler().insertImageCode(imageFileName)
+ "');");
return true;
}

Expand Down

0 comments on commit a50d836

Please sign in to comment.