Statistiques
| Révision :

root / tmp / org.txm.analec.rcp / src / visuAnalec / PanneauChamps.java @ 3093

Historique | Voir | Annoter | Télécharger (4,56 ko)

1
/*
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
4
 */
5
package visuAnalec;
6

    
7
import java.awt.*;
8
import java.awt.event.ActionEvent;
9
import java.awt.event.ActionListener;
10
import java.util.*;
11
import javax.swing.*;
12
import javax.swing.border.*;
13
import visuAnalec.elements.*;
14
import visuAnalec.util.*;
15
import visuAnalec.vue.*;
16

    
17
import ca.odell.glazedlists.swing.AutoCompleteSupport;
18
import ca.odell.glazedlists.GlazedLists;
19

    
20
/**
21
 *
22
 * @author Bernard, cplancq
23
 */
24
public class PanneauChamps extends JPanel {
25
        
26
        ArrayList<GChamp> champs = new ArrayList<>();
27
        
28
        private Vue vue;
29
        
30
        private Element element = null;
31
        
32
        private Class<? extends Element> classe = null;
33
        
34
        private String type = null;
35
        
36
        private JPanel cadre;
37
        
38
        public PanneauChamps(PanneauEditeur pEdit, Vue vue) {
39
                super(new BorderLayout());
40
                setBorder(new TitledBorder("Champs"));
41
                cadre = new JPanel(new GridLayout(0, 1));
42
                JScrollPane scroll = new JScrollPane(cadre);
43
                add(scroll, BorderLayout.CENTER);
44
                this.vue = vue;
45
        }
46
        
47
        void reafficher() {
48
                if (element == null) return;
49
                type = null;
50
                afficher(element);
51
        }
52
        
53
        void afficher(Element elt) {
54
                this.element = elt;
55
                if (elt.getClass() != classe || !elt.getType().equals(type)) {
56
                        type = elt.getType();
57
                        classe = elt.getClass();
58
                        afficherTouslesChamps();
59
                }
60
                ((TitledBorder) getBorder()).setTitle("Champs " + GVisu.getNomDeLaClasse(elt) + " :  \"" +
61
                                vue.getIdElement(elt) + "\"");
62
                for (GChamp gc : champs) {
63
                        gc.afficher(elt);
64
                }
65
                revalidate();
66
                repaint();
67
        }
68
        
69
        void effacer() {
70
                cadre.removeAll();
71
                type = null;
72
                element = null;
73
                ((TitledBorder) getBorder()).setTitle("Champs");
74
                revalidate();
75
                repaint();
76
        }
77
        
78
        private void afficherTouslesChamps() {
79
                cadre.removeAll();
80
                champs.clear();
81
                
82
                int niveau = 0;
83
                for (String[] champsniveau : vue.getChamps(classe, type)) {
84
                        JPanel panneauniveau = new JPanel();
85
                        // panneauniveau.setBorder(new TitledBorder("Niveau " + ++niveau));
86
                        for (String champ : champsniveau) {
87
                                GChamp gc = new GChamp(vue, classe, type, champ);
88
                                champs.add(gc);
89
                                panneauniveau.add(gc);
90
                        }
91
                        cadre.add(panneauniveau);
92
                }
93
        }
94
        
95
        private static class GChamp extends JPanel implements ActionListener {
96
                
97
                JComboBox afficheValeurChamp;
98
                
99
                Vue vue;
100
                
101
                Class<? extends Element> classe;
102
                
103
                String type;
104
                
105
                String champ;
106
                
107
                Element element = null;
108
                
109
                GChamp(Vue vue, Class<? extends Element> classe, String type, String champ) {
110
                        super();
111
                        this.vue = vue;
112
                        this.classe = classe;
113
                        this.type = type;
114
                        this.champ = champ;
115
                        Box groupe = Box.createVerticalBox();
116
                        Box titre = Box.createHorizontalBox();
117
                        titre.add(Box.createHorizontalGlue());
118
                        JLabel label = new JLabel(champ, JLabel.CENTER);
119
                        // label.setPreferredSize(new Dimension(50, 10));
120
                        titre.add(label);
121
                        titre.add(Box.createHorizontalGlue());
122
                        groupe.add(titre);
123
                        groupe.add(Box.createVerticalStrut(2));
124
                        afficheValeurChamp = new JComboBox(vue.getValeursChamp(classe, type, champ));
125
                        /*
126
                         * CP: JComboBox avec autocompletion. Dépend de la lib glazedlists
127
                         * (http://www.glazedlists.com/), (https://github.com/glazedlists/glazedlists)
128
                         */
129
                        AutoCompleteSupport<String> support = AutoCompleteSupport.install(afficheValeurChamp, GlazedLists.eventListOf(vue.getValeursChamp(classe, type, champ)));
130
                        support.setCorrectsCase(false);
131
                        afficheValeurChamp.setEditable(vue.isChampModifiable(classe, type, champ));
132
                        afficheValeurChamp.addActionListener(this);
133
                        groupe.add(afficheValeurChamp);
134
                        add(groupe);
135
                }
136
                
137
                void afficher(Element elt) {
138
                        this.element = elt;
139
                        afficheValeurChamp.removeActionListener(this);
140
                        String val = vue.getValeurChamp(elt, champ);
141
                        if (val.isEmpty()) afficheValeurChamp.setSelectedIndex(-1);
142
                        else afficheValeurChamp.setSelectedItem(val);
143
                        afficheValeurChamp.addActionListener(this);
144
                }
145
                
146
                @Override
147
                public void actionPerformed(ActionEvent evt) {
148
                        try {
149
                                if (!afficheValeurChamp.isEditable()) {
150
                                        JOptionPane.showMessageDialog(getTopLevelAncestor(), "Champ non modifiable pour ce type d'éléments",
151
                                                        "Modification impossible", JOptionPane.ERROR_MESSAGE);
152
                                        afficher(element);
153
                                        return;
154
                                }
155
                                String newval = ((String) afficheValeurChamp.getSelectedItem());
156
                                if (newval == null) newval = "";
157
                                else newval = newval.trim();
158
                                
159
                                if (!vue.setValeurChamp(element, champ, newval)) {
160
                                        JOptionPane.showMessageDialog(getTopLevelAncestor(), "Champ non défini pour cet élément",
161
                                                        "Saisie de valeur de champ impossible", JOptionPane.ERROR_MESSAGE);
162
                                        afficher(element);
163
                                }
164
                        }
165
                        catch (Throwable ex) {
166
                                GMessages.erreurFatale(ex);
167
                        }
168
                }
169
        }
170
}