Skip to content

Package: Bindings

Bindings

nameinstructionbranchcomplexitylinemethod
bind(BindingGroup, Object, JList)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
bind(BindingGroup, Object, JTable)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
lambda$bind$0(JTableBinding, TableColumnDescriptor)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * 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
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.thesefoolishthings.examples.dci.swing.swing;
27:
28: import javax.annotation.Nonnull;
29: import javax.swing.JList;
30: import javax.swing.JTable;
31: import org.jdesktop.beansbinding.BeanProperty;
32: import org.jdesktop.beansbinding.BindingGroup;
33: import org.jdesktop.observablecollections.ObservableList;
34: import org.jdesktop.swingbinding.SwingBindings;
35: import it.tidalwave.util.AsExtensions;
36: import it.tidalwave.thesefoolishthings.examples.dci.swing.role.ObservableListProvider;
37: import it.tidalwave.thesefoolishthings.examples.dci.swing.role.TableHeaderDescriptor;
38: import lombok.AccessLevel;
39: import lombok.NoArgsConstructor;
40: import lombok.experimental.ExtensionMethod;
41: import static it.tidalwave.thesefoolishthings.examples.dci.swing.role.ObservableListProvider._ObservableListProvider_;
42: import static it.tidalwave.thesefoolishthings.examples.dci.swing.role.TableHeaderDescriptor._TableHeaderDescriptor_;
43: import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.*;
44:
45: /***************************************************************************************************************************************************************
46: *
47: * A facility to bind some Swing components to data.
48: *
49: * @author Fabrizio Giudici
50: *
51: **************************************************************************************************************************************************************/
52: @ExtensionMethod(AsExtensions.class)
53: @NoArgsConstructor(access = AccessLevel.PRIVATE)
54: public final class Bindings
55: {
56: /***********************************************************************************************************************************************************
57: * Binds a source to a {@link JList}. Two roles are used:
58: *
59: * <ol>
60: * <li>{@link ObservableListProvider} to retrieve the list model</li>
61: * <li>{@link HtmlRenderableListCellRenderer} to retrieve rendering for each item</li>
62: * </ol>
63: *
64: * @param bindings the {@link BindingGroup} to add the new binding to
65: * @param datum the datum
66: * @param jList the {@code JList}
67: **********************************************************************************************************************************************************/
68: public static void bind (@Nonnull final BindingGroup bindings,
69: @Nonnull final Object datum,
70: @Nonnull final JList jList)
71: {
72: final ObservableList<?> ol = datum.as(_ObservableListProvider_).createObservableList();
73: bindings.addBinding(SwingBindings.createJListBinding(READ, ol, jList));
74: jList.setCellRenderer(new HtmlRenderableListCellRenderer());
75: }
76:
77: /***********************************************************************************************************************************************************
78: * Binds a source to a {@link JTable}. Two roles are used:
79: *
80: * <ol>
81: * <li>{@link ObservableListProvider} to retrieve the list model</li>
82: * <li>{@link TableHeaderDescriptor} to retrieve the column definition</li>
83: * </ol>
84: *
85: * @param bindings the {@link BindingGroup} to add the new binding to
86: * @param datum the datum
87: * @param jTable the {@code JTable}
88: **********************************************************************************************************************************************************/
89: public static <T> void bind (@Nonnull final BindingGroup bindings,
90: @Nonnull final Object datum,
91: @Nonnull final JTable jTable)
92: {
93: final ObservableList<T> ol = datum.as(_ObservableListProvider_).createObservableList();
94: final var tb = SwingBindings.createJTableBinding(READ_WRITE, ol, jTable);
95:
96: datum.as(_TableHeaderDescriptor_).getColumnDescriptors().forEach(cd ->
97: tb.addColumnBinding(BeanProperty.create(cd.propertyName())).setColumnName(cd.headerName()));
98:
99: bindings.addBinding(tb);
100: }
101: }