View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.maturity;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (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 General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.persistence.entities.protocol.MaturityCaracteristic;
26  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
27  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
28  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
29  import jaxx.runtime.SwingUtil;
30  import jaxx.runtime.validator.swing.SwingValidator;
31  
32  import javax.swing.JCheckBox;
33  import javax.swing.JComponent;
34  import javax.swing.JList;
35  import javax.swing.ListCellRenderer;
36  import java.awt.Component;
37  import java.awt.event.MouseAdapter;
38  import java.awt.event.MouseEvent;
39  import java.util.List;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * @author Kevin Morin - kmorin@codelutin.com
45   * @since 4.5
46   */
47  public class EditMaturityCaracteristicPopupUIHandler extends AbstractTuttiUIHandler<EditMaturityCaracteristicPopupUIModel, EditMaturityCaracteristicPopupUI> {
48  
49      @Override
50      public void afterInit(EditMaturityCaracteristicPopupUI ui) {
51          initUI(ui);
52  
53          JList<CaracteristicQualitativeValue> maturityValuesEditor = ui.getMaturityValuesEditor();
54          maturityValuesEditor.setCellRenderer(new CheckboxListRenderer());
55          maturityValuesEditor.addMouseListener(new MouseAdapter() {
56              public void mouseClicked(MouseEvent event) {
57                  JList<CaracteristicQualitativeValue> list = (JList<CaracteristicQualitativeValue>) event.getSource();
58  
59                  // Get index of item clicked
60  
61                  int index = list.locationToIndex(event.getPoint());
62                  CaracteristicQualitativeValue item = list.getModel().getElementAt(index);
63  
64                  // Toggle selected state
65                  if (item != null) {
66                      if (getModel().isMature(item)) {
67                          getModel().removeMatureState(item);
68  
69                      } else {
70                          getModel().addMatureState(item);
71                      }
72                  }
73  
74                  // Repaint cell
75  
76                  list.repaint(list.getCellBounds(index, index));
77              }
78          });
79  
80          getModel().addPropertyChangeListener(EditMaturityCaracteristicPopupUIModel.PROPERTY_ALL_MATURITY_STATES, evt -> {
81              List<CaracteristicQualitativeValue> newValue = (List<CaracteristicQualitativeValue>) evt.getNewValue();
82              if (newValue != null) {
83                  maturityValuesEditor.setListData(newValue.toArray(new CaracteristicQualitativeValue[newValue.size()]));
84              } else {
85                  maturityValuesEditor.setListData(new CaracteristicQualitativeValue[0]);
86              }
87          });
88      }
89  
90      @Override
91      public void onCloseUI() {
92          getUI().dispose();
93      }
94  
95      @Override
96      public SwingValidator<EditMaturityCaracteristicPopupUIModel> getValidator() {
97          return ui.getValidator();
98      }
99  
100     @Override
101     protected JComponent getComponentToFocus() {
102         return getUI().getMaturityValuesEditor();
103     }
104 
105     public void open(Caracteristic caracteristic, MaturityCaracteristic maturityCaracteristic) {
106         getModel().setAllMaturityStates(caracteristic.getQualitativeValue());
107         getModel().setMatureStateIds(maturityCaracteristic.getMatureStateIds());
108         getModel().setValid(false);
109 
110         getUI().getMessage().setText(t("tutti.editProtocol.maturityCaracteristic.dialog.message", decorate(caracteristic)));
111 
112         getUI().pack();
113         SwingUtil.center(getContext().getMainUI(), ui);
114         getUI().setVisible(true);
115     }
116 
117     public void validate() {
118         if (getValidator().isValid()) {
119             getModel().setValid(true);
120             onCloseUI();
121         }
122     }
123 
124     public void cancel() {
125         getModel().setAllMaturityStates(null);
126         getModel().setMatureStateIds(null);
127         onCloseUI();
128     }
129 
130     class CheckboxListRenderer extends JCheckBox implements
131             ListCellRenderer<CaracteristicQualitativeValue> {
132 
133         @Override
134         public Component getListCellRendererComponent(
135                 JList<? extends CaracteristicQualitativeValue> list, CaracteristicQualitativeValue value,
136                 int index, boolean isSelected, boolean cellHasFocus) {
137             setEnabled(list.isEnabled());
138             setSelected(EditMaturityCaracteristicPopupUIHandler.this.getModel().isMature(value));
139             setFont(list.getFont());
140             setBackground(list.getBackground());
141             setForeground(list.getForeground());
142             setText(decorate(value));
143             return this;
144         }
145     }
146 }