Java tutorial
/* * The DecidR Development Team 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 de.decidr.ui.view; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.vaadin.data.Item; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.data.util.IndexedContainer; import com.vaadin.event.ItemClickEvent; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.ItemClickEvent.ItemClickListener; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Table; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import de.decidr.model.annotations.Reviewed; import de.decidr.model.annotations.Reviewed.State; import de.decidr.model.entities.User; import de.decidr.model.exceptions.TransactionException; import de.decidr.model.schema.decidrtypes.ObjectFactory; import de.decidr.model.schema.decidrtypes.TConfiguration; import de.decidr.model.schema.dwdl.TRole; import de.decidr.model.schema.dwdl.TRoles; import de.decidr.ui.main.DecidrUI; import de.decidr.ui.main.ModelFacades; import de.decidr.ui.view.windows.TransactionErrorDialogComponent; /** * TODO: add comment * * @author metzlerd, meyerms */ @Reviewed(reviewers = { "unknown" }, lastRevision = "0", currentReviewState = State.NeedsReview) public class ConfigRoles extends VerticalLayout { private static final long serialVersionUID = 930396754543658581L; private Map<String, IndexedContainer> roleTables = new HashMap<String, IndexedContainer>(); private Map<String, TRole> roleNames = new HashMap<String, TRole>(); private Map<String, User> userNames = new HashMap<String, User>(); private Table userTable; private ComboBox chooseUser; public ConfigRoles(TRoles roles) { init(roles); } private void init(TRoles roles) { userTable = new Table(); userTable.addContainerProperty("Username", String.class, ""); userTable.setWriteThrough(true); userTable.setImmediate(true); userTable.setHeight(5.0f, UNITS_CM); userTable.setWidth(100.0f, UNITS_PERCENTAGE); ComboBox chooseRoles = new ComboBox("Role: "); chooseRoles.setNewItemsAllowed(false); chooseRoles.setNullSelectionAllowed(false); chooseRoles.setImmediate(true); chooseRoles.setWriteThrough(true); for (TRole role : roles.getRole()) { chooseRoles.addItem(role.getLabel()); IndexedContainer co = new IndexedContainer(); co.addContainerProperty("Username", String.class, ""); roleTables.put(role.getLabel(), co); roleNames.put(role.getLabel(), role); } // As a workaround, since the value change events tends to be fired very // late chooseRoles.addListener(new BlurListener() { private static final long serialVersionUID = 1727886677015952723L; @Override public void blur(BlurEvent event) { userTable.setContainerDataSource(roleTables.get(((ComboBox) event.getSource()).getValue())); } }); chooseRoles.addListener(new ValueChangeListener() { private static final long serialVersionUID = 3539972189878693446L; @Override public void valueChange(ValueChangeEvent event) { userTable.setContainerDataSource(roleTables.get(event.getProperty().getValue())); userTable.requestRepaintAll(); } }); chooseUser = new ComboBox(); List<User> userList; try { userList = ModelFacades.getTenantFacade().getUsersOfTenant(DecidrUI.getCurrent().getCurrentTenantId(), null); } catch (TransactionException e) { DecidrUI.getCurrent().getMainWindow().addWindow(new TransactionErrorDialogComponent(e)); userList = new ArrayList<User>(0); } for (User u : userList) { chooseUser.addItem(u.getUserProfile().getUsername()); userNames.put(u.getUserProfile().getUsername(), u); } chooseUser.setNewItemsAllowed(true); userTable.addListener(new ItemClickListener() { private static final long serialVersionUID = 2064501225684227835L; @Override public void itemClick(ItemClickEvent event) { chooseUser.setValue(event.getItem().getItemProperty("Username")); } }); Button addUser = new Button("add", new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { Item newItem = userTable.getContainerDataSource().addItem(chooseUser.getValue()); newItem.getItemProperty("Username").setValue(chooseUser.getValue()); userTable.requestRepaintAll(); } }); Button delUser = new Button("remove", new ClickListener() { private static final long serialVersionUID = -5312856358004084337L; @Override public void buttonClick(ClickEvent event) { userTable.getContainerDataSource().removeItem(chooseUser.getValue()); userTable.requestRepaintAll(); } }); this.addComponent(chooseRoles); this.addComponent(userTable); HorizontalLayout hLayout = new HorizontalLayout(); hLayout.addComponent(chooseUser); hLayout.addComponent(addUser); hLayout.addComponent(delUser); this.addComponent(hLayout); } /** * Store role-actor assignments in tConfiguration. */ public void storeRoles(TConfiguration tConfiguration) { ObjectFactory of = new ObjectFactory(); de.decidr.model.schema.decidrtypes.TRoles DTRoles = of.createTRoles(); for (String role : roleTables.keySet()) { TRole DWDLRole = roleNames.get(role); de.decidr.model.schema.decidrtypes.TRole DTRole = of.createTRole(); DTRole.setName(DWDLRole.getName()); for (Object username : roleTables.get(role).getItemIds()) { de.decidr.model.schema.decidrtypes.TActor DTActor = of.createTActor(); User u = userNames.get(username); if (u != null) { DTActor.setEmail(u.getEmail()); DTActor.setName(u.getUserProfile().getUsername()); DTActor.setUserid(u.getId()); } else { DTActor.setEmail((String) username); } DTRole.getActor().add(DTActor); } DTRoles.getRole().add(DTRole); } tConfiguration.setRoles(DTRoles); } }