001: /*
002: * ConfigParamsTreeView.java
003: *
004: * Created on November 20, 2006, 7:04 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009: package org.netbeans.modules.etl.ui.view;
010:
011: import java.awt.BorderLayout;
012: import java.awt.Component;
013: import java.beans.PropertyChangeEvent;
014: import java.beans.PropertyChangeListener;
015: import java.io.BufferedReader;
016: import java.io.File;
017: import java.io.FileReader;
018: import java.net.URL;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import javax.swing.ImageIcon;
023: import javax.swing.JLabel;
024: import javax.swing.JPanel;
025: import javax.swing.JScrollPane;
026: import javax.swing.JTree;
027: import javax.swing.event.TreeSelectionEvent;
028: import javax.swing.event.TreeSelectionListener;
029: import javax.swing.tree.DefaultMutableTreeNode;
030: import javax.swing.tree.DefaultTreeCellRenderer;
031: import javax.swing.tree.DefaultTreeModel;
032: import javax.swing.tree.TreePath;
033:
034: import org.w3c.dom.Node;
035: import org.w3c.dom.Element;
036:
037: import org.netbeans.modules.etl.ui.DataObjectHelper;
038: import org.netbeans.modules.etl.ui.ETLDataObject;
039: import org.netbeans.modules.sql.framework.common.jdbc.SQLDBConnectionDefinition;
040: import org.netbeans.modules.sql.framework.model.SQLConstants;
041: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
042: import org.netbeans.modules.sql.framework.model.SQLDBModel;
043: import org.netbeans.modules.sql.framework.model.SQLDBTable;
044: import org.netbeans.modules.sql.framework.model.SQLDefinition;
045: import org.netbeans.modules.sql.framework.model.SourceTable;
046: import org.netbeans.modules.sql.framework.model.TargetTable;
047: import org.netbeans.modules.sql.framework.model.impl.SQLDefinitionImpl;
048: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyGroup;
049: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertySheet;
050: import org.netbeans.modules.sql.framework.ui.editor.property.impl.PropertyViewManager;
051: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
052: import net.java.hulp.i18n.Logger;
053: import com.sun.sql.framework.utils.XmlUtil;
054: import org.netbeans.modules.etl.logger.Localizer;
055: import org.netbeans.modules.etl.logger.LogUtil;
056: import org.netbeans.modules.sql.framework.model.DBTable;
057:
058: /**
059: *
060: * @author karthik
061: */
062: public class ConfigParamsTreeView extends JPanel implements
063: PropertyChangeListener {
064:
065: private String LOG_CATEGORY = DBModelTreeView.class.getName();
066: private static transient final Logger mLogger = LogUtil
067: .getLogger(ConfigParamsTreeView.class.getName());
068: private static transient final Localizer mLoc = Localizer.get();
069: private JTree tree;
070: private List srcdbModels;
071: private List tgtdbModels;
072: private ConfigureParametersPanel editPanel;
073: private Component comp;
074: private IPropertySheet propSheet;
075: private ETLDataObject dObj;
076: private File confFile;
077: private SQLDefinition sqlDefn;
078: private final String CONFIG_FILE_PATH = "\\..\\..\\nbproject\\config\\";
079: private final String CONFIG_FILE = ".conf";
080: private final String ETL_CONFIG_TAG = "ETLConfig";
081: private final String ETL_COLLAB_TAG = "ETLCollaboration";
082: private final String ETL_COLLAB_ATTR_1 = "name";
083: private final String ETL_SOURCE_TAG = "source";
084: private final String ETL_TARGET_TAG = "target";
085: private final String ETL_CONNECTION_TAG = "jdbcConnection";
086: private final String ETL_CONNECTION_ATTR_1 = "name";
087: private final String ETL_CONNECTION_ATTR_2 = "url";
088: private final String ETL_CONNECTION_ATTR_3 = "username";
089: private final String ETL_CONNECTION_ATTR_4 = "password";
090: private final String ETL_CONNECTION_ATTR_5 = "schema";
091: private final String ETL_CONNECTION_ATTR_6 = "catalog";
092: private final String ETL_CONNECTION_ATTR_7 = "dbTable";
093: private final String ETL_CONNECTION_ATTR_8 = "dataDir";
094: private static URL rootImgUrl = ConfigParamsTreeView.class
095: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/root.png");
096: private static URL columnImgUrl = ConfigParamsTreeView.class
097: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/column.png");
098: private static URL tableImgUrl = ConfigParamsTreeView.class
099: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/SourceTable.png");
100: private static URL targetTableImgUrl = ConfigParamsTreeView.class
101: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/TargetTable.png");
102: private static ImageIcon rootIcon;
103: private static ImageIcon tableIcon;
104: private static ImageIcon targetTableIcon;
105: private static ImageIcon columnIcon;
106: private IPropertyGroup pGroup;
107:
108: static {
109: rootIcon = new ImageIcon(rootImgUrl);
110: tableIcon = new ImageIcon(tableImgUrl);
111: columnIcon = new ImageIcon(columnImgUrl);
112: targetTableIcon = new ImageIcon(targetTableImgUrl);
113: }
114:
115: /**
116: * Creates a new instance of ConfigParamsTreeView.
117: *
118: * @param mObj etlDataObject containing designtime db parameters
119: * @param editPanel ConfigureParametersPanel associated with this view
120: */
121: public ConfigParamsTreeView(ETLDataObject mObj,
122: ConfigureParametersPanel editPanel) {
123: super ();
124: this .dObj = mObj;
125: this .editPanel = editPanel;
126: this .confFile = new File(mObj.getPrimaryFile().getPath()
127: + CONFIG_FILE_PATH + mObj.getName() + CONFIG_FILE);
128: this .srcdbModels = getDBModels(true);
129: this .tgtdbModels = getDBModels(false);
130: initGui();
131: }
132:
133: private void initGui() {
134:
135: this .setLayout(new BorderLayout());
136: DefaultTreeModel treeModel = createTreeModel();
137: tree = new JTree();
138:
139: tree.setCellRenderer(new TableTreeCellRenderer());
140: tree.addTreeSelectionListener(new TreeSelectionListener() {
141:
142: public void valueChanged(TreeSelectionEvent e) {
143: if (propSheet != null) {
144: propSheet.commitChanges();
145: if (pGroup != null) {
146: pGroup
147: .removePropertyChangeListener(ConfigParamsTreeView.this );
148: }
149: tree.repaint();
150: }
151:
152: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
153: .getLastSelectedPathComponent();
154: if (node == null) {
155: return;
156: }
157:
158: Object nodeInfo = node.getUserObject();
159: if (nodeInfo instanceof SQLDBModel) {
160: if (comp != null) {
161: editPanel.remove(comp);
162: }
163: PropertyViewManager pvMgr = DataObjectHelper
164: .getPropertyViewManager();
165: DBModelObj dbObj = new DBModelObj(
166: (SQLDBModel) nodeInfo);
167: propSheet = pvMgr
168: .getPropertySheet(dbObj, "DBModel");
169: comp = propSheet.getPropertySheet();
170: editPanel.setRightComponent(comp);
171: editPanel.updateUI();
172: } else if (nodeInfo instanceof DBTable) {
173: if (comp != null) {
174: editPanel.remove(comp);
175: }
176: PropertyViewManager pvMgr = DataObjectHelper
177: .getPropertyViewManager();
178: DBTableObj dbTab = new DBTableObj(
179: (SQLDBTable) nodeInfo);
180: propSheet = pvMgr
181: .getPropertySheet(dbTab, "DBTable");
182: pGroup = propSheet.getPropertyGroup("default");
183: if (pGroup != null) {
184: pGroup
185: .addPropertyChangeListener(ConfigParamsTreeView.this );
186: }
187: comp = propSheet.getPropertySheet();
188: editPanel.setRightComponent(comp);
189: editPanel.updateUI();
190: } else {
191: editPanel.setRightComponent(new JPanel());
192: editPanel.updateUI();
193: }
194: }
195: });
196:
197: tree.setModel(treeModel);
198:
199: tree.setRootVisible(false);
200: tree.setDragEnabled(true);
201: tree.setShowsRootHandles(true);
202:
203: Object root = treeModel.getRoot();
204: Object rootFirstChild = treeModel.getChild(root, 0);
205:
206: Object pathArray[] = new Object[2];
207: pathArray[0] = root;
208: pathArray[1] = rootFirstChild;
209:
210: TreePath tpath = new TreePath(pathArray);
211: tree.setSelectionPath(tpath);
212: JScrollPane treePane = new JScrollPane(tree);
213: this .add(BorderLayout.CENTER, treePane);
214: }
215:
216: /**
217: * Gets current IPropertySheet instance, if any.
218: *
219: * @return current IPropertySheet, possibly null.
220: */
221: public IPropertySheet getPropSheet() {
222: return propSheet;
223: }
224:
225: /**
226: * Gets sql definition data corresponding to the collab.
227: *
228: * @return SQLDefinition representing the collab DB models.
229: */
230: public SQLDefinition getData() {
231: return sqlDefn;
232: }
233:
234: private DefaultTreeModel createTreeModel() {
235: DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
236: "Configuration");
237: DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
238: DefaultMutableTreeNode srcNode = new DefaultMutableTreeNode(
239: "Source");
240: DefaultMutableTreeNode tgtNode = new DefaultMutableTreeNode(
241: "Target");
242: rootNode.add(srcNode);
243: Iterator it = srcdbModels.iterator();
244: while (it.hasNext()) {
245: SQLDBModel dbModel = (SQLDBModel) it.next();
246: DefaultMutableTreeNode dbModelNode = new DefaultMutableTreeNode(
247: dbModel);
248: srcNode.add(dbModelNode);
249: createTableNodes(dbModel, dbModelNode);
250: }
251: rootNode.add(tgtNode);
252: it = tgtdbModels.iterator();
253: while (it.hasNext()) {
254: SQLDBModel dbModel = (SQLDBModel) it.next();
255: DefaultMutableTreeNode dbModelNode = new DefaultMutableTreeNode(
256: dbModel);
257: tgtNode.add(dbModelNode);
258: createTableNodes(dbModel, dbModelNode);
259: }
260: return treeModel;
261: }
262:
263: private void createTableNodes(SQLDBModel dbModel,
264: DefaultMutableTreeNode dbModelNode) {
265: Iterator it = dbModel.getTables().iterator();
266: while (it.hasNext()) {
267: DBTable table = (DBTable) it.next();
268: DefaultMutableTreeNode tableNode = new TableNode(table);
269: dbModelNode.add(tableNode);
270: }
271: }
272:
273: private class TableNode extends DefaultMutableTreeNode {
274:
275: private SQLDBTable table;
276:
277: public TableNode(Object userObj) {
278: super (userObj);
279: this .table = (SQLDBTable) userObj;
280: }
281:
282: public String toString() {
283: String displayPrefix = table.getSchema();
284: displayPrefix += (((displayPrefix != null) && displayPrefix
285: .trim().length() != 0) ? "." : "");
286:
287: return displayPrefix + table.getQualifiedName();
288: }
289: }
290:
291: /**
292: * This method gets called when a bound property is changed.
293: *
294: * @param evt A PropertyChangeEvent object describing the event source and the
295: * property that has changed.
296: */
297: public void propertyChange(PropertyChangeEvent evt) {
298: if (propSheet != null) {
299: propSheet.commitChanges();
300: }
301: tree.repaint();
302: }
303:
304: /**
305: * get the DBModels list from the config file if present or from the etl definition file.
306: *
307: * @param isSource type of DBModel
308: *
309: */
310: private List getDBModels(boolean isSource) {
311: Node rootNode = null;
312: try {
313: Element element = XmlUtil.loadXMLFile(new BufferedReader(
314: new FileReader(this .confFile)));
315: rootNode = (Node) element;
316: } catch (Exception ex) {
317: mLogger.infoNoloc(mLoc.t(
318: "PRSR041: ConfigParamsTreeView.class.getName(){0}",
319: ex.getMessage()));
320: }
321: Node node;
322: if (rootNode != null) {
323: Node sqlNode = rootNode.getFirstChild();
324: try {
325: sqlDefn = new SQLDefinitionImpl((Element) sqlNode);
326: } catch (Exception ex) {
327: mLogger
328: .infoNoloc(mLoc
329: .t(
330: "PRSR042: ConfigParamsTreeView.class.getName(){0}",
331: ex.getMessage()));
332: }
333: }
334: if (isSource) {
335: return sqlDefn.getSourceDatabaseModels();
336: } else {
337: return sqlDefn.getTargetDatabaseModels();
338: }
339: }
340:
341: /**
342: * Wrapper object around an instance of SQLDBModel. Required to restrict
343: * getter and setter access to SQLDBModel instances by PropertySheet
344: * implementations.
345: */
346: public class DBModelObj {
347:
348: private SQLDBModel dbModel;
349: private SQLDBModel orgDBModel;
350:
351: /**
352: * Creates an instance of DBModelObj associated with the given
353: * SQLDBModel.
354: *
355: * @param dbModel SQLDBModel to be wrapped
356: */
357: public DBModelObj(SQLDBModel dbModel) {
358: this .dbModel = dbModel;
359: this .orgDBModel = dbModel;
360: }
361:
362: /**
363: * Gets user name.
364: *
365: * @return current user name
366: */
367: public String getUserName() {
368: return dbModel.getConnectionDefinition().getUserName();
369: }
370:
371: /**
372: * Sets username with given String.
373: *
374: * @param userName new user name
375: */
376: public void setUserName(String userName) {
377: ((SQLDBConnectionDefinition) this .dbModel
378: .getConnectionDefinition()).setUserName(userName);
379: try {
380: sqlDefn.removeObject(orgDBModel);
381: sqlDefn.addObject(dbModel);
382: orgDBModel = dbModel;
383: } catch (Exception ex) {
384: // ignore
385: }
386: }
387:
388: /**
389: * Gets current password.
390: *
391: * @return current password
392: */
393: public String getPassword() {
394: return dbModel.getConnectionDefinition().getPassword();
395: }
396:
397: /**
398: * Sets password with given String.
399: *
400: * @param password new user name
401: */
402: public void setPassword(String password) {
403: ((SQLDBConnectionDefinition) this .dbModel
404: .getConnectionDefinition()).setPassword(password);
405: try {
406: sqlDefn.removeObject(orgDBModel);
407: sqlDefn.addObject(dbModel);
408: orgDBModel = dbModel;
409: } catch (Exception ex) {
410: // ignore
411: }
412: }
413:
414: /**
415: * Gets current connection URL.
416: *
417: * @return current connection URL
418: */
419: public String getConnectionURL() {
420: return dbModel.getConnectionDefinition().getConnectionURL();
421: }
422:
423: /**
424: * Sets connection URL with given String.
425: *
426: * @param newURL new connection URL
427: */
428: public void setConnectionURL(String newURL) {
429: ((SQLDBConnectionDefinition) this .dbModel
430: .getConnectionDefinition())
431: .setConnectionURL(newURL);
432: try {
433: sqlDefn.removeObject(orgDBModel);
434: sqlDefn.addObject(dbModel);
435: orgDBModel = dbModel;
436: } catch (Exception ex) {
437: // ignore
438: }
439: }
440: }
441:
442: /**
443: * Wrapper object around an instance of AbstractDBTable. Required to restrict getter
444: * and setter access to AbstractDBTable instances by PropertySheet implementations.
445: */
446: public class DBTableObj {
447:
448: private SQLDBTable dbTable;
449: private SQLDBTable orgDBTable;
450:
451: /**
452: * Creates an instance of DBTableObj associated with the given AbstractDBTable.
453: *
454: * @param dbTable AbstractDBTable to be wrapped
455: */
456: public DBTableObj(SQLDBTable dbTable) {
457: this .orgDBTable = dbTable;
458: this .dbTable = dbTable;
459: }
460:
461: /**
462: * Sets current schema name.
463: *
464: * @param newSchema new catalog name
465: */
466: public void setSchema(String newSchema) {
467: dbTable.setSchema(newSchema);
468: try {
469: SQLDBModel dbModel = (SQLDBModel) dbTable.getParent();
470: sqlDefn.removeObject(dbModel);
471: dbModel.deleteTable(dbTable.getName());
472: sqlDefn.addObject(dbModel);
473: orgDBTable = dbTable;
474: } catch (Exception ex) {
475: // ignore
476: }
477: }
478:
479: /**
480: * Gets current schema name.
481: *
482: * @return current catalog name
483: */
484: public String getSchema() {
485: return dbTable.getSchema();
486: }
487:
488: /**
489: * Gets current catalog name.
490: *
491: * @return current catalog name
492: */
493: public String getCatalog() {
494: return dbTable.getCatalog();
495: }
496:
497: /**
498: * Sets current catalog name.
499: *
500: * @param newCatalog new catalog name
501: */
502: public void setCatalog(String newCatalog) {
503: dbTable.setCatalog(newCatalog);
504: try {
505: SQLDBModel dbModel = (SQLDBModel) dbTable.getParent();
506: sqlDefn.removeObject(dbModel);
507: dbModel.deleteTable(dbTable.getName());
508: sqlDefn.addObject(dbModel);
509: orgDBTable = dbTable;
510: } catch (Exception ex) {
511: // ignore
512: }
513: }
514: }
515:
516: private class TableTreeCellRenderer extends DefaultTreeCellRenderer {
517:
518: /**
519: * Configures the renderer based on the passed in components. The value is set
520: * from messaging the tree with <code>convertValueToText</code>, which
521: * ultimately invokes <code>toString</code> on <code>value</code>. The
522: * foreground color is set based on the selection and the icon is set based on on
523: * leaf and expanded.
524: */
525: public Component getTreeCellRendererComponent(JTree tree1,
526: Object value, boolean sel, boolean expanded,
527: boolean leaf, int row, boolean hasFocus1) {
528:
529: JLabel renderer = (JLabel) super
530: .getTreeCellRendererComponent(tree1, value, sel,
531: expanded, leaf, row, hasFocus1);
532: if (value instanceof DefaultMutableTreeNode) {
533: DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
534: Object obj = treeNode.getUserObject();
535: if (obj instanceof SQLDBModel) {
536: SQLDBModel dbModel = (SQLDBModel) obj;
537: String dbName = dbModel.getModelName();
538: String nbBundle1 = mLoc.t("PRSR001: {0} [Source]",
539: dbName);
540: String nbBundle2 = mLoc.t("PRSR001: {0} [Target]",
541: dbName);
542: switch (dbModel.getObjectType()) {
543: case SQLConstants.SOURCE_DBMODEL:
544: dbName = Localizer.parse(nbBundle1);
545: break;
546:
547: case SQLConstants.TARGET_DBMODEL:
548: dbName = Localizer.parse(nbBundle2);
549: break;
550:
551: default:
552: break;
553: }
554: renderer.setText(dbName);
555: renderer.setIcon(rootIcon);
556: renderer.setToolTipText(dbName);
557: } else if (obj instanceof SourceTable) {
558: renderer.setIcon(tableIcon);
559: renderer.setToolTipText(UIUtil
560: .getTableToolTip((SQLDBTable) obj));
561: } else if (obj instanceof TargetTable) {
562: renderer.setIcon(targetTableIcon);
563: renderer.setToolTipText(UIUtil
564: .getTableToolTip((SQLDBTable) obj));
565: } else if (obj instanceof SQLDBColumn) {
566: renderer.setIcon(columnIcon);
567: renderer.setToolTipText(UIUtil
568: .getColumnToolTip((SQLDBColumn) obj));
569: }
570: }
571: return renderer;
572: }
573: }
574: }
|