001: package net.sourceforge.squirrel_sql.plugins.hibernate;
002:
003: import net.sourceforge.squirrel_sql.client.preferences.GlobalPreferencesSheet;
004: import net.sourceforge.squirrel_sql.client.session.ISession;
005: import net.sourceforge.squirrel_sql.client.session.mainpanel.IMainPanelTab;
006: import net.sourceforge.squirrel_sql.fw.util.StringManager;
007: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
008: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
009: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
010: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
011: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
012: import net.sourceforge.squirrel_sql.plugins.hibernate.configuration.HibernateConfiguration;
013: import net.sourceforge.squirrel_sql.plugins.hibernate.configuration.HibernateConfigController;
014: import net.sourceforge.squirrel_sql.plugins.hibernate.configuration.HibernateConfigPanel;
015: import net.sourceforge.squirrel_sql.plugins.hibernate.mapping.MappedObjectPanelManager;
016:
017: import javax.swing.*;
018: import java.awt.*;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.prefs.Preferences;
026:
027: public class HibernateTabController implements IMainPanelTab,
028: IHibernateTabController, IHibernateConnectionProvider {
029:
030: private static final StringManager s_stringMgr = StringManagerFactory
031: .getStringManager(HibernateTabController.class);
032:
033: private static ILogger s_log = LoggerController
034: .createLogger(HibernateTabController.class);
035:
036: private HibernateTabPanel _panel;
037: private ISession _session;
038: private HibernatePlugin _plugin;
039: private static final String PREF_KEY_LAST_SELECTED_CONFIG = "SQuirreL.hibernateplugin.lastSelectedConfig";
040: private HibernateConnection _con;
041: private HibnerateConnector _hibnerateConnector;
042: private HibernatePluginResources _resource;
043: private HQLPanelController _hqlPanelController;
044: private ArrayList<ConnectionListener> _listeners = new ArrayList<ConnectionListener>();
045: private SQLPanelManager _sqlPanelManager;
046: private MappedObjectPanelManager _mappedObjectsPanelManager;
047:
048: public HibernateTabController(ISession session,
049: HibernatePlugin plugin, HibernatePluginResources resource) {
050: _resource = resource;
051: try {
052: _session = session;
053: _plugin = plugin;
054:
055: _hqlPanelController = new HQLPanelController(this ,
056: _session, resource);
057: _sqlPanelManager = new SQLPanelManager(_session);
058: _mappedObjectsPanelManager = new MappedObjectPanelManager(
059: this , _session, resource);
060:
061: _panel = new HibernateTabPanel(_mappedObjectsPanelManager
062: .getComponent(),
063: _hqlPanelController.getComponent(),
064: _sqlPanelManager.getComponent(), _resource);
065: _panel.btnConnected
066: .setIcon(resource
067: .getIcon(HibernatePluginResources.IKeys.DISCONNECTED_IMAGE));
068:
069: Dimension oldSize = _panel.btnConnected.getPreferredSize();
070: Dimension newSize = new Dimension(oldSize.width - 20,
071: oldSize.height);
072: _panel.btnConnected.setPreferredSize(newSize);
073:
074: _hibnerateConnector = new HibnerateConnector(
075: new HibnerateConnectorListener() {
076: public void connected(HibernateConnection con,
077: HibernateConfiguration cfg) {
078: onConnected(con, cfg);
079: }
080:
081: public void connectFailed(Throwable t) {
082: onConnectFailed(t);
083: }
084: });
085:
086: _panel.btnConnected.addActionListener(new ActionListener() {
087: public void actionPerformed(ActionEvent e) {
088: onConnect();
089: }
090: });
091:
092: _panel.btnOpenConfigs.setPreferredSize(_panel.btnConnected
093: .getPreferredSize());
094: _panel.btnOpenConfigs
095: .addActionListener(new ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097: onOpenConfigs();
098: }
099: });
100:
101: loadConfigsFromXml();
102:
103: _hqlPanelController.initActions();
104:
105: } catch (Exception e) {
106: throw new RuntimeException(e);
107: }
108: }
109:
110: private void onOpenConfigs() {
111: _plugin.setHibernatePrefsListener(new HibernatePrefsListener() {
112: public void configurationChanged(
113: ArrayList<HibernateConfiguration> changedCfgs) {
114: onConfigurationChanged(changedCfgs);
115: }
116:
117: public HibernateConfiguration getPreselectedCfg() {
118: return onGetPreselectedCfg();
119: }
120: });
121: GlobalPreferencesSheet.showSheet(_plugin.getApplication(),
122: HibernateConfigPanel.class);
123: }
124:
125: private HibernateConfiguration onGetPreselectedCfg() {
126: return (HibernateConfiguration) _panel.cboConfigurations
127: .getSelectedItem();
128: }
129:
130: private void onConfigurationChanged(
131: ArrayList<HibernateConfiguration> changedCfgs) {
132: HibernateConfiguration selCfg = (HibernateConfiguration) _panel.cboConfigurations
133: .getSelectedItem();
134:
135: if (null != selCfg) {
136: loadConfigs(changedCfgs, selCfg.getName());
137: } else {
138: loadConfigs(changedCfgs, null);
139: }
140: }
141:
142: private void loadConfigsFromXml() throws IOException, XMLException {
143: XMLBeanReader reader = new XMLBeanReader();
144: File pluginUserSettingsFolder = _plugin
145: .getPluginUserSettingsFolder();
146:
147: File xmlFile = new File(pluginUserSettingsFolder.getPath(),
148: HibernateConfigController.HIBERNATE_CONFIGS_XML_FILE);
149:
150: if (xmlFile.exists()) {
151: reader.load(xmlFile, _plugin.getClass().getClassLoader());
152: loadConfigs(reader, Preferences.userRoot().get(
153: PREF_KEY_LAST_SELECTED_CONFIG, null));
154: }
155: }
156:
157: private void loadConfigs(Iterable reader, String cfgNameToSelect) {
158: _panel.cboConfigurations.removeAllItems();
159:
160: HashMap<String, HibernateConfiguration> cfgByName = new HashMap<String, HibernateConfiguration>();
161: for (Object o : reader) {
162: HibernateConfiguration cfg = (HibernateConfiguration) o;
163:
164: cfgByName.put(cfg.getName(), cfg);
165: _panel.cboConfigurations.addItem(cfg);
166: }
167:
168: if (null != cfgNameToSelect) {
169: _panel.cboConfigurations.setSelectedItem(cfgByName
170: .get(cfgNameToSelect));
171: }
172: }
173:
174: private void onConnect() {
175: if (null == _con) {
176: if (null != _panel.cboConfigurations.getSelectedItem()) {
177: _panel.btnConnected.setEnabled(false);
178: _panel.btnConnected
179: .setDisabledSelectedIcon(_resource
180: .getIcon(HibernatePluginResources.IKeys.CONNECTING_IMAGE));
181: _panel.btnConnected.repaint();
182: _hibnerateConnector
183: .connect(
184: (HibernateConfiguration) _panel.cboConfigurations
185: .getSelectedItem(), _session);
186: } else {
187: _panel.btnConnected.setSelected(false);
188:
189: // i18n[HibernateTabController.noConfigSelected=Please select a Hibernate configuration to connect to.\nHibernate configurations can be defined in the global preferences window.\nWould you like to open the window now?]
190: int opt = JOptionPane
191: .showConfirmDialog(
192: _session.getApplication()
193: .getMainFrame(),
194: s_stringMgr
195: .getString("HQLTabController.noConfigSelected"));
196:
197: if (JOptionPane.YES_OPTION == opt) {
198: GlobalPreferencesSheet.showSheet(_plugin
199: .getApplication(),
200: HibernateConfigPanel.class);
201: }
202: }
203:
204: } else {
205: _panel.btnConnected
206: .setIcon(_resource
207: .getIcon(HibernatePluginResources.IKeys.DISCONNECTED_IMAGE));
208: try {
209: closeConnection();
210: } catch (Exception e) {
211: s_log.error(e);
212: } finally {
213: _con = null;
214: _hqlPanelController.setConnection(null);
215: }
216: }
217: }
218:
219: private void onConnected(HibernateConnection con,
220: HibernateConfiguration cfg) {
221: _con = con;
222: _panel.btnConnected
223: .setIcon(_resource
224: .getIcon(HibernatePluginResources.IKeys.CONNECTED_IMAGE));
225: _panel.btnConnected.setEnabled(true);
226: _hqlPanelController.setConnection(con);
227:
228: for (ConnectionListener listener : _listeners) {
229: listener.connectionOpened(con, cfg);
230: }
231:
232: }
233:
234: private void onConnectFailed(Throwable t) {
235: _panel.btnConnected
236: .setIcon(_resource
237: .getIcon(HibernatePluginResources.IKeys.DISCONNECTED_IMAGE));
238: _panel.btnConnected.setEnabled(true);
239: _panel.btnConnected.setSelected(false);
240: _session.showErrorMessage(t);
241: s_log.error(t);
242: _con = null;
243: _hqlPanelController.setConnection(null);
244:
245: }
246:
247: public String getTitle() {
248: // i18n[HibernateTabController.title=Hibernate]
249: return s_stringMgr.getString("HQLTabController.title");
250: }
251:
252: public String getHint() {
253: // i18n[HibernateTabController.hint=Support for Hibernate]
254: return s_stringMgr.getString("HQLTabController.hint");
255: }
256:
257: public Component getComponent() {
258: return _panel;
259: }
260:
261: public void sessionClosing(ISession session) {
262: HibernateConfiguration cfg = (HibernateConfiguration) _panel.cboConfigurations
263: .getSelectedItem();
264:
265: if (null != cfg) {
266: Preferences.userRoot().put(PREF_KEY_LAST_SELECTED_CONFIG,
267: cfg.getName());
268: }
269:
270: _panel.closing();
271:
272: _mappedObjectsPanelManager.closing();
273:
274: }
275:
276: public void select() {
277: _hqlPanelController.requestFocus();
278: }
279:
280: public void setSession(ISession session) {
281: }
282:
283: public void addToToolbar(AbstractAction action) {
284: JButton btn = new JButton(action);
285: Dimension size = btn.getPreferredSize();
286: size.height = _panel.btnConnected.getPreferredSize().height;
287: btn.setPreferredSize(size);
288:
289: _panel.addToToolbar(btn);
290: }
291:
292: public void displaySqls(ArrayList<String> sqls) {
293: _sqlPanelManager.displaySqls(sqls);
294: }
295:
296: public IHibernateConnectionProvider getHibernateConnectionProvider() {
297: return this ;
298: }
299:
300: public void sessionEnding() {
301: if (null != _con) {
302: closeConnection();
303: }
304: }
305:
306: private void closeConnection() {
307: _con.close();
308:
309: for (ConnectionListener listener : _listeners) {
310: listener.connectionClosed();
311: }
312: }
313:
314: public HibernateConnection getHibernateConnection() {
315: return _con;
316: }
317:
318: public void addConnectionListener(
319: ConnectionListener connectionListener) {
320: _listeners.add(connectionListener);
321: }
322:
323: }
|