OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 26 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 25
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.map.ui;
14
 package org.openconcerto.map.ui;
15
 
15
 
16
import org.openconcerto.map.model.Ville;
16
import org.openconcerto.map.model.Ville;
17
import org.openconcerto.ui.PopupMouseListener;
-
 
18
import org.openconcerto.ui.component.ComboLockedMode;
17
import org.openconcerto.ui.component.ComboLockedMode;
19
import org.openconcerto.ui.component.ITextSelector;
18
import org.openconcerto.ui.component.IComboCacheListModel;
-
 
19
import org.openconcerto.ui.component.combo.ISearchableTextCombo;
20
import org.openconcerto.ui.component.text.DocumentComponent;
20
import org.openconcerto.ui.component.text.DocumentComponent;
21
import org.openconcerto.ui.component.text.TextComponent;
21
import org.openconcerto.ui.component.text.TextComponent;
22
import org.openconcerto.ui.state.WindowStateManager;
22
import org.openconcerto.ui.state.WindowStateManager;
23
import org.openconcerto.ui.valuewrapper.ValueChangeSupport;
23
import org.openconcerto.ui.valuewrapper.ValueChangeSupport;
24
import org.openconcerto.ui.valuewrapper.ValueWrapper;
24
import org.openconcerto.ui.valuewrapper.ValueWrapper;
25
import org.openconcerto.utils.checks.EmptyListener;
25
import org.openconcerto.utils.checks.EmptyListener;
26
import org.openconcerto.utils.checks.EmptyObject;
26
import org.openconcerto.utils.checks.EmptyObject;
27
import org.openconcerto.utils.checks.EmptyObjectHelper;
27
import org.openconcerto.utils.checks.EmptyObjectHelper;
28
import org.openconcerto.utils.checks.ValidListener;
28
import org.openconcerto.utils.checks.ValidListener;
29
import org.openconcerto.utils.checks.ValidState;
29
import org.openconcerto.utils.checks.ValidState;
-
 
30
import org.openconcerto.utils.text.SimpleDocumentListener;
30
 
31
 
31
import java.awt.BorderLayout;
32
import java.awt.BorderLayout;
32
import java.awt.Dimension;
33
import java.awt.Dimension;
33
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionListener;
35
import java.awt.event.ActionListener;
Line 37... Line 38...
37
import java.io.File;
38
import java.io.File;
38
 
39
 
39
import javax.swing.JButton;
40
import javax.swing.JButton;
40
import javax.swing.JComponent;
41
import javax.swing.JComponent;
41
import javax.swing.JFrame;
42
import javax.swing.JFrame;
42
import javax.swing.JMenuItem;
-
 
43
import javax.swing.JPanel;
43
import javax.swing.JPanel;
44
import javax.swing.JPopupMenu;
-
 
45
import javax.swing.WindowConstants;
44
import javax.swing.WindowConstants;
46
import javax.swing.event.DocumentEvent;
45
import javax.swing.event.DocumentEvent;
47
import javax.swing.event.DocumentListener;
-
 
48
import javax.swing.text.Document;
46
import javax.swing.text.Document;
49
import javax.swing.text.JTextComponent;
47
import javax.swing.text.JTextComponent;
50
 
48
 
51
import org.apache.commons.collections.Predicate;
49
import org.apache.commons.collections.Predicate;
52
 
50
 
53
public class ITextComboVilleViewer extends JPanel implements ValueWrapper<String>, DocumentComponent, TextComponent, EmptyObject {
51
public class ITextComboVilleViewer extends JPanel implements ValueWrapper<String>, DocumentComponent, TextComponent, EmptyObject {
54
    /**
52
    /**
55
     * Selecteur de Ville
53
     * Selecteur de Ville
56
     */
54
     */
57
    private static final long serialVersionUID = 3397210337907076649L;
55
    private static final long serialVersionUID = 3397210337907076649L;
58
    private final ITextSelector text = new ITextSelector(ComboLockedMode.ITEMS_LOCKED);
56
    private final ISearchableTextCombo text = new ISearchableTextCombo(ComboLockedMode.UNLOCKED);
59
    private final JButton button = new JButton("Afficher sur la carte");
57
    private final JButton button = new JButton("Afficher sur la carte");
60
    private Ville currentVille = null;
58
    private Ville currentVille = null;
61
    private final EmptyObjectHelper emptyHelper;
59
    private final EmptyObjectHelper emptyHelper;
62
 
60
 
63
    private final ValueChangeSupport<String> supp;
61
    private final ValueChangeSupport<String> supp;
Line 79... Line 77...
79
                ITextComboVilleViewer.this.supp.fireValueChange();
77
                ITextComboVilleViewer.this.supp.fireValueChange();
80
            }
78
            }
81
        });
79
        });
82
 
80
 
83
        this.cache = new ITextComboCacheVille();
81
        this.cache = new ITextComboCacheVille();
84
        this.text.initCache(this.cache);
82
        new IComboCacheListModel(this.cache).initCacheLater(this.text);
85
        this.add(this.text, BorderLayout.CENTER);
83
        this.add(this.text, BorderLayout.CENTER);
86
 
84
 
87
        this.add(this.button, BorderLayout.EAST);
85
        this.add(this.button, BorderLayout.EAST);
