Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (4,81 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
  ArrayList<GChamp> champs = new ArrayList<GChamp>();
26
  private Vue vue;
27
  private Element element = null;
28
  private Class<? extends Element> classe = null;
29
  private String type = null;
30
  private JPanel cadre;
31
  public PanneauChamps(PanneauEditeur pEdit, Vue vue) {
32
    super(new BorderLayout());
33
    setBorder(new TitledBorder("Champs"));
34
    cadre = new JPanel(new GridLayout(0, 1));
35
    JScrollPane scroll = new JScrollPane(cadre);
36
    add(scroll, BorderLayout.CENTER);
37
    this.vue = vue;
38
  }
39
  void reafficher() {
40
    if (element == null) return;
41
    type = null;
42
    afficher(element);
43
  }
44
  void afficher(Element elt) {
45
    this.element = elt;
46
    if (elt.getClass() != classe || !elt.getType().equals(type)) {
47
      type = elt.getType();
48
      classe = elt.getClass();
49
      afficherTouslesChamps();
50
    }
51
    ((TitledBorder) getBorder()).setTitle("Champs " + GVisu.getNomDeLaClasse(elt) + " :  \"" +
52
            vue.getIdElement(elt) + "\"");
53
    for (GChamp gc : champs) {
54
      gc.afficher(elt);
55
    }
56
    revalidate();
57
    repaint();
58
  }
59
  void effacer() {
60
    cadre.removeAll();
61
    type = null;
62
    element = null;
63
    ((TitledBorder) getBorder()).setTitle("Champs");
64
    revalidate();
65
    repaint();
66
  }
67
  private void afficherTouslesChamps() {
68
    cadre.removeAll();
69
    champs.clear();
70

    
71
    int niveau = 0;
72
    for (String[] champsniveau : vue.getChamps(classe, type)) {
73
      JPanel panneauniveau = new JPanel();
74
 //     panneauniveau.setBorder(new TitledBorder("Niveau " + ++niveau));
75
      for (String champ : champsniveau) {
76
        GChamp gc = new GChamp(vue, classe, type, champ);
77
        champs.add(gc);
78
        panneauniveau.add(gc);
79
      }
80
      cadre.add(panneauniveau);
81
    }
82
  }
83

    
84
  private static class GChamp extends JPanel implements ActionListener {
85
    JComboBox afficheValeurChamp;
86
    Vue vue;
87
    Class<? extends Element> classe;
88
    String type;
89
    String champ;
90
    Element element = null;
91
    GChamp(Vue vue, Class<? extends Element> classe, String type, String champ) {
92
      super();
93
      this.vue = vue;
94
      this.classe = classe;
95
      this.type = type;
96
      this.champ = champ;
97
      Box groupe = Box.createVerticalBox();
98
      Box titre = Box.createHorizontalBox();
99
      titre.add(Box.createHorizontalGlue());
100
      JLabel label = new JLabel(champ, JLabel.CENTER);
101
 //     label.setPreferredSize(new Dimension(50, 10));
102
      titre.add(label);
103
      titre.add(Box.createHorizontalGlue());
104
      groupe.add(titre);
105
      groupe.add(Box.createVerticalStrut(2));
106
      afficheValeurChamp = new JComboBox(vue.getValeursChamp(classe, type, champ));
107
      /*
108
      * CP: JComboBox avec autocompletion. Dépend de la lib glazedlists
109
      * (http://www.glazedlists.com/), (https://github.com/glazedlists/glazedlists)
110
      */
111
      AutoCompleteSupport support = AutoCompleteSupport.install(afficheValeurChamp, GlazedLists.eventListOf(vue.getValeursChamp(classe, type, champ)));
112
      support.setCorrectsCase(false);
113
      afficheValeurChamp.setEditable(vue.isChampModifiable(classe, type, champ));
114
      afficheValeurChamp.addActionListener(this);
115
      groupe.add(afficheValeurChamp);
116
      add(groupe);
117
    }
118
    void afficher(Element elt) {
119
      this.element = elt;
120
      afficheValeurChamp.removeActionListener(this);
121
      String val = vue.getValeurChamp(elt, champ);
122
      if (val.isEmpty()) afficheValeurChamp.setSelectedIndex(-1);
123
      else afficheValeurChamp.setSelectedItem(val);
124
      afficheValeurChamp.addActionListener(this);
125
    }
126
    public void actionPerformed(ActionEvent evt) {
127
      try {
128
        if (!afficheValeurChamp.isEditable()) {
129
          JOptionPane.showMessageDialog(getTopLevelAncestor(), "Champ non modifiable pour ce type d'éléments",
130
                  "Modification impossible", JOptionPane.ERROR_MESSAGE);
131
          afficher(element);
132
          return;
133
        }
134
        String newval = ((String) afficheValeurChamp.getSelectedItem());
135
        if (newval == null) newval = "";
136
        else newval = newval.trim();
137
        
138
        if (!vue.setValeurChamp(element, champ, newval)) {
139
          JOptionPane.showMessageDialog(getTopLevelAncestor(), "Champ non défini pour cet élément",
140
                  "Saisie de valeur de champ impossible", JOptionPane.ERROR_MESSAGE);
141
          afficher(element);
142
        }
143
      } catch (Throwable ex) {
144
        GMessages.erreurFatale(ex);
145
      }
146
    }
147
  }
148
}