Java tutorial
/* * 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.openaz.xacml.admin.view.components; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.openaz.xacml.admin.jpa.PIPConfigParam; import org.apache.openaz.xacml.admin.jpa.PIPConfiguration; import org.apache.openaz.xacml.admin.jpa.PIPResolver; import org.apache.openaz.xacml.admin.jpa.PIPResolverParam; import org.apache.openaz.xacml.admin.view.events.FormChangedEventListener; import org.apache.openaz.xacml.admin.view.events.FormChangedEventNotifier; import org.apache.openaz.xacml.admin.view.windows.PIPParamEditorWindow; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Item; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.data.util.BeanItem; import com.vaadin.data.util.BeanItemContainer; import com.vaadin.event.Action; import com.vaadin.event.Action.Handler; import com.vaadin.event.ItemClickEvent; import com.vaadin.event.ItemClickEvent.ItemClickListener; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Table; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window.CloseEvent; import com.vaadin.ui.Window.CloseListener; public class PIPParameterComponent extends CustomComponent implements FormChangedEventNotifier { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ @AutoGenerated private VerticalLayout mainLayout; @AutoGenerated private Table tableParameters; @AutoGenerated private HorizontalLayout horizontalLayout_1; @AutoGenerated private Button buttonClear; @AutoGenerated private Button buttonClone; @AutoGenerated private Button buttonRemove; @AutoGenerated private Button buttonAdd; /** * */ private static final long serialVersionUID = 1L; private static final Log logger = LogFactory.getLog(PIPParameterComponent.class); private final PIPParameterComponent self = this; private final Object config; private final BasicNotifier notifier = new BasicNotifier(); private static final Action ADD_PARAM = new Action("Add Parameter"); private static final Action EDIT_PARAM = new Action("Edit Parameter"); private static final Action REMOVE_PARAM = new Action("Remove Parameter"); private static final Action CLONE_PARAM = new Action("Clone Parameter"); /** * The constructor should first build the main layout, set the * composition root and then do any custom initialization. * * The constructor will not be automatically regenerated by the * visual editor. */ public PIPParameterComponent(Object config) { buildMainLayout(); setCompositionRoot(mainLayout); // // Save // this.config = config; // // Initialize // this.initializeTable(); this.initializeButtons(); // // Initial button setup // this.setupButtons(); } protected void initializeTable() { // // Initialize GUI properties // this.tableParameters.setImmediate(true); this.tableParameters.setSelectable(true); // // Add in the data // if (this.config instanceof PIPConfiguration) { BeanItemContainer<PIPConfigParam> container = new BeanItemContainer<PIPConfigParam>( PIPConfigParam.class); PIPConfiguration config = (PIPConfiguration) this.config; for (PIPConfigParam param : config.getPipconfigParams()) { container.addBean(param); } this.tableParameters.setContainerDataSource(container); } else if (this.config instanceof PIPResolver) { BeanItemContainer<PIPResolverParam> container = new BeanItemContainer<PIPResolverParam>( PIPResolverParam.class); PIPResolver resolver = (PIPResolver) this.config; for (PIPResolverParam param : resolver.getPipresolverParams()) { container.addBean(param); } this.tableParameters.setContainerDataSource(container); } else { throw new IllegalArgumentException("Unsupported object"); } // // Finish more gui initialization // this.tableParameters.setPageLength(5);//this.tableParameters.getContainerDataSource().size() + 1); this.tableParameters.setVisibleColumns(new Object[] { "paramName", "paramValue" }); this.tableParameters.setColumnHeaders(new String[] { "Name", "Value" }); this.tableParameters.setSizeFull(); // // Action Handler // this.tableParameters.addActionHandler(new Handler() { private static final long serialVersionUID = 1L; @Override public Action[] getActions(Object target, Object sender) { if (target == null) { return new Action[] { ADD_PARAM }; } return new Action[] { EDIT_PARAM, REMOVE_PARAM, CLONE_PARAM }; } @Override public void handleAction(Action action, Object sender, Object target) { if (action == ADD_PARAM) { self.editParameter(null); return; } if (action == EDIT_PARAM) { self.editParameter(target); return; } if (action == REMOVE_PARAM) { self.removeParameter(target); return; } if (action == CLONE_PARAM) { self.cloneParameter(target); return; } } }); // // Respond to events // this.tableParameters.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 1L; @Override public void valueChange(ValueChangeEvent event) { self.setupButtons(); } }); // // Double click // this.tableParameters.addItemClickListener(new ItemClickListener() { private static final long serialVersionUID = 1L; @Override public void itemClick(ItemClickEvent event) { if (event.isDoubleClick()) { self.editParameter(event.getItemId()); } } }); } protected void initializeButtons() { this.buttonAdd.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { self.editParameter(null); } }); this.buttonClone.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { self.cloneParameter(self.tableParameters.getValue()); } }); this.buttonRemove.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { self.removeParameter(self.tableParameters.getValue()); } }); this.buttonClear.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { self.clearParameters(); } }); } protected void setupButtons() { if (this.tableParameters.getValue() != null) { this.buttonClone.setEnabled(true); this.buttonRemove.setEnabled(true); } else { this.buttonClone.setEnabled(false); this.buttonRemove.setEnabled(false); } } protected void editParameter(final Object source) { // // Make a copy // final Object target = source != null ? source : this.config instanceof PIPConfiguration ? new PIPConfigParam() : new PIPResolverParam(); final PIPParamEditorWindow window = new PIPParamEditorWindow(target); window.setModal(true); window.setCaption((source == null ? "Create New Parameter" : "Edit Parameter")); window.center(); window.addCloseListener(new CloseListener() { private static final long serialVersionUID = 1L; @Override public void windowClose(CloseEvent e) { // // Did user save? // if (window.isSaved() == false) { return; } // // Yes - was it a brand new object? // if (source == null) { // // yes add it to the parent object // if (self.config instanceof PIPConfiguration) { ((PIPConfiguration) self.config).addPipconfigParam((PIPConfigParam) target); } else { ((PIPResolver) self.config).addPipresolverParam((PIPResolverParam) target); } // // add it to the table // Item item = self.tableParameters.addItem(target); if (item == null) { logger.error("Failed to add parameter: " + target); } else { self.tableParameters.select(target); } } else { // // Copy the parameters over // if (source instanceof PIPConfigParam) { ((PIPConfigParam) source).setParamName(((PIPConfigParam) target).getParamName()); ((PIPConfigParam) source).setParamValue(((PIPConfigParam) target).getParamValue()); } else { ((PIPResolverParam) source).setParamName(((PIPResolverParam) target).getParamName()); ((PIPResolverParam) source).setParamValue(((PIPResolverParam) target).getParamValue()); } // // Update the table // self.tableParameters.refreshRowCache(); } } }); UI.getCurrent().addWindow(window); } protected void cloneParameter(Object target) { if (target == null) { logger.error("null target sent to clone method"); return; } Item item; if (target instanceof PIPConfigParam && this.config instanceof PIPConfiguration) { PIPConfigParam param = new PIPConfigParam((PIPConfigParam) target); ((PIPConfiguration) this.config).addPipconfigParam(param); item = this.tableParameters.addItem(new BeanItem<PIPConfigParam>(param)); } else if (target instanceof PIPResolverParam && this.config instanceof PIPResolver) { PIPResolverParam param = new PIPResolverParam((PIPResolverParam) target); ((PIPResolver) this.config).addPipresolverParam(param); item = this.tableParameters.addItem(new BeanItem<PIPResolverParam>(param)); } else { throw new IllegalArgumentException("Unsupported param and config combination."); } if (item == null) { logger.error("Failed to add parameter to table: " + target); } else { this.tableParameters.select(target); } } protected void removeParameter(Object target) { if (target == null) { logger.error("null target sent to remove method"); return; } if (this.config instanceof PIPConfiguration) { if (((PIPConfiguration) this.config).removePipconfigParam((PIPConfigParam) target) == null) { logger.error("Failed to remove parameter from pip configuration"); return; } } else { if (((PIPResolver) this.config).removePipresolverParam((PIPResolverParam) target) == null) { logger.error("Failed to remove parameter from pip resolver"); return; } } if (this.tableParameters.removeItem(target) == false) { logger.error("Failed to remove parameter from table"); } } protected void clearParameters() { this.tableParameters.removeAllItems(); if (this.config instanceof PIPConfiguration) { ((PIPConfiguration) this.config).clearConfigParams(); } else { ((PIPResolver) this.config).clearParams(); } } @Override public boolean addListener(FormChangedEventListener listener) { return this.notifier.addListener(listener); } @Override public boolean removeListener(FormChangedEventListener listener) { return this.notifier.removeListener(listener); } @Override public void fireFormChangedEvent() { this.notifier.fireFormChangedEvent(); } @AutoGenerated private VerticalLayout buildMainLayout() { // common part: create layout mainLayout = new VerticalLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("-1px"); mainLayout.setHeight("-1px"); mainLayout.setMargin(false); mainLayout.setSpacing(true); // top-level component properties setWidth("-1px"); setHeight("-1px"); // horizontalLayout_1 horizontalLayout_1 = buildHorizontalLayout_1(); mainLayout.addComponent(horizontalLayout_1); // tableParameters tableParameters = new Table(); tableParameters.setCaption("Configuration Parameters"); tableParameters.setImmediate(false); tableParameters.setWidth("-1px"); tableParameters.setHeight("-1px"); mainLayout.addComponent(tableParameters); return mainLayout; } @AutoGenerated private HorizontalLayout buildHorizontalLayout_1() { // common part: create layout horizontalLayout_1 = new HorizontalLayout(); horizontalLayout_1.setImmediate(false); horizontalLayout_1.setWidth("-1px"); horizontalLayout_1.setHeight("-1px"); horizontalLayout_1.setMargin(false); horizontalLayout_1.setSpacing(true); // buttonAdd buttonAdd = new Button(); buttonAdd.setCaption("Add"); buttonAdd.setImmediate(false); buttonAdd.setWidth("-1px"); buttonAdd.setHeight("-1px"); horizontalLayout_1.addComponent(buttonAdd); // buttonRemove buttonRemove = new Button(); buttonRemove.setCaption("Remove"); buttonRemove.setImmediate(false); buttonRemove.setWidth("-1px"); buttonRemove.setHeight("-1px"); horizontalLayout_1.addComponent(buttonRemove); // buttonClone buttonClone = new Button(); buttonClone.setCaption("Clone"); buttonClone.setImmediate(false); buttonClone.setWidth("-1px"); buttonClone.setHeight("-1px"); horizontalLayout_1.addComponent(buttonClone); // buttonClear buttonClear = new Button(); buttonClear.setCaption("Clear All"); buttonClear.setImmediate(false); buttonClear.setWidth("-1px"); buttonClear.setHeight("-1px"); horizontalLayout_1.addComponent(buttonClear); return horizontalLayout_1; } }