/* * Copyright 2013 JBoss Inc * * 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 org.overlord.rtgov.ui.client.local.pages.services; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.Dependent; import javax.inject.Inject; import org.jboss.errai.ui.shared.api.annotations.DataField; import org.jboss.errai.ui.shared.api.annotations.Templated; import org.overlord.rtgov.ui.client.model.QName; import org.overlord.rtgov.ui.client.model.ServicesFilterBean; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.logical.shared.HasValueChangeHandlers; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.TextBox; /** * The services filtersPanel sidebar. Whenever the user changes any of the settings in * the filter sidebar, a ValueChangeEvent will be fired. * * @author eric.wittmann@redhat.com */ @Templated("/org/overlord/rtgov/ui/client/local/site/services.html#filter-sidebar") @Dependent public class ServiceFilters extends Composite implements HasValueChangeHandlers<ServicesFilterBean> { private ServicesFilterBean currentState = new ServicesFilterBean(); @Inject @DataField protected ApplicationNameListBox applicationName; @Inject @DataField protected TextBox serviceName; @Inject @DataField protected Anchor clearFilters; /** * Constructor. */ public ServiceFilters() { } /** * Called after construction and injection. */ @SuppressWarnings("unchecked") @PostConstruct protected void postConstruct() { ClickHandler clearFilterHandler = new ClickHandler() { @Override public void onClick(ClickEvent event) { setValue(new ServicesFilterBean()); onFilterValueChange(); } }; clearFilters.addClickHandler(clearFilterHandler); @SuppressWarnings("rawtypes") ValueChangeHandler valueChangeHandler = new ValueChangeHandler() { @Override public void onValueChange(ValueChangeEvent event) { onFilterValueChange(); } }; applicationName.addValueChangeHandler(valueChangeHandler); serviceName.addValueChangeHandler(valueChangeHandler); } /** * Called whenever any filter value changes. */ protected void onFilterValueChange() { ServicesFilterBean newState = new ServicesFilterBean(); newState.setServiceName(serviceName.getValue()) .setApplicationName(applicationName.getValue()); ServicesFilterBean oldState = this.currentState; this.currentState = newState; // Only fire a change event if something actually changed. ValueChangeEvent.fireIfNotEqual(this, oldState, currentState); } /** * @return the current filter settings */ public ServicesFilterBean getValue() { return this.currentState; } /** * @param value the new filter settings */ public void setValue(ServicesFilterBean value) { applicationName.setValue(value.getApplicationName() == null ? "" : value.getApplicationName()); //$NON-NLS-1$ serviceName.setValue(value.getServiceName() == null ? "" : value.getServiceName()); //$NON-NLS-1$ onFilterValueChange(); } /** * Refresh any data in the filter panel. */ public void refresh() { } /** * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) */ @Override public HandlerRegistration addValueChangeHandler(ValueChangeHandler<ServicesFilterBean> handler) { return addHandler(handler, ValueChangeEvent.getType()); } /** * @param appNames */ public void setApplicationNames(List<QName> appNames) { applicationName.clear(); for (QName qName : appNames) { applicationName.addItem(qName.getLocalPart(), qName.toString()); } } }