Example usage for org.apache.wicket.markup.html WebMarkupContainer urlFor

List of usage examples for org.apache.wicket.markup.html WebMarkupContainer urlFor

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html WebMarkupContainer urlFor.

Prototype

public final CharSequence urlFor(final IRequestHandler requestHandler) 

Source Link

Document

Returns a URL that references the given request target.

Usage

From source file:org.headsupdev.agile.web.components.EmbeddedFilePanel.java

License:Open Source License

public EmbeddedFilePanel(final String id, final File file, final Project project) {
    super(id);// w  w  w  .jav a  2s . c om

    add(CSSPackageResource.getHeaderContribution(getClass(), "embeddedfile.css"));

    add(JavascriptPackageResource.getHeaderContribution(getClass(), "highlight/shCore.js"));
    add(JavascriptPackageResource.getHeaderContribution(getClass(), "highlight/shAutoloader.js"));
    add(JavascriptPackageResource.getHeaderContribution(getClass(), "highlight/bootstrap.js"));
    add(CSSPackageResource.getHeaderContribution(getClass(), "highlight/shCoreDefault.css"));

    final Mime mime = Mime.get(file.getName());

    if (mime.isEmbeddableImage()) {
        WebMarkupContainer image = new WebMarkupContainer("image-content");
        image.add(new Image("image", new DynamicImageResource() {
            protected byte[] getImageData() {
                try {
                    return toImageData(ImageIO.read(file));
                } catch (IOException e) {
                    Manager.getLogger("BrowseFile").error("Unable to load data to image", e);
                    return null;
                }
            }
        }.setCacheable(false)));
        add(image);

        add(new WebMarkupContainer("text-content").setVisible(false));
        add(new WebMarkupContainer("binary-content").setVisible(false));
        add(new WebMarkupContainer("object-content").setVisible(false));
        return;
    }
    if (mime.isEmbeddableAudio() || mime.isEmbeddableVideo()) {
        WebMarkupContainer container = new WebMarkupContainer("object-content");
        final WebMarkupContainer object = new WebMarkupContainer("object");
        object.add(new AttributeModifier("type", true, new Model<String>() {
            @Override
            public String getObject() {
                // TODO add real mime types to the mime library
                if (mime.isEmbeddableAudio()) {
                    return "audio/" + mime.getExtension();
                } else {
                    return "video/" + mime.getExtension();
                }
            }
        }));

        object.add(new AttributeModifier("data", true, new Model<String>() {
            @Override
            public String getObject() {
                String storagePath = Manager.getStorageInstance().getDataDirectory().getAbsolutePath();

                if (file.getAbsolutePath().length() > storagePath.length() + 1) {
                    String filePath = file.getAbsolutePath().substring(storagePath.length() + 1);
                    filePath = filePath.replace(File.separatorChar, ':');
                    try {
                        filePath = URLEncoder.encode(filePath, "UTF-8");
                        // funny little hack here, guess the decoding is not right
                        filePath = filePath.replace("+", "%20");
                    } catch (UnsupportedEncodingException e) {
                        // ignore
                    }

                    String urlPath = object.urlFor(new ResourceReference("embed")).toString();
                    return urlPath.replace("/all/", "/" + project.getId() + "/") + "/path/" + filePath;
                }

                return ""; // not supported, someone is hacking the system...
            }
        }));

        container.add(object);
        add(container);

        add(new WebMarkupContainer("text-content").setVisible(false));
        add(new WebMarkupContainer("binary-content").setVisible(false));
        add(new WebMarkupContainer("image-content").setVisible(false));
        return;
    }

    // offer a download link for binary (or unknown) files
    if (mime.isBinary()) {
        WebMarkupContainer binary = new WebMarkupContainer("binary-content");
        binary.add(new DownloadLink("download", file));
        add(binary);

        add(new WebMarkupContainer("text-content").setVisible(false));
        add(new WebMarkupContainer("image-content").setVisible(false));
        add(new WebMarkupContainer("object-content").setVisible(false));

        return;
    }

    String content = "(unable to read file)";
    // for other types try to parse the content
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        content = IOUtil.toString(in).replace("<", "&lt;").replace(">", "&gt;");
    } catch (IOException e) {
        Manager.getLogger("BrowseFile").error("Exception rendering file highlighting", e);
    } finally {
        IOUtil.close(in);
    }

    add(new Label("text-content", content).setEscapeModelStrings(false)
            .add(new AttributeModifier("class", true, new Model<String>() {
                @Override
                public String getObject() {
                    if (mime.getSyntax() != null) {
                        return "code brush: " + mime.getSyntax();
                    }

                    return "code brush: text";
                }
            })));

    add(new WebMarkupContainer("binary-content").setVisible(false));
    add(new WebMarkupContainer("image-content").setVisible(false));
    add(new WebMarkupContainer("object-content").setVisible(false));
}