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.components; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.openaz.xacml.admin.XacmlAdminAuthorization; import org.apache.openaz.xacml.admin.XacmlAdminUI; import org.apache.openaz.xacml.admin.jpa.Attribute; import org.apache.openaz.xacml.admin.jpa.Category; import org.apache.openaz.xacml.admin.jpa.Datatype; import org.apache.openaz.xacml.admin.view.windows.AttributeEditorWindow; import com.vaadin.addon.jpacontainer.EntityItem; import com.vaadin.addon.jpacontainer.JPAContainer; import com.vaadin.addon.jpacontainer.provider.CachingLocalEntityProvider; import com.vaadin.addon.jpacontainer.provider.CachingMutableLocalEntityProvider; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Container.Filter; import com.vaadin.data.Item; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.data.util.filter.Compare; import com.vaadin.event.ItemClickEvent; import com.vaadin.event.ItemClickEvent.ItemClickListener; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.AbstractSelect.ItemCaptionMode; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.ComboBox; 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 AttributeDictionary extends CustomComponent { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ @AutoGenerated private VerticalLayout mainLayout; @AutoGenerated private Table table; @AutoGenerated private HorizontalLayout horizontalLayoutToolbar; @AutoGenerated private ComboBox comboBoxFilterDatatype; @AutoGenerated private ComboBox comboBoxFilterCategory; @AutoGenerated private Button buttonClone; @AutoGenerated private Button buttonRemove; @AutoGenerated private Button buttonNew; private static final long serialVersionUID = 4553719412188869190L; private static final Log logger = LogFactory.getLog(AttributeDictionary.class); private static final Object[] visibleColumns = new Object[] { "xacmlId", "description", "categoryBean", "datatypeBean", "constraintType", "modifiedBy", "modifiedDate", "createdBy", "createdDate" }; private static final String[] columnHeaders = new String[] { "Attribute ID", "Description", "Category", "DataType", "Constraint", "Modified By", "Modified Date", "Created By", "Created Date" }; private AttributeDictionary self = this; private final JPAContainer<Attribute> attributes = new JPAContainer<Attribute>(Attribute.class); private final JPAContainer<Category> categories = new JPAContainer<Category>(Category.class); private final JPAContainer<Datatype> datatypes = new JPAContainer<Datatype>(Datatype.class); /** * 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 AttributeDictionary() { buildMainLayout(); setCompositionRoot(mainLayout); // // Finish initializing the container // boolean isReadOnly; if (((XacmlAdminUI) UI.getCurrent()).isAuthorized(XacmlAdminAuthorization.AdminAction.ACTION_WRITE, XacmlAdminAuthorization.AdminResource.RESOURCE_DICTIONARIES)) { if (logger.isDebugEnabled()) { logger.debug("write access"); } // // Make it mutable // isReadOnly = false; this.attributes.setEntityProvider(new CachingMutableLocalEntityProvider<Attribute>(Attribute.class, ((XacmlAdminUI) UI.getCurrent()).getEntityManager())); } else { if (logger.isDebugEnabled()) { logger.debug("read access"); } // // Make it read-only // isReadOnly = true; this.attributes.setEntityProvider(new CachingLocalEntityProvider<Attribute>(Attribute.class, ((XacmlAdminUI) UI.getCurrent()).getEntityManager())); } this.categories.setEntityProvider(new CachingLocalEntityProvider<Category>(Category.class, ((XacmlAdminUI) UI.getCurrent()).getEntityManager())); this.categories.sort(new String[] { "xacmlId" }, new boolean[] { true }); this.datatypes.setEntityProvider(new CachingLocalEntityProvider<Datatype>(Datatype.class, ((XacmlAdminUI) UI.getCurrent()).getEntityManager())); this.datatypes.sort(new String[] { "xacmlId" }, new boolean[] { true }); // // Initialize // this.initializeTable(isReadOnly); this.initializeButtons(isReadOnly); this.initializeCategoryComboFilter(); this.initializeDatatypeComboFilter(); } protected void initializeTable(boolean isReadOnly) { // // This is the data source // this.table.setContainerDataSource(this.attributes); // // Setup table // this.table.setVisibleColumns(visibleColumns); this.table.setColumnHeaders(columnHeaders); this.table.setImmediate(true); this.table.setColumnCollapsingAllowed(true); // // Read only? // if (isReadOnly) { if (logger.isDebugEnabled()) { logger.debug("read only table"); } return; } this.table.setSelectable(true); // // Respond to clicks // this.table.addItemClickListener(new ItemClickListener() { private static final long serialVersionUID = 1L; @Override public void itemClick(ItemClickEvent event) { if (event.isDoubleClick()) { // // Create our editor window // final AttributeEditorWindow attributeEditor = new AttributeEditorWindow( self.attributes.getItem(event.getItemId())); attributeEditor.setCaption("Edit Attribute"); attributeEditor.setModal(true); attributeEditor.center(); UI.getCurrent().addWindow(attributeEditor); } } }); // // Respond to selections // this.table.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 1L; @Override public void valueChange(ValueChangeEvent event) { Object value = self.table.getValue(); if (value == null) { self.buttonRemove.setEnabled(false); self.buttonClone.setEnabled(false); } else { self.buttonRemove.setEnabled(true); self.buttonClone.setEnabled(true); } } }); } protected void initializeButtons(boolean isReadOnly) { if (isReadOnly) { this.buttonNew.setVisible(false); this.buttonRemove.setVisible(false); this.buttonClone.setVisible(false); return; } this.buttonNew.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { AttributeDictionary.createNewAttributeWindow(); } }); this.buttonRemove.setEnabled(false); this.buttonRemove.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { // // Get the selected item // Object id = self.table.getValue(); // // Sanity check // if (id == null) { return; } // // Remove the attribute // self.attributes.removeItem(id); // // Unfortunately, removing the item does NOT result // in a ValueChange event being generated. So we must // trigger it ourselves. // self.table.select(self.table.getNullSelectionItemId()); } }); this.buttonClone.setEnabled(false); this.buttonClone.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { Object id = self.table.getValue(); if (id == null) { return; } Item item = self.table.getItem(id); if (item instanceof EntityItem) { @SuppressWarnings("unchecked") // // Get the entity // EntityItem<Attribute> entityItem = (EntityItem<Attribute>) item; // // Clone it // Attribute newAttribute = new Attribute(entityItem.getEntity(), ((XacmlAdminUI) UI.getCurrent()).getUserid()); // // Add it to the database // id = self.attributes.addEntity(newAttribute); // // Now select it // self.table.select(id); // // Refresh it to get the latest modified date // self.attributes.refreshItem(id); } } }); } protected void initializeCategoryComboFilter() { // // Set data source // this.comboBoxFilterCategory.setContainerDataSource(self.categories); this.comboBoxFilterCategory.setItemCaptionMode(ItemCaptionMode.PROPERTY); this.comboBoxFilterCategory.setItemCaptionPropertyId("xacmlId"); // // Initialize GUI properties // this.comboBoxFilterCategory.setNullSelectionAllowed(true); this.comboBoxFilterCategory.setImmediate(true); // // Respond to value changes // this.comboBoxFilterCategory.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 1L; Filter currentFilter = null; @Override public void valueChange(ValueChangeEvent event) { // // Remove filter // if (currentFilter != null) { self.attributes.removeContainerFilter(this.currentFilter); this.currentFilter = null; } // // Set the new one // Object id = self.comboBoxFilterCategory.getValue(); if (id == null) { return; } Category cat = self.categories.getItem(id).getEntity(); this.currentFilter = new Compare.Equal("categoryBean", cat); self.attributes.addContainerFilter(this.currentFilter); } }); } protected void initializeDatatypeComboFilter() { // // Set data source // this.comboBoxFilterDatatype.setContainerDataSource(self.datatypes); this.comboBoxFilterDatatype.setItemCaptionMode(ItemCaptionMode.PROPERTY); this.comboBoxFilterDatatype.setItemCaptionPropertyId("xacmlId"); // // Initialize GUI properties // this.comboBoxFilterDatatype.setNullSelectionAllowed(true); this.comboBoxFilterDatatype.setImmediate(true); // // Respond to value changes // this.comboBoxFilterDatatype.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 1L; Filter currentFilter = null; @Override public void valueChange(ValueChangeEvent event) { // // Remove filter // if (currentFilter != null) { self.attributes.removeContainerFilter(this.currentFilter); this.currentFilter = null; } // // Set the new one // Object id = self.comboBoxFilterDatatype.getValue(); if (id == null) { return; } Datatype cat = self.datatypes.getItem(id).getEntity(); this.currentFilter = new Compare.Equal("datatypeBean", cat); self.attributes.addContainerFilter(this.currentFilter); } }); } public void refreshContainer() { this.attributes.refresh(); this.categories.refresh(); this.datatypes.refresh(); } public static void createNewAttributeWindow() { // // Create our new attribute // String domain = XacmlAdminUI.getDomain(); String userid = ((XacmlAdminUI) UI.getCurrent()).getUserid(); final Attribute newAttribute = new Attribute(domain, userid); try { newAttribute.setCategoryBean(((XacmlAdminUI) UI.getCurrent()).getDefaultCategory()); newAttribute.setDatatypeBean(((XacmlAdminUI) UI.getCurrent()).getDefaultDatatype()); } catch (Exception e) { logger.error(e); return; } // // Create our editor window // final AttributeEditorWindow attributeEditor = new AttributeEditorWindow( ((XacmlAdminUI) UI.getCurrent()).getAttributes().createEntityItem(newAttribute)); attributeEditor.setCaption("Add New Attribute"); attributeEditor.setModal(true); attributeEditor.addCloseListener(new CloseListener() { private static final long serialVersionUID = 1L; @Override public void windowClose(CloseEvent e) { // // Did the user hit save? // if (attributeEditor.isSaved()) { // // Add the new attribute // ((XacmlAdminUI) UI.getCurrent()).getAttributes().addEntity(newAttribute); ((XacmlAdminUI) UI.getCurrent()).refreshAttributes(); } } }); attributeEditor.center(); UI.getCurrent().addWindow(attributeEditor); } @AutoGenerated private VerticalLayout buildMainLayout() { // common part: create layout mainLayout = new VerticalLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("-1px"); mainLayout.setHeight("-1px"); mainLayout.setMargin(true); mainLayout.setSpacing(true); // top-level component properties setWidth("-1px"); setHeight("-1px"); // horizontalLayoutToolbar horizontalLayoutToolbar = buildHorizontalLayoutToolbar(); mainLayout.addComponent(horizontalLayoutToolbar); // table table = new Table(); table.setImmediate(false); table.setWidth("100.0%"); table.setHeight("-1px"); mainLayout.addComponent(table); return mainLayout; } @AutoGenerated private HorizontalLayout buildHorizontalLayoutToolbar() { // common part: create layout horizontalLayoutToolbar = new HorizontalLayout(); horizontalLayoutToolbar.setImmediate(false); horizontalLayoutToolbar.setWidth("-1px"); horizontalLayoutToolbar.setHeight("-1px"); horizontalLayoutToolbar.setMargin(false); horizontalLayoutToolbar.setSpacing(true); // buttonNew buttonNew = new Button(); buttonNew.setCaption("New"); buttonNew.setImmediate(true); buttonNew.setDescription("Create a new attribute"); buttonNew.setWidth("70px"); buttonNew.setHeight("-1px"); horizontalLayoutToolbar.addComponent(buttonNew); horizontalLayoutToolbar.setComponentAlignment(buttonNew, new Alignment(9)); // buttonRemove buttonRemove = new Button(); buttonRemove.setCaption("Remove"); buttonRemove.setImmediate(true); buttonRemove.setDescription("Remove the selected attribute(s)"); buttonRemove.setWidth("-1px"); buttonRemove.setHeight("-1px"); horizontalLayoutToolbar.addComponent(buttonRemove); horizontalLayoutToolbar.setComponentAlignment(buttonRemove, new Alignment(9)); // buttonClone buttonClone = new Button(); buttonClone.setCaption("Clone"); buttonClone.setImmediate(true); buttonClone.setDescription("Clone an attribute."); buttonClone.setWidth("-1px"); buttonClone.setHeight("-1px"); horizontalLayoutToolbar.addComponent(buttonClone); horizontalLayoutToolbar.setComponentAlignment(buttonClone, new Alignment(9)); // comboBoxFilterCategory comboBoxFilterCategory = new ComboBox(); comboBoxFilterCategory.setCaption("Filter By Category"); comboBoxFilterCategory.setImmediate(false); comboBoxFilterCategory.setWidth("-1px"); comboBoxFilterCategory.setHeight("-1px"); horizontalLayoutToolbar.addComponent(comboBoxFilterCategory); // comboBoxFilterDatatype comboBoxFilterDatatype = new ComboBox(); comboBoxFilterDatatype.setCaption("Filter By Data Type"); comboBoxFilterDatatype.setImmediate(false); comboBoxFilterDatatype.setWidth("-1px"); comboBoxFilterDatatype.setHeight("-1px"); horizontalLayoutToolbar.addComponent(comboBoxFilterDatatype); return horizontalLayoutToolbar; } }