List of usage examples for org.apache.wicket.ajax AjaxRequestTarget appendJavaScript
void appendJavaScript(CharSequence javascript);
If the javascript needs to do something asynchronously (i.e.
From source file:org.geoserver.python.web.PythonConsolePage.java
License:Open Source License
public PythonConsolePage() { Python python = getGeoServerApplication().getBeanOfType(Python.class); python.interpreter();//from ww w . ja v a 2 s . c o m model = new PythonInterpreterDetachableModel(); Form form = new Form("form"); add(form); final AjaxButton execute = new AjaxButton("execute") { @Override protected void onSubmit(AjaxRequestTarget target, Form form) { String[] lines = output.split("\n"); String line = lines[lines.length - 1]; if (line.startsWith(">>> ")) { line = line.substring(4); } PythonInterpreter pi = (PythonInterpreter) model.getObject(); ByteArrayOutputStream out = new ByteArrayOutputStream(); pi.setOut(out); try { pi.exec(line); } catch (PyException pe) { pe.printStackTrace(new PrintWriter(out, true)); } output += "\n"; output += new String(out.toByteArray()); output += ">>> "; target.addComponent(outputTextArea); target.appendJavascript("var ta = document.getElementById('" + outputTextArea.getMarkupId() + "');" + "ta.scrollTop = ta.scrollHeight;"); } }; form.add(execute); AjaxButton clear = new AjaxButton("clear") { @Override protected void onSubmit(AjaxRequestTarget target, Form form) { output = ">>> "; target.addComponent(outputTextArea); } }; form.add(clear); outputTextArea = new TextArea("output", new PropertyModel(this, "output")) { @Override protected void onComponentTag(ComponentTag tag) { super.onComponentTag(tag); tag.put("onkeypress", "if (event.keyCode == 13) { " + "document.getElementById('" + execute.getMarkupId() + "').click();" + "return true;" + "}"); } }; outputTextArea.setOutputMarkupId(true); //outputTextArea.setEnabled(false); form.add(outputTextArea); /*form.add(new Label("prompt", ">>> ")); form.add(new TextField("input", new PropertyModel(this, "input")));*/ }
From source file:org.geoserver.web.istyle.OpenLayersMapPanel.java
License:Open Source License
public void update(LayerInfo layer, StyleInfo style, AjaxRequestTarget target) { layer = layer != null ? layer : this.layer; if (style == null) { if (!layer.equals(this.layer)) { //no style specified, and the layer was changed style = layer.getDefaultStyle(); } else {//from w w w . ja v a 2 s . com //no style specified and layer did not change, do not change style } } try { SimpleHash model = new SimpleHash(); model.put("markupId", getMarkupId()); model.put("layers", layer.getName()); model.put("styles", style.getName()); bbox(layer, model); model.put("ran", rand.nextInt()); model.put("layerChanged", !layer.equals(this.layer)); target.appendJavascript(renderTemplate("OL-update.ftl", model)); this.layer = layer; this.style = style; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.geoserver.wms.web.data.AbstractStylePage.java
License:Open Source License
protected void initUI(StyleInfo style) { CompoundPropertyModel<StyleInfo> styleModel = new CompoundPropertyModel( style != null ? new StyleDetachableModel(style) : getCatalog().getFactory().createStyle()); format = style != null ? style.getFormat() : getCatalog().getFactory().createStyle().getFormat(); // Make sure the legend object isn't null if (null == styleModel.getObject().getLegend()) { styleModel.getObject().setLegend(getCatalog().getFactory().createLegend()); }/*w w w. ja v a 2s. c om*/ styleForm = new Form("form", styleModel) { @Override protected void onSubmit() { super.onSubmit(); onStyleFormSubmit(); } }; styleForm.setMarkupId("mainForm"); add(styleForm); styleForm.add(nameTextField = new TextField("name")); nameTextField.setRequired(true); wsChoice = new DropDownChoice<WorkspaceInfo>("workspace", new WorkspacesModel(), new WorkspaceChoiceRenderer()); wsChoice.setNullValid(true); if (!isAuthenticatedAsAdmin()) { wsChoice.setNullValid(false); wsChoice.setRequired(true); } styleForm.add(wsChoice); formatChoice = new DropDownChoice<String>("format", new PropertyModel(this, "format"), new StyleFormatsModel(), new ChoiceRenderer<String>() { @Override public String getIdValue(String object, int index) { return object; } @Override public Object getDisplayValue(String object) { return Styles.handler(object).getName(); } }); formatChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") { @Override protected void onUpdate(AjaxRequestTarget target) { target.appendJavascript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", styleHandler().getCodeMirrorEditMode())); } }); styleForm.add(formatChoice); formatReadOnlyMessage = new WebMarkupContainer("formatReadOnly", new Model()); formatReadOnlyMessage.setVisible(false); styleForm.add(formatReadOnlyMessage); styleForm.add(editor = new CodeMirrorEditor("styleEditor", styleHandler().getCodeMirrorEditMode(), new PropertyModel(this, "rawStyle"))); // force the id otherwise this blasted thing won't be usable from other forms editor.setTextAreaMarkupId("editor"); editor.setOutputMarkupId(true); editor.setRequired(true); styleForm.add(editor); // add the Legend fields ExternalGraphicPanel legendPanel = new ExternalGraphicPanel("legendPanel", styleModel, styleForm); legendPanel.setOutputMarkupId(true); styleForm.add(legendPanel); if (style != null) { try { setRawStyle(readFile(style)); } catch (IOException e) { // ouch, the style file is gone! Register a generic error message Session.get().error(new ParamResourceModel("styleNotFound", this, style.getFilename()).getString()); } } // style generation functionality templates = new DropDownChoice("templates", new Model(), new StyleTypeModel(), new StyleTypeChoiceRenderer()); templates.setOutputMarkupId(true); templates.add(new AjaxFormComponentUpdatingBehavior("onchange") { @Override protected void onUpdate(AjaxRequestTarget target) { templates.validate(); generateLink.setEnabled(templates.getConvertedInput() != null); target.addComponent(generateLink); } }); styleForm.add(templates); generateLink = generateLink(); generateLink.setEnabled(false); styleForm.add(generateLink); // style copy functionality styles = new DropDownChoice("existingStyles", new Model(), new StylesModel(), new StyleChoiceRenderer()); styles.setOutputMarkupId(true); styles.add(new AjaxFormComponentUpdatingBehavior("onchange") { @Override protected void onUpdate(AjaxRequestTarget target) { styles.validate(); copyLink.setEnabled(styles.getConvertedInput() != null); target.addComponent(copyLink); } }); styleForm.add(styles); copyLink = copyLink(); copyLink.setEnabled(false); styleForm.add(copyLink); uploadForm = uploadForm(styleForm); uploadForm.setMultiPart(true); uploadForm.setMaxSize(Bytes.megabytes(1)); uploadForm.setMarkupId("uploadForm"); add(uploadForm); uploadForm.add(fileUploadField = new FileUploadField("filename")); add(validateLink()); add(previewLink()); Link cancelLink = new Link("cancel") { @Override public void onClick() { doReturn(StylePage.class); } }; add(cancelLink); legendContainer = new WebMarkupContainer("legendContainer"); legendContainer.setOutputMarkupId(true); add(legendContainer); this.legendImg = new Image("legendImg"); legendContainer.add(this.legendImg); this.legendImg.setVisible(false); this.legendImg.setOutputMarkupId(true); this.legendImg.setImageResource(new DynamicWebResource() { @Override protected ResourceState getResourceState() { return new ResourceState() { @Override public byte[] getData() { GeoServerDataDirectory dd = GeoServerExtensions.bean(GeoServerDataDirectory.class, getGeoServerApplication().getApplicationContext()); StyleInfo si = new StyleInfoImpl(getCatalog()); String styleName = "tmp" + UUID.randomUUID().toString(); String styleFileName = styleName + ".sld"; si.setFilename(styleFileName); si.setName(styleName); si.setWorkspace(wsChoice.getModel().getObject()); Resource styleResource = null; try { styleResource = dd.style(si); try (OutputStream os = styleResource.out()) { IOUtils.write(lastStyle, os); } Style style = dd.parsedStyle(si); if (style != null) { GetLegendGraphicRequest request = new GetLegendGraphicRequest(); request.setLayer(null); request.setStyle(style); request.setStrict(false); Map<String, String> legendOptions = new HashMap<String, String>(); legendOptions.put("forceLabels", "on"); legendOptions.put("fontAntiAliasing", "true"); request.setLegendOptions(legendOptions); BufferedImageLegendGraphicBuilder builder = new BufferedImageLegendGraphicBuilder(); BufferedImage image = builder.buildLegendGraphic(request); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, "PNG", bos); return bos.toByteArray(); } error("Failed to build legend preview"); return null; } catch (IOException e) { throw new RuntimeException(e); } finally { if (styleResource != null) { styleResource.delete(); } } } @Override public String getContentType() { return "image/png"; } }; } }); }
From source file:org.geoserver.wms.web.data.AbstractStylePage.java
License:Open Source License
AjaxSubmitLink generateLink() { return new AjaxSubmitLink("generate") { @Override/*from www. j a va 2 s . co m*/ protected void onSubmit(AjaxRequestTarget target, Form form) { // we need to force validation or the value won't be converted templates.processInput(); StyleType template = (StyleType) templates.getConvertedInput(); StyleGenerator styleGen = new StyleGenerator(getCatalog()); styleGen.setWorkspace(wsChoice.getModel().getObject()); if (template != null) { try { // same here, force validation or the field won't be updated editor.reset(); setRawStyle(new StringReader( styleGen.generateStyle(styleHandler(), template, nameTextField.getInput()))); target.appendJavascript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", styleHandler().getCodeMirrorEditMode())); } catch (Exception e) { error("Errors occurred generating the style"); } target.addComponent(styleForm); } } @Override protected IAjaxCallDecorator getAjaxCallDecorator() { return new AjaxPreprocessingCallDecorator(super.getAjaxCallDecorator()) { @Override public CharSequence preDecorateScript(CharSequence script) { return "var val = event.view.document.gsEditors ? " + "event.view.document.gsEditors." + editor.getTextAreaMarkupId() + ".getValue() : " + "event.view.document.getElementById(\"" + editor.getTextAreaMarkupId() + "\").value; " + "if(val != '' &&" + "!confirm('" + new ParamResourceModel("confirmOverwrite", AbstractStylePage.this).getString() + "')) return false;" + script; } }; } @Override public boolean getDefaultFormProcessing() { return false; } }; }
From source file:org.geoserver.wms.web.data.AbstractStylePage.java
License:Open Source License
AjaxSubmitLink copyLink() { return new AjaxSubmitLink("copy") { @Override//from w w w.j a va 2s.co m protected void onSubmit(AjaxRequestTarget target, Form form) { // we need to force validation or the value won't be converted styles.processInput(); StyleInfo style = (StyleInfo) styles.getConvertedInput(); if (style != null) { try { // same here, force validation or the field won't be udpated editor.reset(); setRawStyle(readFile(style)); formatChoice.setModelObject(style.getFormat()); target.appendJavascript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", styleHandler().getCodeMirrorEditMode())); } catch (Exception e) { error("Errors occurred loading the '" + style.getName() + "' style"); } target.addComponent(styleForm); } } @Override protected IAjaxCallDecorator getAjaxCallDecorator() { return new AjaxPreprocessingCallDecorator(super.getAjaxCallDecorator()) { @Override public CharSequence preDecorateScript(CharSequence script) { return "var val = event.view.document.gsEditors ? " + "event.view.document.gsEditors." + editor.getTextAreaMarkupId() + ".getValue() : " + "event.view.document.getElementById(\"" + editor.getTextAreaMarkupId() + "\").value; " + "if(val != '' &&" + "!confirm('" + new ParamResourceModel("confirmOverwrite", AbstractStylePage.this).getString() + "')) return false;" + script; } }; } @Override public boolean getDefaultFormProcessing() { return false; } }; }
From source file:org.geoserver.wms.web.data.StyleAdminPanel.java
License:Open Source License
public void initUI(CompoundPropertyModel<StyleInfo> styleModel) { StyleInfo style = getStylePage().getStyleInfo(); IModel<String> nameBinding = styleModel.bind("name"); add(nameTextField = new TextField<String>("name", nameBinding)); nameTextField.setRequired(true);// w w w . j av a 2 s . c om IModel<WorkspaceInfo> wsBinding = styleModel.bind("workspace"); wsChoice = new DropDownChoice<WorkspaceInfo>("workspace", wsBinding, new WorkspacesModel(), new WorkspaceChoiceRenderer()); wsChoice.setNullValid(true); if (!stylePage.isAuthenticatedAsAdmin()) { wsChoice.setNullValid(false); wsChoice.setRequired(true); } add(wsChoice); //always disable the workspace toggle if not admin if (!stylePage.isAuthenticatedAsAdmin()) { wsChoice.setEnabled(false); } IModel<String> formatBinding = styleModel.bind("format"); formatChoice = new DropDownChoice<String>("format", formatBinding, new StyleFormatsModel(), new ChoiceRenderer<String>() { private static final long serialVersionUID = 2064887235303504013L; @Override public String getIdValue(String object, int index) { return object; } @Override public Object getDisplayValue(String object) { return Styles.handler(object).getName(); } }); formatChoice.add(new AjaxFormComponentUpdatingBehavior("change") { private static final long serialVersionUID = -8372146231225388561L; @Override protected void onUpdate(AjaxRequestTarget target) { target.appendJavaScript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", stylePage.styleHandler().getCodeMirrorEditMode())); } }); add(formatChoice); formatReadOnlyMessage = new WebMarkupContainer("formatReadOnly", new Model<String>()); formatReadOnlyMessage.setVisible(false); add(formatReadOnlyMessage); // add the Legend fields legendPanel = new ExternalGraphicPanel("legendPanel", styleModel, stylePage.styleForm); legendPanel.setOutputMarkupId(true); add(legendPanel); if (style.getId() != null) { try { stylePage.setRawStyle(stylePage.readFile(style)); } catch (IOException e) { // ouch, the style file is gone! Register a generic error message Session.get().error(new ParamResourceModel("styleNotFound", this, style.getFilename()).getString()); } } // style generation functionality templates = new DropDownChoice<StyleType>("templates", new Model<StyleType>(), new StyleTypeModel(), new StyleTypeChoiceRenderer()); templates.setOutputMarkupId(true); templates.add(new AjaxFormComponentUpdatingBehavior("change") { private static final long serialVersionUID = -6649152103570059645L; @Override protected void onUpdate(AjaxRequestTarget target) { templates.validate(); generateLink.setEnabled(templates.getConvertedInput() != null); target.add(generateLink); } }); add(templates); generateLink = generateLink(); generateLink.setEnabled(false); add(generateLink); // style copy functionality styles = new DropDownChoice<StyleInfo>("existingStyles", new Model<StyleInfo>(), new StylesModel(), new StyleChoiceRenderer()); styles.setOutputMarkupId(true); styles.add(new AjaxFormComponentUpdatingBehavior("change") { private static final long serialVersionUID = 8098121930876372129L; @Override protected void onUpdate(AjaxRequestTarget target) { styles.validate(); copyLink.setEnabled(styles.getConvertedInput() != null); target.add(copyLink); } }); add(styles); copyLink = copyLink(); copyLink.setEnabled(false); add(copyLink); uploadLink = uploadLink(); //uploadLink.setEnabled(false); add(uploadLink); fileUploadField = new FileUploadField("filename"); //Explicitly set model so this doesn't use the form model fileUploadField.setDefaultModel(new Model<String>("")); add(fileUploadField); add(previewLink()); legendContainer = new WebMarkupContainer("legendContainer"); legendContainer.setOutputMarkupId(true); add(legendContainer); this.legendImg = new Image("legendImg", new AbstractResource() { private static final long serialVersionUID = -6932528694575832606L; @Override protected ResourceResponse newResourceResponse(Attributes attributes) { ResourceResponse rr = new ResourceResponse(); rr.setContentType("image/png"); rr.setWriteCallback(new WriteCallback() { @Override public void writeData(Attributes attributes) throws IOException { ImageIO.write(legendImage, "PNG", attributes.getResponse().getOutputStream()); } }); return rr; } }); legendContainer.add(this.legendImg); this.legendImg.setVisible(false); this.legendImg.setOutputMarkupId(true); }
From source file:org.geoserver.wms.web.data.StyleAdminPanel.java
License:Open Source License
protected AjaxSubmitLink generateLink() { return new ConfirmOverwriteSubmitLink("generate") { private static final long serialVersionUID = 55921414750155395L; @Override//from w w w . j a va 2 s . c o m protected void onSubmit(AjaxRequestTarget target, Form<?> form) { // we need to force validation or the value won't be converted templates.processInput(); nameTextField.processInput(); wsChoice.processInput(); StyleType template = (StyleType) templates.getConvertedInput(); StyleGenerator styleGen = new StyleGenerator(stylePage.getCatalog()); styleGen.setWorkspace(getStylePage().getStyleInfo().getWorkspace()); if (template != null) { try { // same here, force validation or the field won't be updated stylePage.editor.reset(); stylePage.setRawStyle(new StringReader(styleGen.generateStyle(stylePage.styleHandler(), template, getStylePage().getStyleInfo().getName()))); target.appendJavaScript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", stylePage.styleHandler().getCodeMirrorEditMode())); clearFeedbackMessages(); } catch (Exception e) { clearFeedbackMessages(); stylePage.editor.getFeedbackMessages().clear(); stylePage.error("Errors occurred generating the style"); LOGGER.log(Level.WARNING, "Errors occured generating the style", e); } target.add(stylePage); } } }; }
From source file:org.geoserver.wms.web.data.StyleAdminPanel.java
License:Open Source License
protected AjaxSubmitLink copyLink() { return new ConfirmOverwriteSubmitLink("copy") { private static final long serialVersionUID = -6388040033082157163L; @Override/*from w w w. j av a2s.c o m*/ protected void onSubmit(AjaxRequestTarget target, Form<?> form) { // we need to force validation or the value won't be converted styles.processInput(); StyleInfo style = (StyleInfo) styles.getConvertedInput(); if (style != null) { try { // same here, force validation or the field won't be updated stylePage.editor.reset(); stylePage.setRawStyle(stylePage.readFile(style)); target.appendJavaScript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", stylePage.styleHandler().getCodeMirrorEditMode())); clearFeedbackMessages(); } catch (Exception e) { clearFeedbackMessages(); stylePage.error("Errors occurred loading the '" + style.getName() + "' style"); LOGGER.log(Level.WARNING, "Errors occurred loading the '" + style.getName() + "' style", e); } target.add(stylePage); } } }; }
From source file:org.geoserver.wms.web.data.StyleAdminPanel.java
License:Open Source License
AjaxSubmitLink uploadLink() { return new ConfirmOverwriteSubmitLink("upload", stylePage.styleForm) { private static final long serialVersionUID = 658341311654601761L; @Override/*w ww .j a v a 2 s. c o m*/ protected void onSubmit(AjaxRequestTarget target, Form<?> form) { FileUpload upload = fileUploadField.getFileUpload(); if (upload == null) { warn("No file selected."); return; } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { IOUtils.copy(upload.getInputStream(), bout); stylePage.editor.reset(); stylePage.setRawStyle( new InputStreamReader(new ByteArrayInputStream(bout.toByteArray()), "UTF-8")); target.appendJavaScript(String.format( "if (document.gsEditors) { document.gsEditors.editor.setOption('mode', '%s'); }", stylePage.styleHandler().getCodeMirrorEditMode())); clearFeedbackMessages(); } catch (IOException e) { throw new WicketRuntimeException(e); } catch (Exception e) { clearFeedbackMessages(); stylePage.error("Errors occurred uploading the '" + upload.getClientFileName() + "' style"); LOGGER.log(Level.WARNING, "Errors occurred uploading the '" + upload.getClientFileName() + "' style", e); } // update the style object StyleInfo s = getStylePage().getStyleInfo(); if (s.getName() == null || "".equals(s.getName().trim())) { // set it nameTextField.setModelValue( new String[] { ResponseUtils.stripExtension(upload.getClientFileName()) }); nameTextField.modelChanged(); } target.add(stylePage); } }; }
From source file:org.headsupdev.agile.web.components.configuration.SQLURLField.java
License:Open Source License
private void layout() { final SQLURLField self = this; final TextField urlField = new TextField<String>("url", new PropertyModel<String>(this, "url") { public void setObject(String o) { super.setObject(o); updateModelObject();//from w ww .j av a2 s .co m } }); urlField.setMarkupId("sqlurl").setOutputMarkupId(true); final DropDownChoice types = new DropDownChoice<String>("type", new PropertyModel<String>(this, "type"), DatabaseRegistry.getTypes()); types.add(new OnChangeAjaxBehavior() { protected void onUpdate(final AjaxRequestTarget target) { String newType = (String) types.getModelObject(); url = DatabaseRegistry.getDefaultUrl(newType); target.addComponent(urlField); updateModelObject(); target.appendJavascript("document.getElementById('sqlurl').onblur()"); } }); add(types); urlField.add(new AjaxFormComponentUpdatingBehavior("onblur") { protected void onUpdate(AjaxRequestTarget target) { self.onUpdate(target); } }); add(urlField); }