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

Quick Search    Search Deep

Source code: jaxe/elements/JEDivision.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.Jaxe;
17  import jaxe.JaxeDocument;
18  import jaxe.JaxeElement;
19  import jaxe.Preferences;
20  
21  import java.awt.*;
22  import java.util.ArrayList;
23  import javax.swing.*;
24  import javax.swing.text.Position;
25  import javax.swing.text.BadLocationException;
26  import javax.swing.text.Style;
27  import javax.swing.text.StyleConstants;
28  
29  import org.w3c.dom.*;
30  
31  /**
32   * Zone de division. Les balises sont affichées comme des bandes prenant toute la largeur de la page,
33   * et le texte à l'intérieur est indenté.
34   * Type d'élément Jaxe: 'division'
35   * paramètre: titreAtt: un attribut pouvant servir de titre
36   */
37  public class JEDivision extends JaxeElement {
38  
39      static String newline = "\n";
40      static final String titreAttParDefaut = "titre";
41      ArrayList attributsTitre = null;
42      MonBouton lstart = null;
43      MonBouton lend = null;
44      boolean valide = true;
45  
46      public JEDivision(JaxeDocument doc) {
47          this.doc = doc;
48      }
49      
50      public void init(Position pos, Node noeud) {
51          Element el = (Element)noeud;
52  
53          Element defbalise = null;
54          if (doc.cfg != null)
55              defbalise = doc.cfg.getBaliseDef(el.getTagName());
56          if (defbalise != null) {
57              attributsTitre = doc.cfg.getValeursParam(defbalise, "titreAtt");
58              String param = defbalise.getAttribute("param");
59              if (!"".equals(param))
60                  attributsTitre.add(param);
61              if (attributsTitre.size() == 0)
62                  attributsTitre.add(titreAttParDefaut);
63          }
64          
65          //debut = null;
66          int offsetdebut = pos.getOffset();
67          //fin = null;
68          //try {
69              //Position newpos = doc.createPosition(pos.getOffset());
70              
71              String titreBstart = el.getTagName();
72              String titreBend = "< " + el.getTagName();
73              String valeurTitre = null;
74              if (attributsTitre != null)
75                  for (int i=0; i<attributsTitre.size() && valeurTitre == null; i++)
76                      if (!"".equals(el.getAttribute((String)attributsTitre.get(i))))
77                          valeurTitre = el.getAttribute((String)attributsTitre.get(i));
78              if (valeurTitre != null) {
79                  titreBstart += " '" + valeurTitre + "'";
80                  titreBend += " '" + valeurTitre + "'";
81              }
82              titreBstart += " >";
83  
84              lstart = new MonBouton(titreBstart, true);
85              if (el.getPrefix() != null)
86                  lstart.setEnsembleCouleurs(1);
87              Position newpos = insertComponent(pos, lstart);
88              //if (newpos.getOffset() == 0) // bug fix with insertString
89              //  newpos = doc.createPosition(newpos.getOffset() + 1);
90              
91              /*
92              // on insère un \n si on ne peut pas utiliser celui de l'élément suivant pour changer le style
93              Node suivant = null;
94              JaxeElement jesuivant = null;
95              if (noeud.getNextSibling() != null && noeud.getNextSibling().getNodeType() == Node.TEXT_NODE) {
96                  suivant = noeud.getNextSibling();
97                  if (suivant.getNodeValue() != null && suivant.getNodeValue().startsWith(newline)) {
98                      jesuivant = doc.getElementForNode(suivant);
99                  }
100             }
101             
102             if (jesuivant == null) {
103                 if (suivant != null)
104                     suivant.setNodeValue(suivant.getNodeValue().substring(newline.length()));
105                 doc.insertString(newpos.getOffset(), newline, null);
106                 newpos = doc.createPosition(newpos.getOffset()-newline.length());
107             }
108             
109             Style s = doc.textPane.addStyle(null, null);
110             StyleConstants.setLeftIndent(s, (float)20.0*indentations());
111             doc.setParagraphAttributes(newpos.getOffset(), newline.length(), s, false);
112             
113             creerEnfants(newpos);
114             
115             //doc.insertString(newpos.getOffset(), newline, null);
116             lend = new MonBouton(titreBend);
117             lend.addMouseListener(new MyMouseListener(this, doc.jframe, false));
118             insertComponent(newpos, lend);
119             */
120             
121             /* au lieu du \n potentiellement ajouté, il vaut mieux:
122              - insérer le premier bouton
123              - le décaler vers la droite
124              - insérer les enfants
125              - ajouter le bouton de fin
126              - décaler les 2 boutons vers la gauche
127              
128              Ca marche bien avec les bons \n après les balises de division, et le programme
129              ne se plante pas s'il n'y a pas les \n (les indentations ne sont pas très jolies dans ce cas)
130             */
131             Style s = null;
132             if (!"true".equals(Preferences.getPref().getProperty("consIndent"))) {
133                 s = doc.textPane.addStyle(null, null);
134                 StyleConstants.setLeftIndent(s, (float)20.0*(indentations()+1));
135                 doc.setParagraphAttributes(offsetdebut, 1, s, false);
136             }
137             
138             creerEnfants(newpos);
139             
140             lend = new MonBouton(titreBend, true);
141             if (el.getPrefix() != null)
142                 lend.setEnsembleCouleurs(1);
143             newpos = insertComponent(newpos, lend);
144 
145             if (!"true".equals(Preferences.getPref().getProperty("consIndent"))) {
146                 StyleConstants.setLeftIndent(s, (float)20.0*indentations());
147                 doc.setParagraphAttributes(offsetdebut, 1, s, false);
148                 doc.setParagraphAttributes(newpos.getOffset()-1, 1, s, false);
149             }
150             
151             //debut = doc.createPosition(offsetdebut);
152             //fin = doc.createPosition(newpos.getOffset() - 1);
153 
154         //} catch (BadLocationException ex) {
155         //    System.err.println("BadLocationException: " + ex.getMessage());
156         //}
157     }
158     
159     public Element nouvelElement(Element defbalise) {
160         String nombalise = doc.cfg.nomBalise(defbalise);
161         Element newel = nouvelElementDOM(doc, nombalise);
162         ArrayList latt = doc.cfg.listeAttributs(defbalise);
163         if (latt != null && latt.size() > 0) {
164             DialogueAttributs dlg = new DialogueAttributs(doc.jframe, doc,
165                 "division: " + nombalise, defbalise, newel);
166             if (!dlg.afficher())
167                 return null;
168             dlg.enregistrerReponses();
169         }
170 
171         Node textnode = doc.DOMdoc.createTextNode(newline+newline);
172         newel.appendChild(textnode);
173                 
174         return(newel);
175     }
176     
177     public boolean avecIndentation() {
178         return(true);
179     }
180     
181     public Position insPosition() {
182         try {
183             return(doc.createPosition(debut.getOffset() + 1 + newline.length()));
184         } catch (BadLocationException ex) {
185             System.err.println("BadLocationException: " + ex.getMessage());
186             return(null);
187         }
188     }
189     
190     public void afficherDialogue(JFrame jframe) {
191         Element el = (Element)noeud;
192 
193         Element defbalise = doc.cfg.getBaliseDef(el.getTagName());
194         ArrayList latt = doc.cfg.listeAttributs(defbalise);
195         if (latt != null && latt.size() > 0) {
196             DialogueAttributs dlg = new DialogueAttributs(doc.jframe, doc,
197                 "division: " + el.getTagName(), defbalise, el);
198             if (!dlg.afficher())
199                 return;
200             dlg.enregistrerReponses();
201             
202             majAffichage();
203             doc.textPane.miseAJourArbre();
204         }
205     }
206     
207     public void majAffichage() {
208         Element el = (Element)noeud;
209         
210         String titreBstart = el.getTagName();
211         String titreBend = "< " + el.getTagName();
212         String valeurTitre = null;
213         for (int i=0; i<attributsTitre.size() && valeurTitre == null; i++)
214             if (!"".equals(el.getAttribute((String)attributsTitre.get(i))))
215                 valeurTitre = el.getAttribute((String)attributsTitre.get(i));
216         if (valeurTitre != null) {
217             titreBstart += " '" + valeurTitre + "'";
218             titreBend += " '" + valeurTitre + "'";
219         }
220         titreBstart += " >";
221         lstart.setValidite(valide);
222         lend.setValidite(valide);
223         lstart.setText(titreBstart);
224         lend.setText(titreBend);
225         doc.imageChanged(lstart);
226         doc.imageChanged(lend);
227     }
228     
229     public void majValidite() {
230         boolean valide2 = doc.cfg.elementValide(this, false, null);
231         if (valide2 != valide) {
232             valide = valide2;
233             majAffichage();
234         }
235     }
236     
237     public void selection(boolean select) {
238         super.selection(select);
239         lstart.selection(select);
240         lend.selection(select);
241     }
242 }