com.evolveum.midpoint.web.component.search.SearchFragmentBase.java Source code

Java tutorial

Introduction

Here is the source code for com.evolveum.midpoint.web.component.search.SearchFragmentBase.java

Source

/*
 * Copyright (c) 2010-2015 Evolveum
 *
 * Licensed 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 com.evolveum.midpoint.web.component.search;

import com.evolveum.midpoint.util.DisplayableValue;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;

import java.io.Serializable;
import java.util.List;

/**
 * @author Viliam Repan (lazyman)
 */
abstract class SearchFragmentBase<T extends Serializable> extends Fragment {

    private static final String ID_REMOVE = "remove";
    private static final String ID_ADD = "add";

    private IModel<T> data;

    SearchFragmentBase(String id, String markupId, MarkupContainer markupProvider, IModel<T> data) {
        super(id, markupId, markupProvider);

        this.data = data;
        initButtons();
    }

    private void initButtons() {
        AjaxLink remove = new AjaxLink(ID_REMOVE) {

            @Override
            public void onClick(AjaxRequestTarget target) {
                removePerformed(target);
            }
        };
        add(remove);

        AjaxLink add = new AjaxLink(ID_ADD) {

            @Override
            public void onClick(AjaxRequestTarget target) {
                addPerformed(target);
            }
        };
        add(add);
    }

    protected void addPerformed(AjaxRequestTarget target) {
        SearchItemPanel panel = findParent(SearchItemPanel.class);

        SearchItemPopoverDto dto = panel.getPopoverModel().getObject();
        List<DisplayableValue> values = dto.getValues();
        values.add(new SearchValue());

        panel.updatePopupBody(target);
    }

    protected void removePerformed(AjaxRequestTarget target) {
        SearchItemPanel panel = findParent(SearchItemPanel.class);
        T val = data.getObject();

        SearchItemPopoverDto dto = panel.getPopoverModel().getObject();
        List<DisplayableValue> values = dto.getValues();

        values.remove(val);

        if (values.isEmpty()) {
            values.add(new SearchValue());
        }

        panel.updatePopupBody(target);
    }
}