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

Quick Search    Search Deep

Source code: jaxe/Preferences.java


1   /*
2   Jaxe - Editeur XML en Java
3   
4   Copyright (C) 2003 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;
14  
15  import java.awt.*;
16  import java.awt.event.*;
17  import java.io.*;
18  import java.util.*;
19  import javax.swing.*;
20  
21  /**
22   * Préférences:
23   * 
24   *     fenetreArbre
25   *     fenetreInsertion
26   *     fenetreAttributs
27   *     navigateur
28   *     consIndent
29   *     dictionnaire
30   */
31  public class Preferences extends JDialog implements ActionListener {
32  
33      private static File fpref = trouverFichier();
34      private static Properties prefs = null;
35      
36      private JCheckBox caseArbre;
37      private JCheckBox caseInsertion;
38      private JCheckBox caseAttributs;
39      private JLabel labelNav;
40      private String prefNav;
41      private JCheckBox caseIndent;
42      private JLabel labelDico;
43      private String prefDico;
44      
45      public static File trouverFichier() {
46          String userHome = System.getProperty("user.home");
47          String osName = System.getProperty("os.name");
48          String nomFichierPref;
49          if (osName.indexOf("Windows") != -1)
50              nomFichierPref = "preferences";
51          else
52              nomFichierPref = userHome + File.separator + ".jaxe";
53          return(new File(nomFichierPref));
54      }
55      
56      public static Properties chargerPref() {
57          if (!fpref.exists()) {
58              enregistrerPref(new Properties());
59              if (!fpref.exists())
60                  return(null);
61          }
62          prefs = new Properties();
63          try {
64              prefs.load(new FileInputStream(fpref));
65          } catch (IOException ex) {
66              System.err.println("IOException: " + ex.getMessage());
67              return(null);
68          }
69          return(prefs);
70      }
71      
72      public static Properties getPref() {
73          return(prefs);
74      }
75      
76      public static void enregistrerPref(Properties prefs1) {
77          if (prefs1 == null)
78              return;
79          prefs = prefs1;
80          try {
81              prefs.store(new FileOutputStream(fpref), "Préférences de Jaxe");
82          } catch (IOException ex) {
83              System.err.println("IOException: " + ex.getMessage());
84          }
85      }
86      
87      public Preferences(JFrame jframe) {
88    super(jframe, JaxeResourceBundle.getRB().getString("pref.Preferences"), true);
89          this.getContentPane().setLayout(new BorderLayout());
90          
91          Properties prefs = getPref();
92          String prefArbre = prefs.getProperty("fenetreArbre");
93          if (prefArbre == null)
94              prefArbre = "true";
95          String prefInsertion = prefs.getProperty("fenetreInsertion");
96          if (prefInsertion == null)
97              prefInsertion = "true";
98          String prefAttributs = prefs.getProperty("fenetreAttributs");
99          if (prefAttributs == null)
100             prefAttributs = "true";
101         prefNav = prefs.getProperty("navigateur");
102         String prefIndent = prefs.getProperty("consIndent");
103         if (prefIndent == null)
104             prefIndent = "false";
105         prefDico = prefs.getProperty("dictionnaire");
106         
107         JPanel prefPanes = new JPanel();
108         prefPanes.setLayout(new BoxLayout(prefPanes, BoxLayout.Y_AXIS));
109         
110         JPanel fenPane = new JPanel();
111         fenPane.setLayout(new BoxLayout(fenPane, BoxLayout.Y_AXIS));
112         fenPane.setBorder(BorderFactory.createTitledBorder(
113           JaxeResourceBundle.getRB().getString("pref.Fenetres")));
114         caseArbre = new JCheckBox(
115             JaxeResourceBundle.getRB().getString("pref.Arbre"));
116         caseArbre.setSelected("true".equals(prefArbre));
117         fenPane.add(caseArbre);
118         caseInsertion = new JCheckBox(
119             JaxeResourceBundle.getRB().getString("pref.Insertion"));
120         caseInsertion.setSelected("true".equals(prefInsertion));
121         fenPane.add(caseInsertion);
122         caseAttributs = new JCheckBox(
123             JaxeResourceBundle.getRB().getString("pref.Attributs"));
124         caseAttributs.setSelected("true".equals(prefAttributs));
125         fenPane.add(caseAttributs);
126         prefPanes.add(fenPane);
127         fenPane.setAlignmentX(Component.LEFT_ALIGNMENT);
128         fenPane.setMaximumSize(new Dimension(Short.MAX_VALUE,Short.MAX_VALUE));
129         
130         JPanel navPane = new JPanel(new FlowLayout());
131         navPane.setBorder(BorderFactory.createTitledBorder(
132             JaxeResourceBundle.getRB().getString("pref.Navigateur")));
133         String nomNav = null;
134         if (prefNav != null)
135             nomNav = (new File(prefNav)).getName();
136         labelNav = new JLabel(nomNav);
137         navPane.add(labelNav);
138         JButton defNav = new JButton(
139             JaxeResourceBundle.getRB().getString("pref.Definir"));
140         defNav.addActionListener(this);
141         defNav.setActionCommand("defNav");
142         navPane.add(defNav);
143         prefPanes.add(navPane);
144         navPane.setAlignmentX(Component.LEFT_ALIGNMENT);
145         
146         JPanel enrPane = new JPanel(new FlowLayout());
147         enrPane.setBorder(BorderFactory.createTitledBorder(
148             JaxeResourceBundle.getRB().getString("pref.Indentations")));
149         caseIndent = new JCheckBox(
150             JaxeResourceBundle.getRB().getString("pref.consIndent"));
151         caseIndent.setSelected("true".equals(prefIndent));
152         enrPane.add(caseIndent);
153         prefPanes.add(enrPane);
154         enrPane.setAlignmentX(Component.LEFT_ALIGNMENT);
155         
156         JPanel dicoPane = new JPanel(new FlowLayout());
157         dicoPane.setBorder(BorderFactory.createTitledBorder(
158             JaxeResourceBundle.getRB().getString("pref.Dictionnaire")));
159         String nomDico = null;
160         if (prefDico != null) {
161             nomDico = (new File(prefDico)).getName();
162             int pp = nomDico.lastIndexOf('.');
163             if (pp != -1)
164                 nomDico = nomDico.substring(0, pp);
165         }
166         labelDico = new JLabel(nomDico);
167         dicoPane.add(labelDico);
168         JButton defDico = new JButton(
169             JaxeResourceBundle.getRB().getString("pref.Definir"));
170         defDico.addActionListener(this);
171         defDico.setActionCommand("defDico");
172         dicoPane.add(defDico);
173         prefPanes.add(dicoPane);
174         dicoPane.setAlignmentX(Component.LEFT_ALIGNMENT);
175         
176         this.getContentPane().add(prefPanes, BorderLayout.CENTER);
177         
178         JPanel bPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
179         JButton boutonAnnuler = new JButton(
180             JaxeResourceBundle.getRB().getString("pref.Annuler"));
181         boutonAnnuler.addActionListener(this);
182         boutonAnnuler.setActionCommand("Annuler");
183         bPane.add(boutonAnnuler);
184         JButton boutonOK = new JButton(
185             JaxeResourceBundle.getRB().getString("pref.Enregistrer"));
186         boutonOK.addActionListener(this);
187         boutonOK.setActionCommand("Enregistrer");
188         bPane.add(boutonOK);
189         getRootPane().setDefaultButton(boutonOK);
190         this.getContentPane().add(bPane, BorderLayout.SOUTH);
191         Rectangle r = jframe.getBounds();
192         setLocation(r.x + r.width/4, r.y + r.height/4);
193         this.pack();
194     }
195     
196     public void actionPerformed(ActionEvent e) {
197         String cmd = e.getActionCommand();
198         
199         if ("Enregistrer".equals(cmd)) {
200             Properties prefs = getPref();
201             
202             String prefArbre;
203             if (caseArbre.isSelected())
204                 prefArbre = "true";
205             else
206                 prefArbre = "false";
207             prefs.setProperty("fenetreArbre", prefArbre);
208             
209             String prefInsertion;
210             if (caseInsertion.isSelected())
211                 prefInsertion = "true";
212             else
213                 prefInsertion = "false";
214             prefs.setProperty("fenetreInsertion", prefInsertion);
215             
216             String prefAttributs;
217             if (caseAttributs.isSelected())
218                 prefAttributs = "true";
219             else
220                 prefAttributs = "false";
221             prefs.setProperty("fenetreAttributs", prefAttributs);
222              
223             if (prefNav != null) {
224                 prefs.setProperty("navigateur", prefNav);
225             } else {
226                 prefs.setProperty("navigateur", "");
227             }
228             
229             if (prefDico != null) {
230                 prefs.setProperty("dictionnaire", prefDico);
231             } else {
232                 prefs.setProperty("dictionnaire", "");
233             }
234             
235             String prefIndent;
236             if (caseIndent.isSelected())
237                 prefIndent = "true";
238             else
239                 prefIndent = "false";
240             prefs.setProperty("consIndent", prefIndent);
241              
242             Preferences.enregistrerPref(prefs);
243             
244         } else if ("defNav".equals(cmd)) {
245             defNavigateur();
246         } else if ("defDico".equals(cmd)) {
247             defDictionnaire();
248         }
249         if ("Enregistrer".equals(cmd) || "Annuler".equals(cmd))
250             setVisible(false);
251     }
252     
253     public void defNavigateur() {
254         FileDialog fdlg = new FileDialog((JFrame)getOwner(),
255             JaxeResourceBundle.getRB().getString("pref.DefNavigateur"), FileDialog.LOAD);
256         fdlg.show();
257         String chemin = null;
258         String dir = fdlg.getDirectory();
259         if (dir != null && dir.endsWith(File.separator))
260             dir = dir.substring(0, dir.length()-1);
261         String nom = fdlg.getFile();
262         if (dir == null)
263             chemin = nom;
264         else if (nom != null)
265             chemin = dir + File.separator + nom;
266         if (chemin != null) {
267             prefNav = chemin;
268             labelNav.setText(nom);
269         }
270     }
271     
272     public void defDictionnaire() {
273         FileDialog fdlg = new FileDialog((JFrame)getOwner(),
274             JaxeResourceBundle.getRB().getString("pref.Dictionnaire"), FileDialog.LOAD);
275         fdlg.setFilenameFilter(new ExtFilter("dico"));
276         fdlg.setDirectory(System.getProperty("user.dir") + File.separator + "dicos");
277         fdlg.show();
278         String chemin = null;
279         String dir = fdlg.getDirectory();
280         if (dir != null && dir.endsWith(File.separator))
281             dir = dir.substring(0, dir.length()-1);
282         String nom = fdlg.getFile();
283         if (dir == null)
284             chemin = nom;
285         else if (nom != null)
286             chemin = dir + File.separator + nom;
287         if (chemin != null) {
288             prefDico = chemin;
289             if (nom != null) {
290                 int pp = nom.lastIndexOf('.');
291                 if (pp != -1)
292                     nom = nom.substring(0, pp);
293             }
294             labelDico.setText(nom);
295         }
296     }
297     
298     class ExtFilter implements FilenameFilter {
299         String[] exta;
300         public ExtFilter(String ext) {
301             exta = new String[1];
302             exta[0] = ext;
303         }
304         public ExtFilter(String[] exta) {
305             this.exta = exta;
306         }
307         public boolean accept(File dir, String name) {
308             for (int i=0; i<exta.length; i++)
309                 if (name.endsWith("." + exta[i]))
310                     return(true);
311             return(false);
312         }
313     }
314 }