001: /*
002: *****************************************************************************
003: * Copyright (C) 2000-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *****************************************************************************
006: */
007: package com.ibm.rbm;
008:
009: import java.io.*;
010: import java.awt.*;
011: import java.awt.event.*;
012: import javax.swing.*;
013: import javax.swing.border.*;
014: import java.util.*;
015:
016: /**
017: * An exporter plug-in class for RBManager. The resources exported here conform to
018: * the Java standard for Resource Bundles as specified in java.util.ListResourceBundle.
019: * The output files are compilable java files that are not associated with any
020: * package.
021: *
022: * @author Jared Jackson
023: * @see com.ibm.rbm.RBManager
024: */
025: public class RBJavaExporter extends RBExporter {
026: private String packageName = null;
027: private boolean publicClass = true;
028: private boolean publicMethods = true;
029:
030: public RBJavaExporter() {
031: super ();
032:
033: // Initialize the file chooser if necessary
034: if (chooser == null) {
035: chooser = new JFileChooser();
036: chooser
037: .setFileFilter(new javax.swing.filechooser.FileFilter() {
038: public String getDescription() {
039: return "Java Source Files";
040: }
041:
042: public boolean accept(File f) {
043: if (f.isDirectory())
044: return true;
045: if (f.getName().endsWith(".java")
046: && f.getName().indexOf("_") < 0)
047: return true;
048: return false;
049: }
050: });
051: }
052: }
053:
054: public void export(RBManager rbm) throws IOException {
055: if (rbm == null)
056: return;
057: // Open the additional Dialog
058: RBJavaExporterDialog parametersDialog = new RBJavaExporterDialog();
059: packageName = parametersDialog.getPackageName();
060: publicClass = parametersDialog.isClassPublic();
061: publicMethods = parametersDialog.isMethodsPublic();
062:
063: // Open the Save Dialog
064: int ret_val = chooser.showSaveDialog(null);
065: if (ret_val != JFileChooser.APPROVE_OPTION)
066: return;
067: // Retrieve basic file information
068: File file = chooser.getSelectedFile(); // The file(s) we will be working with
069: File directory = new File(file.getParent()); // The directory we will be writing to
070: String base_name = file.getName(); // The base name of the files we will write
071: if (base_name == null || base_name.equals(""))
072: base_name = rbm.getBaseClass();
073: if (base_name.endsWith(".java"))
074: base_name = base_name.substring(0, base_name.length() - 5);
075:
076: Vector bundle_v = rbm.getBundles();
077: for (int i = 0; i < bundle_v.size(); i++) {
078: Bundle bundle = (Bundle) bundle_v.elementAt(i);
079: String base_enc = base_name;
080: if (bundle.encoding != null && !bundle.encoding.equals(""))
081: base_enc = base_enc + "_" + bundle.encoding;
082: String file_name = base_enc + ".java";
083:
084: StringBuffer buffer = new StringBuffer();
085: buffer.append("/* File: " + file_name + "\n");
086: buffer.append(" * Date: " + (new Date()) + "\n");
087: buffer
088: .append(" * Comment: This file was generated automatically by RBManager"
089: + "\n");
090: buffer.append(" */\n\n");
091: if (packageName != null) {
092: buffer.append("package " + packageName + ";\n\n");
093: }
094: buffer.append("import java.util.ListResourceBundle;\n\n");
095: buffer.append((publicClass ? "public " : "protected "));
096: buffer.append("class " + base_enc
097: + " extends ListResourceBundle {\n");
098: buffer.append("\t"
099: + (publicMethods ? "public" : "protected")
100: + " Object[][] getContents() {\n");
101: buffer.append("\t\treturn contents;\n");
102: buffer.append("\t}\n");
103: buffer
104: .append("\tprivate static final Object[][] contents = {\n");
105: buffer.append("\t// LOCALIZE THIS\n");
106:
107: Vector group_v = bundle.getGroupsAsVector();
108: for (int j = 0; j < group_v.size(); j++) {
109: BundleGroup group = (BundleGroup) group_v.elementAt(j);
110: Vector item_v = group.getItemsAsVector();
111: for (int k = 0; k < item_v.size(); k++) {
112: BundleItem item = (BundleItem) item_v.elementAt(k);
113: buffer.append("\t\t{\"" + item.getKey() + "\", \""
114: + item.getTranslation() + "\"},\t// "
115: + item.getComment() + "\n");
116: } // end for - k
117: } // end for - j
118:
119: buffer.append("\t// END OF MATERIAL TO LOCALIZE\n");
120: buffer.append("\t};\n");
121: buffer.append("}");
122:
123: // Write out the file
124: File write_file = new File(directory, file_name);
125: FileWriter writer = new FileWriter(write_file);
126: writer.write(buffer.toString());
127: writer.flush();
128: writer.close();
129: } // end for - i
130: }
131: }
132:
133: class RBJavaExporterDialog extends JDialog {
134: JCheckBox packageCheck;
135: JRadioButton classPublicRadio;
136: JRadioButton classProtectedRadio;
137: JRadioButton methodsPublicRadio;
138: JRadioButton methodsProtectedRadio;
139: JTextField packageField;
140:
141: public RBJavaExporterDialog() {
142: super (new JFrame(), Resources
143: .getTranslation("dialog_title_export_java_options"),
144: true);
145: initComponents();
146: }
147:
148: public String getPackageName() {
149: if (!(packageCheck.isSelected()))
150: return null;
151: String retVal = packageField.getText();
152: if (retVal == null || retVal.trim().equals(""))
153: return null;
154: return retVal.trim();
155: }
156:
157: public boolean isClassPublic() {
158: return classPublicRadio.isSelected();
159: }
160:
161: public boolean isMethodsPublic() {
162: return methodsPublicRadio.isSelected();
163: }
164:
165: private void handleClose() {
166: setVisible(false);
167: dispose();
168: }
169:
170: private void initComponents() {
171: getContentPane().setLayout(new BorderLayout());
172: getContentPane().removeAll();
173:
174: packageCheck = new JCheckBox(Resources
175: .getTranslation("export_java_package"), false);
176: classPublicRadio = new JRadioButton(Resources
177: .getTranslation("export_java_class_public"), true);
178: classProtectedRadio = new JRadioButton(Resources
179: .getTranslation("export_java_class_protected"), false);
180: methodsPublicRadio = new JRadioButton(Resources
181: .getTranslation("export_java_class_public"), true);
182: methodsProtectedRadio = new JRadioButton(Resources
183: .getTranslation("export_java_class_protected"), false);
184: packageField = new JTextField();
185: packageField.setColumns(30);
186:
187: JButton okButton = new JButton(Resources.getTranslation("OK"));
188: JLabel titleLabel = new JLabel(Resources
189: .getTranslation("export_java_title"),
190: SwingConstants.LEFT);
191:
192: JPanel okPanel = new JPanel();
193: okPanel.add(okButton);
194: JPanel centerPanel = new JPanel(new GridLayout(1, 1));
195: centerPanel.setBorder(BorderFactory
196: .createBevelBorder(BevelBorder.RAISED));
197: Box centerBox = Box.createVerticalBox();
198: Box packageBox = Box.createHorizontalBox();
199: packageBox.add(packageCheck);
200: packageBox.add(packageField);
201: centerBox.add(packageBox);
202: centerBox.add(new JSeparator());
203: centerBox.add(classPublicRadio);
204: centerBox.add(classProtectedRadio);
205: centerBox.add(new JSeparator());
206: centerBox.add(methodsPublicRadio);
207: centerBox.add(methodsProtectedRadio);
208: centerPanel.add(centerBox);
209:
210: getContentPane().add(titleLabel, BorderLayout.NORTH);
211: getContentPane().add(okPanel, BorderLayout.SOUTH);
212: getContentPane().add(centerPanel, BorderLayout.CENTER);
213:
214: okButton.addActionListener(new ActionListener() {
215: public void actionPerformed(ActionEvent ev) {
216: handleClose();
217: }
218: });
219:
220: ButtonGroup classGroup = new ButtonGroup();
221: ButtonGroup methodsGroup = new ButtonGroup();
222: classGroup.add(classPublicRadio);
223: classGroup.add(classProtectedRadio);
224: methodsGroup.add(methodsPublicRadio);
225: methodsGroup.add(methodsProtectedRadio);
226:
227: //validateTree();
228: pack();
229: //setLocation(new Point(25,25));
230: setVisible(true);
231: }
232: }
|