001: package com.xoetrope.builder.database;
002:
003: import com.xoetrope.carousel.builder.XFormGeneratorInfo;
004: import com.xoetrope.carousel.builder.XFormGeneratorWizard;
005: import java.awt.Point;
006: import java.awt.datatransfer.DataFlavor;
007: import java.awt.datatransfer.Transferable;
008: import java.sql.Connection;
009: import java.sql.DatabaseMetaData;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Types;
013: import javax.swing.JComponent;
014: import net.xoetrope.editor.project.XEditorProject;
015: import net.xoetrope.editor.project.pages.BindingTransferHandler;
016: import net.xoetrope.editor.project.pages.IEditorUtility;
017: import net.xoetrope.editor.project.pages.PageDesigner;
018: import net.xoetrope.editor.dnd.PageDesignerDndHelper;
019: import net.xoetrope.editor.project.XEditorProjectManager;
020:
021: /**
022: * A transfer handler helper for the page designer
023: * <p> Copyright (c) Xoetrope Ltd., 2002-2007</p>
024: * <p> $Revision: 1.77 $</p>
025: * <p> License: see License.txt</p>
026: */
027: public class DatabaseFormBuilderDndHelper implements
028: PageDesignerDndHelper {
029: // This is dirty hack for quick prototyping
030: private DataFlavor importFlavor;
031: private String tableName;
032:
033: /** Creates a new instance of DatabaseFormBuilderDndHelper */
034: public DatabaseFormBuilderDndHelper() {
035: }
036:
037: /**
038: * Overridden to import a supported flavor if it is available.
039: */
040: public boolean importData(XEditorProject currentProject,
041: PageDesigner pd, JComponent c, Transferable t, Point pt) {
042: XFormGeneratorInfo builderInfo = getFormBuilderInfo(
043: currentProject, t);
044: builderInfo.setGenerationParameter("title", tableName);
045: builderInfo.setGenerationParameter("numColumns", "2");
046:
047: XFormGeneratorWizard wizard = new XFormGeneratorWizard(
048: currentProject, builderInfo);
049: wizard.setLocationRelativeTo(pd);
050: if (wizard.showDialog()) {
051: try {
052: Object tableObject = t.getTransferData(importFlavor);
053: BindingTransferHandler.registerDbTable(currentProject,
054: tableObject);
055: DatabaseFormGenerator formGenerator = new DatabaseFormGenerator(
056: currentProject, pd);
057: formGenerator.generatePanel(builderInfo, c, pt);
058: } catch (Exception ex) {
059: ex.printStackTrace();
060: }
061: }
062:
063: return true;
064: }
065:
066: public XFormGeneratorInfo getFormBuilderInfo(
067: XEditorProject currentProject, Transferable t) {
068: try {
069: Object tableObject = t.getTransferData(importFlavor);
070: IEditorUtility editorUtility = currentProject
071: .getEditorUtility();
072: Connection conn = editorUtility
073: .getDbConnection(tableObject);
074: tableName = editorUtility.getDbTableName(tableObject);
075:
076: DatabaseMetaData dbmd = conn.getMetaData();
077: ResultSet columnMetaData = dbmd.getColumns(null, null,
078: tableName.toUpperCase(), null);
079:
080: XFormGeneratorInfo fbInfo = new XFormGeneratorInfo();
081:
082: try {
083: String fields = "";
084: while (columnMetaData.next()) {
085: String columnName = columnMetaData
086: .getString("COLUMN_NAME");
087: fbInfo.addField(columnName);
088: fbInfo
089: .setProperty(columnName, "prefix",
090: columnName);
091:
092: int dataType = columnMetaData.getInt("DATA_TYPE");
093: switch (dataType) {
094: case -10: // NTEXT
095: case -9: // NVARCHAR
096: case Types.VARCHAR:
097: case Types.CHAR:
098: case Types.LONGVARCHAR:
099: break;
100:
101: case Types.DECIMAL:
102: fbInfo.setProperty(columnName, "component",
103: "TextArea");
104: break;
105:
106: case Types.BIT:
107: case Types.BOOLEAN:
108: fbInfo.setProperty(columnName, "component",
109: "CheckBox");
110: break;
111:
112: case Types.DATE:
113: case Types.TIME:
114: fbInfo.setProperty(columnName, "component",
115: "DateEdit");
116: break;
117:
118: case Types.DOUBLE:
119: case Types.FLOAT:
120: case Types.NUMERIC:
121: case Types.REAL:
122: fbInfo.setProperty(columnName, "format",
123: "decimal");
124: break;
125:
126: case Types.INTEGER:
127: case Types.SMALLINT:
128: case Types.TINYINT:
129: fbInfo.setProperty(columnName, "format", "int");
130: break;
131:
132: case Types.TIMESTAMP:
133: break;
134: }
135: }
136: } catch (SQLException ex) {
137: ex.printStackTrace();
138: }
139:
140: columnMetaData.close();
141:
142: return fbInfo;
143: } catch (Exception ex) {
144: ex.printStackTrace();
145: }
146:
147: return null;
148: }
149:
150: /**
151: * Overridden to include a check for a color flavor.
152: */
153: public boolean canImport(XEditorProject currentProject,
154: PageDesigner pd, JComponent c, DataFlavor[] flavors) {
155: if (!XEditorProjectManager
156: .isUserRegistered("XUI Pro Form Builder"))
157: return false;
158:
159: IEditorUtility editorUtility = currentProject
160: .getEditorUtility();
161:
162: for (DataFlavor df : flavors) {
163: if (editorUtility.canImportDbTable(df)) {
164: importFlavor = df;
165: return true;
166: }
167: }
168:
169: return false;
170: }
171: }
|