Statistiques
| Révision :

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

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

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