View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.component.bean;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * %%
7    * Copyright (C) 2017 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.util.List;
27  import javax.swing.ImageIcon;
28  import javax.swing.JButton;
29  import jaxx.runtime.JAXXContext;
30  import jaxx.runtime.JAXXObjectDescriptor;
31  import jaxx.runtime.JAXXUtil;
32  import jaxx.runtime.SwingUtil;
33  import jaxx.runtime.binding.SimpleJAXXObjectBinding;
34  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import static org.nuiton.i18n.I18n.t;
38  
39  public class ExtendedComboBox<O> extends BeanFilterableComboBox<O> {
40  
41      /*-----------------------------------------------------------------------*/
42      /*---------------- Constants for all javaBean properties ----------------*/
43      /*-----------------------------------------------------------------------*/
44  
45      public static final String PROPERTY_ACTION_ENABLED = "actionEnabled";
46      public static final String PROPERTY_ACTION_TOOL_TIP_I18N = "actionToolTipI18n";
47      public static final String PROPERTY_AUTO_DROP_DOWN = "autoDropDown";
48      public static final String PROPERTY_ICON = "icon";
49      public static final String PROPERTY_SHOW_ACTION_BUTTON = "showActionButton";
50  
51      /*-----------------------------------------------------------------------*/
52      /*------------------ Constants for all public bindings ------------------*/
53      /*-----------------------------------------------------------------------*/
54  
55      public static final String BINDING_ACTION_BUTTON_ENABLED = "actionButton.enabled";
56      public static final String BINDING_ACTION_BUTTON_ICON = "actionButton.icon";
57      public static final String BINDING_ACTION_BUTTON_TOOL_TIP_TEXT = "actionButton.toolTipText";
58      public static final String BINDING_ACTION_BUTTON_VISIBLE = "actionButton.visible";
59      public static final String BINDING_CHANGE_DECORATOR_ENABLED = "changeDecorator.enabled";
60      public static final String BINDING_CHANGE_DECORATOR_VISIBLE = "changeDecorator.visible";
61  
62      /*-----------------------------------------------------------------------*/
63      /*------------------------- Other static fields -------------------------*/
64      /*-----------------------------------------------------------------------*/
65  
66      private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAK1Uz08TQRSeFkotPwRECUZMUAwBE6fqwZhgVEohVPFHgIOxF2d3p9sx25l1ZhbWEI1/gn+C3r2YePNkPHj24MX4LxjjwavxzW7bXZYaMLFJt83Me9/73rfve2++o4KS6MxjEoZYBlyzFsW3lh48uGc9prauUmVL5mshUfzJ5VG+joac7rnSaLa+btLL7fTysmj5glOeyl5cR4NKP/WoalKqNTq9N8NWqrzZvV4M/UB2ULukeqG++vkj/9J58TqPUOgDu0FoZeagrKST/nWUZ45Gx6DSNil7hLtAQzLuAt8Rc7bsEaXukhZ9gp6j4joa8IkEMI3OHr7lCCPKD32Nps5VKOGrzNNUEgtuRcsSFRFe1KjakJg1JG1RiZ8ExJHMpZdxwLDaAUrYFq2W4OYnLoUtAMIroabcoU4Hx/ejWgMaDZNAi6oUflXscI3GTT/Y9IgrQniQm4SOqabYWbI1E7wSaC24OT+a3I+Q6G6FG8ZO5rKf2QLwTxj8sM211iIurcF5EjYeY2xB7S3m1y5dhZyxhFOsexJebBLueFRqdPt/yLIWo6XU0UDEInKDuU14n8fT7G8ZkhWSjiYpcTSa2BOdSDbdTRi1gb9Lq9QWksA0aHQyU8F1PRpnQhmJTu0ZWzAETgyRTGyujgoygGMYpPp+D23AVeyeqYx7DGB0+3ty4sv7b+9WO5bph9oneoamHA+j7MMgwdtgpvRo7JdAM698h/iLdVRS1IN1Ea2D6R7ENtvXQA7qxaNo0vEaUU2AKBS/fvg4+ehzH8qvokFPEGeVmPgaKummBBWE54T+jZsRo+GdI/Acg28fjKYlpEPlfcK4puDlQoN4CiQrbjPFYFphPrUM4KfUAFhNoqM4KASdpnvo1CVrlT79mth8e7OjVQ64n/xreKJX4SEaYNxjnEYLpr07ei6UIV/RwBHJjui1NVBmXP32YJ6PnnifLAPp9id3mdrM2Ht+4VnXt0O7LtXGqtFhqSHsQKVVgggde3aLhuCUKRO/lPVylFyk8YKA9QhF29tifmFmbm6GqaX0ConCh6NamTcXBqanK5Ha5t9VlHHfIZofjONrUX8jtlkBlggvKLBhWpmJtjJdkx5SgWmzYTqoOK6FDTjWsPlTKoykVfiXhrPb46Cec923efSaA0M+YzHuwJ65vg88Z56zgPcHHaOWmvkHAAA=";
67      private static final Log log = LogFactory.getLog(ExtendedComboBox.class);
68      private static final long serialVersionUID = 1L;
69  
70      /*-----------------------------------------------------------------------*/
71      /*--------------------------- Internal states ---------------------------*/
72      /*-----------------------------------------------------------------------*/
73  
74      private boolean allComponentsCreated;
75  
76      /*-----------------------------------------------------------------------*/
77      /*------------------------ Protected components  ------------------------*/
78      /*-----------------------------------------------------------------------*/
79  
80      protected JButton actionButton;
81      protected Boolean actionEnabled;
82      protected String actionToolTipI18n;
83      protected Boolean autoDropDown;
84      protected ImageIcon icon;
85      protected Boolean showActionButton;
86  
87      /*-----------------------------------------------------------------------*/
88      /*------------------------- Private components  -------------------------*/
89      /*-----------------------------------------------------------------------*/
90  
91      private ExtendedComboBox<O> $BeanFilterableComboBox0 = this;
92  
93      /*-----------------------------------------------------------------------*/
94      /*---------------------- Raw body code from script ----------------------*/
95      /*-----------------------------------------------------------------------*/
96  
97      public void setActionListener(ActionListener actionListener) {
98            getHandler().setActionListener(actionListener);
99          }
100     
101         @Override
102         public void setData(List<O> data) {
103             getCombobox().setFocusable(false);
104             super.setData(data);
105             getCombobox().setFocusable(true);
106         }
107 
108     /*-----------------------------------------------------------------------*/
109     /*---------------------------- Constructors  ----------------------------*/
110     /*-----------------------------------------------------------------------*/
111 
112     public ExtendedComboBox() {
113         $initialize();
114     }
115 
116     public ExtendedComboBox(JAXXContext param0) {
117         super(param0);
118         $initialize();
119     }
120 
121     /*-----------------------------------------------------------------------*/
122     /*--------------------------- Statics methods ---------------------------*/
123     /*-----------------------------------------------------------------------*/
124 
125     public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
126         return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
127     }
128 
129     /*-----------------------------------------------------------------------*/
130     /*---------------------------- Event methods ----------------------------*/
131     /*-----------------------------------------------------------------------*/
132 
133     public void doActionPerformed__on__actionButton(ActionEvent event) {
134         if (log.isDebugEnabled()) {
135             log.debug(event);
136         }
137         getHandler().doAction();
138     }
139 
140     public void doActionPerformed__on__changeDecorator(ActionEvent event) {
141         if (log.isDebugEnabled()) {
142             log.debug(event);
143         }
144         getHandler().togglePopup();
145     }
146 
147     /*-----------------------------------------------------------------------*/
148     /*----------------------- Public acessor methods  -----------------------*/
149     /*-----------------------------------------------------------------------*/
150 
151     public JButton getActionButton() {
152         return actionButton;
153     }
154 
155     public Boolean getActionEnabled() {
156         return actionEnabled;
157     }
158 
159     public String getActionToolTipI18n() {
160         return actionToolTipI18n;
161     }
162 
163     public Boolean getAutoDropDown() {
164         return autoDropDown;
165     }
166 
167     @Override
168     public ExtendedComboBoxHandler<O> getHandler() {
169         return (ExtendedComboBoxHandler<O>) super.getHandler();
170     }
171 
172     public ImageIcon getIcon() {
173         return icon;
174     }
175 
176     public Boolean getShowActionButton() {
177         return showActionButton;
178     }
179 
180     public Boolean isActionEnabled() {
181         return actionEnabled !=null && actionEnabled;
182     }
183 
184     public Boolean isAutoDropDown() {
185         return autoDropDown !=null && autoDropDown;
186     }
187 
188     public Boolean isShowActionButton() {
189         return showActionButton !=null && showActionButton;
190     }
191 
192     /*-----------------------------------------------------------------------*/
193     /*----------------------- Public mutator methods  -----------------------*/
194     /*-----------------------------------------------------------------------*/
195 
196     public void setActionEnabled(Boolean actionEnabled) {
197         Boolean oldValue = this.actionEnabled;
198         this.actionEnabled = actionEnabled;
199         firePropertyChange(PROPERTY_ACTION_ENABLED, oldValue, actionEnabled);
200     }
201 
202     public void setActionToolTipI18n(String actionToolTipI18n) {
203         String oldValue = this.actionToolTipI18n;
204         this.actionToolTipI18n = actionToolTipI18n;
205         firePropertyChange(PROPERTY_ACTION_TOOL_TIP_I18N, oldValue, actionToolTipI18n);
206     }
207 
208     public void setAutoDropDown(Boolean autoDropDown) {
209         Boolean oldValue = this.autoDropDown;
210         this.autoDropDown = autoDropDown;
211         firePropertyChange(PROPERTY_AUTO_DROP_DOWN, oldValue, autoDropDown);
212     }
213 
214     public void setIcon(ImageIcon icon) {
215         ImageIcon oldValue = this.icon;
216         this.icon = icon;
217         firePropertyChange(PROPERTY_ICON, oldValue, icon);
218     }
219 
220     public void setShowActionButton(Boolean showActionButton) {
221         Boolean oldValue = this.showActionButton;
222         this.showActionButton = showActionButton;
223         firePropertyChange(PROPERTY_SHOW_ACTION_BUTTON, oldValue, showActionButton);
224     }
225 
226     /*-----------------------------------------------------------------------*/
227     /*--------------------- Components creation methods ---------------------*/
228     /*-----------------------------------------------------------------------*/
229 
230     protected void addChildrenToToolbarRight() {
231         if (!allComponentsCreated) {
232             return;
233         }
234         toolbarRight.add(actionButton);
235     }
236 
237     protected void createActionButton() {
238         $objectMap.put("actionButton", actionButton = new JButton());
239         
240         actionButton.setName("actionButton");
241         actionButton.setFocusable(false);
242         actionButton.setFocusPainted(false);
243         actionButton.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__actionButton"));
244     }
245 
246     protected void createActionEnabled() {
247         $objectMap.put("actionEnabled", actionEnabled = true);
248     }
249 
250     protected void createActionToolTipI18n() {
251         $objectMap.put("actionToolTipI18n", actionToolTipI18n = "");
252     }
253 
254     protected void createAutoDropDown() {
255         $objectMap.put("autoDropDown", autoDropDown = true);
256     }
257 
258     @Override
259     protected void createChangeDecorator() {
260         super.createChangeDecorator();
261         changeDecorator.setName("changeDecorator");
262         changeDecorator.setFocusable(false);
263         changeDecorator.setToolTipText(t("beancombobox.action.sort.tip"));
264         changeDecorator.setFocusPainted(false);
265         changeDecorator.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__changeDecorator"));
266     }
267 
268     @Override
269     protected void createHandler() {
270         $objectMap.put("handler", handler = new ExtendedComboBoxHandler<O>(this));
271     }
272 
273     protected void createIcon() {
274         $objectMap.put("icon", icon = null);
275     }
276 
277     protected void createShowActionButton() {
278         $objectMap.put("showActionButton", showActionButton = false);
279     }
280 
281     @Override
282     protected void createToolbarRight() {
283         super.createToolbarRight();
284         toolbarRight.setName("toolbarRight");
285         toolbarRight.setBorderPainted(false);
286         toolbarRight.setVisible(true);
287         toolbarRight.setFloatable(false);
288     }
289 
290     /*-----------------------------------------------------------------------*/
291     /*------------------------ Internal jaxx methods ------------------------*/
292     /*-----------------------------------------------------------------------*/
293 
294     private void $completeSetup() {
295         allComponentsCreated = true;
296         if (log.isDebugEnabled()) {
297             log.debug(this);
298         }
299         addChildrenToToolbarRight();
300         
301         // apply 6 data bindings
302         JAXXUtil.applyDataBinding(this, $bindings.keySet());
303         
304         // apply 1 property setters
305         changeDecorator.setIcon(SwingUtil.createActionIcon("combobox-sort"));
306     }
307 
308     private void $initialize() {
309         if (allComponentsCreated) {
310             return;
311         }
312         if (log.isDebugEnabled()) {
313             log.debug(this);
314         }
315         $objectMap.put("$BeanFilterableComboBox0", $BeanFilterableComboBox0);
316         createAutoDropDown();
317         createShowActionButton();
318         createActionEnabled();
319         createIcon();
320         createActionToolTipI18n();
321         createActionButton();
322         // inline creation of $BeanFilterableComboBox0
323         setName("$BeanFilterableComboBox0");
324         
325         // registers 6 data bindings
326         $registerDefaultBindings();
327         $completeSetup();
328     }
329 
330     private void $registerDefaultBindings() {
331         // register 6 data bindings
332         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_VISIBLE, true ,"showActionButton") {
333         
334             @Override
335             public void processDataBinding() {
336                 actionButton.setVisible(isShowActionButton());
337             }
338         });
339         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_ICON, true ,"icon") {
340         
341             @Override
342             public void processDataBinding() {
343                 actionButton.setIcon(getIcon());
344             }
345         });
346         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_TOOL_TIP_TEXT, true ,"actionToolTipI18n") {
347         
348             @Override
349             public void processDataBinding() {
350                 actionButton.setToolTipText(t(getActionToolTipI18n()));
351             }
352         });
353         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_ENABLED, true ,"enabled" ,"actionEnabled") {
354         
355             @Override
356             public void processDataBinding() {
357                 actionButton.setEnabled(isEnabled() && isActionEnabled());
358             }
359         });
360         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_CHANGE_DECORATOR_VISIBLE, true ,"showDecorator") {
361         
362             @Override
363             public void processDataBinding() {
364                 changeDecorator.setVisible(isShowDecorator());
365             }
366         });
367         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_CHANGE_DECORATOR_ENABLED, true ,"enabled") {
368         
369             @Override
370             public void processDataBinding() {
371                 changeDecorator.setEnabled(isEnabled());
372             }
373         });
374     }
375 
376 }