Source code: jaxe/elements/JEZone.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.JaxeDocument;
17 import jaxe.JaxeElement;
18 import jaxe.JaxeResourceBundle;
19 import jaxe.Preferences;
20
21 import java.util.ArrayList;
22 import javax.swing.JFrame;
23 import javax.swing.text.JTextComponent;
24 import javax.swing.text.Position;
25 import javax.swing.text.BadLocationException;
26 import javax.swing.text.SimpleAttributeSet;
27 import javax.swing.text.Style;
28 import javax.swing.text.StyleConstants;
29
30 import org.w3c.dom.*;
31
32 /**
33 * Zone de texte. Le texte à l'intérieur est indenté.
34 * Type d'élément Jaxe: 'zone'
35 * paramètre: titreAtt: un attribut pouvant servir de titre
36 * paramètre: style: NORMAL | GRAS | ITALIQUE | EXPOSANT | INDICE | SOULIGNE
37 */
38 public class JEZone extends JaxeElement {
39
40 static String newline = "\n";
41 /*JButton bstart = null;
42 JButton bend = null;*/
43 MonBouton lstart = null;
44 MonBouton lend = null;
45 static final String titreAttParDefaut = "titre";
46 ArrayList attributsTitre = null;
47 boolean valide = true;
48
49 public JEZone(JaxeDocument doc) {
50 this.doc = doc;
51 }
52
53 public void init(Position pos, Node noeud) {
54 Element el = (Element)noeud;
55
56 int offsetdebut = pos.getOffset();
57
58 Element defbalise = null;
59 if (doc.cfg != null)
60 defbalise = doc.cfg.getBaliseDef(el.getTagName());
61 if (defbalise != null) {
62 attributsTitre = doc.cfg.getValeursParam(defbalise, "titreAtt");
63 String param = defbalise.getAttribute("param");
64 if (!"".equals(param))
65 attributsTitre.add(param);
66 if (attributsTitre.size() == 0)
67 attributsTitre.add(titreAttParDefaut);
68 }
69
70 String titreBstart = el.getTagName();
71 String titreBend = "< " + el.getTagName();
72 String valeurTitre = null;
73 if (attributsTitre != null)
74 for (int i=0; i<attributsTitre.size() && valeurTitre == null; i++)
75 if (!"".equals(el.getAttribute((String)attributsTitre.get(i))))
76 valeurTitre = el.getAttribute((String)attributsTitre.get(i));
77 if (valeurTitre != null) {
78 titreBstart += " '" + valeurTitre + "'";
79 titreBend += " '" + valeurTitre +"'";
80 }
81 titreBstart += " >";
82
83 lstart = new MonBouton(titreBstart, false);
84 if (el.getPrefix() != null)
85 lstart.setEnsembleCouleurs(1);
86 Position newpos = insertComponent(pos, lstart);
87
88 Style s = null;
89 if (!"true".equals(Preferences.getPref().getProperty("consIndent"))) {
90 s = doc.textPane.addStyle(null, null);
91 StyleConstants.setLeftIndent(s, (float)20.0*(indentations()+1));
92 doc.setParagraphAttributes(offsetdebut, 1, s, false);
93 }
94
95 creerEnfants(newpos);
96
97 lend = new MonBouton(titreBend, false);
98 if (el.getPrefix() != null)
99 lend.setEnsembleCouleurs(1);
100 newpos = insertComponent(newpos, lend);
101
102 if (!"true".equals(Preferences.getPref().getProperty("consIndent"))) {
103 StyleConstants.setLeftIndent(s, (float)20.0*indentations());
104 doc.setParagraphAttributes(offsetdebut, 1, s, false);
105 doc.setParagraphAttributes(newpos.getOffset()-1, 1, s, false);
106 }
107
108 if (defbalise != null && newpos.getOffset() - offsetdebut - 1 > 0) {
109 SimpleAttributeSet style = attStyle(null);
110 if (style != null)
111 doc.setCharacterAttributes(offsetdebut, newpos.getOffset() - offsetdebut - 1, style, false);
112 }
113 }
114
115 public Element nouvelElement(Element defbalise) {
116 String nombalise = doc.cfg.nomBalise(defbalise);
117 Element newel = nouvelElementDOM(doc, nombalise);
118 ArrayList latt = doc.cfg.listeAttributs(defbalise);
119 if (latt != null && latt.size() > 0) {
120 DialogueAttributs dlg = new DialogueAttributs(doc.jframe, doc,
121 JaxeResourceBundle.getRB().getString("zone.NouvelleBalise") + " " + nombalise, defbalise, newel);
122 if (!dlg.afficher())
123 return null;
124 dlg.enregistrerReponses();
125 }
126
127 Node textnode = doc.DOMdoc.createTextNode(newline+newline);
128 newel.appendChild(textnode);
129
130 return(newel);
131 }
132
133 public boolean avecIndentation() {
134 return(true);
135 }
136
137 public Position insPosition() {
138 try {
139 return(doc.createPosition(debut.getOffset() + 1 + newline.length()));
140 } catch (BadLocationException ex) {
141 System.err.println("BadLocationException: " + ex.getMessage());
142 return(null);
143 }
144 }
145
146 public void afficherDialogue(JFrame jframe) {
147 Element el = (Element)noeud;
148
149 Element defbalise = doc.cfg.getBaliseDef(el.getTagName());
150 ArrayList latt = doc.cfg.listeAttributs(defbalise);
151 if (latt != null && latt.size() > 0) {
152 DialogueAttributs dlg = new DialogueAttributs(doc.jframe, doc,
153 getString("zone.Zone") + ": " + el.getTagName(), defbalise, el);
154 if (!dlg.afficher())
155 return;
156 dlg.enregistrerReponses();
157
158 doc.textPane.miseAJourArbre();
159 }
160 majAffichage();
161 }
162
163 public void majAffichage() {
164 Element el = (Element)noeud;
165
166 String titreBstart = el.getTagName();
167 //String titreBend = "FIN " + el.getTagName();
168 String titreBend = el.getTagName();
169 String valeurTitre = null;
170 for (int i=0; i<attributsTitre.size() && valeurTitre == null; i++)
171 if (!"".equals(el.getAttribute((String)attributsTitre.get(i))))
172 valeurTitre = el.getAttribute((String)attributsTitre.get(i));
173 if (valeurTitre != null) {
174 titreBstart += " '" + valeurTitre + "'";
175 titreBend += " '" + valeurTitre +"'";
176 }
177 titreBstart += " >";
178 lstart.setValidite(valide);
179 lend.setValidite(valide);
180 titreBend = "< " + titreBend;
181 lstart.setText(titreBstart);
182 lend.setText(titreBend);
183 doc.imageChanged(lstart);
184 doc.imageChanged(lend);
185 }
186
187 public void majValidite() {
188 boolean valide2 = doc.cfg.elementValide(this, false, null);
189 if (valide2 != valide) {
190 valide = valide2;
191 majAffichage();
192 }
193 }
194
195 /*class MyActionListener implements ActionListener {
196 JEZone jei;
197 JFrame jframe;
198 public MyActionListener(JEZone obj, JFrame jframe) {
199 super();
200 jei = obj;
201 this.jframe = jframe;
202 }
203 public void actionPerformed(ActionEvent e) {
204 jei.afficherDialogue(jframe);
205 }
206 }*/
207
208 }