Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jaxe/elements/JETableTexte.java


1   /*
2   Jaxe - Editeur XML en Java
3   
4   Copyright (C) 2002 Observatoire de Paris-Meudon
5   
6   Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier conformément aux dispositions de la Licence Publique Générale GNU, telle que publiée par la Free Software Foundation ; version 2 de la licence, ou encore (à votre choix) toute version ultérieure.
7   
8   Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; sans même la garantie implicite de COMMERCIALISATION ou D'ADAPTATION A UN OBJET PARTICULIER. Pour plus de détail, voir la Licence Publique Générale GNU .
9   
10  Vous devez avoir reçu un exemplaire de la Licence Publique Générale GNU en même temps que ce programme ; si ce n'est pas le cas, écrivez à la Free Software Foundation Inc., 675 Mass Ave, Cambridge, MA 02139, Etats-Unis.
11  */
12  
13  package jaxe.elements;
14  
15  import jaxe.DialogueAttributs;
16  import jaxe.DialogueChamps;
17  import jaxe.JaxeDocument;
18  import jaxe.JaxeElement;
19  import jaxe.JaxeResourceBundle;
20  
21  import java.awt.*;
22  import java.awt.event.*;
23  import java.util.ArrayList;
24  import javax.swing.*;
25  import javax.swing.text.AbstractDocument;
26  import javax.swing.text.BadLocationException;
27  import javax.swing.text.JTextComponent;
28  import javax.swing.text.Position;
29  import javax.swing.text.SimpleAttributeSet;
30  import javax.swing.text.Style;
31  import javax.swing.text.StyleConstants;
32  
33  import org.w3c.dom.*;
34  
35  
36  /**
37   * Table dans le texte, permettant l'insertion de sous-éléments dans les cellules.
38   * Type d'élément Jaxe: 'tabletexte'
39   * paramètre: trTag: un attribut correspondant à une ligne de tableau
40   * paramètre: tdTag: un attribut correspondant à une cellule de tableau
41   * paramètre: thTag: un attribut correspondant à une cellule d'entête de tableau
42   */
43  public class JETableTexte extends JaxeElement implements ActionListener {
44  
45      static String newline = "\n";
46      String TRtag = "tr";
47      String TDtag = "td";
48      String THtag = null;
49      JTable jtable = null;
50      boolean avecEntete;
51  
52      public JETableTexte(JaxeDocument doc) {
53          this.doc = doc;
54      }
55      
56      protected void obtenirTags(Element defbalise) {
57          if (defbalise != null) {
58              String paramatt = defbalise.getAttribute("param");
59              if (paramatt != null && !"".equals(paramatt) && paramatt.indexOf('/') != -1) {
60                  int inds1 = paramatt.indexOf('/');
61                  TRtag = paramatt.substring(0, inds1);
62                  String param2 = paramatt.substring(inds1+1);
63                  int inds2 = param2.indexOf('/');
64                  if (inds2 == -1) {
65                      TDtag = param2;
66                      THtag = null;
67                  } else {
68                      TDtag = param2.substring(0, inds2);
69                      THtag = param2.substring(inds2+1);
70                  }
71              } else {
72                  TRtag = doc.cfg.getParamFromDefinition(defbalise, "trTag", TRtag);
73                  TDtag = doc.cfg.getParamFromDefinition(defbalise, "tdTag", TDtag);
74                  THtag = doc.cfg.getParamFromDefinition(defbalise, "thTag", THtag);
75              }
76          }
77      }
78      
79      public void init(Position pos, Node noeud) {
80          Element el = (Element)noeud;
81          Element defbalise = doc.cfg.getBaliseNomType(el.getTagName(), "tabletexte");
82          obtenirTags(defbalise);
83          
84          Style s = doc.textPane.addStyle(null, null);
85          
86          JPanel pboutons = new JPanel();
87          pboutons.setLayout(new FlowLayout(FlowLayout.LEFT));
88          JButton bmodtable = new JButton(getString("table.Table"));
89          bmodtable.addActionListener(this);
90          bmodtable.setActionCommand("modtable");
91          bmodtable.setFont(bmodtable.getFont().deriveFont((float)9));
92          bmodtable.putClientProperty("JButton.buttonType", "toolbar"); // pour MacOS X
93          pboutons.add(bmodtable);
94          pboutons.add(Box.createRigidArea(new Dimension(5,0)));
95          JButton bmodligne = new JButton(getString("table.Ligne"));
96          bmodligne.addActionListener(this);
97          bmodligne.setActionCommand("modligne");
98          bmodligne.setFont(bmodligne.getFont().deriveFont((float)9));
99          bmodligne.putClientProperty("JButton.buttonType", "toolbar");
100         pboutons.add(bmodligne);
101         JButton bajligne = new JButton("+");
102         bajligne.addActionListener(this);
103         bajligne.setActionCommand("ajligne");
104         bajligne.setFont(bajligne.getFont().deriveFont((float)9));
105         bajligne.putClientProperty("JButton.buttonType", "toolbar");
106         pboutons.add(bajligne);
107         JButton bsupligne = new JButton("-");
108         bsupligne.addActionListener(this);
109         bsupligne.setActionCommand("supligne");
110         bsupligne.setFont(bsupligne.getFont().deriveFont((float)9));
111         bsupligne.putClientProperty("JButton.buttonType", "toolbar");
112         pboutons.add(bsupligne);
113         pboutons.add(Box.createRigidArea(new Dimension(5,0)));
114         JLabel lcol = new JLabel(getString("table.Colonne"));
115         lcol.setFont(lcol.getFont().deriveFont((float)9));
116         pboutons.add(lcol);
117         JButton bajcolonne = new JButton("+");
118         bajcolonne.addActionListener(this);
119         bajcolonne.setActionCommand("ajcolonne");
120         bajcolonne.setFont(bajcolonne.getFont().deriveFont((float)9));
121         bajcolonne.putClientProperty("JButton.buttonType", "toolbar");
122         pboutons.add(bajcolonne);
123         JButton bsupcolonne = new JButton("-");
124         bsupcolonne.addActionListener(this);
125         bsupcolonne.setActionCommand("supcolonne");
126         bsupcolonne.setFont(bsupcolonne.getFont().deriveFont((float)9));
127         bsupcolonne.putClientProperty("JButton.buttonType", "toolbar");
128         pboutons.add(bsupcolonne);
129         pboutons.add(Box.createRigidArea(new Dimension(5,0)));
130         if (THtag != null) {
131             Element tr = trouverLigne(0);
132             Element tdh = null;
133             if (tr != null)
134               tdh = trouverCellule(tr, 0);
135             avecEntete = (tdh != null && THtag.equals(tdh.getNodeName()));
136             JCheckBox bcheck = new JCheckBox(getString("table.Entete"), avecEntete);
137             bcheck.addActionListener(this);
138             bcheck.setActionCommand("entête");
139             bcheck.setFont(bcheck.getFont().deriveFont((float)9));
140             pboutons.add(bcheck);
141         } else
142             avecEntete = false;
143 
144         insertComponent(pos, pboutons);
145         
146         // les \n sont pour la déco, il ne faut pas qu'ils s'ajoutent au DOM
147         Node frero;
148         for (Node n=el.getFirstChild(); n != null; n=frero) {
149             frero = n.getNextSibling();
150             if (n.getNodeType() == Node.TEXT_NODE)
151                 el.removeChild(n);
152         }
153         insertText(pos, "\n");
154         insertText(pos, "\n");
155         
156         int offsetdebut = pos.getOffset();
157         
158         JaxeDocument.SwingElementSpec tableSpec = preparerSpecTable(el, offsetdebut);
159         
160         javax.swing.text.Element elSwing = doc.insereSpec(tableSpec, offsetdebut);
161         creerElementsTableJaxe(el, elSwing);
162         
163         // correction des indentations
164         s = doc.textPane.addStyle(null, null);
165         StyleConstants.setLeftIndent(s, (float)0);
166         doc.setParagraphAttributes(debut.getOffset(), fin.getOffset() - debut.getOffset(), s, false);
167     }
168     
169     protected JaxeDocument.SwingElementSpec preparerSpecTable(Element el, int offset) {
170         JaxeDocument.SwingElementSpec tableSpec = doc.prepareSpec("table");
171         int offc = offset;
172         int nligne = 0;
173         int nbcells1 = 0;
174         int nbcells;
175         for (Node n=el.getFirstChild(); n != null; n=n.getNextSibling()) {
176             if (n.getNodeType() == Node.ELEMENT_NODE) {
177                 String bal = n.getNodeName();
178                 if (bal.equals(TRtag)) {
179                     JaxeDocument.SwingElementSpec ligneSpec = doc.prepareSpec("tr");
180                     doc.sousSpec(tableSpec, ligneSpec);
181                     nbcells = 0;
182                     for (Node n2=n.getFirstChild(); n2 != null; n2=n2.getNextSibling()) {
183                         if (n2.getNodeType() == Node.ELEMENT_NODE) {
184                             String bal2 = n2.getNodeName();
185                             if (bal2.equals(TDtag) || bal2.equals(THtag)) {
186                                 if (nligne == 0)
187                                     nbcells1++;
188                                 nbcells++;
189                                 JaxeDocument.SwingElementSpec celluleSpec = doc.prepareSpec("td");
190                                 doc.sousSpec(ligneSpec, celluleSpec);
191                                 //Object contenuCelluleSpec = doc.prepareSpec("tdd");
192                                 JaxeDocument.SwingElementSpec contenuCelluleSpec = doc.prepareSpec(AbstractDocument.ParagraphElementName);
193                                 doc.sousSpec(celluleSpec, contenuCelluleSpec);
194                                 String sval = "\n";
195                                 JaxeDocument.SwingElementSpec contenuSpec = doc.prepareSpec("content", offc, sval);
196                                 offc += sval.length();
197                                 doc.sousSpec(contenuCelluleSpec, contenuSpec);
198                             }
199                         }
200                     }
201                     if (nbcells == nbcells1)
202                         nligne++;
203                     else
204                         System.err.println("Erreur: nombre de cellules incorrect dans la ligne");
205                 }
206             }
207             // on ignore le reste
208         }
209         return(tableSpec);
210     }
211     
212     protected JaxeDocument.SwingElementSpec preparerSpecLigne(Element el, int offset) {
213         int offc = offset;
214         int nligne = 0;
215         int nbcells;
216         JaxeDocument.SwingElementSpec ligneSpec = doc.prepareSpec("tr");
217         for (Node n2=el.getFirstChild(); n2 != null; n2=n2.getNextSibling()) {
218             if (n2.getNodeType() == Node.ELEMENT_NODE) {
219                 String bal2 = n2.getNodeName();
220                 if (bal2.equals(TDtag) || bal2.equals(THtag)) {
221                     JaxeDocument.SwingElementSpec celluleSpec = doc.prepareSpec("td");
222                     doc.sousSpec(ligneSpec, celluleSpec);
223                     JaxeDocument.SwingElementSpec contenuCelluleSpec = doc.prepareSpec(AbstractDocument.ParagraphElementName);
224                     doc.sousSpec(celluleSpec, contenuCelluleSpec);
225                     String sval = "\n";
226                     JaxeDocument.SwingElementSpec contenuSpec = doc.prepareSpec("content", offc, sval);
227                     offc += sval.length();
228                     doc.sousSpec(contenuCelluleSpec, contenuSpec);
229                 }
230             }
231         }
232         return(ligneSpec);
233     }
234     
235     protected void creerElementsTableJaxe(Element elDOM, javax.swing.text.Element elSwing) {
236         javax.swing.text.Element trSwing = null;
237         int itrSwing = 0;
238         Position dernierePos = fin;
239         for (Node n=elDOM.getFirstChild(); n != null; n=n.getNextSibling()) {
240             if (n.getNodeType() == Node.ELEMENT_NODE) {
241                 String bal = n.getNodeName();
242                 if (bal.equals(TRtag)) {
243                     if (itrSwing >= elSwing.getElementCount())
244                         System.err.println("JETableTexte: Erreur: arbre swing != arbre DOM (ligne)");
245                     trSwing = elSwing.getElement(itrSwing++);
246                     dernierePos = creerElementsLigneJaxe((Element)n, trSwing);
247                 }
248             }
249         }
250         fin = dernierePos;
251     }
252     
253     protected Position creerElementsLigneJaxe(Element trDOM, javax.swing.text.Element trSwing) {
254         javax.swing.text.Element tdSwing = null;
255         Position dernierePos = null;
256         JESwing trje = new JESwing(doc, trDOM, trSwing);
257         trje.setEffacementAutorise(false);
258         trje.creer(trje.debut, trDOM);
259         int offsetdebutLigne = trje.debut.getOffset();
260         int itdSwing = 0;
261         for (Node n2=trDOM.getFirstChild(); n2 != null; n2=n2.getNextSibling()) {
262             if (n2.getNodeType() == Node.ELEMENT_NODE) {
263                 String bal2 = n2.getNodeName();
264                 if (bal2.equals(TDtag) || bal2.equals(THtag)) {
265                     if (itdSwing >= trSwing.getElementCount())
266                         System.err.println("JETableTexte: Erreur: arbre swing != arbre DOM (cellule)");
267                     tdSwing = trSwing.getElement(itdSwing++);
268                     JESwing tdje = new JESwing(doc, (Element)n2, tdSwing);
269                     tdje.setEffacementAutorise(false);
270                     int offsetdebut = tdje.debut.getOffset();
271                     tdje.creer(tdje.debut, n2);
272                     tdje.creerEnfants(tdje.debut);
273                     tdje.fin = tdje.debut;
274                     try {
275                         tdje.debut = doc.createPosition(offsetdebut);
276                     } catch (BadLocationException ex) {
277                         ex.printStackTrace();
278                     }
279                     dernierePos = tdje.fin;
280                     if (dernierePos.getOffset() - offsetdebut > 0) {
281                         SimpleAttributeSet style = tdje.attStyle(null);
282                         if (style != null)
283                             doc.setCharacterAttributes(offsetdebut,
284                                 dernierePos.getOffset() - offsetdebut, style, false);
285                     }
286                 }
287             }
288         }
289         try {
290             trje.debut = doc.createPosition(offsetdebutLigne);
291         } catch (BadLocationException ex) {
292             ex.printStackTrace();
293         }
294         trje.fin = dernierePos;
295         return(dernierePos);
296     }
297     
298     public Element nouvelElement(Element defbalise) {
299         String nombalise = doc.cfg.nomBalise(defbalise);
300         String[] titres = {JaxeResourceBundle.getRB().getString("table.NbLignes"),
301             JaxeResourceBundle.getRB().getString("table.NbColonnes")};
302         JTextComponent[] champs = new JTextComponent[2];
303         champs[0] = new JTextField(10);
304         champs[1] = new JTextField(10);
305         DialogueChamps dlg = new DialogueChamps(doc.jframe,
306             JaxeResourceBundle.getRB().getString("table.NouvelleBalise"), titres, champs);
307         if (!dlg.afficher())
308             return null;
309         String slignes = champs[0].getText();
310         String scolonnes = champs[1].getText();
311         
312         int nlignes, ncolonnes;
313         try {
314             nlignes = (Integer.valueOf(slignes)).intValue();
315             ncolonnes = (Integer.valueOf(scolonnes)).intValue();
316         } catch (NumberFormatException ex) {
317             JOptionPane.showMessageDialog(doc.jframe, JaxeResourceBundle.getRB().getString("erreur.Conversion"),
318                 JaxeResourceBundle.getRB().getString("table.NouvelleBalise"), JOptionPane.ERROR_MESSAGE);
319             return(null);
320         }
321         if (nlignes <= 0 || ncolonnes <= 0)
322             return(null);
323         
324         obtenirTags(defbalise);
325         avecEntete = false;
326 
327         Element newel = nouvelElementDOM(doc, nombalise);
328         for (int i=0; i<nlignes; i++) {
329             Element ligneel = doc.DOMdoc.createElement(TRtag);
330             newel.appendChild(ligneel);
331             for (int j=0; j<ncolonnes; j++) {
332                 Element cellel = doc.DOMdoc.createElement(TDtag);
333                 ligneel.appendChild(cellel);
334             }
335             Node textnode = doc.DOMdoc.createTextNode(newline);
336             newel.appendChild(textnode);
337         }
338 
339         return(newel);
340     }
341     
342     public void afficherDialogue(JFrame jframe) {
343         Element el = (Element)noeud;
344 
345         Element defbalise = doc.cfg.getBaliseDef(el.getTagName());
346         ArrayList latt = doc.cfg.listeAttributs(defbalise);
347         if (latt != null && latt.size() > 0) {
348             DialogueAttributs dlg = new DialogueAttributs(doc.jframe, doc,
349                 el.getTagName(), defbalise, el);
350             if (!dlg.afficher())
351                 return;
352             dlg.enregistrerReponses();
353         }
354     }
355     
356     protected Element trouverLigne(int lsel) {
357         Element el = (Element)noeud;
358         int l = 0;
359         for (Node n=el.getFirstChild(); n != null; n=n.getNextSibling()) {
360             if (n.getNodeType() == Node.ELEMENT_NODE) {
361                 String bal = n.getNodeName();
362                 if (bal.equals(TRtag)) {
363                     if (l == lsel) {
364                         Element tr = (Element)n;
365                         return(tr);
366                     }
367                     l++;
368                 }
369             }
370         }
371         return(null);
372     }
373     
374     protected Element trouverCellule(Element tr, int csel) {
375         int c = 0;
376         for (Node n=tr.getFirstChild(); n != null; n=n.getNextSibling()) {
377             if (n.getNodeType() == Node.ELEMENT_NODE) {
378                 String bal = n.getNodeName();
379                 if (bal.equals(TDtag) || bal.equals(THtag)) {
380                     if (c == csel) {
381                         Element tdh = (Element)n;
382                         return(tdh);
383                     }
384                     c++;
385                 }
386             }
387         }
388         return(null);
389     }
390     
391     public int nbLignes() {
392         int nb = 0;
393         Element el = (Element)noeud;
394         for (Node n=el.getFirstChild(); n != null; n=n.getNextSibling())
395             if (n.getNodeType() == Node.ELEMENT_NODE)
396                 if (n.getNodeName().equals(TRtag))
397                     nb++;
398         return(nb);
399     }
400     
401     public int nbColonnes() {
402         Element tr = trouverLigne(0);
403         int nb = 0;
404         for (Node n=tr.getFirstChild(); n != null; n=n.getNextSibling())
405             if (n.getNodeType() == Node.ELEMENT_NODE) {
406                 String bal = n.getNodeName();
407                 if (bal.equals(TDtag) || bal.equals(THtag))
408                     nb++;
409             }
410         return(nb);
411     }
412     
413     protected void rechercherComposants(JaxeElement je, ArrayList al) {
414         al.addAll(je.getComponents());
415         for (Node n=je.noeud.getFirstChild(); n != null; n=n.getNextSibling()) {
416             if (n.getNodeType() == Node.ELEMENT_NODE || n.getNodeType() == Node.TEXT_NODE)  {
417                 JaxeElement je2 = doc.getElementForNode(n);
418                 if (je2 != null) {
419                     rechercherComposants(je2, al);
420                 }
421             }
422         }
423     }
424     
425     protected ArrayList recupererComposants() {
426         int offsetdebut = debut.getOffset()+3;
427         ArrayList tel = elementsDans(offsetdebut, fin.getOffset());
428         ArrayList allcomp = new ArrayList();
429         for (int i=0; i<tel.size(); i++)
430             rechercherComposants((JaxeElement)tel.get(i), allcomp);
431         return(allcomp);
432     }
433     
434     protected void effacerComposants(ArrayList allcomp) {
435         // on utiliser parentContainer.remove pour retirer les composants, sinon un bug de Java 1.4 les affiche
436         for (int i=0; i<allcomp.size(); i++) {
437             Container parentContainer = ((JComponent)allcomp.get(i)).getParent();
438             if (parentContainer != null)
439                 parentContainer.remove((JComponent)allcomp.get(i));
440         }
441     }
442     
443     public void recreerTable(ArrayList allcomp) {
444         doc.textPane.debutIgnorerEdition();
445         int offsetdebut = debut.getOffset()+3;
446         try {
447             doc.remove(offsetdebut, fin.getOffset()-offsetdebut+1);
448         } catch (BadLocationException ex) {
449             ex.printStackTrace();
450         }
451         effacerComposants(allcomp);
452         
453         Element el = (Element)noeud;
454         JaxeDocument.SwingElementSpec tableSpec = preparerSpecTable(el, offsetdebut);
455         javax.swing.text.Element elSwing = doc.insereSpec(tableSpec, offsetdebut);
456         creerElementsTableJaxe(el, elSwing);
457         doc.textPane.finIgnorerEdition();
458     }
459     
460     public void ajligne() {
461         int pos = doc.textPane.getCaretPosition();
462         JaxeElement jetrsel = lignesel();
463         Element trsel = null;
464         Element el = (Element)noeud;
465         if (jetrsel != null) {
466             trsel = (Element)jetrsel.noeud;
467             Node next = trsel.getNextSibling();
468             if (next == null)
469                 trsel = null;
470             else if (TRtag.equals(next.getNodeName()))
471                 trsel = (Element)next;
472             else {
473                 next = next.getNextSibling();
474                 if (next != null && TRtag.equals(next.getNodeName()))
475                     trsel = (Element)next;
476                 else
477                     trsel = null;
478             }
479         }
480         Element tr = doc.DOMdoc.createElement(TRtag);
481         Node textnode = doc.DOMdoc.createTextNode(newline);
482         for (int i=0; i<nbColonnes(); i++) {
483             Element td = doc.DOMdoc.createElement(TDtag);
484             tr.appendChild(td);
485         }
486         if (trsel == null) {
487             el.appendChild(tr);
488             el.appendChild(textnode);
489         } else {
490             el.insertBefore(tr, trsel);
491             el.insertBefore(textnode, trsel);
492         }
493         int offset;
494         if (trsel != null) {
495             jetrsel = doc.getElementForNode(trsel);
496             offset = jetrsel.fin.getOffset() + 1;
497         } else
498             offset = fin.getOffset() + 1;
499         /*
500         Object specLigne = preparerSpecLigne(tr, offset);
501         javax.swing.text.Element trSwing = doc.insereSpec(specLigne, offset);
502         creerElementsLigneJaxe(tr, trSwing);
503         */ // la ligne n'est pas insérée au bon endroit...
504         recreerTable(recupererComposants());
505         doc.textPane.setCaretPosition(pos);
506     }
507     
508     public void supligne() {
509         JaxeElement jetrsel = lignesel();
510         if (jetrsel == null || nbLignes() == 1)
511             return;
512         
513         ArrayList allcomp = recupererComposants();
514         
515         jetrsel.getParent().supprimerEnfantDOM(jetrsel);
516         
517         recreerTable(allcomp);
518     }
519     
520     public void ajcolonne() {
521         int csel = colonnesel();
522         Element el = (Element)noeud;
523         for (Node n=el.getFirstChild(); n != null; n=n.getNextSibling()) {
524             if (n.getNodeType() == Node.ELEMENT_NODE) {
525                 String bal = n.getNodeName();
526                 if (bal.equals(TRtag)) {
527                     Element tr = (Element)n;
528                     Element td;
529                     if (n == el.getFirstChild() && avecEntete)
530                         td = doc.DOMdoc.createElement(THtag);
531                     else
532                         td = doc.DOMdoc.createElement(TDtag);
533                     if (csel == -1) {
534                         tr.appendChild(td);
535                     } else {
536                         Element tdsel = trouverCellule(tr, csel+1);
537                         if (tdsel == null)
538                             tr.appendChild(td);
539                         else
540                             tr.insertBefore(td, tdsel);
541                     }
542                 }
543             }
544         }
545         recreerTable(recupererComposants());
546     }
547     
548     public void supcolonne() {
549         int csel = colonnesel();
550         if (csel == -1 || nbColonnes() == 1)
551             return;
552         
553         ArrayList allcomp = recupererComposants();
554         
555         Element el = (Element)noeud;
556         for (Node n=el.getFirstChild(); n != null; n=n.getNextSibling()) {
557             if (n.getNodeType() == Node.ELEMENT_NODE) {
558                 String bal = n.getNodeName();
559                 if (bal.equals(TRtag)) {
560                     Element td = trouverCellule((Element)n, csel);
561                     if (td != null)
562                         n.removeChild(td);
563                 }
564             }
565         }
566         recreerTable(allcomp);
567     }
568     
569     public void modifEntete() {
570         avecEntete = !avecEntete;
571         Element tr1 = trouverLigne(0);
572         if (tr1 == null)
573             return;
574         ArrayList allcomp = recupererComposants();
575         if (avecEntete) {
576             for (Node n = tr1.getFirstChild(); n != null; n=n.getNextSibling()) {
577                 if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(TDtag)) {
578                     Element td = (Element)n;
579                     Element th = doc.DOMdoc.createElement(THtag);
580                     Node frero = null;
581                     for (Node n2 = td.getFirstChild(); n2 != null; n2=frero) {
582                         frero = n2.getNextSibling();
583                         th.appendChild(n2);
584                     }
585                     tr1.replaceChild(th, td);
586                     n = th;
587                 }
588             }
589         } else {
590             for (Node n = tr1.getFirstChild(); n != null; n=n.getNextSibling()) {
591                 if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(THtag)) {
592                     Element th = (Element)n;
593                     Element td = doc.DOMdoc.createElement(TDtag);
594                     Node frero = null;
595                     for (Node n2 = th.getFirstChild(); n2 != null; n2=frero) {
596                         frero = n2.getNextSibling();
597                         td.appendChild(n2);
598                     }
599                     tr1.replaceChild(td, th);
600                     n = td;
601                 }
602             }
603         }
604         recreerTable(allcomp);
605     }
606     
607     private JaxeElement lignesel() {
608         // si on utilise elementA on risque de tomber sur des éléments d'une sous-table
609         int pos = doc.textPane.getCaretPosition();
610         Element el = (Element)noeud;
611         for (Node n=el.getFirstChild(); n != null; n=n.getNextSibling()) {
612             if (n.getNodeType() == Node.ELEMENT_NODE) {
613                 String bal = n.getNodeName();
614                 if (bal.equals(TRtag)) {
615                     JaxeElement je = doc.getElementForNode(n);
616                     if (je.debut.getOffset() <= pos && je.fin.getOffset() >= pos) {
617                         return(je);
618                     }
619                 }
620             }
621         }
622         return(null);
623     }
624     
625     private int colonnesel() {
626         // si on utilise elementA on risque de tomber sur des éléments d'une sous-table
627         int pos = doc.textPane.getCaretPosition();
628         JaxeElement lsel = lignesel();
629         if (lsel == null)
630             return(-1);
631         Element tr = (Element)lsel.noeud;
632         int c = 0;
633         for (Node n=tr.getFirstChild(); n != null; n=n.getNextSibling()) {
634             if (n.getNodeType() == Node.ELEMENT_NODE) {
635                 String bal = n.getNodeName();
636                 if (bal.equals(TDtag) || bal.equals(THtag)) {
637                     JaxeElement je = doc.getElementForNode(n);
638                     if (je.debut.getOffset() <= pos && je.fin.getOffset() >= pos) {
639                         return(c);
640                     }
641                     c++;
642                 }
643             }
644         }
645         return(-1);
646     }
647     
648     public void modligne() {
649         JaxeElement jsel = lignesel();
650         if (jsel == null)
651             return;
652         jsel.afficherDialogue(doc.jframe);
653     }
654     
655     public void effacer() {
656         effacerComposants(recupererComposants());
657         super.effacer();
658     }
659     
660     public void actionPerformed(ActionEvent e) {
661         String cmd = e.getActionCommand();
662         if ("ajligne".equals(cmd))
663             ajligne();
664         else if ("ajcolonne".equals(cmd))
665             ajcolonne();
666         else if ("supligne".equals(cmd))
667             supligne();
668         else if ("supcolonne".equals(cmd))
669             supcolonne();
670         else if ("entête".equals(cmd))
671             modifEntete();
672         else if ("modtable".equals(cmd))
673             afficherDialogue(doc.jframe);
674         else if ("modligne".equals(cmd))
675             modligne();
676     }
677 
678 }