View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.component.bean;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: UI Swing Common
6    * %%
7    * Copyright (C) 2017 - 2024 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  
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.util.List;
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import jaxx.runtime.JAXXContext;
31  import jaxx.runtime.JAXXObjectDescriptor;
32  import jaxx.runtime.JAXXUtil;
33  import jaxx.runtime.SwingUtil;
34  import jaxx.runtime.binding.SimpleJAXXObjectBinding;
35  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import static org.nuiton.i18n.I18n.t;
39  
40  public class ExtendedComboBox<O> extends BeanFilterableComboBox<O> {
41  
42      /*-----------------------------------------------------------------------*/
43      /*---------------- Constants for all javaBean properties ----------------*/
44      /*-----------------------------------------------------------------------*/
45  
46      public static final String PROPERTY_ACTION_ENABLED = "actionEnabled";
47      public static final String PROPERTY_ACTION_TOOL_TIP_I18N = "actionToolTipI18n";
48      public static final String PROPERTY_AUTO_DROP_DOWN = "autoDropDown";
49      public static final String PROPERTY_ICON = "icon";
50      public static final String PROPERTY_SHOW_ACTION_BUTTON = "showActionButton";
51  
52      /*-----------------------------------------------------------------------*/
53      /*------------------ Constants for all public bindings ------------------*/
54      /*-----------------------------------------------------------------------*/
55  
56      public static final String BINDING_ACTION_BUTTON_ENABLED = "actionButton.enabled";
57      public static final String BINDING_ACTION_BUTTON_ICON = "actionButton.icon";
58      public static final String BINDING_ACTION_BUTTON_TOOL_TIP_TEXT = "actionButton.toolTipText";
59      public static final String BINDING_ACTION_BUTTON_VISIBLE = "actionButton.visible";
60      public static final String BINDING_CHANGE_DECORATOR_ENABLED = "changeDecorator.enabled";
61      public static final String BINDING_CHANGE_DECORATOR_VISIBLE = "changeDecorator.visible";
62  
63      /*-----------------------------------------------------------------------*/
64      /*------------------------- Other static fields -------------------------*/
65      /*-----------------------------------------------------------------------*/
66  
67      private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAKVUTU8UQRDtXVjW5UNAhEDEBMUQMLFXY2JI8AOWhbAEPwIciHuxZ6Z3tsns9NDdA2OIxp/gT9C7FxNvnowHzx68GP+CMR68GqtndneGYQ0YSRhId9WrV6/r1ZvvKCcFurRLggAL31WsQfH60s7OQ2OXmqpMpSmYp7hA0U8mi7JV1Ge1z6VC09UNnV5spheXecPjLnUT2QsbqFeqpw6VdUqVQhePZphSFrfa1wuB54sWaptUJ9RXP39kX1ovXmcRCjxg1wutTJ2UFXfSvYGyzFLoHFTaJ0WHuDbQEMy1ge+APlt2iJQPSIPuoecov4F6PCIATKHLp285xAjzA0+h8SslStxV5igqiAG3vGHwEg+uK3SnJjCrCdqgAu/5xBLMpjexz7A8AErYbNXABiDglUBR16JWC8DzwiI9CvUTX/Gy4F6ZH7gKDetGsG4Olzh3IDcOHZJ1frBkKsbdkq8Ud/X52fh+gIR3K66maqUuu5nJAX9U4wdNkpUGsWkFzuOw4QhjG2pvM69yYx5yhmJOkeBxeL5OXMuhQqHyf+mxFsEkZFHAwCBik9l1eMHzSdrrml2JJKNJQhWFRo5Ex1pNthMGTSBu0zI1uSDw/gpNpCrYtkOjTCgj0IUjgwoWwLEF4hnNVFFO+HAMo1M97ppNuIr8Mp7yiwYMb3+PjXx5/+3dassk3VB7tGNowuMwvB5MEDwD06UHI4f4ijnF+8RbqKKCpA4siHABTHYgttW8BnJQL5pBnY7XiKwDRC7/9cPHsSefu1B2FfU6nFirRMdXUEHVBajAHSvw7i2GjPoPzsB3CH67YCYNLiwqHhHmKgruzdWII0Gy/D6TDMYUBlMJH/4UagCrSHgUBQWg02QHndpkjcKnXyNbbxdbWmWA+8Rfw2O9co9RD3Md5tJwpTS3RccV0udJ6ls83gqd9gRKjavXHMyr4Rcfk6Un2f7YIZNbKV/Pzj1rG7bv0KZKezQ8LNS46cukShChIrNu0wCcMq7jl9ImDpPzNNoMsBChaHNNzM5NzcxMMbmU3B1heH9YK/Vyga97uhWqrf+bRyn3naL53ii+EvY3YOoVYPDgmgQbJpUZaSrTNukpFZjUG6aFiqNaWINjBbs+ocJAUoV/aTi9PU7qOdN+zbO3LRjyKYO5FuyZu8fAM/o7DXh/AEF1/UXrBwAA";
68      private static final Log log = LogFactory.getLog(ExtendedComboBox.class);
69      private static final long serialVersionUID = 1L;
70  
71      /*-----------------------------------------------------------------------*/
72      /*--------------------------- Internal states ---------------------------*/
73      /*-----------------------------------------------------------------------*/
74  
75      private boolean allComponentsCreated;
76  
77      /*-----------------------------------------------------------------------*/
78      /*------------------------ Protected components  ------------------------*/
79      /*-----------------------------------------------------------------------*/
80  
81      protected JButton actionButton;
82      protected Boolean actionEnabled;
83      protected String actionToolTipI18n;
84      protected Boolean autoDropDown;
85      protected ImageIcon icon;
86      protected Boolean showActionButton;
87  
88      /*-----------------------------------------------------------------------*/
89      /*------------------------- Private components  -------------------------*/
90      /*-----------------------------------------------------------------------*/
91  
92      private ExtendedComboBox<O> $BeanFilterableComboBox0 = this;
93  
94      /*-----------------------------------------------------------------------*/
95      /*---------------------- Raw body code from script ----------------------*/
96      /*-----------------------------------------------------------------------*/
97  
98      public void setActionListener(ActionListener actionListener) {
99            getHandler().setActionListener(actionListener);
100         }
101     
102         @Override
103         public void setData(List<O> data) {
104             getCombobox().setFocusable(false);
105             super.setData(data);
106             getCombobox().setFocusable(true);
107         }
108 
109     /*-----------------------------------------------------------------------*/
110     /*---------------------------- Constructors  ----------------------------*/
111     /*-----------------------------------------------------------------------*/
112 
113     public ExtendedComboBox() {
114         $initialize();
115     }
116 
117     public ExtendedComboBox(JAXXContext param0) {
118         super(param0);
119         $initialize();
120     }
121 
122     /*-----------------------------------------------------------------------*/
123     /*--------------------------- Statics methods ---------------------------*/
124     /*-----------------------------------------------------------------------*/
125 
126     public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
127         return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
128     }
129 
130     /*-----------------------------------------------------------------------*/
131     /*---------------------------- Event methods ----------------------------*/
132     /*-----------------------------------------------------------------------*/
133 
134     public void doActionPerformed__on__actionButton(ActionEvent event) {
135         if (log.isDebugEnabled()) {
136             log.debug(event);
137         }
138         getHandler().doAction();
139     }
140 
141     public void doActionPerformed__on__changeDecorator(ActionEvent event) {
142         if (log.isDebugEnabled()) {
143             log.debug(event);
144         }
145         getHandler().togglePopup();
146     }
147 
148     /*-----------------------------------------------------------------------*/
149     /*----------------------- Public acessor methods  -----------------------*/
150     /*-----------------------------------------------------------------------*/
151 
152     public JButton getActionButton() {
153         return actionButton;
154     }
155 
156     public Boolean getActionEnabled() {
157         return actionEnabled;
158     }
159 
160     public String getActionToolTipI18n() {
161         return actionToolTipI18n;
162     }
163 
164     public Boolean getAutoDropDown() {
165         return autoDropDown;
166     }
167 
168     @Override
169     public ExtendedComboBoxHandler<O> getHandler() {
170         return (ExtendedComboBoxHandler<O>) super.getHandler();
171     }
172 
173     public ImageIcon getIcon() {
174         return icon;
175     }
176 
177     public Boolean getShowActionButton() {
178         return showActionButton;
179     }
180 
181     public Boolean isActionEnabled() {
182         return actionEnabled !=null && actionEnabled;
183     }
184 
185     public Boolean isAutoDropDown() {
186         return autoDropDown !=null && autoDropDown;
187     }
188 
189     public Boolean isShowActionButton() {
190         return showActionButton !=null && showActionButton;
191     }
192 
193     /*-----------------------------------------------------------------------*/
194     /*----------------------- Public mutator methods  -----------------------*/
195     /*-----------------------------------------------------------------------*/
196 
197     public void setActionEnabled(Boolean actionEnabled) {
198         Boolean oldValue = this.actionEnabled;
199         this.actionEnabled = actionEnabled;
200         firePropertyChange(PROPERTY_ACTION_ENABLED, oldValue, actionEnabled);
201     }
202 
203     public void setActionToolTipI18n(String actionToolTipI18n) {
204         String oldValue = this.actionToolTipI18n;
205         this.actionToolTipI18n = actionToolTipI18n;
206         firePropertyChange(PROPERTY_ACTION_TOOL_TIP_I18N, oldValue, actionToolTipI18n);
207     }
208 
209     public void setAutoDropDown(Boolean autoDropDown) {
210         Boolean oldValue = this.autoDropDown;
211         this.autoDropDown = autoDropDown;
212         firePropertyChange(PROPERTY_AUTO_DROP_DOWN, oldValue, autoDropDown);
213     }
214 
215     public void setIcon(ImageIcon icon) {
216         ImageIcon oldValue = this.icon;
217         this.icon = icon;
218         firePropertyChange(PROPERTY_ICON, oldValue, icon);
219     }
220 
221     public void setShowActionButton(Boolean showActionButton) {
222         Boolean oldValue = this.showActionButton;
223         this.showActionButton = showActionButton;
224         firePropertyChange(PROPERTY_SHOW_ACTION_BUTTON, oldValue, showActionButton);
225     }
226 
227     /*-----------------------------------------------------------------------*/
228     /*--------------------- Components creation methods ---------------------*/
229     /*-----------------------------------------------------------------------*/
230 
231     protected void addChildrenToToolbarRight() {
232         if (!allComponentsCreated) {
233             return;
234         }
235         toolbarRight.add(actionButton);
236     }
237 
238     protected void createActionButton() {
239         $objectMap.put("actionButton", actionButton = new JButton());
240         
241         actionButton.setName("actionButton");
242         actionButton.setFocusable(false);
243         actionButton.setFocusPainted(false);
244         actionButton.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__actionButton"));
245     }
246 
247     protected void createActionEnabled() {
248         $objectMap.put("actionEnabled", actionEnabled = true);
249     }
250 
251     protected void createActionToolTipI18n() {
252         $objectMap.put("actionToolTipI18n", actionToolTipI18n = "");
253     }
254 
255     protected void createAutoDropDown() {
256         $objectMap.put("autoDropDown", autoDropDown = true);
257     }
258 
259     @Override
260     protected void createChangeDecorator() {
261         super.createChangeDecorator();
262         changeDecorator.setName("changeDecorator");
263         changeDecorator.setFocusable(false);
264         changeDecorator.setToolTipText(t("beancombobox.action.sort.tip"));
265         changeDecorator.setFocusPainted(false);
266         changeDecorator.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__changeDecorator"));
267     }
268 
269     @Override
270     protected void createHandler() {
271         $objectMap.put("handler", handler = new ExtendedComboBoxHandler<O>(this));
272     }
273 
274     protected void createIcon() {
275         $objectMap.put("icon", icon = null);
276     }
277 
278     protected void createShowActionButton() {
279         $objectMap.put("showActionButton", showActionButton = false);
280     }
281 
282     @Override
283     protected void createToolbarRight() {
284         super.createToolbarRight();
285         toolbarRight.setName("toolbarRight");
286         toolbarRight.setBorderPainted(false);
287         toolbarRight.setVisible(true);
288         toolbarRight.setFloatable(false);
289     }
290 
291     /*-----------------------------------------------------------------------*/
292     /*------------------------ Internal jaxx methods ------------------------*/
293     /*-----------------------------------------------------------------------*/
294 
295     private void $completeSetup() {
296         allComponentsCreated = true;
297         if (log.isDebugEnabled()) {
298             log.debug(this);
299         }
300         addChildrenToToolbarRight();
301         
302         // apply 6 data bindings
303         JAXXUtil.applyDataBinding(this, $bindings.keySet());
304         
305         // apply 1 property setters
306         changeDecorator.setIcon(SwingUtil.createActionIcon("combobox-sort"));
307     }
308 
309     private void $initialize() {
310         if (allComponentsCreated) {
311             return;
312         }
313         if (log.isDebugEnabled()) {
314             log.debug(this);
315         }
316         $objectMap.put("$BeanFilterableComboBox0", $BeanFilterableComboBox0);
317         createAutoDropDown();
318         createShowActionButton();
319         createActionEnabled();
320         createIcon();
321         createActionToolTipI18n();
322         createActionButton();
323         // inline creation of $BeanFilterableComboBox0
324         setName("$BeanFilterableComboBox0");
325         
326         // registers 6 data bindings
327         $registerDefaultBindings();
328         $completeSetup();
329     }
330 
331     private void $registerDefaultBindings() {
332         // register 6 data bindings
333         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_VISIBLE, true ,"showActionButton") {
334         
335             @Override
336             public void processDataBinding() {
337                 actionButton.setVisible(isShowActionButton());
338             }
339         });
340         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_ICON, true ,"icon") {
341         
342             @Override
343             public void processDataBinding() {
344                 actionButton.setIcon(getIcon());
345             }
346         });
347         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_TOOL_TIP_TEXT, true ,"actionToolTipI18n") {
348         
349             @Override
350             public void processDataBinding() {
351                 actionButton.setToolTipText(t(getActionToolTipI18n()));
352             }
353         });
354         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_ACTION_BUTTON_ENABLED, true ,"enabled" ,"actionEnabled") {
355         
356             @Override
357             public void processDataBinding() {
358                 actionButton.setEnabled(isEnabled() && isActionEnabled());
359             }
360         });
361         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_CHANGE_DECORATOR_VISIBLE, true ,"showDecorator") {
362         
363             @Override
364             public void processDataBinding() {
365                 changeDecorator.setVisible(isShowDecorator());
366             }
367         });
368         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_CHANGE_DECORATOR_ENABLED, true ,"enabled") {
369         
370             @Override
371             public void processDataBinding() {
372                 changeDecorator.setEnabled(isEnabled());
373             }
374         });
375     }
376 
377 }