View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application.views.common.formfactory.impl;
20  
21  import java.io.Serializable;
22  import java.text.MessageFormat;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.stereotype.Service;
29  
30  import com.hack23.cia.web.impl.ui.application.views.common.formfactory.api.FormFactory;
31  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
32  import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentSize;
33  import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.CommitFormWrapperClickListener;
34  import com.vaadin.data.fieldgroup.BeanFieldGroup;
35  import com.vaadin.data.util.BeanItem;
36  import com.vaadin.server.FontAwesome;
37  import com.vaadin.ui.AbstractOrderedLayout;
38  import com.vaadin.ui.Alignment;
39  import com.vaadin.ui.Button;
40  import com.vaadin.ui.Button.ClickListener;
41  import com.vaadin.ui.Field;
42  import com.vaadin.ui.FormLayout;
43  import com.vaadin.ui.Panel;
44  import com.vaadin.ui.PasswordField;
45  import com.vaadin.ui.VerticalLayout;
46  
47  /**
48   * The Class FormFactoryImpl.
49   */
50  @Service
51  public final class FormFactoryImpl implements FormFactory {
52  
53  	/** The Constant LOG_MSG_PROPERTY. */
54  	private static final String LOG_MSG_PROPERTY = "property:{}";
55  
56  	/** The Constant HIDDEN_FIELD_NAME. */
57  	private static final String HIDDEN_FIELD_NAME = "password";
58  
59  	/** The Constant LOGGER. */
60  	private static final Logger LOGGER = LoggerFactory.getLogger(FormFactoryImpl.class);
61  
62  	@Override
63  	public <T extends Serializable> void addRequestInputFormFields(final FormLayout panelContent, final BeanItem<T> item,
64  			final Class<T> beanType, final List<String> displayProperties,final String buttonLabel,final ClickListener buttonListener) {
65  		final BeanFieldGroup<T> fieldGroup = new BeanFieldGroup<>(beanType);
66  		fieldGroup.setItemDataSource(item);
67  		fieldGroup.setReadOnly(true);
68  
69  		for (final String property : displayProperties) {
70  
71  			final Field<?> buildAndBind;
72  			if (property.contains(HIDDEN_FIELD_NAME)) {
73  				buildAndBind = fieldGroup.buildAndBind(property,property, PasswordField.class);
74  			} else {
75  				buildAndBind = fieldGroup.buildAndBind(property);
76  			}
77  
78  			buildAndBind.setId(MessageFormat.format("{0}.{1}", buttonLabel, property));
79  			buildAndBind.setReadOnly(false);
80  			buildAndBind.setWidth(ContentSize.HALF_SIZE);
81  
82  			panelContent.addComponent(buildAndBind);
83  		}
84  		final Collection<Object> unboundPropertyIds = fieldGroup.getUnboundPropertyIds();
85  		for (final Object property : unboundPropertyIds) {
86  			LOGGER.debug(LOG_MSG_PROPERTY, property);
87  		}
88  
89  		final VerticalLayout verticalLayout = new VerticalLayout();
90  		verticalLayout.setWidth("50%");
91  
92  		final Button button = new Button(buttonLabel,new CommitFormWrapperClickListener(fieldGroup,buttonListener));
93  		button.setId(buttonLabel);
94  		button.setWidth("25%");
95  		button.setIcon(FontAwesome.PAW);
96  
97  		verticalLayout.addComponent(button);
98  		verticalLayout.setComponentAlignment(button, Alignment.MIDDLE_RIGHT);
99  
100 		panelContent.addComponent(verticalLayout);
101 	}
102 
103 	@Override
104 	public <T extends Serializable> void addFormPanelTextFields(final AbstractOrderedLayout panelContent, final BeanItem<T> item,
105 			final Class<T> beanType, final List<String> displayProperties) {
106 
107 
108 		final Panel formPanel = new Panel();
109 		formPanel.setSizeFull();
110 
111 		panelContent.addComponent(formPanel);
112 		panelContent.setExpandRatio(formPanel, ContentRatio.GRID);
113 
114 		final FormLayout formContent = new FormLayout();
115 		formPanel.setContent(formContent);
116 
117 
118 		final BeanFieldGroup<T> fieldGroup = new BeanFieldGroup<>(beanType);
119 		fieldGroup.setItemDataSource(item);
120 		fieldGroup.setReadOnly(true);
121 
122 		for (final String property : displayProperties) {
123 
124 			final Field<?> buildAndBind = fieldGroup.buildAndBind(property);
125 			buildAndBind.setWidth(ContentSize.FULL_SIZE);
126 
127 			formContent.addComponent(buildAndBind);
128 		}
129 		final Collection<Object> unboundPropertyIds = fieldGroup.getUnboundPropertyIds();
130 		for (final Object property : unboundPropertyIds) {
131 			LOGGER.debug(LOG_MSG_PROPERTY, property);
132 		}
133 
134 
135 	}
136 
137 
138 
139 }