Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.sql.view.list.action;
import org.openconcerto.utils.cc.IPredicate;
import org.openconcerto.utils.i18n.TranslationManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
/**
* An action that act on rows of a list. Either {@link #enabledFor(ListEvent)} or
* {@link #enabledFor(List)} must be overloaded.
*
* @author Sylvain CUAZ
*/
public abstract class SQLRowValuesAction {
public static class PredicateRowAction extends SQLRowValuesAction {
private IPredicate<? super ListEvent> pred = null;
public PredicateRowAction(boolean header, Consumer<? super ListEvent> action) {
super(header, action);
}
public PredicateRowAction(boolean header, boolean popupMenu, Consumer<? super ListEvent> action) {
super(header, popupMenu, action);
}
public PredicateRowAction(boolean header, final String id, Consumer<? super ListEvent> action) {
super(header, id, action);
}
public PredicateRowAction(boolean header, boolean popupMenu, final String id, Consumer<? super ListEvent> action) {
super(header, popupMenu, id, action);
}
public final PredicateRowAction setPredicate(IPredicate<? super ListEvent> pred) {
if (pred == null) {
throw new IllegalArgumentException("null predicate");
}
this.pred = pred;
return this;
}
public final IPredicate<? super ListEvent> getPredicate() {
return this.pred;
}
@Override
public boolean enabledFor(ListEvent evt) {
if (this.pred == null) {
throw new IllegalStateException("No predicate for " + this);
}
return this.pred.evaluateChecked(evt);
}
}
private final Consumer<? super ListEvent> action;
private final boolean header, popupMenu;
private List<String> path;
private final String id;
private String name;
public SQLRowValuesAction(boolean header, Consumer<? super ListEvent> action) {
this(header, true, action);
}
public SQLRowValuesAction(boolean header, boolean popupMenu, Consumer<? super ListEvent> action) {
this(header, popupMenu, null, action);
}
public SQLRowValuesAction(boolean header, final String id, Consumer<? super ListEvent> action) {
this(header, true, id, action);
}
public SQLRowValuesAction(boolean header, boolean popupMenu, final String id, Consumer<? super ListEvent> action) {
super();
this.action = action;
this.header = header;
this.popupMenu = popupMenu;
this.setGroup(null);
this.id = id;
if (id != null)
this.setName(TranslationManager.getInstance().getTranslationForAction(id));
}
public final String getID() {
return this.id;
}
public final SQLRowValuesAction setName(String name) {
this.name = name;
return this;
}
public final String getName() {
return this.name;
}
public final Consumer<? super ListEvent> getAction() {
return this.action;
}
public final boolean inHeader() {
return this.header;
}
public final boolean inPopupMenu() {
return this.popupMenu;
}
public final SQLRowValuesAction setGroup(String groupName) {
this.path = Arrays.asList(groupName);
return this;
}
public final SQLRowValuesAction setPath(List<String> path) {
this.path = Collections.unmodifiableList(new ArrayList<String>(path));
return this;
}
public final List<String> getPath() {
return this.path;
}
/**
* Whether the action should be enabled in the header or in the popup.
*
* @param evt the state of the IListe.
* @return <code>true</code> if the action can be performed.
*/
public abstract boolean enabledFor(ListEvent evt);
public Consumer<? super ListEvent> getDefaultAction(ListEvent evt) {
return this.enabledFor(evt) ? this.getAction() : null;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + (this.getID() == null ? "" : " ID '" + this.getID()) + "'";
}
}