/**
* Copyright 2011 the original author or authors.
*
* Licensed 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.bricket.plugin.language.web;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.bricket.plugin.language.domain.Language;
import org.bricket.plugin.language.service.LanguageService;
import org.bricket.plugin.language.domain.LanguagePlugin;
import org.bricket.plugin.language.service.LanguagePluginService;
import org.bricket.service.BricketServiceException;
import org.bricket.web.BricketPanel;
import brix.jcr.wrapper.BrixNode;
/**
* @author Ingo Renner
* @author Henning Teek
*/
public class ManageLanguagePluginPanel extends BricketPanel {
@SpringBean
private LanguageService languageService;
@SpringBean
private LanguagePluginService languagePluginService;
private LanguagePlugin languagePlugin;
public ManageLanguagePluginPanel(String id, IModel<BrixNode> tileNode) {
super(id, tileNode);
}
@Override
protected void initGui() {
languagePlugin = languagePluginService.loadLanguagePlugin();
add(getDropDownChoice());
}
private Component getDropDownChoice() {
return new DropDownChoice<Language>("languages",
new PropertyModel<Language>(languagePlugin, "defaultLanguage"),
new LoadableDetachableModel<List<Language>>() {
@Override
protected List<Language> load() {
return languageService.loadAllLanguage();
}
}, new ChoiceRenderer<Language>("name", "id")).add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
try {
// feedback
getSession().cleanupFeedbackMessages();
target.addComponent(getFeedback());
languagePlugin = languagePluginService.saveLanguagePlugin(languagePlugin);
Component ddc = getDropDownChoice();
ManageLanguagePluginPanel.this.addOrReplace(ddc);
target.addComponent(ddc);
} catch (BricketServiceException e) {
error(new StringResourceModel(e.getKey(), ManageLanguagePluginPanel.this, null).getString());
target.addComponent(getFeedback());
}
}
});
}
}