@Reflectable
public class ANDValidationItem extends FBValidationItem implements OtherValidationsAware {

  private final I18NConstants i18n = CommonGlobals.getInstance().getI18n();

  private SubValidationsList subValidations = null;

  @Override
  public String getName() {
    return i18n.ANDValidationName();
  }

  @Override
  public void setExistingValidations(List<FBValidationItem> existingValidations) {
    subValidations = new SubValidationsList("AND", existingValidations);
  }

  @Override
  public FBValidation createValidation() {
    ANDValidation validation = getRepresentation(new ANDValidation());
    if (subValidations != null && subValidations.getItems() != null) {
      for (FBValidationItem subValidationItem : subValidations.getItems()) {
        FBValidation subValidation = subValidationItem.createValidation();
        validation.addValidation(subValidation);
      }
    }
    return validation;
  }

  @Override
  public Widget createDisplay() {
    return subValidations;
  }

  @Override
  public FBValidationItem cloneItem() {
    ANDValidationItem clone = new ANDValidationItem();
    for (FBValidationItem item : this.subValidations.getItems()) {
      clone.subValidations.addItem(item.cloneItem());
    }
    return clone;
  }

  @Override
  public void populate(FBValidation validation) throws FormBuilderException {
    if (!(validation instanceof ANDValidation)) {
      throw new FormBuilderException(
          i18n.RepNotOfType(validation.getClass().getName(), "ANDValidation"));
    }
    subValidations.clearItems();
    ANDValidation and = (ANDValidation) validation;
    List<FBValidation> subVals = and.getValidations();
    for (FBValidation subVal : subVals) {
      FBValidationItem item = createValidation(subVal);
      subValidations.addItem(item);
    }
  }
}
/**
 * Tree presenter. Handles notifications of form items added and removed and tells the view to
 * update itself
 */
public class TreePresenter implements TreeView.Presenter {

  private final TreeView view;
  private final EventBus bus = CommonGlobals.getInstance().getEventBus();

  public TreePresenter(TreeView treeView) {
    this.view = treeView;
    bus.addHandler(
        FormItemRemovedEvent.TYPE,
        new FormItemRemovedHandler() {
          @Override
          public void onEvent(FormItemRemovedEvent event) {
            FBFormItem item = event.getFormItem();
            view.removeFormItem(item);
          }
        });
    bus.addHandler(
        FormItemAddedEvent.TYPE,
        new FormItemAddedHandler() {
          @Override
          public void onEvent(FormItemAddedEvent event) {
            FBFormItem item = event.getFormItem();
            Widget parent = event.getFormItemHolder();
            FBCompositeItem parentItem = null;
            while (parent != null && parentItem == null) {
              if (parent instanceof FBCompositeItem) {
                parentItem = (FBCompositeItem) parent;
              } else {
                parent = parent.getParent();
              }
            }
            view.addFormItem(item, parentItem);
          }
        });
  }
}
/** Handles the action of saving a form on the server */
@Reflectable
public class SettingsCommand implements BaseCommand {

  private static final String SAVE_TYPE = SettingsCommand.class.getName();
  private final I18NConstants i18n = CommonGlobals.getInstance().getI18n();
  private final EventBus bus = CommonGlobals.getInstance().getEventBus();
  private final FormBuilderService service = FormBuilderGlobals.getInstance().getService();

  public SettingsCommand() {
    super();
    bus.addHandler(
        LoadSettingsResponseEvent.TYPE,
        new LoadSettingsResponseHandler() {

          public void onEvent(LoadSettingsResponseEvent event) {
            showSettingsPanel(event.getSettings());
          }
        });
  }

