12e5b6d6dSopenharmony_ci/*
22e5b6d6dSopenharmony_ci *****************************************************************************
32e5b6d6dSopenharmony_ci * Copyright (C) 2000-2004, International Business Machines Corporation and  *
42e5b6d6dSopenharmony_ci * others. All Rights Reserved.                                              *
52e5b6d6dSopenharmony_ci *****************************************************************************
62e5b6d6dSopenharmony_ci */
72e5b6d6dSopenharmony_cipackage com.ibm.rbm;
82e5b6d6dSopenharmony_ci
92e5b6d6dSopenharmony_ciimport java.io.*;
102e5b6d6dSopenharmony_ciimport java.awt.*;
112e5b6d6dSopenharmony_ciimport java.awt.event.*;
122e5b6d6dSopenharmony_ciimport javax.swing.*;
132e5b6d6dSopenharmony_ciimport javax.swing.border.*;
142e5b6d6dSopenharmony_ciimport java.util.*;
152e5b6d6dSopenharmony_ci
162e5b6d6dSopenharmony_ci/**
172e5b6d6dSopenharmony_ci * An exporter plug-in class for RBManager. The resources exported here conform to
182e5b6d6dSopenharmony_ci * the Java standard for Resource Bundles as specified in java.util.ListResourceBundle.
192e5b6d6dSopenharmony_ci * The output files are compilable java files that are not associated with any
202e5b6d6dSopenharmony_ci * package.
212e5b6d6dSopenharmony_ci *
222e5b6d6dSopenharmony_ci * @author Jared Jackson
232e5b6d6dSopenharmony_ci * @see com.ibm.rbm.RBManager
242e5b6d6dSopenharmony_ci */
252e5b6d6dSopenharmony_cipublic class RBJavaExporter extends RBExporter {
262e5b6d6dSopenharmony_ci    private String packageName = null;
272e5b6d6dSopenharmony_ci    private boolean publicClass = true;
282e5b6d6dSopenharmony_ci    private boolean publicMethods = true;
292e5b6d6dSopenharmony_ci
302e5b6d6dSopenharmony_ci
312e5b6d6dSopenharmony_ci    public RBJavaExporter() {
322e5b6d6dSopenharmony_ci        super();
332e5b6d6dSopenharmony_ci
342e5b6d6dSopenharmony_ci        // Initialize the file chooser if necessary
352e5b6d6dSopenharmony_ci        if (chooser == null) {
362e5b6d6dSopenharmony_ci            chooser = new JFileChooser();
372e5b6d6dSopenharmony_ci            chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
382e5b6d6dSopenharmony_ci                public String getDescription() {
392e5b6d6dSopenharmony_ci                    return "Java Source Files";
402e5b6d6dSopenharmony_ci                }
412e5b6d6dSopenharmony_ci                public boolean accept(File f) {
422e5b6d6dSopenharmony_ci                    if (f.isDirectory()) return true;
432e5b6d6dSopenharmony_ci                    if (f.getName().endsWith(".java") && f.getName().indexOf("_") < 0) return true;
442e5b6d6dSopenharmony_ci                    return false;
452e5b6d6dSopenharmony_ci                }
462e5b6d6dSopenharmony_ci            });
472e5b6d6dSopenharmony_ci        }
482e5b6d6dSopenharmony_ci    }
492e5b6d6dSopenharmony_ci
502e5b6d6dSopenharmony_ci    public void export(RBManager rbm) throws IOException {
512e5b6d6dSopenharmony_ci        if (rbm == null) return;
522e5b6d6dSopenharmony_ci        // Open the additional Dialog
532e5b6d6dSopenharmony_ci        RBJavaExporterDialog parametersDialog = new RBJavaExporterDialog();
542e5b6d6dSopenharmony_ci        packageName = parametersDialog.getPackageName();
552e5b6d6dSopenharmony_ci        publicClass = parametersDialog.isClassPublic();
562e5b6d6dSopenharmony_ci        publicMethods = parametersDialog.isMethodsPublic();
572e5b6d6dSopenharmony_ci
582e5b6d6dSopenharmony_ci        // Open the Save Dialog
592e5b6d6dSopenharmony_ci        int ret_val = chooser.showSaveDialog(null);
602e5b6d6dSopenharmony_ci        if (ret_val != JFileChooser.APPROVE_OPTION) return;
612e5b6d6dSopenharmony_ci        // Retrieve basic file information
622e5b6d6dSopenharmony_ci        File file = chooser.getSelectedFile();                  // The file(s) we will be working with
632e5b6d6dSopenharmony_ci        File directory = new File(file.getParent());            // The directory we will be writing to
642e5b6d6dSopenharmony_ci        String base_name = file.getName();                      // The base name of the files we will write
652e5b6d6dSopenharmony_ci        if (base_name == null || base_name.equals("")) base_name = rbm.getBaseClass();
662e5b6d6dSopenharmony_ci        if (base_name.endsWith(".java")) base_name = base_name.substring(0,base_name.length()-5);
672e5b6d6dSopenharmony_ci
682e5b6d6dSopenharmony_ci        Vector bundle_v = rbm.getBundles();
692e5b6d6dSopenharmony_ci        for (int i=0; i < bundle_v.size(); i++) {
702e5b6d6dSopenharmony_ci            Bundle bundle = (Bundle)bundle_v.elementAt(i);
712e5b6d6dSopenharmony_ci            String base_enc = base_name;
722e5b6d6dSopenharmony_ci            if (bundle.encoding != null && !bundle.encoding.equals("")) base_enc = base_enc + "_" + bundle.encoding;
732e5b6d6dSopenharmony_ci            String file_name = base_enc + ".java";
742e5b6d6dSopenharmony_ci
752e5b6d6dSopenharmony_ci            StringBuffer buffer = new StringBuffer();
762e5b6d6dSopenharmony_ci            buffer.append("/* File:    " + file_name + "\n");
772e5b6d6dSopenharmony_ci            buffer.append(" * Date:    " + (new Date()) + "\n");
782e5b6d6dSopenharmony_ci            buffer.append(" * Comment: This file was generated automatically by RBManager" + "\n");
792e5b6d6dSopenharmony_ci            buffer.append(" */\n\n");
802e5b6d6dSopenharmony_ci            if (packageName != null) {
812e5b6d6dSopenharmony_ci                buffer.append("package " + packageName + ";\n\n");
822e5b6d6dSopenharmony_ci            }
832e5b6d6dSopenharmony_ci            buffer.append("import java.util.ListResourceBundle;\n\n");
842e5b6d6dSopenharmony_ci            buffer.append((publicClass ? "public " : "protected "));
852e5b6d6dSopenharmony_ci            buffer.append("class " + base_enc + " extends ListResourceBundle {\n");
862e5b6d6dSopenharmony_ci            buffer.append("\t" + (publicMethods ? "public" : "protected") + " Object[][] getContents() {\n");
872e5b6d6dSopenharmony_ci            buffer.append("\t\treturn contents;\n");
882e5b6d6dSopenharmony_ci            buffer.append("\t}\n");
892e5b6d6dSopenharmony_ci            buffer.append("\tprivate static final Object[][] contents = {\n");
902e5b6d6dSopenharmony_ci            buffer.append("\t// LOCALIZE THIS\n");
912e5b6d6dSopenharmony_ci
922e5b6d6dSopenharmony_ci            Vector group_v = bundle.getGroupsAsVector();
932e5b6d6dSopenharmony_ci            for (int j=0; j < group_v.size(); j++) {
942e5b6d6dSopenharmony_ci                BundleGroup group = (BundleGroup)group_v.elementAt(j);
952e5b6d6dSopenharmony_ci                Vector item_v = group.getItemsAsVector();
962e5b6d6dSopenharmony_ci                for (int k=0; k < item_v.size(); k++) {
972e5b6d6dSopenharmony_ci                    BundleItem item = (BundleItem)item_v.elementAt(k);
982e5b6d6dSopenharmony_ci                    buffer.append("\t\t{\"" + item.getKey() + "\", \"" + item.getTranslation() + "\"},\t// " + item.getComment() + "\n");
992e5b6d6dSopenharmony_ci                } // end for - k
1002e5b6d6dSopenharmony_ci            } // end for - j
1012e5b6d6dSopenharmony_ci
1022e5b6d6dSopenharmony_ci            buffer.append("\t// END OF MATERIAL TO LOCALIZE\n");
1032e5b6d6dSopenharmony_ci            buffer.append("\t};\n");
1042e5b6d6dSopenharmony_ci            buffer.append("}");
1052e5b6d6dSopenharmony_ci
1062e5b6d6dSopenharmony_ci            // Write out the file
1072e5b6d6dSopenharmony_ci            File write_file = new File(directory, file_name);
1082e5b6d6dSopenharmony_ci            FileWriter writer = new FileWriter(write_file);
1092e5b6d6dSopenharmony_ci            writer.write(buffer.toString());
1102e5b6d6dSopenharmony_ci            writer.flush();
1112e5b6d6dSopenharmony_ci            writer.close();
1122e5b6d6dSopenharmony_ci        } // end for - i
1132e5b6d6dSopenharmony_ci    }
1142e5b6d6dSopenharmony_ci}
1152e5b6d6dSopenharmony_ci
1162e5b6d6dSopenharmony_ciclass RBJavaExporterDialog extends JDialog {
1172e5b6d6dSopenharmony_ci    JCheckBox packageCheck;
1182e5b6d6dSopenharmony_ci    JRadioButton classPublicRadio;
1192e5b6d6dSopenharmony_ci    JRadioButton classProtectedRadio;
1202e5b6d6dSopenharmony_ci    JRadioButton methodsPublicRadio;
1212e5b6d6dSopenharmony_ci    JRadioButton methodsProtectedRadio;
1222e5b6d6dSopenharmony_ci    JTextField packageField;
1232e5b6d6dSopenharmony_ci
1242e5b6d6dSopenharmony_ci    public RBJavaExporterDialog() {
1252e5b6d6dSopenharmony_ci        super(new JFrame(), Resources.getTranslation("dialog_title_export_java_options"), true);
1262e5b6d6dSopenharmony_ci        initComponents();
1272e5b6d6dSopenharmony_ci    }
1282e5b6d6dSopenharmony_ci
1292e5b6d6dSopenharmony_ci    public String getPackageName() {
1302e5b6d6dSopenharmony_ci        if (!(packageCheck.isSelected())) return null;
1312e5b6d6dSopenharmony_ci        String retVal = packageField.getText();
1322e5b6d6dSopenharmony_ci        if (retVal == null || retVal.trim().equals("")) return null;
1332e5b6d6dSopenharmony_ci        return retVal.trim();
1342e5b6d6dSopenharmony_ci    }
1352e5b6d6dSopenharmony_ci
1362e5b6d6dSopenharmony_ci    public boolean isClassPublic() {
1372e5b6d6dSopenharmony_ci        return classPublicRadio.isSelected();
1382e5b6d6dSopenharmony_ci    }
1392e5b6d6dSopenharmony_ci
1402e5b6d6dSopenharmony_ci    public boolean isMethodsPublic() {
1412e5b6d6dSopenharmony_ci        return methodsPublicRadio.isSelected();
1422e5b6d6dSopenharmony_ci    }
1432e5b6d6dSopenharmony_ci
1442e5b6d6dSopenharmony_ci    private void handleClose() {
1452e5b6d6dSopenharmony_ci        setVisible(false);
1462e5b6d6dSopenharmony_ci        dispose();
1472e5b6d6dSopenharmony_ci    }
1482e5b6d6dSopenharmony_ci
1492e5b6d6dSopenharmony_ci    private void initComponents() {
1502e5b6d6dSopenharmony_ci        getContentPane().setLayout(new BorderLayout());
1512e5b6d6dSopenharmony_ci        getContentPane().removeAll();
1522e5b6d6dSopenharmony_ci
1532e5b6d6dSopenharmony_ci        packageCheck = new JCheckBox(Resources.getTranslation("export_java_package"), false);
1542e5b6d6dSopenharmony_ci        classPublicRadio = new JRadioButton(Resources.getTranslation("export_java_class_public"), true);
1552e5b6d6dSopenharmony_ci        classProtectedRadio = new JRadioButton(Resources.getTranslation("export_java_class_protected"), false);
1562e5b6d6dSopenharmony_ci        methodsPublicRadio = new JRadioButton(Resources.getTranslation("export_java_class_public"), true);
1572e5b6d6dSopenharmony_ci        methodsProtectedRadio = new JRadioButton(Resources.getTranslation("export_java_class_protected"), false);
1582e5b6d6dSopenharmony_ci        packageField = new JTextField();
1592e5b6d6dSopenharmony_ci        packageField.setColumns(30);
1602e5b6d6dSopenharmony_ci
1612e5b6d6dSopenharmony_ci        JButton okButton = new JButton(Resources.getTranslation("OK"));
1622e5b6d6dSopenharmony_ci        JLabel titleLabel = new JLabel(Resources.getTranslation("export_java_title"), SwingConstants.LEFT);
1632e5b6d6dSopenharmony_ci
1642e5b6d6dSopenharmony_ci        JPanel okPanel = new JPanel();
1652e5b6d6dSopenharmony_ci        okPanel.add(okButton);
1662e5b6d6dSopenharmony_ci        JPanel centerPanel = new JPanel(new GridLayout(1,1));
1672e5b6d6dSopenharmony_ci        centerPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
1682e5b6d6dSopenharmony_ci        Box centerBox = Box.createVerticalBox();
1692e5b6d6dSopenharmony_ci        Box packageBox = Box.createHorizontalBox();
1702e5b6d6dSopenharmony_ci        packageBox.add(packageCheck);
1712e5b6d6dSopenharmony_ci        packageBox.add(packageField);
1722e5b6d6dSopenharmony_ci        centerBox.add(packageBox);
1732e5b6d6dSopenharmony_ci        centerBox.add(new JSeparator());
1742e5b6d6dSopenharmony_ci        centerBox.add(classPublicRadio);
1752e5b6d6dSopenharmony_ci        centerBox.add(classProtectedRadio);
1762e5b6d6dSopenharmony_ci        centerBox.add(new JSeparator());
1772e5b6d6dSopenharmony_ci        centerBox.add(methodsPublicRadio);
1782e5b6d6dSopenharmony_ci        centerBox.add(methodsProtectedRadio);
1792e5b6d6dSopenharmony_ci        centerPanel.add(centerBox);
1802e5b6d6dSopenharmony_ci
1812e5b6d6dSopenharmony_ci        getContentPane().add(titleLabel, BorderLayout.NORTH);
1822e5b6d6dSopenharmony_ci        getContentPane().add(okPanel, BorderLayout.SOUTH);
1832e5b6d6dSopenharmony_ci        getContentPane().add(centerPanel, BorderLayout.CENTER);
1842e5b6d6dSopenharmony_ci
1852e5b6d6dSopenharmony_ci        okButton.addActionListener(new ActionListener(){
1862e5b6d6dSopenharmony_ci            public void actionPerformed(ActionEvent ev) {
1872e5b6d6dSopenharmony_ci                handleClose();
1882e5b6d6dSopenharmony_ci            }
1892e5b6d6dSopenharmony_ci        });
1902e5b6d6dSopenharmony_ci
1912e5b6d6dSopenharmony_ci        ButtonGroup classGroup = new ButtonGroup();
1922e5b6d6dSopenharmony_ci        ButtonGroup methodsGroup = new ButtonGroup();
1932e5b6d6dSopenharmony_ci        classGroup.add(classPublicRadio);
1942e5b6d6dSopenharmony_ci        classGroup.add(classProtectedRadio);
1952e5b6d6dSopenharmony_ci        methodsGroup.add(methodsPublicRadio);
1962e5b6d6dSopenharmony_ci        methodsGroup.add(methodsProtectedRadio);
1972e5b6d6dSopenharmony_ci
1982e5b6d6dSopenharmony_ci        //validateTree();
1992e5b6d6dSopenharmony_ci        pack();
2002e5b6d6dSopenharmony_ci        //setLocation(new Point(25,25));
2012e5b6d6dSopenharmony_ci        setVisible(true);
2022e5b6d6dSopenharmony_ci    }
2032e5b6d6dSopenharmony_ci}