001: package net.sourceforge.squirrel_sql.plugins.hibernate.mapping;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.plugins.hibernate.ConnectionListener;
005: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernateConnection;
006: import net.sourceforge.squirrel_sql.plugins.hibernate.HibernatePluginResources;
007: import net.sourceforge.squirrel_sql.plugins.hibernate.IHibernateConnectionProvider;
008: import net.sourceforge.squirrel_sql.plugins.hibernate.configuration.HibernateConfiguration;
009:
010: import javax.swing.*;
011: import javax.swing.event.TreeExpansionEvent;
012: import javax.swing.event.TreeExpansionListener;
013: import javax.swing.event.TreeSelectionEvent;
014: import javax.swing.event.TreeSelectionListener;
015: import javax.swing.tree.DefaultMutableTreeNode;
016: import javax.swing.tree.DefaultTreeModel;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Collections;
022: import java.util.prefs.Preferences;
023:
024: public class MappedObjectPanelManager {
025:
026: private static final String PERF_KEY_OBJ_TAB_CHKSHOWQUALIFIED = "Squirrel.hibernateplugin.chkShowQualified";
027:
028: private MappedObjectPanel _panel;
029: private IHibernateConnectionProvider _connectionProvider;
030: private ISession _session;
031: private DefaultMutableTreeNode _root;
032: private HashMap<String, MappedClassInfo> _mappedClassInfoByClassName;
033: private DetailPanelController _detailPanelController;
034: private ArrayList<MappedClassInfoTreeWrapper> _mappedClassInfoTreeWrappers;
035:
036: public MappedObjectPanelManager(
037: IHibernateConnectionProvider connectionProvider,
038: ISession session, HibernatePluginResources resource) {
039: _connectionProvider = connectionProvider;
040: _session = session;
041:
042: _detailPanelController = new DetailPanelController();
043: _panel = new MappedObjectPanel(_detailPanelController
044: .getDetailComponent());
045:
046: _root = new DefaultMutableTreeNode(new MappingRoot());
047: _panel.objectTree.setModel(new DefaultTreeModel(_root));
048:
049: _panel.objectTree.setCellRenderer(new MappingTreeCellRenderer(
050: resource));
051:
052: nodeStructurChanged(_root);
053:
054: _panel.objectTree
055: .addTreeExpansionListener(new TreeExpansionListener() {
056: public void treeExpanded(TreeExpansionEvent event) {
057: onTreeExpanded(event);
058: }
059:
060: public void treeCollapsed(TreeExpansionEvent event) {
061: }
062: });
063:
064: _panel.objectTree
065: .addTreeSelectionListener(new TreeSelectionListener() {
066: public void valueChanged(TreeSelectionEvent e) {
067: onTreeSelectionChanged(e);
068: }
069: });
070:
071: _connectionProvider
072: .addConnectionListener(new ConnectionListener() {
073: public void connectionOpened(
074: HibernateConnection con,
075: HibernateConfiguration cfg) {
076: initRoot(con, cfg);
077: initTree(con);
078: }
079:
080: public void connectionClosed() {
081: onConnectionClosed();
082: }
083: });
084:
085: _panel.chkShowQualified.addActionListener(new ActionListener() {
086: public void actionPerformed(ActionEvent e) {
087: onChkQualified();
088: }
089: });
090:
091: _panel.chkShowQualified.setSelected(Preferences.userRoot()
092: .getBoolean(PERF_KEY_OBJ_TAB_CHKSHOWQUALIFIED, false));
093:
094: }
095:
096: private void initRoot(HibernateConnection con,
097: HibernateConfiguration cfg) {
098: MappingRoot mr = (MappingRoot) _root.getUserObject();
099:
100: mr.init(con, cfg);
101:
102: }
103:
104: private void onChkQualified() {
105: HibernateConnection con = _connectionProvider
106: .getHibernateConnection();
107:
108: if (null == con) {
109: return;
110: }
111:
112: _root.removeAllChildren();
113:
114: initTree(con);
115: }
116:
117: private void onTreeSelectionChanged(TreeSelectionEvent e) {
118: DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.getPath()
119: .getLastPathComponent();
120:
121: if (null != n && null != n.getUserObject()) {
122: _detailPanelController.selectionChanged(n.getUserObject());
123: } else {
124: _detailPanelController.clearDetail();
125: }
126: }
127:
128: private void onTreeExpanded(TreeExpansionEvent event) {
129: DefaultMutableTreeNode mappedClassInfoWrapperNode = (DefaultMutableTreeNode) event
130: .getPath().getLastPathComponent();
131:
132: Object userObject = mappedClassInfoWrapperNode.getUserObject();
133:
134: if (userObject instanceof MappedClassInfoTreeWrapper
135: && false == ((MappedClassInfoTreeWrapper) userObject)
136: .isExpanded()) {
137:
138: for (int i = 0; i < mappedClassInfoWrapperNode
139: .getChildCount(); ++i) {
140: DefaultMutableTreeNode propertyInfoNode = (DefaultMutableTreeNode) mappedClassInfoWrapperNode
141: .getChildAt(i);
142: PropertyInfoTreeWrapper propertyInfoTreeWrapper = (PropertyInfoTreeWrapper) propertyInfoNode
143: .getUserObject();
144: MappedClassInfo mappedClassInfo = propertyInfoTreeWrapper
145: .getMappedClassInfo();
146: addMappedClassInfoNode(
147: createMappedClassInfoTreeWrapper(mappedClassInfo),
148: propertyInfoNode);
149: }
150:
151: ((MappedClassInfoTreeWrapper) userObject).setExpanded(true);
152:
153: nodeStructurChanged(mappedClassInfoWrapperNode);
154: }
155: }
156:
157: private MappedClassInfoTreeWrapper createMappedClassInfoTreeWrapper(
158: MappedClassInfo mappedClassInfo) {
159: return new MappedClassInfoTreeWrapper(mappedClassInfo,
160: _panel.chkShowQualified.isSelected());
161: }
162:
163: private void onConnectionClosed() {
164: MappingRoot mr = (MappingRoot) _root.getUserObject();
165: mr.clear();
166:
167: _root.removeAllChildren();
168: nodeStructurChanged(_root);
169: _detailPanelController.clearDetail();
170: }
171:
172: private void initTree(HibernateConnection con) {
173: ArrayList<MappedClassInfo> mappedClassInfos = con
174: .getMappedClassInfos();
175:
176: ArrayList<MappedClassInfoTreeWrapper> wrappers = initMappedClassInfos(mappedClassInfos);
177:
178: for (MappedClassInfoTreeWrapper wrapper : wrappers) {
179: addMappedClassInfoNode(wrapper, _root);
180: }
181:
182: nodeStructurChanged(_root);
183: }
184:
185: private void nodeStructurChanged(DefaultMutableTreeNode node) {
186: ((DefaultTreeModel) _panel.objectTree.getModel())
187: .nodeStructureChanged(node);
188: }
189:
190: private void addMappedClassInfoNode(
191: MappedClassInfoTreeWrapper mappedClassInfoTreeWrapper,
192: DefaultMutableTreeNode parent) {
193: _mappedClassInfoTreeWrappers.add(mappedClassInfoTreeWrapper);
194:
195: DefaultMutableTreeNode mappedClassInfoNode = new DefaultMutableTreeNode(
196: mappedClassInfoTreeWrapper);
197:
198: PropertyInfo[] propertyInfos = mappedClassInfoTreeWrapper
199: .getMappedClassInfo().getAttributes();
200:
201: for (PropertyInfo propertyInfo : propertyInfos) {
202: String className = propertyInfo.getHibernatePropertyInfo()
203: .getClassName();
204: if (_mappedClassInfoByClassName.containsKey(className)) {
205: PropertyInfoTreeWrapper propertyInfoTreeWrapper = new PropertyInfoTreeWrapper(
206: propertyInfo, _mappedClassInfoByClassName
207: .get(className));
208: DefaultMutableTreeNode propertyInfoNode = new DefaultMutableTreeNode(
209: propertyInfoTreeWrapper);
210: mappedClassInfoNode.add(propertyInfoNode);
211: }
212: }
213:
214: parent.add(mappedClassInfoNode);
215: }
216:
217: private ArrayList<MappedClassInfoTreeWrapper> initMappedClassInfos(
218: ArrayList<MappedClassInfo> mappedClassInfos) {
219: ArrayList<MappedClassInfoTreeWrapper> ret = new ArrayList<MappedClassInfoTreeWrapper>();
220:
221: _mappedClassInfoByClassName = new HashMap<String, MappedClassInfo>();
222: for (MappedClassInfo mappedClassInfo : mappedClassInfos) {
223: _mappedClassInfoByClassName.put(mappedClassInfo
224: .getClassName(), mappedClassInfo);
225: ret.add(createMappedClassInfoTreeWrapper(mappedClassInfo));
226: }
227:
228: _mappedClassInfoTreeWrappers = new ArrayList<MappedClassInfoTreeWrapper>();
229:
230: Collections.sort(ret);
231:
232: return ret;
233: }
234:
235: public JComponent getComponent() {
236: return _panel;
237: }
238:
239: public void closing() {
240: _panel.closing();
241: Preferences.userRoot().putBoolean(
242: PERF_KEY_OBJ_TAB_CHKSHOWQUALIFIED,
243: _panel.chkShowQualified.isSelected());
244: }
245: }
|