  private void showSettingsPanel(Settings settings) {

    final SettingsPopupPanel panel = new SettingsPopupPanel(settings);
    panel.setModal(true);
    panel.setPopupPosition(
        RootPanel.getBodyElement().getClientWidth() / 2 - 150,
        RootPanel.getBodyElement().getClientHeight() / 2 - 150);
    panel.show();
    panel.addCloseHandler(
        new CloseHandler<PopupPanel>() {

          @Override
          public void onClose(CloseEvent<PopupPanel> event) {
            // Send notification???
          }
        });
  }

  @Override
  public void execute() {
    bus.fireEvent(new LoadSettingsEvent());
  }

  private MenuItem item = null;

  @Override
  public void setItem(MenuItem item) {
    this.item = item;
    item.setEnabled(true);
  }

  @Override
  public void setEmbeded(String profile) {
    if (item != null) {
      // When embedded on guvnor, saving is done through guvnor
      if (profile != null && "guvnor".equals(profile)) {
        item.getParentMenu().removeItem(item);
      }
    }
  }
}
/** UI form item. Represents a combo box. */
@Reflectable
public class ComboBoxFormItem extends OptionsFormItem {

  private final I18NConstants i18n = CommonGlobals.getInstance().getI18n();

  private ListBox listBox = new ListBox();

  Map<String, String> items = new HashMap<String, String>();

  private Boolean multiple = null;
  private Integer visibleItems = null;
  private String title;
  private String name;
  private String id;

  public ComboBoxFormItem() {
    this(new ArrayList<FBFormEffect>());
  }

  public ComboBoxFormItem(List<FBFormEffect> formEffects) {
    super(formEffects);
    add(listBox);
    setWidth("50px");
    setHeight("21px");
    listBox.setSize(getWidth(), getHeight());
  }

  @Override
  public void saveValues(Map<String, Object> asPropertiesMap) {
    this.multiple = extractBoolean(asPropertiesMap.get("multipleSelect"));
    this.visibleItems = extractInt(asPropertiesMap.get("verticalSize"));
    this.title = extractString(asPropertiesMap.get("title"));
    this.setWidth(extractString(asPropertiesMap.get("width")));
    this.setHeight(extractString(asPropertiesMap.get("height")));
    this.name = extractString(asPropertiesMap.get("name"));
    this.id = extractString(asPropertiesMap.get("id"));
    populate(this.listBox);
  }

  @SuppressWarnings("deprecation")
  private void populate(ListBox listBox) {
    if (this.multiple != null) {
      this.listBox.setMultipleSelect(this.multiple);
    }
    if (this.visibleItems != null && this.visibleItems > 0) {
      this.listBox.setVisibleItemCount(this.visibleItems);
    }
    if (title != null) {
      this.listBox.setTitle(title);
    }
    if (getWidth() != null) {
      this.listBox.setWidth(getWidth());
    }
    if (getHeight() != null) {
      this.listBox.setHeight(getHeight());
    }
  }

  @Override
  public Map<String, Object> getFormItemPropertiesMap() {
    Map<String, Object> itemPropertiesMap = new HashMap<String, Object>();
    itemPropertiesMap.put("multipleSelect", this.multiple);
    itemPropertiesMap.put("verticalSize", this.visibleItems);
    itemPropertiesMap.put("title", this.title);
    itemPropertiesMap.put("width", this.getWidth());
    itemPropertiesMap.put("height", this.getHeight());
    itemPropertiesMap.put("name", this.name);
    itemPropertiesMap.put("id", this.id);
    return itemPropertiesMap;
  }

  @Override
  public void addItem(String label, String value) {
    if (value == null || "".equals(value)) {
      listBox.addItem(label);
      items.put(label, label);
    } else {
      listBox.addItem(label, value);
      items.put(label, value);
    }
  }

  @Override
  public void deleteItem(String label) {
    if (label != null) {
      items.remove(label);
      int size = 0;
      do {
        size = listBox.getItemCount();
        for (int index = 0; index < listBox.getItemCount(); index++) {
          if (listBox.getItemText(index).equals(label)) {
            listBox.removeItem(index);
            break;
          }
        }
      } while (size != listBox.getItemCount());
    }
  }