88
        this.button.addActionListener(new ActionListener() {
86
        this.button.addActionListener(new ActionListener() {
89
 
87
 
Line 106... Line 104...
106
                    mapViewerPanel.getVilleRendererPanel().setHighlight(ITextComboVilleViewer.this.currentVille);
104
                    mapViewerPanel.getVilleRendererPanel().setHighlight(ITextComboVilleViewer.this.currentVille);
107
                    mapViewerPanel.getVilleRendererPanel().setAlwayVisible(ITextComboVilleViewer.this.currentVille);
105
                    mapViewerPanel.getVilleRendererPanel().setAlwayVisible(ITextComboVilleViewer.this.currentVille);
108
                }
106
                }
109
            }
107
            }
110
        });
108
        });
111
        this.text.getDocument().addDocumentListener(new DocumentListener() {
109
        this.text.getDocument().addDocumentListener(new SimpleDocumentListener() {
112
 
-
 
-
 
110
            @Override
113
            public void changedUpdate(final DocumentEvent e) {
111
            public void update(DocumentEvent e) {
114
                ITextComboVilleViewer.this.currentVille = Ville.getVilleFromVilleEtCode(ITextComboVilleViewer.this.text.getValue());
112
                ITextComboVilleViewer.this.currentVille = Ville.getVilleFromVilleEtCode(getText(e.getDocument()));
115
                ITextComboVilleViewer.this.button.setEnabled(ITextComboVilleViewer.this.currentVille != null && ITextComboVilleViewer.this.isEnabled());
113
                ITextComboVilleViewer.this.button.setEnabled(ITextComboVilleViewer.this.currentVille != null && ITextComboVilleViewer.this.isEnabled());
116
 
114
 
117
            }
115
            }
118
 
-
 
119
            public void insertUpdate(final DocumentEvent e) {
-
 
120
                this.changedUpdate(e);
-
 
121
            }
-
 
122
 
-
 
123
            public void removeUpdate(final DocumentEvent e) {
-
 
124
                this.changedUpdate(e);
-
 
125
            }
-
 
126
        });
116
        });
127
        final JPopupMenu popupMenu = new JPopupMenu();
-
 
128
        // FIXME ajouter la possibilité de supprimer une ville précédemment enregistrée
-
 
129
        final JMenuItem menuItem = new JMenuItem("Enregistrer cette ville");
-
 
130
        menuItem.addActionListener(new ActionListener() {
-
 
131
 
-
 
132
            public void actionPerformed(final ActionEvent e) {
-
 
133
                final String t = ITextComboVilleViewer.this.text.getTextComp().getText();
-
 
134
                ITextComboVilleViewer.this.cache.addToCache(t);
-
 
135
                final Ville createVilleFrom = ITextComboVilleViewer.this.cache.createVilleFrom(t);
-
 
136
                if (createVilleFrom != null) {
-
 
137
                    final String villeEtCode = createVilleFrom.getVilleEtCode();
-
 
138
                    ITextComboVilleViewer.this.setValue(villeEtCode);
-
 
139
                    ITextComboVilleViewer.this.firePropertyChange("value", null, villeEtCode);
-
 
140
                }
-
 
141
            }
-
 
142
        });
-
 
143
        popupMenu.add(menuItem);
-
 
144
 
-
 
145
        this.text.getTextComp().addMouseListener(new PopupMouseListener(popupMenu));
-
 
146
 
-
 
147
    }
117
    }
148
 
118
 
149
    @Override
119
    @Override
150
    public void addEmptyListener(final EmptyListener l) {
120
    public void addEmptyListener(final EmptyListener l) {
151
        this.emptyHelper.addListener(l);
121
        this.emptyHelper.addListener(l);
Line 196... Line 166...
196
        return this.emptyHelper.isEmpty();
166
        return this.emptyHelper.isEmpty();
197
    }
167
    }
198
 
168
 
199
    @Override
169
    @Override
200
    public ValidState getValidState() {
170
    public ValidState getValidState() {
-
 
171
        // TODO listen to Ville list, otherwise if we type a city that doesn't exist, the value
-
 
172
        // change and we're invalid, then we add the city but this does not change the value of the
-
 
173
        // combo and thus we're still invalid even though the city is now in the list
201
        final Ville villeFromVilleEtCode = Ville.getVilleFromVilleEtCode(this.getValue());
174
        final Ville villeFromVilleEtCode = Ville.getVilleFromVilleEtCode(this.getValue());
202
        final boolean b = villeFromVilleEtCode != null;
175
        final boolean b = villeFromVilleEtCode != null;
203
        if (b) {
176
        if (b) {
204
            this.cache.setLastGood(villeFromVilleEtCode);
177
            this.cache.setLastGood(villeFromVilleEtCode);
205
            return ValidState.getTrueInstance();
178
            return ValidState.getTrueInstance();
Line 222... Line 195...
222
 
195
 
223
    @Override
196
    @Override
224
    public void setEnabled(final boolean enabled) {
197
    public void setEnabled(final boolean enabled) {
225
        super.setEnabled(enabled);
198
        super.setEnabled(enabled);
226
        this.text.setEnabled(enabled);
199
        this.text.setEnabled(enabled);
227
        this.text.setEditable(enabled);
-
 
228
        this.button.setEnabled(enabled);
200
        this.button.setEnabled(enabled);
229
    }
201
    }
230
 
202
 
231
    public void setValue(final String val) {
203
    public void setValue(final String val) {
232
        this.text.setValue(val);
204
        this.text.setValue(val);