OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.map.ui;
15
 
16
import org.openconcerto.map.model.Ville;
61 ilm 17
import org.openconcerto.ui.DefaultGridBagConstraints;
17 ilm 18
import org.openconcerto.ui.component.ComboLockedMode;
73 ilm 19
import org.openconcerto.ui.component.combo.ISearchableCombo;
17 ilm 20
import org.openconcerto.ui.component.text.DocumentComponent;
21
import org.openconcerto.ui.component.text.TextComponent;
22
import org.openconcerto.ui.state.WindowStateManager;
23
import org.openconcerto.ui.valuewrapper.ValueChangeSupport;
24
import org.openconcerto.ui.valuewrapper.ValueWrapper;
25
import org.openconcerto.utils.checks.ValidListener;
28 ilm 26
import org.openconcerto.utils.checks.ValidObject;
21 ilm 27
import org.openconcerto.utils.checks.ValidState;
17 ilm 28
 
29
import java.awt.Dimension;
61 ilm 30
import java.awt.Frame;
31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.awt.Insets;
17 ilm 34
import java.awt.event.ActionEvent;
35
import java.awt.event.ActionListener;
61 ilm 36
import java.awt.event.ContainerEvent;
37
import java.awt.event.ContainerListener;
17 ilm 38
import java.beans.PropertyChangeEvent;
39
import java.beans.PropertyChangeListener;
40
import java.io.File;
41
 
80 ilm 42
import javax.swing.BorderFactory;
61 ilm 43
import javax.swing.ImageIcon;
17 ilm 44
import javax.swing.JButton;
45
import javax.swing.JComponent;
61 ilm 46
import javax.swing.JDialog;
17 ilm 47
import javax.swing.JFrame;
48
import javax.swing.JPanel;
61 ilm 49
import javax.swing.SwingUtilities;
17 ilm 50
import javax.swing.WindowConstants;
51
import javax.swing.text.Document;
52
import javax.swing.text.JTextComponent;
53
 