  @Override
  public FormItemRepresentation getRepresentation() {
    ComboBoxRepresentation rep = super.getRepresentation(new ComboBoxRepresentation());
    List<OptionRepresentation> elements = new ArrayList<OptionRepresentation>();
    for (String label : this.items.keySet()) {
      OptionRepresentation opt = new OptionRepresentation();
      opt.setLabel(label);
      opt.setValue(this.items.get(label));
      elements.add(opt);
    }
    rep.setElements(elements);
    rep.setName(this.name);
    rep.setId(this.id);
    return rep;
  }

  @Override
  public void populate(FormItemRepresentation rep) throws FormBuilderException {
    if (!(rep instanceof ComboBoxRepresentation)) {
      throw new FormBuilderException(
          i18n.RepNotOfType(rep.getClass().getName(), "TextFieldRepresentation"));
    }
    super.populate(rep);
    ComboBoxRepresentation crep = (ComboBoxRepresentation) rep;
    List<OptionRepresentation> options = crep.getElements();
    this.items.clear();
    if (options != null) {
      for (OptionRepresentation option : options) {
        this.items.put(option.getLabel(), option.getValue());
        this.listBox.addItem(option.getLabel(), option.getValue());
      }
    }
    this.listBox.clear();
    addItems(this.items, this.listBox);
    this.name = crep.getName();
    this.id = crep.getId();
    populate(this.listBox);
  }

  @Override
  public void addEffect(FBFormEffect effect) {
    super.addEffect(effect);
    effect.setWidget(this.listBox);
  }

  @Override
  public Map<String, String> getItems() {
    Map<String, String> items = new HashMap<String, String>();
    for (int index = 0; index < listBox.getItemCount(); index++) {
      items.put(listBox.getItemText(index), listBox.getValue(index));
    }
    return items;
  }

  public void addItems(Map<String, String> items, ListBox listBox) {
    for (Map.Entry<String, String> entry : items.entrySet()) {
      listBox.addItem(entry.getKey(), entry.getValue());
    }
  }

  @Override
  public FBFormItem cloneItem() {
    ComboBoxFormItem clone = new ComboBoxFormItem(getFormEffects());
    clone.setHeight(this.getHeight());
    clone.id = this.id;
    clone.multiple = this.multiple;
    clone.name = this.name;
    clone.title = this.title;
    clone.visibleItems = this.visibleItems;
    clone.setWidth(this.getWidth());
    clone.populate(clone.listBox);
    clone.addItems(this.getItems(), clone.listBox);
    return clone;
  }

  @Override
  public Widget cloneDisplay(Map<String, Object> data) {
    ListBox lb = new ListBox();
    populate(lb);
    addItems(getItems(), lb);
    Object input = getInputValue(data);
    String inputName = getInput() == null ? null : getInput().getName();
    if (input != null && inputName != null) {
      if (input.getClass().isArray()) {
        Object[] arr = (Object[]) input;
        for (Object obj : arr) {
          lb.addItem(obj.toString(), obj.toString());
        }
      } else if (input instanceof Collection) {
        Collection<?> col = (Collection<?>) input;
        for (Object obj : col) {
          lb.addItem(obj.toString(), obj.toString());
        }
      } else if (input instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) input;
        for (Map.Entry<?, ?> entry : map.entrySet()) {
          lb.addItem(entry.getKey().toString(), entry.getValue().toString());
        }
      } else {
        String value = input.toString();
        for (int index = 0; index < lb.getItemCount(); index++) {
          if (value != null && value.equals(lb.getValue(index))) {
            lb.setSelectedIndex(index);
            break;
          }
        }
      }
    }
    super.populateActions(lb.getElement());
    return lb;
  }
}
 @Override
 public Label getDescription() {
   return new Label(CommonGlobals.getInstance().getI18n().MenuItemHorizontalLayout());
 }