001: package net.sourceforge.squirrel_sql.plugins.hibernate.configuration;
002:
003: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
004: import net.sourceforge.squirrel_sql.fw.util.StringManager;
005: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
006: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
007: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
008: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernatePlugin;
009:
010: import javax.swing.*;
011: import javax.swing.filechooser.FileFilter;
012: import java.awt.event.ActionListener;
013: import java.awt.event.ActionEvent;
014: import java.util.prefs.Preferences;
015: import java.io.File;
016: import java.io.FileWriter;
017: import java.io.PrintWriter;
018:
019: public class FactoryProviderController {
020:
021: private static final StringManager s_stringMgr = StringManagerFactory
022: .getStringManager(FactoryProviderController.class);
023:
024: private final static ILogger s_log = LoggerController
025: .createLogger(FactoryProviderController.class);
026:
027: private HibernatePlugin _plugin;
028: private String _className;
029: private FactoryProviderDialog _dlg;
030:
031: public FactoryProviderController(HibernatePlugin plugin,
032: String className) {
033: _plugin = plugin;
034:
035: _dlg = new FactoryProviderDialog(_plugin.getApplication()
036: .getMainFrame());
037:
038: _dlg.txtClassName.setText(className);
039:
040: _dlg.btnWriteExampleFactorProvider
041: .addActionListener(new ActionListener() {
042: public void actionPerformed(ActionEvent e) {
043: onWriteExampleFactorProvider();
044: }
045: });
046:
047: _dlg.btnOk.addActionListener(new ActionListener() {
048: public void actionPerformed(ActionEvent e) {
049: onOK();
050: }
051: });
052:
053: _dlg.btnCancel.addActionListener(new ActionListener() {
054: public void actionPerformed(ActionEvent e) {
055: onCancel();
056: }
057: });
058:
059: GUIUtils.centerWithinParent(_dlg);
060:
061: _dlg.setVisible(true);
062:
063: }
064:
065: private void onCancel() {
066: close();
067: }
068:
069: private void onOK() {
070: if (null != _dlg.txtClassName.getText()
071: && 0 < _dlg.txtClassName.getText().trim().length())
072:
073: _className = _dlg.txtClassName.getText();
074: close();
075: }
076:
077: private void close() {
078: _dlg.setVisible(false);
079: _dlg.dispose();
080: }
081:
082: private void onWriteExampleFactorProvider() {
083: String dirPath = Preferences.userRoot().get(
084: HibernateConfigController.PERF_KEY_LAST_DIR,
085: System.getProperty("user.home"));
086:
087: JFileChooser fc = new JFileChooser(dirPath);
088:
089: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
090:
091: fc.setFileFilter(new FileFilter() {
092: public boolean accept(File f) {
093: if (f.isDirectory()) {
094: return true;
095: }
096: return false;
097: }
098:
099: public String getDescription() {
100: return null;
101: }
102: });
103:
104: if (JFileChooser.APPROVE_OPTION != fc.showSaveDialog(_plugin
105: .getApplication().getMainFrame())) {
106: return;
107: }
108:
109: File dir = fc.getSelectedFile();
110:
111: File pack = new File(dir.getPath(), "pack");
112:
113: File javaFile = new File(pack,
114: "ExampleSessionFactorImplProvider.java");
115:
116: try {
117: pack.mkdirs();
118:
119: FileWriter fw = new FileWriter(javaFile);
120: PrintWriter pw = new PrintWriter(fw);
121:
122: pw.println(CODE);
123:
124: pw.flush();
125: fw.flush();
126: pw.close();
127: fw.close();
128:
129: // i18n[FactoryProviderController.fileCreated=File {0} has been successfully created.]
130: _plugin.getApplication().getMessageHandler().showMessage(
131: s_stringMgr.getString(
132: "FactoryProviderController.fileCreated",
133: javaFile));
134: } catch (Exception e) {
135: // i18n[FactoryProviderController.fileCreateFailed=File {0} could not be created: {1}]
136: String msg = s_stringMgr.getString(
137: "FactoryProviderController.fileCreateFailed",
138: new Object[] { javaFile, e });
139: _plugin.getApplication().getMessageHandler()
140: .showErrorMessage(msg);
141: s_log.error(msg, e);
142: }
143:
144: Preferences.userRoot().put(
145: HibernateConfigController.PERF_KEY_LAST_DIR,
146: dir.getPath());
147: }
148:
149: public String getClassName() {
150: return _className;
151: }
152:
153: private static final String CODE = "package pack;\n"
154: + "\n"
155: + "import org.hibernate.impl.SessionFactoryImpl;\n"
156: + "import org.hibernate.cfg.Configuration;\n"
157: + "import org.hibernate.SessionFactory;\n"
158: + "\n"
159: + "/**\n"
160: + " * Example for a class that can be used by\n"
161: + " * SQuirreLSQL's Hibernate Plugin to provide\n"
162: + " * a Hibernate SessionFactoryImpl object.\n"
163: + " *\n"
164: + " */\n"
165: + "public class ExampleSessionFactorImplProvider\n"
166: + "{\n"
167: + " /**\n"
168: + " * The SessionFactorImpl provider class must have a method\n"
169: + " * with exactly this signature and name.\n"
170: + " */\n"
171: + " public SessionFactoryImpl getSessionFactoryImpl()\n"
172: + " {\n"
173: + " // Normally you can cast this object to org.hibernate.impl.SessionFactoryImpl as shown below. \n"
174: + " SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();\n"
175: + "\n"
176: + " return (SessionFactoryImpl) sessionFactory;\n"
177: + " }\n" + "\n" + "}";
178: }
|