73 ilm 54
public class ITextComboVilleViewer extends JPanel implements ValueWrapper<Ville>, DocumentComponent, TextComponent {
17 ilm 55
    /**
56
     * Selecteur de Ville
57
     */
58
    private static final long serialVersionUID = 3397210337907076649L;
73 ilm 59
    private final ISearchableCombo<Ville> text;
17 ilm 60
    private final JButton button = new JButton("Afficher sur la carte");
61 ilm 61
    private final JButton buttonAdd;
17 ilm 62
    private Ville currentVille = null;
63
 
73 ilm 64
    private final ValueChangeSupport<Ville> supp;
17 ilm 65
    private final ITextComboCacheVille cache;
66
 
67
    public ITextComboVilleViewer() {
68
        this.setOpaque(false);
61 ilm 69
        this.setLayout(new GridBagLayout());
70
        GridBagConstraints c = new DefaultGridBagConstraints();
71
        c.insets = new Insets(0, 0, 0, 2);
17 ilm 72
 
80 ilm 73
        this.buttonAdd = new JButton(new ImageIcon(ITextComboVilleViewer.class.getResource("add.png")));
73 ilm 74
        this.supp = new ValueChangeSupport<Ville>(this);
28 ilm 75
        this.cache = new ITextComboCacheVille();
61 ilm 76
 
83 ilm 77
        this.text = new ISearchableCombo<Ville>(ComboLockedMode.ITEMS_LOCKED, 0, 17) {
73 ilm 78
 
28 ilm 79
            @Override
73 ilm 80
            protected Ville stringToT(String t) {
83 ilm 81
                Ville v = Ville.getVilleFromVilleEtCode(t);
73 ilm 82
                return v;
83
            }
28 ilm 84
        };
73 ilm 85
 
28 ilm 86
        this.text.setMaxVisibleRows(20);
87
        this.text.addValueListener(new PropertyChangeListener() {
88
            public void propertyChange(final PropertyChangeEvent evt) {
89
                ITextComboVilleViewer.this.supp.fireValueChange();
90
            }
91
        });
92
        this.text.addValidListener(new ValidListener() {
93
            @Override
94
            public void validChange(ValidObject src, ValidState newValue) {
95
                ITextComboVilleViewer.this.supp.fireValidChange();
96
            }
97
        });
17 ilm 98
 
73 ilm 99
        final VilleListModel acache = new VilleListModel();
100
        this.text.initCache(acache);
101
 
61 ilm 102
        // Listen on data
103
        final PropertyChangeListener listener = new PropertyChangeListener() {
104
            @Override
105
            public void propertyChange(PropertyChangeEvent evt) {
73 ilm 106
                acache.fireModify();
61 ilm 107
            }
108
        };
17 ilm 109
 
61 ilm 110
        this.addContainerListener(new ContainerListener() {
111
            @Override
112
            public void componentRemoved(ContainerEvent e) {
113
                Ville.removeListener(listener);
114
            }
115
 
116
            @Override
117
            public void componentAdded(ContainerEvent e) {
118
                Ville.addListener(listener);
119
            }
120
        });
121
 
122
        c.weightx = 1;
123
        c.fill = GridBagConstraints.HORIZONTAL;
124
        this.add(this.text, c);
125
        c.weightx = 0;
126
        c.gridx++;
127
        this.button.setOpaque(false);
128
        this.add(this.button, c);
129
        c.gridx++;
130
        this.buttonAdd.setPreferredSize(new Dimension(24, 16));
80 ilm 131
        this.buttonAdd.setBorder(BorderFactory.createEmptyBorder());
61 ilm 132
        this.buttonAdd.setOpaque(false);
133
        this.buttonAdd.setContentAreaFilled(false);
134
        this.buttonAdd.setFocusPainted(false);
135
        this.buttonAdd.setFocusable(false);
136
        this.add(this.buttonAdd, c);
17 ilm 137
        this.button.addActionListener(new ActionListener() {
138
 
139
            public void actionPerformed(final ActionEvent e) {
140
                final JFrame f = new JFrame();
141
                final MapViewerPanel mapViewerPanel = new MapViewerPanel(true);
142
 
143
                f.setContentPane(mapViewerPanel);
144
                f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
145
                f.setSize(600, 500);
146
                f.setMinimumSize(new Dimension(600, 500));
147
                final File conf = new File(System.getProperty("user.home"), ".java" + File.separator + "ilm" + File.separator + "map" + File.separator);
148
                new WindowStateManager(f, new File(conf, "Configuration" + File.separator + "MapFrame.properties"), true).loadState();
149
                f.setVisible(true);
150
                if (ITextComboVilleViewer.this.currentVille != null) {
151
                    final long x = ITextComboVilleViewer.this.currentVille.getXLambert();
152
                    final long y = ITextComboVilleViewer.this.currentVille.getYLambert();
153
                    f.setTitle(ITextComboVilleViewer.this.currentVille.getName());
154
                    mapViewerPanel.getVilleRendererPanel().centerScreenXYLambert(x, y);
155
                    mapViewerPanel.getVilleRendererPanel().setHighlight(ITextComboVilleViewer.this.currentVille);
156
                    mapViewerPanel.getVilleRendererPanel().setAlwayVisible(ITextComboVilleViewer.this.currentVille);
157
                }
158
            }
159
        });
28 ilm 160
        this.addValueListener(new PropertyChangeListener() {
25 ilm 161
            @Override
28 ilm 162
            public void propertyChange(PropertyChangeEvent evt) {
163
                ITextComboVilleViewer.this.currentVille = evt.getNewValue() == null ? null : Ville.getVilleFromVilleEtCode(evt.getNewValue().toString());
17 ilm 164
                ITextComboVilleViewer.this.button.setEnabled(ITextComboVilleViewer.this.currentVille != null && ITextComboVilleViewer.this.isEnabled());
165
            }
166
        });
61 ilm 167
        this.buttonAdd.addActionListener(new ActionListener() {
168
 
169
            @Override
170
            public void actionPerformed(ActionEvent e) {
171
                final JDialog d = new JDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, ITextComboVilleViewer.this), true);
172
 
173
                final String rawTtext = text.getTextComp().getText();
174
                d.setContentPane(new VilleEditorPanel(rawTtext));
175
                d.setTitle("Nouvelle ville");
176
                d.pack();
177
                d.setResizable(false);
178
                d.setLocationRelativeTo(ITextComboVilleViewer.this);
179
                d.setVisible(true);
180
            }
181
        });
182
 
17 ilm 183
    }
184
 
185
    public void addValidListener(final ValidListener l) {
186
        this.supp.addValidListener(l);
187
    }
188
 
19 ilm 189
    @Override
190
    public void removeValidListener(ValidListener l) {
191
        this.supp.removeValidListener(l);
192
    }
193
 
17 ilm 194
    public void addValueListener(final PropertyChangeListener l) {
195
        this.supp.addValueListener(l);
196
    }
197
 
198
    // *** value
199
 
200
    public JComponent getComp() {
201
        return this;
202
    }
203
 
204
    public Document getDocument() {
205
        return this.text.getDocument();
206
    }
207
 
208
    // *** valid
209
 
210
    public JTextComponent getTextComp() {
211
        return this.text.getTextComp();
212
    }
213
 
214
    // *** text
215
 
73 ilm 216
    public Ville getValue() {
17 ilm 217
        return this.text.getValue();
218
    }
219
 
220
    @Override
21 ilm 221
    public ValidState getValidState() {
28 ilm 222
        return this.text.getValidState();
17 ilm 223
    }
224
 
225
    public void resetValue() {
226
        this.text.resetValue();
227
    }
228
 
229
    public void rmValueListener(final PropertyChangeListener l) {
230
        this.supp.rmValueListener(l);
231
    }
232
 
233
    public void setButtonVisible(final boolean b) {
234
        this.button.setVisible(b);
235
    }
236
 
237
    @Override
238
    public void setEnabled(final boolean enabled) {
239
        super.setEnabled(enabled);
240
        this.text.setEnabled(enabled);
241
        this.button.setEnabled(enabled);
242
    }
243
 
73 ilm 244
    public void setValue(final Ville val) {
17 ilm 245
        this.text.setValue(val);
246
    }
61 ilm 247
 
17 ilm 248
}