19 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
19 |
ilm |
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;
|
93 |
ilm |
17 |
import org.openconcerto.sql.model.SQLRowValues;
|
182 |
ilm |
18 |
import org.openconcerto.sql.view.list.action.ListEvent;
|
19 |
ilm |
19 |
import org.openconcerto.utils.cc.IClosure;
|
21 |
ilm |
20 |
import org.openconcerto.utils.cc.IPredicate;
|
67 |
ilm |
21 |
import org.openconcerto.utils.i18n.TranslationManager;
|
19 |
ilm |
22 |
|
|
|
23 |
import java.awt.event.ActionEvent;
|
|
|
24 |
import java.util.ArrayList;
|
|
|
25 |
import java.util.Arrays;
|
|
|
26 |
import java.util.Collections;
|
|
|
27 |
import java.util.List;
|
|
|
28 |
|
|
|
29 |
import javax.swing.AbstractAction;
|
|
|
30 |
import javax.swing.Action;
|
|
|
31 |
import javax.swing.Icon;
|
21 |
ilm |
32 |
import javax.swing.JButton;
|
|
|
33 |
import javax.swing.JMenuItem;
|
19 |
ilm |
34 |
|
|
|
35 |
/**
|
21 |
ilm |
36 |
* An action that act on rows of a {@link IListe}. Either {@link #enabledFor(IListeEvent)} or
|
|
|
37 |
* {@link #enabledFor(List)} must be overloaded.
|
19 |
ilm |
38 |
*
|
|
|
39 |
* @author Sylvain CUAZ
|
21 |
ilm |
40 |
* @see IListe#addIListeAction(RowAction)
|
19 |
ilm |
41 |
*/
|
21 |
ilm |
42 |
public abstract class RowAction implements IListeAction {
|
19 |
ilm |
43 |
|
93 |
ilm |
44 |
public static Action createAction(String name, Icon icon, final IClosure<List<? extends SQLRowAccessor>> action) {
|
19 |
ilm |
45 |
return new AbstractAction(name, icon) {
|
|
|
46 |
@Override
|
|
|
47 |
public void actionPerformed(ActionEvent e) {
|
|
|
48 |
action.executeChecked(IListe.get(e).getSelectedRows());
|
|
|
49 |
}
|
|
|
50 |
};
|
|
|
51 |
}
|
|
|
52 |
|
21 |
ilm |
53 |
public static class PredicateRowAction extends RowAction {
|
182 |
ilm |
54 |
private IPredicate<? super ListEvent> pred = null;
|
19 |
ilm |
55 |
|
21 |
ilm |
56 |
public PredicateRowAction(Action action, boolean header) {
|
19 |
ilm |
57 |
super(action, header);
|
|
|
58 |
}
|
|
|
59 |
|
21 |
ilm |
60 |
public PredicateRowAction(Action action, boolean header, boolean popupMenu) {
|
19 |
ilm |
61 |
super(action, header, popupMenu);
|
|
|
62 |
}
|
|
|
63 |
|
67 |
ilm |
64 |
public PredicateRowAction(Action action, boolean header, final String id) {
|
|
|
65 |
super(action, header, id);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public PredicateRowAction(Action action, boolean header, boolean popupMenu, final String id) {
|
|
|
69 |
super(action, header, popupMenu, id);
|
|
|
70 |
}
|
|
|
71 |
|
182 |
ilm |
72 |
public final PredicateRowAction setPredicate(IPredicate<? super ListEvent> pred) {
|
25 |
ilm |
73 |
if (pred == null) {
|
|
|
74 |
throw new IllegalArgumentException("null predicate");
|
|
|
75 |
}
|
21 |
ilm |
76 |
this.pred = pred;
|
19 |
ilm |
77 |
return this;
|
|
|
78 |
}
|
|
|
79 |
|
21 |
ilm |
80 |
public final IPredicate<? super IListeEvent> getPredicate() {
|
|
|
81 |
return this.pred;
|
19 |
ilm |
82 |
}
|
|
|
83 |
|
|
|
84 |
@Override
|
182 |
ilm |
85 |
public boolean enabledFor(ListEvent evt) {
|
67 |
ilm |
86 |
if (this.pred == null) {
|
|
|
87 |
throw new IllegalStateException("No predicate for " + this);
|
25 |
ilm |
88 |
}
|
21 |
ilm |
89 |
return this.pred.evaluateChecked(evt);
|
19 |
ilm |
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private final Action action;
|
|
|
94 |
private final boolean header, popupMenu;
|
|
|
95 |
private List<String> path;
|
67 |
ilm |
96 |
private final String id;
|
19 |
ilm |
97 |
|
|
|
98 |
public RowAction(Action action, boolean header) {
|
|
|
99 |
this(action, header, true);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public RowAction(Action action, boolean header, boolean popupMenu) {
|
67 |
ilm |
103 |
this(action, header, popupMenu, null);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public RowAction(Action action, boolean header, final String id) {
|
|
|
107 |
this(action, header, true, id);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public RowAction(Action action, boolean header, boolean popupMenu, final String id) {
|
19 |
ilm |
111 |
super();
|
|
|
112 |
this.action = action;
|
|
|
113 |
this.header = header;
|
|
|
114 |
this.popupMenu = popupMenu;
|
|
|
115 |
this.setGroup(null);
|
67 |
ilm |
116 |
this.id = id;
|
|
|
117 |
if (id != null && action.getValue(Action.NAME) == null)
|
|
|
118 |
action.putValue(Action.NAME, TranslationManager.getInstance().getTranslationForAction(id));
|
19 |
ilm |
119 |
}
|
|
|
120 |
|
67 |
ilm |
121 |
public final String getID() {
|
|
|
122 |
return this.id;
|
|
|
123 |
}
|
|
|
124 |
|
19 |
ilm |
125 |
public final Action getAction() {
|
|
|
126 |
return this.action;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public final boolean inHeader() {
|
|
|
130 |
return this.header;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public final boolean inPopupMenu() {
|
|
|
134 |
return this.popupMenu;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
public final RowAction setGroup(String groupName) {
|
|
|
138 |
this.path = Arrays.asList(groupName);
|
|
|
139 |
return this;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public final RowAction setPath(List<String> path) {
|
|
|
143 |
this.path = Collections.unmodifiableList(new ArrayList<String>(path));
|
|
|
144 |
return this;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public final List<String> getPath() {
|
|
|
148 |
return this.path;
|
|
|
149 |
}
|
|
|
150 |
|
93 |
ilm |
151 |
public boolean enabledFor(List<SQLRowValues> selection) {
|
182 |
ilm |
152 |
throw new UnsupportedOperationException("Should overload this method or enabledFor(IListeEvent) on : " + this);
|
21 |
ilm |
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Whether the action should be enabled in the header or in the popup.
|
|
|
157 |
*
|
|
|
158 |
* @param evt the state of the IListe.
|
|
|
159 |
* @return <code>true</code> if the action can be performed.
|
|
|
160 |
*/
|
182 |
ilm |
161 |
public boolean enabledFor(ListEvent evt) {
|
21 |
ilm |
162 |
return this.enabledFor(evt.getSelectedRows());
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
@Override
|
|
|
166 |
public ButtonsBuilder getHeaderButtons() {
|
|
|
167 |
return !this.inHeader() ? ButtonsBuilder.emptyInstance() : new ButtonsBuilder().add(new JButton(getAction()), new IPredicate<IListeEvent>() {
|
|
|
168 |
@Override
|
|
|
169 |
public boolean evaluateChecked(IListeEvent evt) {
|
|
|
170 |
return enabledFor(evt);
|
|
|
171 |
}
|
|
|
172 |
});
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
@Override
|
182 |
ilm |
176 |
public Action getDefaultAction(ListEvent evt) {
|
151 |
ilm |
177 |
return this.enabledFor(evt) ? this.getAction() : null;
|
21 |
ilm |
178 |
}
|
|
|
179 |
|
|
|
180 |
@Override
|
|
|
181 |
public PopupBuilder getPopupContent(PopupEvent evt) {
|
|
|
182 |
if (this.inPopupMenu() && evt.isClickOnRows()) {
|
|
|
183 |
final JMenuItem mi = new JMenuItem(getAction());
|
|
|
184 |
mi.setEnabled(this.enabledFor(evt));
|
|
|
185 |
final PopupBuilder res = new PopupBuilder();
|
|
|
186 |
res.getMenu().addItem(mi, getPath());
|
|
|
187 |
return res;
|
|
|
188 |
} else {
|
|
|
189 |
return PopupBuilder.emptyInstance();
|
|
|
190 |
}
|
|
|
191 |
}
|
67 |
ilm |
192 |
|
|
|
193 |
@Override
|
|
|
194 |
public String toString() {
|
|
|
195 |
return this.getClass().getSimpleName() + (this.getID() == null ? "" : " ID '" + this.getID()) + "' with action '" + this.getAction().getValue(Action.NAME) + "'";
|
|
|
196 |
}
|
19 |
ilm |
197 |
}
|