Re-factored the code as per the sonar standards.

Signed-off-by: Gaurav <gaurav.sahay@in.bosch.com>
This commit is contained in:
Gaurav
2016-08-19 10:44:12 +02:00
parent b301f73aa4
commit b80323caf9
12 changed files with 208 additions and 141 deletions

View File

@@ -8,8 +8,6 @@
*/
package org.eclipse.hawkbit.ui.artifacts.smtable;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import org.eclipse.hawkbit.repository.EntityFactory;
@@ -17,6 +15,7 @@ import org.eclipse.hawkbit.repository.SoftwareManagement;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.ui.artifacts.event.SoftwareModuleEvent;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow.SaveDialogCloseListener;
import org.eclipse.hawkbit.ui.common.SoftwareModuleTypeBeanQuery;
import org.eclipse.hawkbit.ui.common.table.BaseEntityEventType;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
@@ -47,7 +46,7 @@ import com.vaadin.ui.themes.ValoTheme;
*/
@SpringComponent
@ViewScope
public class SoftwareModuleAddUpdateWindow extends CustomComponent implements Serializable {
public class SoftwareModuleAddUpdateWindow extends CustomComponent {
private static final long serialVersionUID = -5217675246477211483L;
@@ -183,9 +182,24 @@ public class SoftwareModuleAddUpdateWindow extends CustomComponent implements Se
setCompositionRoot(formLayout);
window = SPUIWindowDecorator.getWindow(i18n.get("upload.caption.add.new.swmodule"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveOrUpdate(), null, null, formLayout, i18n);
window.setCloseListener(() -> !isDuplicate());
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, null, null, formLayout, i18n);
window.setSaveDialogCloseListener(new SaveDialogCloseListener() {
@Override
public void saveOrUpdate() {
if (editSwModule) {
updateSwModule();
} else {
addNewBaseSoftware();
}
}
@Override
public boolean canWindowClose() {
return editSwModule || !isDuplicate();
}
});
nameTextField.setEnabled(!editSwModule);
versionTextField.setEnabled(!editSwModule);
typeComboBox.setEnabled(!editSwModule);
@@ -265,14 +279,6 @@ public class SoftwareModuleAddUpdateWindow extends CustomComponent implements Se
typeComboBox.setValue(swModle.getType().getName());
}
private void saveOrUpdate() {
if (editSwModule) {
updateSwModule();
} else {
addNewBaseSoftware();
}
}
public FormLayout getFormLayout() {
return formLayout;
}

View File

@@ -32,7 +32,6 @@ import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.shared.ui.colorpicker.Color;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.components.colorpicker.ColorChangeListener;
@@ -122,7 +121,7 @@ public class CreateUpdateSoftwareTypeLayout extends CreateUpdateTypeLayout<Softw
super.optionValueChanged(event);
if (updateTypeStr.equals(event.getProperty().getValue())) {
if (updateTagStr.equals(event.getProperty().getValue())) {
assignOptiongroup.setEnabled(false);
} else {
assignOptiongroup.setEnabled(true);
@@ -190,14 +189,13 @@ public class CreateUpdateSoftwareTypeLayout extends CreateUpdateTypeLayout<Softw
}
@Override
protected void save(final ClickEvent event) {
if (optiongroup.getValue().equals(createTypeStr)) {
if (!isDuplicate()) {
createNewSWModuleType();
}
} else {
updateSWModuleType(findEntityByName());
}
protected void createEntity() {
createNewSWModuleType();
}
@Override
protected void updateEntity(final SoftwareModuleType entity) {
updateSWModuleType(entity);
}
@Override

View File

@@ -8,8 +8,6 @@
*/
package org.eclipse.hawkbit.ui.common;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -90,8 +88,6 @@ public class CommonDialogWindow extends Window {
protected ValueChangeListener buttonEnableListener;
private final ClickListener saveButtonClickListener;
private final ClickListener cancelButtonClickListener;
private final ClickListener closeClickListener = event -> onCloseEvent(event);
@@ -102,7 +98,7 @@ public class CommonDialogWindow extends Window {
private final I18N i18n;
private transient CommonDialogWindowCloseListener closeListener;
private transient SaveDialogCloseListener closeListener;
/**
* Constructor.
@@ -123,13 +119,10 @@ public class CommonDialogWindow extends Window {
* the i18n service
*/
public CommonDialogWindow(final String caption, final Component content, final String helpLink,
final ClickListener saveButtonClickListener, final ClickListener cancelButtonClickListener,
final AbstractLayout layout, final I18N i18n) {
checkNotNull(saveButtonClickListener);
final ClickListener cancelButtonClickListener, final AbstractLayout layout, final I18N i18n) {
this.caption = caption;
this.content = content;
this.helpLink = helpLink;
this.saveButtonClickListener = saveButtonClickListener;
this.cancelButtonClickListener = cancelButtonClickListener;
this.orginalValues = new HashMap<>();
this.allComponents = getAllComponents(layout);
@@ -137,7 +130,7 @@ public class CommonDialogWindow extends Window {
init();
}
public void setCloseListener(final CommonDialogWindowCloseListener closeListener) {
public void setSaveDialogCloseListener(final SaveDialogCloseListener closeListener) {
this.closeListener = closeListener;
}
@@ -147,10 +140,14 @@ public class CommonDialogWindow extends Window {
return;
}
if (closeListener != null && !closeListener.canWindowClose()) {
return;
if (closeListener == null || closeListener.canWindowClose()) {
close();
}
close();
if (closeListener.canWindowSaveOrUpdate()) {
closeListener.saveOrUpdate();
}
}
@Override
@@ -460,7 +457,6 @@ public class CommonDialogWindow extends Window {
saveButton.setSizeUndefined();
saveButton.addStyleName("default-color");
addCloseListenerForSaveButton();
saveButton.addClickListener(saveButtonClickListener);
saveButton.setEnabled(false);
buttonsLayout.addComponent(saveButton);
buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
@@ -542,13 +538,28 @@ public class CommonDialogWindow extends Window {
* Called before the save happens.
*
*/
@FunctionalInterface
public interface CommonDialogWindowCloseListener {
public interface SaveDialogCloseListener {
/**
* Default true for all the windows except for DistributionAddUpdate
* window.
*
* @return true/false .
*/
default boolean canWindowSaveOrUpdate() {
return true;
}
/**
* @return true/false based on the dialog window to be closed or not.
*/
boolean canWindowClose();
/**
* Saves/Updates
*
*/
void saveOrUpdate();
}
}

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.ui.decorators;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow.SaveDialogCloseListener;
import org.eclipse.hawkbit.ui.utils.I18N;
import org.eclipse.hawkbit.ui.utils.SPUIDefinitions;
import org.eclipse.hawkbit.ui.utils.SPUIStyleDefinitions;
@@ -44,15 +45,34 @@ public final class SPUIWindowDecorator {
* window type
* @return Window
*/
public static CommonDialogWindow getWindow(final String caption, final String id, final String type,
final Component content, final ClickListener cancelButtonClickListener, final String helpLink,
final AbstractLayout layout, final I18N i18n) {
return getWindow(caption, id, type, content, null, cancelButtonClickListener, helpLink, layout, i18n);
}
public static CommonDialogWindow getWindow(final String caption, final String id, final String type,
final Component content, final ClickListener saveButtonClickListener,
final ClickListener cancelButtonClickListener, final String helpLink, final AbstractLayout layout,
final I18N i18n) {
final CommonDialogWindow window = new CommonDialogWindow(caption, content, helpLink, saveButtonClickListener,
cancelButtonClickListener, layout, i18n);
final CommonDialogWindow window = new CommonDialogWindow(caption, content, helpLink, cancelButtonClickListener,
layout, i18n);
if (SPUIDefinitions.CUSTOM_METADATA_WINDOW.equals(type)) {
window.setCloseListener(() -> false);
window.setSaveDialogCloseListener(new SaveDialogCloseListener() {
@Override
public void saveOrUpdate() {
saveButtonClickListener.buttonClick(null);
}
@Override
public boolean canWindowClose() {
return false;
}
});
window.setDraggable(true);
window.setClosable(true);
} else {

View File

@@ -40,7 +40,6 @@ import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.AbstractSelect.ItemDescriptionGenerator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
@@ -453,7 +452,7 @@ public class CreateUpdateDistSetTypeLayout extends CreateUpdateTypeLayout<Distri
super.optionValueChanged(event);
if (updateTypeStr.equals(event.getProperty().getValue())) {
if (updateTagStr.equals(event.getProperty().getValue())) {
selectedTable.getContainerDataSource().removeAllItems();
getSourceTableData();
distTypeSelectLayout.setEnabled(false);
@@ -565,14 +564,15 @@ public class CreateUpdateDistSetTypeLayout extends CreateUpdateTypeLayout<Distri
}
@Override
protected void save(final ClickEvent event) {
if (optiongroup.getValue().equals(createTypeStr)) {
if (!isDuplicate()) {
createNewDistributionSetType();
}
} else {
updateDistributionSetType(findEntityByKey());
}
protected void updateEntity(final DistributionSetType entity) {
updateDistributionSetType(entity);
}
@Override
protected void createEntity() {
createNewDistributionSetType();
}
@Override

View File

@@ -20,6 +20,7 @@ import org.eclipse.hawkbit.ui.colorpicker.ColorPickerConstants;
import org.eclipse.hawkbit.ui.colorpicker.ColorPickerHelper;
import org.eclipse.hawkbit.ui.colorpicker.ColorPickerLayout;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow.SaveDialogCloseListener;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
import org.eclipse.hawkbit.ui.decorators.SPUIWindowDecorator;
import org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil;
@@ -106,12 +107,9 @@ public abstract class AbstractCreateUpdateTagLayout<E extends NamedEntity> exten
protected abstract String getWindowCaption();
/**
* Save new tag / update new tag.
*
* @param event
*/
protected abstract void save(final Button.ClickEvent event);
protected abstract void updateEntity(E entity);
protected abstract void createEntity();
/**
* Discard the changes and close the popup.
@@ -464,8 +462,29 @@ public abstract class AbstractCreateUpdateTagLayout<E extends NamedEntity> exten
public CommonDialogWindow getWindow() {
reset();
window = SPUIWindowDecorator.getWindow(getWindowCaption(), null, SPUIDefinitions.CREATE_UPDATE_WINDOW, this,
this::save, this::discard, null, mainLayout, i18n);
window.setCloseListener(() -> !isDuplicate());
this::discard, null, mainLayout, i18n);
window.setSaveDialogCloseListener(new SaveDialogCloseListener() {
@Override
public void saveOrUpdate() {
if (optiongroup.getValue().equals(createTagStr)) {
if (!isDuplicate()) {
createEntity();
}
} else {
updateEntity(findEntityByName());
}
}
@Override
public boolean canWindowClose() {
final boolean update = !optiongroup.getValue().equals(createTagStr);
return update || !isDuplicate();
}
});
return window;
}

View File

@@ -20,7 +20,6 @@ import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.colorpicker.Color;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
@@ -32,12 +31,10 @@ import com.vaadin.ui.themes.ValoTheme;
* Superclass defining common properties and methods for creating/updating
* types.
*/
public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends AbstractCreateUpdateTagLayout {
public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends AbstractCreateUpdateTagLayout<E> {
private static final long serialVersionUID = 5732904956185988397L;
protected String createTypeStr;
protected String updateTypeStr;
protected TextField typeKey;
public static final String TYPE_NAME_DYNAMIC_STYLE = "new-tag-name";
@@ -52,8 +49,8 @@ public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends Abst
@Override
protected void createRequiredComponents() {
createTypeStr = i18n.get("label.create.type");
updateTypeStr = i18n.get("label.update.type");
createTagStr = i18n.get("label.create.type");
updateTagStr = i18n.get("label.update.type");
comboLabel = SPUIComponentProvider.getLabel(i18n.get("label.choose.type"), null);
colorLabel = SPUIComponentProvider.getLabel(i18n.get("label.choose.type.color"), null);
colorLabel.addStyleName(SPUIDefinitions.COLOR_LABEL_STYLE);
@@ -133,7 +130,7 @@ public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends Abst
@Override
protected void optionValueChanged(final ValueChangeEvent event) {
if (updateTypeStr.equals(event.getProperty().getValue())) {
if (updateTagStr.equals(event.getProperty().getValue())) {
tagName.clear();
tagDesc.clear();
typeKey.clear();
@@ -202,25 +199,14 @@ public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends Abst
optiongroup.setNullSelectionAllowed(false);
if (hasCreatePermission) {
optiongroup.addItem(createTypeStr);
optiongroup.addItem(createTagStr);
}
if (hasUpdatePermission) {
optiongroup.addItem(updateTypeStr);
optiongroup.addItem(updateTagStr);
}
setOptionGroupDefaultValue(hasCreatePermission, hasUpdatePermission);
}
@Override
protected void setOptionGroupDefaultValue(final boolean hasCreatePermission, final boolean hasUpdatePermission) {
if (hasCreatePermission) {
optiongroup.select(createTypeStr);
}
if (hasUpdatePermission && !hasCreatePermission) {
optiongroup.select(updateTypeStr);
}
}
protected void setColorPickerComponentsColor(final String color) {
if (null == color) {
@@ -274,11 +260,6 @@ public abstract class CreateUpdateTypeLayout<E extends NamedEntity> extends Abst
protected abstract String getDuplicateKeyErrorMessage(E existingType);
@Override
protected void save(final ClickEvent event) {
// is implemented in the inherited class
}
@Override
protected void populateTagNameCombo() {
// is implemented in the inherited class

View File

@@ -23,6 +23,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.TenantMetaData;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow.SaveDialogCloseListener;
import org.eclipse.hawkbit.ui.common.DistributionSetIdName;
import org.eclipse.hawkbit.ui.common.DistributionSetTypeBeanQuery;
import org.eclipse.hawkbit.ui.common.table.BaseEntityEventType;
@@ -179,14 +180,6 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
return tenantMetaData.getDefaultDsType();
}
private void saveDistribution() {
if (editDistribution) {
updateDistribution();
} else {
addNewDistribution();
}
}
/**
* Update Distribution.
*/
@@ -223,9 +216,6 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
*/
private void addNewDistribution() {
editDistribution = Boolean.FALSE;
if (isDuplicate()) {
return;
}
final String name = HawkbitCommonUtil.trimAndNullIfEmpty(distNameTextField.getValue());
final String version = HawkbitCommonUtil.trimAndNullIfEmpty(distVersionTextField.getValue());
@@ -243,9 +233,9 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
i18n.get("message.new.dist.save.success", new Object[] { newDist.getName(), newDist.getVersion() }));
final Set<DistributionSetIdName> s = new HashSet<>();
s.add(new DistributionSetIdName(newDist.getId(), newDist.getName(), newDist.getVersion()));
final DistributionSetTable distributionSetTable = SpringContextHelper.getBean(DistributionSetTable.class);
distributionSetTable.setValue(s);
s.add(new DistributionSetIdName(newDist.getId(), newDist.getName(), newDist.getVersion()));
final DistributionSetTable distributionSetTable = SpringContextHelper.getBean(DistributionSetTable.class);
distributionSetTable.setValue(s);
}
/**
@@ -346,8 +336,30 @@ public class DistributionAddUpdateWindowLayout extends CustomComponent {
populateDistSetTypeNameCombo();
populateValuesOfDistribution(editDistId);
window = SPUIWindowDecorator.getWindow(i18n.get("caption.add.new.dist"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveDistribution(), null, null, formLayout, i18n);
window.setCloseListener(() -> !isDuplicate());
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, null, null, formLayout, i18n);
window.setSaveDialogCloseListener(new SaveDialogCloseListener() {
@Override
public void saveOrUpdate() {
if (editDistribution) {
updateDistribution();
} else {
addNewDistribution();
}
}
@Override
public boolean canWindowSaveOrUpdate() {
return editDistribution || !isDuplicate();
}
@Override
public boolean canWindowClose() {
return !isDuplicate();
}
});
return window;
}

View File

@@ -80,18 +80,16 @@ public class CreateUpdateDistributionTagLayoutWindow extends AbstractCreateUpdat
optiongroup.addValueChangeListener(this::optionValueChanged);
}
/**
* Update DistributionTag.
*/
@Override
public void save(final ClickEvent event) {
if (optiongroup.getValue().equals(createTagStr)) {
if (!isDuplicate()) {
createNewTag();
}
} else {
updateExistingTag(findEntityByName());
}
protected void createEntity() {
createNewTag();
}
@Override
protected void updateEntity(final DistributionSetTag entity) {
updateExistingTag(findEntityByName());
}
@Override

View File

@@ -16,6 +16,7 @@ import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetIdName;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow.SaveDialogCloseListener;
import org.eclipse.hawkbit.ui.common.table.BaseEntityEventType;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
import org.eclipse.hawkbit.ui.decorators.SPUIWindowDecorator;
@@ -134,14 +135,6 @@ public class TargetAddUpdateWindowLayout extends CustomComponent {
eventBus.publish(this, new TargetTableEvent(BaseEntityEventType.UPDATED_ENTITY, latestTarget));
}
private void saveTargetListner() {
if (editTarget) {
updateTarget();
} else {
addNewTarget();
}
}
private void addNewTarget() {
if (isDuplicate()) {
return;
@@ -168,8 +161,25 @@ public class TargetAddUpdateWindowLayout extends CustomComponent {
public Window getWindow() {
eventBus.publish(this, DragEvent.HIDE_DROP_HINT);
window = SPUIWindowDecorator.getWindow(i18n.get("caption.add.new.target"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> saveTargetListner(), null, null, formLayout, i18n);
window.setCloseListener(() -> !isDuplicate());
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, null, null, formLayout, i18n);
window.setSaveDialogCloseListener(new SaveDialogCloseListener() {
@Override
public void saveOrUpdate() {
if (editTarget) {
updateTarget();
} else {
addNewTarget();
}
}
@Override
public boolean canWindowClose() {
return editTarget || !isDuplicate();
}
});
return window;
}

View File

@@ -26,7 +26,6 @@ import org.vaadin.spring.events.annotation.EventBusListenerMethod;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Button.ClickEvent;
/**
*
@@ -101,14 +100,13 @@ public class CreateUpdateTargetTagLayoutWindow extends AbstractCreateUpdateTagLa
}
@Override
public void save(final ClickEvent event) {
if (optiongroup.getValue().equals(createTagStr)) {
if (!isDuplicate()) {
createNewTag();
}
} else {
updateExistingTag(findEntityByName());
}
protected void updateEntity(final TargetTag entity) {
updateExistingTag(entity);
}
@Override
protected void createEntity() {
createNewTag();
}
@Override

View File

@@ -29,6 +29,7 @@ import org.eclipse.hawkbit.repository.model.RolloutGroupConditionBuilder;
import org.eclipse.hawkbit.repository.model.RolloutGroupConditions;
import org.eclipse.hawkbit.ui.UiProperties;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow;
import org.eclipse.hawkbit.ui.common.CommonDialogWindow.SaveDialogCloseListener;
import org.eclipse.hawkbit.ui.common.DistributionSetIdName;
import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
import org.eclipse.hawkbit.ui.decorators.SPUIWindowDecorator;
@@ -165,9 +166,30 @@ public class AddUpdateRolloutWindowLayout extends GridLayout {
public CommonDialogWindow getWindow() {
resetComponents();
return SPUIWindowDecorator.getWindow(i18n.get("caption.configure.rollout"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, event -> onRolloutSave(), null,
final CommonDialogWindow window = SPUIWindowDecorator.getWindow(i18n.get("caption.configure.rollout"), null,
SPUIDefinitions.CREATE_UPDATE_WINDOW, this, null,
uiProperties.getLinks().getDocumentation().getRolloutView(), this, i18n);
window.setSaveDialogCloseListener(new SaveDialogCloseListener() {
@Override
public void saveOrUpdate() {
if (editRolloutEnabled) {
editRollout();
} else {
createRollout();
}
}
@Override
public boolean canWindowClose() {
if (editRolloutEnabled) {
return duplicateCheckForEdit();
}
return duplicateCheck();
}
});
return window;
}
/**
@@ -393,14 +415,6 @@ public class AddUpdateRolloutWindowLayout extends GridLayout {
targetFilterQF);
}
private void onRolloutSave() {
if (editRolloutEnabled) {
editRollout();
} else {
createRollout();
}
}
private void editRollout() {
if (duplicateCheckForEdit() && rolloutForEdit != null) {
rolloutForEdit.setName(rolloutName.getValue());