OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 67 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.view.list;
15
 
16
import org.openconcerto.sql.model.SQLRowAccessor;
17
import org.openconcerto.utils.cc.IClosure;
21 ilm 18
import org.openconcerto.utils.cc.IPredicate;
19 ilm 19
 
20
import java.awt.event.ActionEvent;
21
import java.util.ArrayList;
22
import java.util.Arrays;
23
import java.util.Collections;
24
import java.util.List;
25
 
26
import javax.swing.AbstractAction;
27
import javax.swing.Action;
28
import javax.swing.Icon;
21 ilm 29
import javax.swing.JButton;
30
import javax.swing.JMenuItem;
19 ilm 31
 
32
/**
21 ilm 33
 * An action that act on rows of a {@link IListe}. Either {@link #enabledFor(IListeEvent)} or
34
 * {@link #enabledFor(List)} must be overloaded.
19 ilm 35
 *
36
 * @author Sylvain CUAZ
21 ilm 37
 * @see IListe#addIListeAction(RowAction)
19 ilm 38
 */
21 ilm 39
public abstract class RowAction implements IListeAction {
19 ilm 40
 
41
    public static Action createAction(String name, Icon icon, final IClosure<List<SQLRowAccessor>> action) {
42
        return new AbstractAction(name, icon) {
43
            @Override
44
            public void actionPerformed(ActionEvent e) {
45
                action.executeChecked(IListe.get(e).getSelectedRows());
46
            }
47
        };
48
    }
49
 
21 ilm 50
    public static class PredicateRowAction extends RowAction {
51
        private IPredicate<? super IListeEvent> pred = null;
19 ilm 52
 
21 ilm 53
        public PredicateRowAction(Action action, boolean header) {
19 ilm 54
            super(action, header);
55
        }
56
 
21 ilm 57
        public PredicateRowAction(Action action, boolean header, boolean popupMenu) {
19 ilm 58
            super(action, header, popupMenu);
59
        }
60
 
21 ilm 61
        public final PredicateRowAction setPredicate(IPredicate<? super IListeEvent> pred) {
25 ilm 62
            if (pred == null) {
63
                throw new IllegalArgumentException("null predicate");
64
            }
21 ilm 65
            this.pred = pred;
19 ilm 66
            return this;
67
        }
68
 
21 ilm 69
        public final IPredicate<? super IListeEvent> getPredicate() {
70
            return this.pred;
19 ilm 71
        }
72
 
73
        @Override
21 ilm 74
        public boolean enabledFor(IListeEvent evt) {
25 ilm 75
            if (pred == null) {
76
                throw new IllegalStateException("No predicate for action:" + this.getAction() + ":" + this.getAction().getValue(Action.NAME));
77
            }
21 ilm 78
            return this.pred.evaluateChecked(evt);
19 ilm 79
        }
80
    }
81
 
82
    private final Action action;
83
    private final boolean header, popupMenu;
84
    private List<String> path;
85
 
86
    public RowAction(Action action, boolean header) {
87
        this(action, header, true);
88
    }
89
 
90
    public RowAction(Action action, boolean header, boolean popupMenu) {
91
        super();
92
        this.action = action;
93
        this.header = header;
94
        this.popupMenu = popupMenu;
95
        this.setGroup(null);
96
    }
97
 
98
    public final Action getAction() {
99
        return this.action;
100
    }
101
 
102
    public final boolean inHeader() {
103
        return this.header;
104
    }
105
 
106
    public final boolean inPopupMenu() {
107
        return this.popupMenu;
108
    }
109
 
110
    public final RowAction setGroup(String groupName) {
111
        this.path = Arrays.asList(groupName);
112
        return this;
113
    }
114
 
115
    public final RowAction setPath(List<String> path) {
116
        this.path = Collections.unmodifiableList(new ArrayList<String>(path));
117
        return this;
118
    }
119
 
120
    public final List<String> getPath() {
121
        return this.path;
122
    }
123
 
21 ilm 124
    public boolean enabledFor(List<SQLRowAccessor> selection) {
125
        throw new UnsupportedOperationException("Should overload this method or enabledFor(IListeEvent)");
126
    }
127
 
128
    /**
129
     * Whether the action should be enabled in the header or in the popup.
130
     *
131
     * @param evt the state of the IListe.
132
     * @return <code>true</code> if the action can be performed.
133
     */
134
    public boolean enabledFor(IListeEvent evt) {
135
        return this.enabledFor(evt.getSelectedRows());
136
    }
137
 
138
    @Override
139
    public ButtonsBuilder getHeaderButtons() {
140
        return !this.inHeader() ? ButtonsBuilder.emptyInstance() : new ButtonsBuilder().add(new JButton(getAction()), new IPredicate<IListeEvent>() {
141
            @Override
142
            public boolean evaluateChecked(IListeEvent evt) {
143
                return enabledFor(evt);
144
            }
145
        });
146
    }
147
 
148
    @Override
149
    public Action getDefaultAction(IListeEvent evt) {
150
        return null;
151
    }
152
 
153
    @Override
154
    public PopupBuilder getPopupContent(PopupEvent evt) {
155
        if (this.inPopupMenu() && evt.isClickOnRows()) {
156
            final JMenuItem mi = new JMenuItem(getAction());
157
            mi.setEnabled(this.enabledFor(evt));
158
            final PopupBuilder res = new PopupBuilder();
159
            res.getMenu().addItem(mi, getPath());
160
            return res;
161
        } else {
162
            return PopupBuilder.emptyInstance();
163
        }
164
    }
19 ilm 165
}