Skip to content

Commit

Permalink
Bundle installation form/page.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/karaf/webconsole/trunk@1243483 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
splatch committed Feb 13, 2012
1 parent 733caaf commit c06c2f0
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 19 deletions.
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.webconsole.core.behavior;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.ComponentTag;

/**
* Simple behavior to remove some attributes from output markup.
*/
public class RemoveAttributeBehavior extends AbstractBehavior {

private static final long serialVersionUID = 1L;

private String attribute;

public RemoveAttributeBehavior(String attribute) {
this.attribute = attribute;
}

public void onComponentTag(Component component, ComponentTag tag) {
tag.getAttributes().remove(attribute);
}

}
Expand Up @@ -20,6 +20,8 @@
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.border.Border;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.SimpleFormComponentLabel;
import org.apache.wicket.markup.html.panel.ComponentFeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

Expand All @@ -28,18 +30,14 @@ public class LabelBorder extends Border {
private static final long serialVersionUID = 1L;
private final FormComponent<?> component;

public LabelBorder(String id, String fieldLabel, FormComponent<?> component) {
this(id, Model.of(fieldLabel), component);
}

public LabelBorder(String id, IModel<?> fieldLabel, FormComponent<?> component) {
public LabelBorder(String id, FormComponent<?> component) {
super(id);
this.component = component;
getBodyContainer().add(component);

add(new Label("label", fieldLabel).setRenderBodyOnly(true));
add(new SimpleFormComponentLabel("label", component));
add(new Label("help", ""));
add(new Label("error", ""));
add(new ComponentFeedbackPanel("error", component));
}

public void setHelp(String message) {
Expand All @@ -55,7 +53,6 @@ protected void onBeforeRender() {
super.onBeforeRender();

if (component.getFeedbackMessage() != null) {
addOrReplace(new Label("error", "" + component.getFeedbackMessage().getMessage()));
add(new AttributeAppender("class", Model.of("error"), " "));
}
}
Expand Down
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.karaf.webconsole.core.form;

import static org.apache.wicket.model.Model.of;

import java.util.Map;

import org.apache.wicket.Component;
Expand Down Expand Up @@ -48,13 +50,10 @@ public MapEditForm(String id, CompoundPropertyModel<Map<K, V>> model) {
add(repeatingView);
}

@SuppressWarnings("unchecked")
protected Component populateItem(String componentId, K key, IModel<V> value) {
FormComponent<V> field = new TextField<V>("value", value, (Class<V>) value.getObject().getClass());
LabelBorder border = new LabelBorder(componentId, ""+ key, field);
// border.add(new Label("label", "" + key));
// border.add();
border.setHelp("Value for " + key);
FormComponent<V> field = new TextField<V>("value", value);
field.setLabel(of(key.toString()));
LabelBorder border = new LabelBorder(componentId, field);
return border;
}
}
Expand Up @@ -16,9 +16,7 @@
limitations under the License.
-->
<wicket:border xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<label class="control-label">
<span wicket:id="label"></span>
</label>
<label class="control-label" wicket:id="label"></label>
<div class="controls">
<wicket:body />
<span wicket:id="help" class="help-inline"></span>
Expand Down
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.webconsole.osgi.core.bundle.install;

import static org.apache.wicket.model.Model.of;

import org.apache.karaf.webconsole.osgi.core.bundle.list.BundlePage;
import org.apache.karaf.webconsole.osgi.core.shared.OsgiPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
import org.ops4j.pax.wicket.api.PaxWicketMountPoint;

/**
* Page with bundle installation form.
*/
@PaxWicketMountPoint(mountPoint = "/osgi/bundle/add")
public class InstallBundlePage extends OsgiPage {

public InstallBundlePage() {
IModel<WicketInstallModel> model = of(new WicketInstallModel());

Form<WicketInstallModel> form = new Form<WicketInstallModel>("install", model);
form.add(new InstallBundlePanel("bundle", model));

form.add(new InstallBundleSubmitLink("confirm"));
form.add(new BookmarkablePageLink<BundlePage>("cancel", BundlePage.class));

add(form);
}

}
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.webconsole.osgi.core.bundle.install;

import static org.apache.wicket.model.Model.of;

import org.apache.karaf.webconsole.core.behavior.RemoveAttributeBehavior;
import org.apache.karaf.webconsole.core.form.LabelBorder;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

/**
* Bundle installation form wrapper.
*/
public class InstallBundlePanel extends Panel {

private static final long serialVersionUID = 1L;
private TextField<String> location;
private CheckBox upload;
private FileUploadField file;

public InstallBundlePanel(String id, IModel<WicketInstallModel> model) {
super(id);

Form<WicketInstallModel> form = new Form<WicketInstallModel>("form", new CompoundPropertyModel<WicketInstallModel>(model));
location = new RequiredTextField<String>("location");
location.setLabel(of("Bundle location"));
LabelBorder border = new LabelBorder("locationGroup", location);
form.add(border);

upload = new AjaxCheckBox("upload") {
private static final long serialVersionUID = 1L;

@Override
protected void onUpdate(AjaxRequestTarget target) {
if (target != null) {
if (upload.getModelObject()) {
onChecked();
} else {
onUnchecked();
}
target.addComponent(file);
}
}
};
upload.setLabel(of("Specify file to install"));
border = new LabelBorder("uploadGroup", upload);
form.add(border);

file = new FileUploadField("file");
file.setLabel(of("Bundle file"));
file.add(new SimpleAttributeModifier("disabled", "disabled"));
file.setOutputMarkupId(true);
border = new LabelBorder("fileGroup", file);
form.add(border);

add(form);
}

protected void onChecked() {
file.add(new RemoveAttributeBehavior("disabled"));
}

protected void onUnchecked() {
file.add(new SimpleAttributeModifier("disabled", "disabled"));
}
}
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.webconsole.osgi.core.bundle.install;

import org.apache.karaf.webconsole.osgi.core.bundle.list.BundlePage;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.ops4j.pax.wicket.api.PaxWicketBean;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Bundle installation link. Requires form with {@link WicketInstallModel}.
*/
public class InstallBundleSubmitLink extends SubmitLink {
private static final long serialVersionUID = 1L;

@PaxWicketBean(name = "blueprintBundleContext")
private BundleContext context;

private transient Logger logger = LoggerFactory.getLogger(InstallBundleSubmitLink.class);

public InstallBundleSubmitLink(String id) {
super(id);
}

@Override
public void onSubmit() {
WicketInstallModel modelObject = (WicketInstallModel) getForm().getModelObject();

try {
if (modelObject.isUpload()) {
context.installBundle(modelObject.getLocation(), modelObject.getInputStream());
} else {
context.installBundle(modelObject.getLocation());
}
Session.get().info("Bundle " + modelObject.getLocation() + " installed");
RequestCycle.get().setResponsePage(BundlePage.class);
} catch (Exception e) {
logger.error("Error during installation", e);
Session.get().error("Can not install bundle " + e.getMessage());
}
}
}
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.webconsole.osgi.core.bundle.install;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import org.apache.wicket.markup.html.form.upload.FileUpload;

/**
* Model for installation form.
*/
public class WicketInstallModel implements Serializable {

private static final long serialVersionUID = 1L;

private String location;
private boolean upload = false;
private FileUpload file;

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public boolean isUpload() {
return upload;
}

public void setUpload(boolean upload) {
this.upload = upload;
}

public void setFile(FileUpload file) {
this.file = file;
}

public InputStream getInputStream() throws IOException {
return file.getInputStream();
}

}

0 comments on commit c06c2f0

Please sign in to comment.