Source code: jaxe/elements/JEFichier.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.JaxeDocument;
16 import jaxe.JaxeElement;
17 import jaxe.JaxeResourceBundle;
18
19 import java.io.*;
20 import java.util.ArrayList;
21 import java.util.Hashtable;
22 import java.awt.*;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import javax.swing.BorderFactory;
26 import javax.swing.ImageIcon;
27 import javax.swing.JLabel;
28 import javax.swing.JDialog;
29 import javax.swing.JFrame;
30 import javax.swing.JOptionPane;
31 import javax.swing.JTextField;
32 import javax.swing.text.JTextComponent;
33 import javax.swing.text.Position;
34 import javax.swing.text.BadLocationException;
35 import javax.swing.text.Style;
36 import javax.swing.text.StyleConstants;
37
38 import org.w3c.dom.*;
39
40 import com.keypoint.PngEncoder;
41
42 /**
43 * Fichier d'image. L'image est affichée dans le texte si elle est trouvée, sinon un message d'erreur
44 * est affiché dans le texte à la place de l'image.
45 * Type d'élément Jaxe: 'fichier'
46 * paramètre: srcAtt: le nom de l'attribut donnant le nom du fichier
47 */
48 public class JEFichier extends JaxeElement {
49
50 static String newline = "\n";
51 public final static String defaultSrcAttr = "nom";
52 public String srcAttr = defaultSrcAttr;
53 JLabel label = null;
54 float alignementY = 1;
55
56 public JEFichier(JaxeDocument doc) {
57 this.doc = doc;
58 }
59
60 public void init(Position pos, Node noeud) {
61 Element el = (Element)noeud;
62
63 Element defbalise = doc.cfg.getBaliseDef(el.getTagName());
64 srcAttr = defbalise.getAttribute("param");
65 if (srcAttr.equals(""))
66 srcAttr = defaultSrcAttr;
67 srcAttr = doc.cfg.getParamFromDefinition(defbalise, "srcAtt", srcAttr);
68
69 File fimg;
70 String nomf = el.getAttribute(srcAttr);
71 if (doc.fsave == null)
72 fimg = new File(nomf);
73 else {
74 fimg = new File(doc.fsave.getParent() + File.separatorChar + nomf);
75 if (!fimg.exists() && nomf.startsWith("symboles"))
76 // essai à partir du répertoire de Jaxe, pour les symboles
77 fimg = new File(nomf);
78 }
79 if (fimg.exists() && fimg.isFile()) {
80 ImageIcon icon = new ImageIcon(fimg.getPath());
81 if (icon.getImageLoadStatus() == MediaTracker.ABORTED ||
82 icon.getImageLoadStatus() == MediaTracker.ERRORED) {
83 label = new JLabel(getString("erreur.AffichageImage") + ": " + nomf);
84 label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
85 } else
86 label = new JLabel(icon);
87 } else {
88 label = new JLabel(getString("erreur.FichierNonTrouve") + ": " + nomf);
89 label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
90 }
91 label.setAlignmentY(alignementY);
92
93 label.addMouseListener(new JEFichierMouseListener(this, doc.jframe));
94 Position newpos = insertComponent(pos, label);
95
96 creerEnfants(newpos);
97 }
98
99 public Element nouvelElement(Element defbalise) {
100 String nombalise = doc.cfg.nomBalise(defbalise);
101 Element newel = nouvelElementDOM(doc, nombalise);
102
103 String srcAttr;
104 srcAttr = defbalise.getAttribute("param");
105 if (srcAttr.equals(""))
106 srcAttr = defaultSrcAttr;
107 srcAttr = doc.cfg.getParamFromDefinition(defbalise, "srcAtt", srcAttr);
108
109 DialogueFichier dlg = new DialogueFichier(doc.jframe, doc,
110 JaxeResourceBundle.getRB().getString("zone.NouvelleBalise") + " " + nombalise, defbalise, newel, srcAttr);
111 if (!dlg.afficher())
112 return null;
113 try {
114 dlg.enregistrerReponses();
115 } catch (Exception ex) {
116 System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
117 return(null);
118 }
119
120 if (doc.fsave != null) {
121 File f = new File(doc.fsave.getParent() + File.separatorChar + newel.getAttribute(srcAttr));
122 if (!f.exists()) {
123 JOptionPane.showMessageDialog(doc.jframe,
124 JaxeResourceBundle.getRB().getString("erreur.FichierNonTrouve"),
125 JaxeResourceBundle.getRB().getString("zone.NouvelleBalise") + " " + nombalise,
126 JOptionPane.ERROR_MESSAGE);
127 //return(null);
128 }
129 }
130 return(newel);
131 }
132
133 public void afficherDialogue(JFrame jframe) {
134 Element el = (Element)noeud;
135
136 Element defbalise = doc.cfg.getBaliseDef(el.getTagName());
137 ArrayList latt = doc.cfg.listeAttributs(defbalise);
138 if (latt != null && latt.size() > 0) {
139 DialogueFichier dlg = new DialogueFichier(doc.jframe, doc,
140 "Fichier", defbalise, el, srcAttr);
141 if (!dlg.afficher())
142 return;
143 dlg.enregistrerReponses();
144
145 majAffichage();
146 }
147 }
148
149 public void majAffichage() {
150 File fimg;
151 Element el = (Element)noeud;
152 if (doc.fsave == null)
153 fimg = new File(el.getAttribute(srcAttr));
154 else
155 fimg = new File(doc.fsave.getParent() + File.separatorChar +
156 el.getAttribute(srcAttr));
157 if (fimg.exists()) {
158 if ((ImageIcon)label.getIcon() != null) {
159 ((ImageIcon)label.getIcon()).getImage().flush();
160 try {
161 // workaround bug 4725530 (JEditorPane: Unintended caching of images)
162 // for the HTMLFrame (which refers to images with a URL)
163 Toolkit.getDefaultToolkit().getImage(fimg.toURL()).flush();
164 } catch (Exception ex) {
165 ex.printStackTrace();
166 }
167 }
168 ImageIcon icon = new ImageIcon(fimg.getPath());
169 label.setIcon(icon);
170 label.setText(null);
171 label.setBorder(null);
172 } else {
173 label.setText(getString("erreur.FichierNonTrouve") + ": " + el.getAttribute(srcAttr));
174 label.setIcon(null);
175 label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
176 }
177 doc.imageChanged(label);
178 }
179
180 public static void collerImage(Image img, JaxeDocument doc, Position pos, Element defbalise) {
181 if (doc.fsave == null)
182 return; // on pourrait afficher un message d'erreur
183 File imgFile = null;
184 String baseNom = "coller";
185 String nouveauNom = null;
186 File dossier = new File(doc.fsave.getParent() + File.separator + "images-collees");
187 if (!dossier.exists())
188 if (!dossier.mkdir()) {
189 System.err.println("Erreur à la création du dossier des images collées");
190 return;
191 }
192 int i=1;
193 while (imgFile == null || imgFile.exists()) {
194 nouveauNom = baseNom + i + ".png";
195 imgFile = new File(dossier.getPath() + File.separator + nouveauNom);
196 i++;
197 }
198 PngEncoder png = new PngEncoder(img, PngEncoder.ENCODE_ALPHA, PngEncoder.FILTER_NONE, 9);
199 try {
200 FileOutputStream out = new FileOutputStream(imgFile);
201 byte[] pngbytes = png.pngEncode();
202 if (pngbytes == null)
203 throw new IOException("PNG encoding");
204 else
205 out.write(pngbytes);
206 out.flush();
207 out.close();
208 } catch (IOException ex) {
209 System.err.println("IOException: " + ex.getMessage());
210 JOptionPane.showMessageDialog(doc.jframe, JaxeResourceBundle.getRB().getString("erreur.Enregistrement") + ": " +
211 ex.getMessage(), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE);
212 return;
213 }
214
215 String nombalise = doc.cfg.nomBalise(defbalise);
216 JEFichier newje = new JEFichier(doc);
217 Element newel = doc.DOMdoc.createElement(nombalise);
218 String srcAttr;
219 srcAttr = defbalise.getAttribute("param");
220 if (srcAttr.equals(""))
221 srcAttr = defaultSrcAttr;
222 srcAttr = doc.cfg.getParamFromDefinition(defbalise, "srcAtt", srcAttr);
223 newel.setAttribute(srcAttr, dossier.getName() + File.separator + nouveauNom);
224 newel.setAttribute("type", "png");
225 newje.inserer(pos, newel);
226 }
227
228 public void selection(boolean select) {
229 super.selection(select);
230 label.setEnabled(!select);
231 }
232
233 class JEFichierMouseListener extends MouseAdapter {
234 JEFichier jei;
235 JFrame jframe;
236 public JEFichierMouseListener(JEFichier obj, JFrame jframe) {
237 super();
238 jei = obj;
239 this.jframe = jframe;
240 }
241 public void mouseClicked(MouseEvent e) {
242 jei.afficherDialogue(jframe);
243 }
244 }
245 }