001: package net.sourceforge.squirrel_sql.plugins.hibernate.configuration;
002:
003: import net.sourceforge.squirrel_sql.fw.util.StringManager;
004: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
005: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
006: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
007: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernatePlugin;
008: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernatePrefsListener;
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.awt.event.ItemListener;
015: import java.awt.event.ItemEvent;
016: import java.util.prefs.Preferences;
017: import java.util.Map;
018: import java.util.HashMap;
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.io.File;
022:
023: public class HibernateConfigController {
024: private static final StringManager s_stringMgr = StringManagerFactory
025: .getStringManager(HibernateConfigController.class);
026:
027: private HibernatePlugin _plugin;
028: private HibernateConfigPanel _panel;
029:
030: static final String PERF_KEY_LAST_DIR = "Squirrel.Hibernate.lastDir";
031: public static final String HIBERNATE_CONFIGS_XML_FILE = "hibernateConfigs.xml";
032: private HibernatePrefsListener _hibernatePrefsListener;
033:
034: public HibernateConfigController(HibernatePlugin plugin) {
035: _plugin = plugin;
036: _panel = new HibernateConfigPanel();
037:
038: _hibernatePrefsListener = _plugin
039: .removeHibernatePrefsListener();
040:
041: _panel.lstClassPath.setModel(new DefaultListModel());
042:
043: _panel.btnNewConfig.addActionListener(new ActionListener() {
044: public void actionPerformed(ActionEvent e) {
045: onNewConfig();
046: }
047: });
048:
049: _panel.btnRemoveConfig.addActionListener(new ActionListener() {
050: public void actionPerformed(ActionEvent e) {
051: onRemoveConfig();
052: }
053: });
054:
055: _panel.btnClassPathAdd.addActionListener(new ActionListener() {
056: public void actionPerformed(ActionEvent e) {
057: onAddClasspathEntry();
058: }
059: });
060:
061: _panel.btnClassPathRemove
062: .addActionListener(new ActionListener() {
063: public void actionPerformed(ActionEvent e) {
064: onRemoveSelectedClasspathEntries();
065: }
066: });
067:
068: _panel.btnEditFactoryProviderInfo
069: .addActionListener(new ActionListener() {
070: public void actionPerformed(ActionEvent e) {
071: onFactoryProviderInfo();
072: }
073: });
074:
075: _panel.btnApplyConfigChanges
076: .addActionListener(new ActionListener() {
077: public void actionPerformed(ActionEvent e) {
078: onApplyConfigChanges(false);
079: }
080: });
081:
082: _panel.cboConfigs.addItemListener(new ItemListener() {
083: public void itemStateChanged(ItemEvent e) {
084: onSelectedConfigChanged(e);
085: }
086: });
087:
088: ItemListener radObtainSessFactListener = new ItemListener() {
089: public void itemStateChanged(ItemEvent e) {
090: onObtainSessFactChanged();
091: }
092: };
093:
094: _panel.radConfiguration
095: .addItemListener(radObtainSessFactListener);
096: _panel.radJPA.addItemListener(radObtainSessFactListener);
097: _panel.radUserDefProvider
098: .addItemListener(radObtainSessFactListener);
099:
100: }
101:
102: private void onObtainSessFactChanged() {
103: if (_panel.radUserDefProvider.isSelected()) {
104: _panel.btnEditFactoryProviderInfo.setEnabled(true);
105: _panel.txtPersistenceUnitName.setEnabled(false);
106: } else if (_panel.radJPA.isSelected()) {
107: _panel.btnEditFactoryProviderInfo.setEnabled(false);
108: _panel.txtPersistenceUnitName.setEnabled(true);
109: } else if (_panel.radConfiguration.isSelected()) {
110: _panel.btnEditFactoryProviderInfo.setEnabled(false);
111: _panel.txtPersistenceUnitName.setEnabled(false);
112: }
113: }
114:
115: private void onRemoveConfig() {
116: HibernateConfiguration selConfig = (HibernateConfiguration) _panel.cboConfigs
117: .getSelectedItem();
118:
119: if (null == selConfig) {
120: // i18n[HibernateConfigController.NoConfigToRemove=No configuration selected to remove.]
121: JOptionPane.showMessageDialog(_plugin.getApplication()
122: .getMainFrame(), s_stringMgr
123: .getString("HibernateController.NoConfigToRemove"));
124: } else {
125: // i18n[HibernateConfigController.ReallyRemoveConfig=Are you sure you want to delete configuration "{0}".]
126: if (JOptionPane.YES_OPTION == JOptionPane
127: .showConfirmDialog(_plugin.getApplication()
128: .getMainFrame(), s_stringMgr.getString(
129: "HibernateController.ReallyRemoveConfig",
130: selConfig))) {
131: ((DefaultComboBoxModel) _panel.cboConfigs.getModel())
132: .removeElement(selConfig);
133: }
134: }
135: }
136:
137: private void onRemoveSelectedClasspathEntries() {
138: int[] selIces = _panel.lstClassPath.getSelectedIndices();
139:
140: List<String> toRemove = new ArrayList<String>();
141: DefaultListModel listModel = (DefaultListModel) _panel.lstClassPath
142: .getModel();
143: for (int i = 0; i < selIces.length; i++) {
144: toRemove.add((String) listModel.getElementAt(selIces[i]));
145: }
146:
147: for (String s : toRemove) {
148: listModel.removeElement(s);
149: }
150: }
151:
152: private void onSelectedConfigChanged(ItemEvent e) {
153: if (ItemEvent.SELECTED == e.getStateChange()
154: && null != e.getItem()) {
155: initConfig((HibernateConfiguration) e.getItem());
156: } else if (ItemEvent.DESELECTED == e.getStateChange()
157: && null != e.getItem()) {
158: initConfig(null);
159: }
160: }
161:
162: private boolean onApplyConfigChanges(boolean silent) {
163: String provider = _panel.txtFactoryProvider.getText();
164: String persistenceUnitName = _panel.txtPersistenceUnitName
165: .getText();
166:
167: if (_panel.radUserDefProvider.isSelected()
168: && (null == provider || 0 == provider.trim().length())) {
169: if (false == silent) {
170: // i18n[HibernateConfigController.noProviderMsg=Missing SessionFactoryImplProvider .\nChanges cannot be applied.]
171: JOptionPane
172: .showMessageDialog(
173: _plugin.getApplication().getMainFrame(),
174: s_stringMgr
175: .getString("HibernateController.noProviderMsg"));
176: }
177: return false;
178: }
179:
180: if (_panel.radJPA.isSelected()
181: && (null == persistenceUnitName || 0 == persistenceUnitName
182: .trim().length())) {
183: if (false == silent) {
184: // i18n[HibernateConfigController.noPersistenceUnitName=Missing Persitence-Unit name .\nChanges cannot be applied.]
185: JOptionPane
186: .showMessageDialog(
187: _plugin.getApplication().getMainFrame(),
188: s_stringMgr
189: .getString("HibernateController.noPersistenceUnitName"));
190: }
191: return false;
192: }
193:
194: String cfgName = _panel.txtConfigName.getText();
195:
196: if (null == cfgName || 0 == cfgName.trim().length()) {
197: if (false == silent) {
198: // i18n[HibernateConfigController.noCfgNameMsg=Not a valid configuration name\nChanges cannot be applied.]
199: JOptionPane
200: .showMessageDialog(
201: _plugin.getApplication().getMainFrame(),
202: s_stringMgr
203: .getString("HibernateController.noProviderMsg"));
204: }
205: return false;
206: }
207:
208: HibernateConfiguration cfg = (HibernateConfiguration) _panel.cboConfigs
209: .getSelectedItem();
210:
211: boolean wasNull = false;
212: if (null == cfg) {
213: wasNull = true;
214: cfg = new HibernateConfiguration();
215: }
216:
217: cfg.setProvider(provider);
218: cfg.setPersistenceUnitName(persistenceUnitName);
219: cfg.setName(cfgName);
220:
221: String[] classPathEntries = new String[_panel.lstClassPath
222: .getModel().getSize()];
223:
224: for (int i = 0; i < _panel.lstClassPath.getModel().getSize(); ++i) {
225: classPathEntries[i] = (String) _panel.lstClassPath
226: .getModel().getElementAt(i);
227: }
228: cfg.setClassPathEntries(classPathEntries);
229:
230: if (_panel.radUserDefProvider.isSelected()) {
231: cfg.setUserDefinedProvider(true);
232: cfg.setJPA(false);
233: } else if (_panel.radJPA.isSelected()) {
234: cfg.setUserDefinedProvider(false);
235: cfg.setJPA(true);
236: } else {
237: cfg.setUserDefinedProvider(false);
238: cfg.setJPA(false);
239: }
240:
241: if (wasNull) {
242: _panel.cboConfigs.addItem(cfg);
243: _panel.cboConfigs.setSelectedItem(cfg);
244:
245: }
246:
247: return true;
248: }
249:
250: private void onFactoryProviderInfo() {
251: String className = new FactoryProviderController(_plugin,
252: _panel.txtFactoryProvider.getText()).getClassName();
253: _panel.txtFactoryProvider.setText(className);
254:
255: }
256:
257: private void onAddClasspathEntry() {
258: String dirPath = Preferences.userRoot().get(PERF_KEY_LAST_DIR,
259: System.getProperty("user.home"));
260:
261: JFileChooser fc = new JFileChooser(dirPath);
262:
263: fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
264: fc.setMultiSelectionEnabled(true);
265:
266: fc.setFileFilter(new FileFilter() {
267: public boolean accept(File f) {
268: if (f.isDirectory()
269: || f.getName().toUpperCase().endsWith(".ZIP")
270: || f.getName().toUpperCase().endsWith(".JAR")) {
271: return true;
272: }
273: return false;
274: }
275:
276: public String getDescription() {
277: // i18n[HibernateConfigController.classpathEntryDesc=Jars, Zips or directories]
278: return s_stringMgr
279: .getString("HibernateController.classpathEntryDesc");
280: }
281: });
282:
283: if (JFileChooser.APPROVE_OPTION != fc.showOpenDialog(_plugin
284: .getApplication().getMainFrame())) {
285: return;
286: }
287:
288: File[] files = fc.getSelectedFiles();
289:
290: DefaultListModel listModel = (DefaultListModel) _panel.lstClassPath
291: .getModel();
292: for (int i = 0; i < files.length; i++) {
293: listModel.addElement(files[i].getPath());
294: }
295:
296: if (0 < files.length) {
297: if (null == files[0].getParent()) {
298: Preferences.userRoot().put(PERF_KEY_LAST_DIR,
299: files[0].getPath());
300: } else {
301: Preferences.userRoot().put(PERF_KEY_LAST_DIR,
302: files[0].getParent());
303: }
304: }
305: }
306:
307: private void onNewConfig() {
308: _panel.cboConfigs.setSelectedItem(null);
309: initConfig(null);
310: }
311:
312: private void initConfig(HibernateConfiguration config) {
313:
314: if (null == config) {
315: _panel.txtConfigName.setText(null);
316:
317: DefaultListModel listModel = (DefaultListModel) _panel.lstClassPath
318: .getModel();
319:
320: listModel.clear();
321:
322: _panel.txtFactoryProvider.setText(null);
323:
324: _panel.radConfiguration.setSelected(true);
325: onObtainSessFactChanged();
326:
327: return;
328:
329: }
330:
331: _panel.txtConfigName.setText(config.getName());
332:
333: DefaultListModel listModel = (DefaultListModel) _panel.lstClassPath
334: .getModel();
335:
336: listModel.clear();
337:
338: for (String path : config.getClassPathEntries()) {
339: listModel.addElement(path);
340: }
341:
342: if (config.isUserDefinedProvider()) {
343: _panel.radUserDefProvider.setSelected(true);
344: } else if (config.isJPA()) {
345: _panel.radJPA.setSelected(true);
346: } else {
347: _panel.radConfiguration.setSelected(true);
348: }
349: onObtainSessFactChanged();
350:
351: _panel.txtFactoryProvider.setText(config.getProvider());
352: _panel.txtPersistenceUnitName.setText(config
353: .getPersistenceUnitName());
354: }
355:
356: public HibernateConfigPanel getPanel() {
357: return _panel;
358: }
359:
360: public void applyChanges() {
361: try {
362: if (onApplyConfigChanges(true)) {
363: File pluginUserSettingsFolder = _plugin
364: .getPluginUserSettingsFolder();
365:
366: File cfgsFile = new File(pluginUserSettingsFolder
367: .getPath(), HIBERNATE_CONFIGS_XML_FILE);
368:
369: XMLBeanWriter bw = new XMLBeanWriter();
370:
371: ArrayList<HibernateConfiguration> buf = new ArrayList<HibernateConfiguration>();
372: for (int i = 0; i < _panel.cboConfigs.getItemCount(); i++) {
373: HibernateConfiguration cfg = (HibernateConfiguration) _panel.cboConfigs
374: .getItemAt(i);
375: bw.addToRoot(cfg);
376: buf.add(cfg);
377: }
378:
379: bw.save(cfgsFile);
380:
381: if (null != _hibernatePrefsListener) {
382: _hibernatePrefsListener.configurationChanged(buf);
383: }
384:
385: }
386: } catch (Exception e) {
387: throw new RuntimeException(e);
388: }
389: }
390:
391: public void initialize() {
392: try {
393: XMLBeanReader reader = new XMLBeanReader();
394: File pluginUserSettingsFolder = _plugin
395: .getPluginUserSettingsFolder();
396:
397: File xmlFile = new File(pluginUserSettingsFolder.getPath(),
398: HIBERNATE_CONFIGS_XML_FILE);
399:
400: if (false == xmlFile.exists()) {
401: return;
402: }
403:
404: reader.load(xmlFile, _plugin.getClass().getClassLoader());
405:
406: HibernateConfiguration toSel = null;
407:
408: for (Object o : reader) {
409: HibernateConfiguration cfg = (HibernateConfiguration) o;
410:
411: if (null != _hibernatePrefsListener
412: && null != _hibernatePrefsListener
413: .getPreselectedCfg()
414: && cfg.getName().equals(
415: _hibernatePrefsListener
416: .getPreselectedCfg().getName())) {
417: toSel = cfg;
418: }
419:
420: _panel.cboConfigs.addItem(cfg);
421: }
422:
423: if (null != toSel) {
424: _panel.cboConfigs.setSelectedItem(toSel);
425: } else if (0 < _panel.cboConfigs.getItemCount()) {
426: _panel.cboConfigs.setSelectedItem(_panel.cboConfigs
427: .getItemAt(0));
428: }
429: } catch (Exception e) {
430: throw new RuntimeException(e);
431: }
432: }
433: }
|