Index: lutini18neditor/src/java/org/codelutin/i18n/editor/ui/GBC.java diff -u /dev/null lutini18neditor/src/java/org/codelutin/i18n/editor/ui/GBC.java:1.1 --- /dev/null Fri Jan 18 18:15:10 2008 +++ lutini18neditor/src/java/org/codelutin/i18n/editor/ui/GBC.java Fri Jan 18 18:15:04 2008 @@ -0,0 +1,133 @@ +/* +GBC - A convenience class to tame the GridBagLayout + +Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.codelutin.i18n.editor.ui; + +import java.awt.GridBagConstraints; +import java.awt.Insets; + +/** + This class simplifies the use of the GridBagConstraints + class. + */ +public class GBC extends GridBagConstraints +{ + /** + Constructs a GBC with a given gridx and gridy position and + all other grid bag constraint values set to the default. + @param gridx the gridx position + @param gridy the gridy position + */ + public GBC(int gridx, int gridy) + { + this.gridx = gridx; + this.gridy = gridy; + } + + /** + Constructs a GBC with given gridx, gridy, gridwidth, gridheight + and all other grid bag constraint values set to the default. + @param gridx the gridx position + @param gridy the gridy position + @param gridwidth the cell span in x-direction + @param gridheight the cell span in y-direction + */ + public GBC(int gridx, int gridy, int gridwidth, int gridheight) + { + this.gridx = gridx; + this.gridy = gridy; + this.gridwidth = gridwidth; + this.gridheight = gridheight; + } + + /** + Sets the anchor. + @param anchor the anchor value + @return this object for further modification + */ + public GBC setAnchor(int anchor) + { + this.anchor = anchor; + return this; + } + + /** + Sets the fill direction. + @param fill the fill direction + @return this object for further modification + */ + public GBC setFill(int fill) + { + this.fill = fill; + return this; + } + + /** + Sets the cell weights. + @param weightx the cell weight in x-direction + @param weighty the cell weight in y-direction + @return this object for further modification + */ + public GBC setWeight(double weightx, double weighty) + { + this.weightx = weightx; + this.weighty = weighty; + return this; + } + + /** + Sets the insets of this cell. + @param distance the spacing to use in all directions + @return this object for further modification + */ + public GBC setInsets(int distance) + { + this.insets = new Insets(distance, distance, distance, distance); + return this; + } + + /** + Sets the insets of this cell. + @param top the spacing to use on top + @param left the spacing to use to the left + @param bottom the spacing to use on the bottom + @param right the spacing to use to the right + @return this object for further modification + */ + public GBC setInsets(int top, int left, int bottom, int right) + { + this.insets = new Insets(top, left, bottom, right); + return this; + } + + /** + Sets the internal padding + @param ipadx the internal padding in x-direction + + @param ipady the internal padding in y-direction + @return this object for further modification + */ + public GBC setIpad(int ipadx, int ipady) + { + this.ipadx = ipadx; + this.ipady = ipady; + return this; + } +} Index: lutini18neditor/src/java/org/codelutin/i18n/editor/ui/I18nPrincipalImpl.java diff -u /dev/null lutini18neditor/src/java/org/codelutin/i18n/editor/ui/I18nPrincipalImpl.java:1.1 --- /dev/null Fri Jan 18 18:15:10 2008 +++ lutini18neditor/src/java/org/codelutin/i18n/editor/ui/I18nPrincipalImpl.java Fri Jan 18 18:15:04 2008 @@ -0,0 +1,282 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ + +package org.codelutin.i18n.editor.ui; + +import java.awt.Dimension; +import java.awt.GridBagLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.Serializable; +import java.util.List; + +import javax.swing.BorderFactory; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.border.TitledBorder; +import javax.swing.tree.TreeModel; + +import org.codelutin.i18n.editor.ManagerI18n; +import org.codelutin.i18n.editor.model.PropertieNode; +import org.codelutin.i18n.editor.model.PropertiesTreeModel; + +/** + * Surcharge de l'interface jaax de la fênetre principal pour définir les actions + * associés et l'initialisation des composants graphiques. + * + * @author julien + */ +public class I18nPrincipalImpl extends I18nPrincipal { + + /** + * @see Serializable + */ + private static final long serialVersionUID = 1L; + + /** + * Manager des bundles + */ + protected ManagerI18n manager; + + /** + * Liste des paquetages disponibles pour l'affichage de l'arbre de propriétés + */ + protected List packagesI18n; + + /** + * Liste des bundles diponibles pour visualisation et modification des valeurs + */ + protected List bundlesI18n; + + /** + * Constructeur de l'interface + */ + public I18nPrincipalImpl() { + super(); + + // Initialisation des variables + manager = ManagerI18n.getInstance(); + packagesI18n = manager.getPackages(); + bundlesI18n = manager.getBundles(); + + // Initialisation de l'interface + values.setLayout(new GridBagLayout()); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Création d'une checkbox dans le menu pour chaque bundle + for (String name : packagesI18n) { + createPackageChekBoxMenu(name); + } + + // Création d'une checkbox dans le menu pour chaque locale + for (String name : manager.getBundles()) { + createBundleChekBoxMenu(name); + } + + // A chaque accès à l'arbre, réaffichage des valeurs des bundles pour + // la clé sélectionnée + tree.addMouseListener(new MouseListener(){ + public void mouseClicked(MouseEvent e) { + reloadValues(); + } + + public void mouseEntered(MouseEvent e) { } + public void mouseExited(MouseEvent e) { } + public void mousePressed(MouseEvent e) { } + public void mouseReleased(MouseEvent e) { } + }); + + // Action de fermeture par le menu + quit.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + dispose(); + } + }); + + // Action de création d'un nouveau bundle + create.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // Ouverture de la fenêtre de création + CreateBundleImpl createBundle = new CreateBundleImpl(); + createBundle.setVisible(true); + + // Ajout du nouveau bundle à l'écran et dans le menu + String bundle = createBundle.getBundle(); + if(bundle != null) { + createBundleChekBoxMenu(bundle); + bundlesI18n.add(bundle); + reloadValues(); + } + } + }); + + // Action d'enregistrement des modifications des valeurs des bundles + store.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + manager.store(); + } + }); + + // Initialisation des données + reloadKeys(); + pack(); + } + + /** + * Création d'une checkbox dans le menu pour afficher ou non des bundles parmis + * les valeurs + * @param name nom du bundle + */ + protected void createBundleChekBoxMenu(String name) { + final JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem(name, true); + checkBoxMenuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String text = checkBoxMenuItem.getText(); + if(bundlesI18n.contains(text)) { + bundlesI18n.remove(text); + } else { + bundlesI18n.add(text); + } + reloadValues(); + } + }); + bundles.add(checkBoxMenuItem); + } + + /** + * Création d'une checkbox dans le menu pour afficher ou non les paquetages + * dans l'arbre de propriété + * @param name nom du paquetage + */ + protected void createPackageChekBoxMenu(String name) { + final JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem(name, true); + checkBoxMenuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String text = checkBoxMenuItem.getText(); + if(packagesI18n.contains(text)) { + packagesI18n.remove(text); + } else { + packagesI18n.add(text); + } + reloadKeys(); + reloadValues(); + } + }); + packages.add(checkBoxMenuItem); + } + + /** + * Recharge les clés dans l'arbre des propriétés en fonction des paquetages + */ + protected void reloadKeys() { + TreeModel model = new PropertiesTreeModel(manager.getKeys(packagesI18n)); + tree.setModel(model); + } + + /** + * Recharge les valeurs en fonction des bundles. + */ + protected void reloadValues() { + values.removeAll(); + + PropertieNode node = (PropertieNode) tree.getSelectionValue(); + if(node != null) { + String key = (String) node.getUserObject(); + + // Pas la racine car on i18n. + if(key.startsWith( + ManagerI18n.ROOT_NAME + + ManagerI18n.SEPARATOR)) { + // Supprime le i18n. + key = key.substring( + ManagerI18n.ROOT_NAME.length() + + ManagerI18n.SEPARATOR.length()); + } + + List values = manager.getValues(bundlesI18n, key); + int index = 0; + for (String value : values) { + addValue(bundlesI18n.get(index), key, value, index); + index ++; + } + } + + scrollPane.validate(); + scrollPane.repaint(); + } + + /** + * Ajout du composant pour une valeur de clé dans la fenêtre + * @param bundle nom du bundle + * @param key clé + * @param value valeur associé à la clé + * @param index numéro d'ordonnacement dans la listes des valeurs + */ + protected void addValue(final String bundle, final String key, final String value, int index) { + // Construction du composant + JPanel panel = new JPanel(new GridBagLayout()); + panel.setPreferredSize(new Dimension(-1, 150)); + + TitledBorder title = BorderFactory.createTitledBorder(bundle); + panel.setBorder(title); + + JTextArea textArea = new JTextArea(value); // Zone de modification de la valeur + textArea.setWrapStyleWord(true); + textArea.setLineWrap(true); + + JScrollPane scrollPane = new JScrollPane(textArea); + panel.add(scrollPane, new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 1).setInsets(10)); + + // A chaque modification, engistrement par le manager de la nouvelle + // valeur + textArea.addKeyListener(new KeyListener() { + protected String lastValue = value; + + public void keyReleased(KeyEvent e) { + JTextArea textArea = (JTextArea) e.getSource(); + String newValue = textArea.getText(); + if(!newValue.equals(lastValue)) { + manager.store(bundle, key, newValue); + lastValue = newValue; + } + } + + public void keyPressed(KeyEvent e) {} + public void keyTyped(KeyEvent e) { } + }); + + // Ajout du composant dans la fenêtre + values.add(panel, new GBC(0, index).setFill(GBC.HORIZONTAL).setWeight(1, 1).setInsets(10)); + } + + /** + * Lancement de l'interface + */ + public static void main(String[] args) throws FileNotFoundException, IOException { + new I18nPrincipalImpl().setVisible(true); + } +} Index: lutini18neditor/src/java/org/codelutin/i18n/editor/ui/CreateBundleImpl.java diff -u /dev/null lutini18neditor/src/java/org/codelutin/i18n/editor/ui/CreateBundleImpl.java:1.1 --- /dev/null Fri Jan 18 18:15:10 2008 +++ lutini18neditor/src/java/org/codelutin/i18n/editor/ui/CreateBundleImpl.java Fri Jan 18 18:15:05 2008 @@ -0,0 +1,86 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ + +package org.codelutin.i18n.editor.ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.Serializable; + +import org.codelutin.i18n.editor.ManagerI18n; + +/** + * Surcharge de l'interface jaax de création de nouveau bundle pour définir les + * actions associées aux bouttons. + * + * @author julien + */ +public class CreateBundleImpl extends CreateBundle { + + /** + * @see Serializable + */ + private static final long serialVersionUID = 1L; + + /** + * Manager des bundles + */ + protected ManagerI18n manager; + + /** + * Nom du nouveau bundle créé + */ + protected String bundle; + + /** + * Consturcteur de l'interface + */ + public CreateBundleImpl() { + super(); + + // Récupération du manager + manager = ManagerI18n.getInstance(); + + // Action d'annulation, juste fermeture de la fenêtre + buttonCancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + bundle = null; + dispose(); + } + }); + + // Action de création d'un nouveau bundle par le manager + buttonCreate.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + bundle = textFieldLanguage.getText() + "_" + textFieldLocale.getText(); + // TODO: Vérification bundle n'existe pas déjà + manager.createBundle(bundle); + + dispose(); + } + }); + } + + /** + * Récupération du nouveau bundle + * @return nom du nouveau bundle + */ + public String getBundle() { + return bundle; + } +}