Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNPORTAL-3256 Fix Java generics in UIFormInputItemSelector & Co.
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Sep 10, 2013
1 parent 1d3934d commit a18e53d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 73 deletions.
Expand Up @@ -32,7 +32,7 @@
* An item selector represented by a normal list
*/
@ComponentConfig(template = "system:/groovy/webui/core/UIItemSelector.gtmpl")
public class UIItemSelector extends UIComponent {
public class UIItemSelector<T> extends UIComponent {
/**
* The name of this selector
*/
Expand All @@ -41,26 +41,26 @@ public class UIItemSelector extends UIComponent {
/**
* The item categories, each category contains items
*/
private List<SelectItemCategory> categories_;
private List<SelectItemCategory<T>> categories_;

public UIItemSelector(String name) {
name_ = name;
setComponentConfig(getClass(), null);
categories_ = new ArrayList<SelectItemCategory>();
categories_ = new ArrayList<SelectItemCategory<T>>();
}

public String getName() {
return name_;
}

public List<SelectItemCategory> getItemCategories() {
public List<SelectItemCategory<T>> getItemCategories() {
return categories_;
}

public void setItemCategories(List<SelectItemCategory> categories) {
public void setItemCategories(List<SelectItemCategory<T>> categories) {
categories_ = categories;
boolean selected = false;
for (SelectItemCategory ele : categories) {
for (SelectItemCategory<T> ele : categories) {
if (ele.isSelected()) {
if (selected)
ele.setSelected(false);
Expand All @@ -72,22 +72,22 @@ public void setItemCategories(List<SelectItemCategory> categories) {
categories_.get(0).setSelected(true);
}

public SelectItemCategory getSelectedItemCategory() {
for (SelectItemCategory category : categories_) {
public SelectItemCategory<T> getSelectedItemCategory() {
for (SelectItemCategory<T> category : categories_) {
if (category.isSelected())
return category;
}
if (categories_.size() > 0) {
SelectItemCategory category = categories_.get(0);
SelectItemCategory<T> category = categories_.get(0);
category.setSelected(true);
category.getSelectItemOptions().get(0).setSelected(true);
return category;
}
return null;
}

public SelectItemOption getSelectedItemOption() {
SelectItemCategory selectedCategory = getSelectedItemCategory();
public SelectItemOption<T> getSelectedItemOption() {
SelectItemCategory<T> selectedCategory = getSelectedItemCategory();
if (selectedCategory != null)
return selectedCategory.getSelectedItemOption();
return null;
Expand Down
Expand Up @@ -31,7 +31,7 @@
* @see org.exoplatform.webui.form.UIFormInputItemSelector
* @see SelectItemOption
*/
public class SelectItemCategory {
public class SelectItemCategory<T> {
/**
* The name of the category
*/
Expand All @@ -45,7 +45,7 @@ public class SelectItemCategory {
/**
* The list of SelectItemOption that this category contains
*/
private List<SelectItemOption> options_;
private List<SelectItemOption<T>> options_;

/**
* Whether this category is selected
Expand Down Expand Up @@ -86,27 +86,26 @@ public void setSelected(boolean b) {
selected_ = b;
}

@SuppressWarnings("unchecked")
public <T extends SelectItemOption> List<T> getSelectItemOptions() {
return (List<T>) options_;
public List<SelectItemOption<T>> getSelectItemOptions() {
return options_;
}

@SuppressWarnings("unchecked")
public <T extends SelectItemOption> void setSelectItemOptions(List<T> options) {
options_ = (List<SelectItemOption>) options;
public void setSelectItemOptions(List<SelectItemOption<T>> options) {
this.options_ = options;
}

public SelectItemCategory addSelectItemOption(SelectItemOption option) {
if (options_ == null)
options_ = new ArrayList<SelectItemOption>();
public SelectItemCategory<T> addSelectItemOption(SelectItemOption<T> option) {
if (options_ == null) {
options_ = new ArrayList<SelectItemOption<T>>();
}
options_.add(option);
return this;
}

public SelectItemOption getSelectedItemOption() {
public SelectItemOption<T> getSelectedItemOption() {
if (options_ == null)
return null;
for (SelectItemOption item : options_) {
for (SelectItemOption<T> item : options_) {
if (item.isSelected())
return item;
}
Expand Down
Expand Up @@ -47,7 +47,7 @@ public interface UIFormInput<E> {

UIFormInput setValue(E value) throws Exception;

Class<E> getTypeValue();
Class<? extends E> getTypeValue();

void reset();

Expand Down
Expand Up @@ -82,7 +82,7 @@ public abstract class UIFormInputBase<T> extends UIContainer implements UIFormIn
/**
* The type of value that is expected
*/
protected Class<T> typeValue_;
protected Class<? extends T> typeValue_;

/**
* @deprecated According to the deprecation of the {@link #setEnable(boolean)} method
Expand Down Expand Up @@ -150,7 +150,7 @@ public UIFormInput<T> setValue(T value) {
return this;
}

public Class<T> getTypeValue() {
public Class<? extends T> getTypeValue() {
return typeValue_;
}

Expand Down
Expand Up @@ -33,27 +33,27 @@
* Represents any item selector, of a given type
*/
@ComponentConfig(template = "system:/groovy/webui/form/UIFormInputItemSelector.gtmpl")
public class UIFormInputItemSelector extends UIFormInputBase<Object> {
public class UIFormInputItemSelector<T> extends UIFormInputBase<T> {
/**
* The type of item selectable
*/
private Class type_;
private Class<? extends T> type_;

protected List<SelectItemCategory> categories_ = new ArrayList<SelectItemCategory>();
protected List<SelectItemCategory<T>> categories_ = new ArrayList<SelectItemCategory<T>>();

public UIFormInputItemSelector(String name, String bindingField) {
super(name, bindingField, Object.class);
public UIFormInputItemSelector(String name, String bindingField, Class<T> typeValue) {
super(name, bindingField, typeValue);
setComponentConfig(getClass(), null);
}

public List<SelectItemCategory> getItemCategories() {
public List<SelectItemCategory<T>> getItemCategories() {
return categories_;
}

public void setItemCategories(List<SelectItemCategory> categories) {
public void setItemCategories(List<SelectItemCategory<T>> categories) {
categories_ = categories;
boolean selected = false;
for (SelectItemCategory ele : categories) {
for (SelectItemCategory<T> ele : categories) {
if (ele.isSelected()) {
if (selected)
ele.setSelected(false);
Expand All @@ -65,62 +65,61 @@ public void setItemCategories(List<SelectItemCategory> categories) {
categories_.get(0).setSelected(true);
}

public SelectItemCategory getSelectedCategory() {
for (SelectItemCategory category : categories_) {
public SelectItemCategory<T> getSelectedCategory() {
for (SelectItemCategory<T> category : categories_) {
if (category.isSelected())
return category;
}
if (categories_.size() > 0) {
SelectItemCategory category = categories_.get(0);
SelectItemCategory<T> category = categories_.get(0);
category.setSelected(true);
category.getSelectItemOptions().get(0).setSelected(true);
return category;
}
return null;
}

public SelectItemOption getSelectedItemOption() {
SelectItemCategory selectedCategory = getSelectedCategory();
public SelectItemOption<T> getSelectedItemOption() {
SelectItemCategory<T> selectedCategory = getSelectedCategory();
if (selectedCategory == null)
return null;
return selectedCategory.getSelectedItemOption();
}

public Object getValue() {
SelectItemCategory selectedCategory = getSelectedCategory();
public T getValue() {
SelectItemCategory<T> selectedCategory = getSelectedCategory();
if (selectedCategory == null)
return null;
SelectItemOption selectedItem = selectedCategory.getSelectedItemOption();
SelectItemOption<T> selectedItem = selectedCategory.getSelectedItemOption();
if (selectedItem == null)
return null;
return selectedItem.getValue();
}

@SuppressWarnings("unchecked")
public UIFormInputItemSelector setValue(Object input) {
for (SelectItemCategory category : categories_) {
public UIFormInputItemSelector<T> setValue(Object input) {
for (SelectItemCategory<T> category : categories_) {
category.setSelected(isSelectItemCategory(category, input));
}
return this;
}

@SuppressWarnings("unchecked")
public Class getTypeValue() {
public Class<? extends T> getTypeValue() {
if (type_ != null)
return type_;
if (getSelectedCategory() == null || getSelectedCategory().getSelectedItemOption() == null
|| getSelectedCategory().getSelectedItemOption().getValue() == null)
return typeValue_;
return getSelectedCategory().getSelectedItemOption().getValue().getClass();
T val = getSelectedCategory().getSelectedItemOption().getValue();
return (Class<? extends T>) val.getClass();
}

public void setTypeValue(Class type) {
public void setTypeValue(Class<? extends T> type) {
this.type_ = type;
}

private boolean isSelectItemCategory(SelectItemCategory category, Object input) {
List<SelectItemOption> options = category.getSelectItemOptions();
for (SelectItemOption option : options) {
private boolean isSelectItemCategory(SelectItemCategory<T> category, Object input) {
List<SelectItemOption<T>> options = category.getSelectItemOptions();
for (SelectItemOption<T> option : options) {
if (option.getValue().equals(input)) {
option.setSelected(true);
return true;
Expand All @@ -129,7 +128,6 @@ private boolean isSelectItemCategory(SelectItemCategory category, Object input)
return category.getName().equals(input);
}

@SuppressWarnings("unused")
public void decode(Object input, WebuiRequestContext context) {
// System.out.println("\n\n\n == > current input value is "+input+"\n\n");
if (input == null || String.valueOf(input).length() < 1)
Expand Down
Expand Up @@ -38,23 +38,22 @@
* Created by The eXo Platform SARL Author : Nguyen Viet Chung nguyenchung136@yahoo.com Aug 10, 2006
*/
@ComponentConfig(template = "system:/groovy/portal/webui/page/UIPageTemplateOptions.gtmpl", initParams = @ParamConfig(name = "PageLayout", value = "system:/WEB-INF/conf/uiconf/portal/webui/page/PageConfigOptions.groovy"))
public class UIPageTemplateOptions extends UIFormInputItemSelector {
public class UIPageTemplateOptions extends UIFormInputItemSelector<String> {

private SelectItemOption<?> selectedItemOption_ = null;
private SelectItemOption<String> selectedItemOption_ = null;

@SuppressWarnings("unchecked")
public UIPageTemplateOptions(InitParams initParams) throws Exception {
super("UIPageTemplateOptions", null);
super("UIPageTemplateOptions", null, String.class);
if (initParams == null)
return;
WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
Param param = initParams.getParam("PageLayout");
categories_ = (List<SelectItemCategory>) param.getFreshObject(context);
categories_ = (List<SelectItemCategory<String>>) param.getFreshObject(context);
selectedItemOption_ = getDefaultItemOption();
List<SelectItemOption<String>> itemOptions = new ArrayList<SelectItemOption<String>>();

for (SelectItemCategory itemCategory : categories_) {
itemOptions.add(new SelectItemOption(itemCategory.getName()));
for (SelectItemCategory<String> itemCategory : categories_) {
itemOptions.add(new SelectItemOption<String>(itemCategory.getName()));
}

// modify: Dang.Tung
Expand All @@ -64,30 +63,30 @@ public UIPageTemplateOptions(InitParams initParams) throws Exception {
// end modify
}

public SelectItemOption<?> getDefaultItemOption() {
SelectItemCategory category = getSelectedCategory();
public SelectItemOption<String> getDefaultItemOption() {
SelectItemCategory<String> category = getSelectedCategory();
if (category == null)
return null;
SelectItemOption<?> itemOption = category.getSelectedItemOption();
SelectItemOption<String> itemOption = category.getSelectedItemOption();
if (itemOption == null)
return null;
return itemOption;
}

public void setSelectOptionItem(String value) {
boolean found = false;
for (SelectItemCategory category : categories_) {
for (SelectItemCategory<String> category : categories_) {
category.setSelected(false);
if (!found) {
for (SelectItemOption<?> itemOption : category.getSelectItemOptions()) {
for (SelectItemOption<String> itemOption : category.getSelectItemOptions()) {
if (itemOption.getLabel().equals(value)) {

UIDropDownControl uiItemSelector = findFirstComponentOfType(UIDropDownControl.class);
uiItemSelector.setValue(category.getName());

category.setSelected(true);
selectedItemOption_ = itemOption;
for (SelectItemOption<?> item : category.getSelectItemOptions()) {
for (SelectItemOption<String> item : category.getSelectItemOptions()) {
item.setSelected(false);
}
itemOption.setSelected(true);
Expand All @@ -101,7 +100,7 @@ public void setSelectOptionItem(String value) {
}
}

public SelectItemOption getSelectedItemOption() {
public SelectItemOption<String> getSelectedItemOption() {
return selectedItemOption_;
}

Expand Down
Expand Up @@ -110,7 +110,7 @@ public class UIPortalForm extends UIFormTabPane {
private List<SelectItemOption<String>> languages = new ArrayList<SelectItemOption<String>>();

public void initPortalTemplateTab() throws Exception {
UIFormInputItemSelector uiTemplateInput = new UIFormInputItemSelector("PortalTemplate", null);
UIFormInputItemSelector<String> uiTemplateInput = new UIFormInputItemSelector<String>("PortalTemplate", null, String.class);
addUIFormInput(uiTemplateInput);
setSelectedTab(uiTemplateInput.getId());

Expand All @@ -126,7 +126,7 @@ public void initPortalTemplateTab() throws Exception {
List<String> portalTemplates = new ArrayList<String>(configService.getPortalTemplates());
Collections.sort(portalTemplates);
for (String tempName : portalTemplates) {
SelectItemCategory category = new SelectItemCategory(tempName);
SelectItemCategory<String> category = new SelectItemCategory<String>(tempName);
PortalConfig config = configService.getPortalConfigFromTemplate(tempName);
if (config != null) {
SelectItemOption<String> option = new SelectItemOption<String>(config.getLabel(), tempName,
Expand Down Expand Up @@ -177,8 +177,8 @@ public void setBindingBean() throws Exception {
setActions(new String[] { "Save", "Close" });
}

private class LanguagesComparator implements Comparator<SelectItemOption> {
public int compare(SelectItemOption o1, SelectItemOption o2) {
private class LanguagesComparator implements Comparator<SelectItemOption<String>> {
public int compare(SelectItemOption<String> o1, SelectItemOption<String> o2) {
return o1.getLabel().compareToIgnoreCase(o2.getLabel());
}
}
Expand Down Expand Up @@ -385,7 +385,7 @@ public synchronized void execute(Event<UIPortalForm> event) throws Exception {
public static class SelectItemOptionActionListener extends EventListener<UIPortalForm> {
public void execute(Event<UIPortalForm> event) throws Exception {
UIPortalForm uiForm = event.getSource();
UIFormInputItemSelector templateInput = uiForm.getChild(UIFormInputItemSelector.class);
UIFormInputItemSelector<?> templateInput = uiForm.getChild(UIFormInputItemSelector.class);
uiForm.setSelectedTab(templateInput.getId());
event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
}
Expand Down

0 comments on commit a18e53d

Please sign in to comment.