1 /* 2 ***************************************************************************** 3 * Copyright (C) 2000-2004, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ***************************************************************************** 6 */ 7 package com.ibm.rbm; 8 9 import java.io.*; 10 import java.awt.*; 11 import java.awt.event.*; 12 import javax.swing.*; 13 import javax.swing.border.*; 14 import java.util.*; 15 16 /** 17 * An exporter plug-in class for RBManager. The resources exported here conform to 18 * the Java standard for Resource Bundles as specified in java.util.ListResourceBundle. 19 * The output files are compilable java files that are not associated with any 20 * package. 21 * 22 * @author Jared Jackson 23 * @see com.ibm.rbm.RBManager 24 */ 25 public class RBJavaExporter extends RBExporter { 26 private String packageName = null; 27 private boolean publicClass = true; 28 private boolean publicMethods = true; 29 30 RBJavaExporter()31 public RBJavaExporter() { 32 super(); 33 34 // Initialize the file chooser if necessary 35 if (chooser == null) { 36 chooser = new JFileChooser(); 37 chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){ 38 public String getDescription() { 39 return "Java Source Files"; 40 } 41 public boolean accept(File f) { 42 if (f.isDirectory()) return true; 43 if (f.getName().endsWith(".java") && f.getName().indexOf("_") < 0) return true; 44 return false; 45 } 46 }); 47 } 48 } 49 export(RBManager rbm)50 public void export(RBManager rbm) throws IOException { 51 if (rbm == null) return; 52 // Open the additional Dialog 53 RBJavaExporterDialog parametersDialog = new RBJavaExporterDialog(); 54 packageName = parametersDialog.getPackageName(); 55 publicClass = parametersDialog.isClassPublic(); 56 publicMethods = parametersDialog.isMethodsPublic(); 57 58 // Open the Save Dialog 59 int ret_val = chooser.showSaveDialog(null); 60 if (ret_val != JFileChooser.APPROVE_OPTION) return; 61 // Retrieve basic file information 62 File file = chooser.getSelectedFile(); // The file(s) we will be working with 63 File directory = new File(file.getParent()); // The directory we will be writing to 64 String base_name = file.getName(); // The base name of the files we will write 65 if (base_name == null || base_name.equals("")) base_name = rbm.getBaseClass(); 66 if (base_name.endsWith(".java")) base_name = base_name.substring(0,base_name.length()-5); 67 68 Vector bundle_v = rbm.getBundles(); 69 for (int i=0; i < bundle_v.size(); i++) { 70 Bundle bundle = (Bundle)bundle_v.elementAt(i); 71 String base_enc = base_name; 72 if (bundle.encoding != null && !bundle.encoding.equals("")) base_enc = base_enc + "_" + bundle.encoding; 73 String file_name = base_enc + ".java"; 74 75 StringBuffer buffer = new StringBuffer(); 76 buffer.append("/* File: " + file_name + "\n"); 77 buffer.append(" * Date: " + (new Date()) + "\n"); 78 buffer.append(" * Comment: This file was generated automatically by RBManager" + "\n"); 79 buffer.append(" */\n\n"); 80 if (packageName != null) { 81 buffer.append("package " + packageName + ";\n\n"); 82 } 83 buffer.append("import java.util.ListResourceBundle;\n\n"); 84 buffer.append((publicClass ? "public " : "protected ")); 85 buffer.append("class " + base_enc + " extends ListResourceBundle {\n"); 86 buffer.append("\t" + (publicMethods ? "public" : "protected") + " Object[][] getContents() {\n"); 87 buffer.append("\t\treturn contents;\n"); 88 buffer.append("\t}\n"); 89 buffer.append("\tprivate static final Object[][] contents = {\n"); 90 buffer.append("\t// LOCALIZE THIS\n"); 91 92 Vector group_v = bundle.getGroupsAsVector(); 93 for (int j=0; j < group_v.size(); j++) { 94 BundleGroup group = (BundleGroup)group_v.elementAt(j); 95 Vector item_v = group.getItemsAsVector(); 96 for (int k=0; k < item_v.size(); k++) { 97 BundleItem item = (BundleItem)item_v.elementAt(k); 98 buffer.append("\t\t{\"" + item.getKey() + "\", \"" + item.getTranslation() + "\"},\t// " + item.getComment() + "\n"); 99 } // end for - k 100 } // end for - j 101 102 buffer.append("\t// END OF MATERIAL TO LOCALIZE\n"); 103 buffer.append("\t};\n"); 104 buffer.append("}"); 105 106 // Write out the file 107 File write_file = new File(directory, file_name); 108 FileWriter writer = new FileWriter(write_file); 109 writer.write(buffer.toString()); 110 writer.flush(); 111 writer.close(); 112 } // end for - i 113 } 114 } 115 116 class RBJavaExporterDialog extends JDialog { 117 JCheckBox packageCheck; 118 JRadioButton classPublicRadio; 119 JRadioButton classProtectedRadio; 120 JRadioButton methodsPublicRadio; 121 JRadioButton methodsProtectedRadio; 122 JTextField packageField; 123 RBJavaExporterDialog()124 public RBJavaExporterDialog() { 125 super(new JFrame(), Resources.getTranslation("dialog_title_export_java_options"), true); 126 initComponents(); 127 } 128 getPackageName()129 public String getPackageName() { 130 if (!(packageCheck.isSelected())) return null; 131 String retVal = packageField.getText(); 132 if (retVal == null || retVal.trim().equals("")) return null; 133 return retVal.trim(); 134 } 135 isClassPublic()136 public boolean isClassPublic() { 137 return classPublicRadio.isSelected(); 138 } 139 isMethodsPublic()140 public boolean isMethodsPublic() { 141 return methodsPublicRadio.isSelected(); 142 } 143 handleClose()144 private void handleClose() { 145 setVisible(false); 146 dispose(); 147 } 148 initComponents()149 private void initComponents() { 150 getContentPane().setLayout(new BorderLayout()); 151 getContentPane().removeAll(); 152 153 packageCheck = new JCheckBox(Resources.getTranslation("export_java_package"), false); 154 classPublicRadio = new JRadioButton(Resources.getTranslation("export_java_class_public"), true); 155 classProtectedRadio = new JRadioButton(Resources.getTranslation("export_java_class_protected"), false); 156 methodsPublicRadio = new JRadioButton(Resources.getTranslation("export_java_class_public"), true); 157 methodsProtectedRadio = new JRadioButton(Resources.getTranslation("export_java_class_protected"), false); 158 packageField = new JTextField(); 159 packageField.setColumns(30); 160 161 JButton okButton = new JButton(Resources.getTranslation("OK")); 162 JLabel titleLabel = new JLabel(Resources.getTranslation("export_java_title"), SwingConstants.LEFT); 163 164 JPanel okPanel = new JPanel(); 165 okPanel.add(okButton); 166 JPanel centerPanel = new JPanel(new GridLayout(1,1)); 167 centerPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); 168 Box centerBox = Box.createVerticalBox(); 169 Box packageBox = Box.createHorizontalBox(); 170 packageBox.add(packageCheck); 171 packageBox.add(packageField); 172 centerBox.add(packageBox); 173 centerBox.add(new JSeparator()); 174 centerBox.add(classPublicRadio); 175 centerBox.add(classProtectedRadio); 176 centerBox.add(new JSeparator()); 177 centerBox.add(methodsPublicRadio); 178 centerBox.add(methodsProtectedRadio); 179 centerPanel.add(centerBox); 180 181 getContentPane().add(titleLabel, BorderLayout.NORTH); 182 getContentPane().add(okPanel, BorderLayout.SOUTH); 183 getContentPane().add(centerPanel, BorderLayout.CENTER); 184 185 okButton.addActionListener(new ActionListener(){ 186 public void actionPerformed(ActionEvent ev) { 187 handleClose(); 188 } 189 }); 190 191 ButtonGroup classGroup = new ButtonGroup(); 192 ButtonGroup methodsGroup = new ButtonGroup(); 193 classGroup.add(classPublicRadio); 194 classGroup.add(classProtectedRadio); 195 methodsGroup.add(methodsPublicRadio); 196 methodsGroup.add(methodsProtectedRadio); 197 198 //validateTree(); 199 pack(); 200 //setLocation(new Point(25,25)); 201 setVisible(true); 202 } 203 }