Statistiques
| Révision :

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

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

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

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