OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 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.
21 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;
21 ilm 19
import org.openconcerto.ui.list.selection.ListSelection;
20
import org.openconcerto.utils.cc.IPredicate;
21
 
22
import java.util.Collections;
23
import java.util.LinkedHashMap;
24
import java.util.List;
25
import java.util.Map;
26
 
27
import javax.swing.Action;
28
import javax.swing.JButton;
29
import javax.swing.JMenuItem;
30
 
31
/**
32
 * Actions that can be performed with an {@link IListe}.
33
 *
34
 * @author Sylvain CUAZ
35
 */
36
public interface IListeAction {
37
 
182 ilm 38
    static public class IListeEvent extends ListEvent {
21 ilm 39
 
40
        private final IListe list;
41
 
182 ilm 42
        IListeEvent(final IListe list, final List<SQLRowValues> selection, final List<? extends SQLRowAccessor> selectionAccessor) {
43
            super(list, list.getSource().getElem(), list.getTotalRowCount(), selection, selectionAccessor);
21 ilm 44
            this.list = list;
45
        }
46
 
47
        public final ListSelection getSelection() {
48
            return this.list.getSelection();
49
        }
50
    }
51
 
52
    /**
53
     * Allow to build a list of buttons and when to enable/disable them.
54
     *
55
     * @author Sylvain CUAZ
56
     */
57
    static public class ButtonsBuilder {
58
 
59
        static final String GROUPNAME_PROPNAME = "GROUPNAME";
60
 
61
        static private final ButtonsBuilder NO_BUTTONS = new ButtonsBuilder(Collections.<JButton, IPredicate<IListeEvent>> emptyMap());
62
 
63
        static public final ButtonsBuilder emptyInstance() {
64
            return NO_BUTTONS;
65
        }
66
 
67
        private final Map<JButton, IPredicate<IListeEvent>> map;
68
        private String defaultGroup;
69
 
70
        public ButtonsBuilder() {
71
            this(new LinkedHashMap<JButton, IPredicate<IListeEvent>>());
72
        }
73
 
74
        private ButtonsBuilder(Map<JButton, IPredicate<IListeEvent>> map) {
75
            super();
76
            this.map = map;
77
            this.defaultGroup = null;
78
        }
79
 
80
        public final String getDefaultGroup() {
81
            return this.defaultGroup;
82
        }
83
 
84
        public ButtonsBuilder setDefaultGroup(String defaultGroup) {
85
            this.defaultGroup = defaultGroup;
86
            return this;
87
        }
88
 
89
        public final ButtonsBuilder add(JButton btn) {
90
            return this.add(btn, IPredicate.<IListeEvent> truePredicate());
91
        }
92
 
93
        public final ButtonsBuilder add(JButton btn, IPredicate<IListeEvent> pred) {
94
            return this.add(btn, pred, getDefaultGroup());
95
        }
96
 
97
        /**
98
         * Add a button in the passed group.
99
         *
100
         * @param btn the button to add.
101
         * @param pred should return <code>true</code> when the button must be enabled.
102
         * @param group the group in which to put the button.
103
         * @return this.
104
         */
105
        public final ButtonsBuilder add(JButton btn, IPredicate<IListeEvent> pred, final String group) {
106
            btn.putClientProperty(GROUPNAME_PROPNAME, group);
107
            this.map.put(btn, pred);
108
            return this;
109
        }
110
 
111
        // * getter
112
 
113
        Map<JButton, IPredicate<IListeEvent>> getContent() {
114
            return this.map;
115
        }
116
    }
117
 
118
    static public class PopupEvent extends IListeEvent {
119
 
120
        private final boolean clickOnRows;
121
 
182 ilm 122
        PopupEvent(final IListe list, final List<SQLRowValues> selection, final List<? extends SQLRowAccessor> selectionAccessor, final boolean clickOnRows) {
123
            super(list, selection, selectionAccessor);
21 ilm 124
            this.clickOnRows = clickOnRows;
125
        }
126
 
127
        public final boolean isClickOnRows() {
128
            return this.clickOnRows;
129
        }
130
    }
131
 
132
    /**
133
     * Allow to build a hierarchical menu.
134
     *
135
     * @author Sylvain CUAZ
136
     */
137
    static public class PopupBuilder {
138
        static private final PopupBuilder EmptyInstance = new PopupBuilder(VirtualMenu.EMPTY);
139
 
140
        static public final PopupBuilder emptyInstance() {
141
            return EmptyInstance;
142
        }
143
 
144
        private final VirtualMenu rootMenu;
145
 
146
        public PopupBuilder() {
147
            this((String) null);
148
        }
149
 
150
        public PopupBuilder(final String defaultGroup) {
151
            this(VirtualMenu.createRoot(defaultGroup));
152
        }
153
 
154
        private PopupBuilder(final VirtualMenu rootMenu) {
155
            this.rootMenu = rootMenu;
156
        }
157
 
158
        /**
159
         * Get the root menu of the popup.
160
         *
161
         * @return the menu at the root.
162
         */
163
        public final VirtualMenu getMenu() {
164
            return this.rootMenu;
165
        }
166
 
167
        public final String getDefaultGroup() {
168
            return this.getMenu().getDefaultGroupName();
169
        }
170
 
171
        final JMenuItem getRootMenuItem(final Action defaultAction) {
151 ilm 172
            // Cannot compare actions since menu items are not required to have an action
21 ilm 173
            String actionCommand = (String) defaultAction.getValue(Action.ACTION_COMMAND_KEY);
174
            if (actionCommand == null)
175
                actionCommand = (String) defaultAction.getValue(Action.NAME);
176
            if (actionCommand == null)
177
                return null;
178
            for (final JMenuItem mi : this.getMenu().getItemsAndPath(false).keySet()) {
179
                if (actionCommand.equals(mi.getActionCommand()))
180
                    return mi;
181
            }
182
            return null;
183
        }
184
 
185
        // * actions
186
 
187
        public final PopupBuilder addAction(Action a) {
188
            return this.addAction(a, getDefaultGroup());
189
        }
190
 
191
        public final PopupBuilder addAction(Action a, String group) {
192
            this.getMenu().addItem(new JMenuItem(a), group);
193
            return this;
194
        }
195
 
196
        // * items
197
 
198
        public final PopupBuilder addItem(JMenuItem mi) {
199
            return this.addItem(mi, getDefaultGroup());
200
        }
201
 
202
        public final PopupBuilder addItem(JMenuItem mi, String group) {
203
            this.getMenu().addItem(mi, group);
204
            return this;
205
        }
206
 
207
        public final PopupBuilder addItemInSubmenu(JMenuItem mi, String submenu) {
208
            return this.addItemInSubmenu(mi, getDefaultGroup(), submenu, getDefaultGroup());
209
        }
210
 
211
        public final PopupBuilder addItemInSubmenu(JMenuItem mi, String group, String submenu, String submenuGroup) {
212
            this.getMenu().getSubmenu(submenu, group).addItem(mi, submenuGroup);
213
            return this;
214
        }
215
    }
216
 
217
    // never null
218
    ButtonsBuilder getHeaderButtons();
219
 
220
    /**
221
     * The action performed if this is the {@link IListe#setDefaultRowAction(IListeAction) default}
222
     * of an <code>IListe</code>. The returned action should have a visible feedback.
223
     *
224
     * @param evt the state of the <code>IListe</code>.
225
     * @return the default action to perform, can be <code>null</code>.
226
     */
182 ilm 227
    Action getDefaultAction(ListEvent evt);
21 ilm 228
 
229
    // never null
230
    PopupBuilder getPopupContent(PopupEvent evt);
231
}