001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb.tree;
023:
024: import it.frb.*;
025: import javax.swing.tree.DefaultMutableTreeNode;
026:
027: /** Oggetto che contiene delle funzioni utili
028: * al treeview.
029: */
030: public class CustomTreeUtil {
031: private java.io.File fl;
032: private String schema = null;
033:
034: /** Metodo che ritorna la root di un treeview
035: * caricato a partire .
036: * @param path Path di sistema da cui iniziare
037: * a caricare il treeview.
038: *
039: * Inserire "." se si vuole partire
040: * dalla posizione corrente.
041: * @param objParent Nodo padre del sottoalbero che
042: * verra' generato.
043: *
044: * Se il sottoalbero che sara' creato
045: * non ha padre allora settare il parametro a Null.
046: * @return DefaultMutableTreeNode
047: */
048: public DefaultMutableTreeNode getNodeRootDatabaseShema() {
049: java.sql.Connection conn = null;
050: java.sql.ResultSet rsTables = null;
051: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
052: "Tables");
053:
054: java.util.Vector vTables = new java.util.Vector();
055:
056: try {
057: conn = Connessione.getConnessione();
058:
059: java.sql.DatabaseMetaData dbMD = conn.getMetaData();
060:
061: java.util.Vector vSchemas = new java.util.Vector();
062:
063: java.sql.ResultSet rsSchemas = null;
064:
065: try {
066: rsSchemas = dbMD.getSchemas();
067:
068: while (rsSchemas.next()) {
069: String schemas = rsSchemas.getString("TABLE_SCHEM");
070: vSchemas.add(schemas);
071: }
072:
073: rsSchemas.close();
074:
075: Object[] possibilities = new Object[vSchemas.size()];
076:
077: for (int i = 0; i < vSchemas.size(); i++) {
078: possibilities[i] = (String) vSchemas.get(i);
079: }
080:
081: if (vSchemas.size() != 1) {
082: schema = (String) javax.swing.JOptionPane
083: .showInputDialog(
084: null,
085: "You choose the scheme.",
086: "Scheme present on the Database",
087: javax.swing.JOptionPane.INFORMATION_MESSAGE,
088: null, possibilities,
089: possibilities[0]);
090: }
091:
092: } catch (java.sql.SQLException s) {
093: }
094:
095: if (schema != null) {
096: rsTables = dbMD.getTables(null, schema, null, null);
097: } else {
098: rsTables = dbMD.getTables(null, null, null, null);
099: }
100:
101: while (rsTables.next()) {
102: String tableName = rsTables.getString("TABLE_NAME");
103: String ttype = rsTables.getString("TABLE_TYPE");
104:
105: if (ttype.equals("TABLE")) {
106: DefaultMutableTreeNode nTable = new DefaultMutableTreeNode(
107: tableName);
108: DefaultMutableTreeNode nColonne = new DefaultMutableTreeNode(
109: "Colonne");
110: DefaultMutableTreeNode nPrimaryKey = new DefaultMutableTreeNode(
111: "Primary Key");
112: DefaultMutableTreeNode nPrimaryKeyExp = new DefaultMutableTreeNode(
113: "Primary Exported");
114: DefaultMutableTreeNode nForeignKey = new DefaultMutableTreeNode(
115: "Foreign Key");
116: DefaultMutableTreeNode nIndexKey = new DefaultMutableTreeNode(
117: "Index");
118: nTable.add(nColonne);
119: nTable.add(nPrimaryKey);
120: nTable.add(nPrimaryKeyExp);
121: nTable.add(nForeignKey);
122: nTable.add(nIndexKey);
123:
124: root.add(nTable);
125: }
126: }
127:
128: rsTables.close();
129: boolean bMltStm = ConfigurationProperties.properties()
130: .getUseMultiTransaction(false);
131:
132: if (!bMltStm) {
133: conn.close();
134: }
135:
136: } catch (java.sql.SQLException s) {
137: javax.swing.JOptionPane.showMessageDialog(null, s
138: .getMessage());
139:
140: } finally {
141: try {
142: boolean bMltStm = ConfigurationProperties.properties()
143: .getUseMultiTransaction(false);
144:
145: if (!bMltStm) {
146: conn.close();
147: }
148:
149: } catch (java.sql.SQLException s) {
150: javax.swing.JOptionPane.showMessageDialog(null, s
151: .getMessage());
152: }
153: }
154:
155: return root;
156: }
157:
158: public String getSchema() {
159: return schema;
160: }
161:
162: public static javax.swing.tree.TreePath getChildPath(
163: javax.swing.JTree jT, javax.swing.tree.TreePath pathParent,
164: int nChildIndex) {
165: javax.swing.tree.TreeModel model = jT.getModel();
166: return pathParent.pathByAddingChild(model.getChild(pathParent
167: .getLastPathComponent(), nChildIndex));
168: }
169:
170: public static boolean isRootPath(javax.swing.JTree jT,
171: javax.swing.tree.TreePath path) {
172: return jT.isRootVisible() && jT.getRowForPath(path) == 0;
173: }
174:
175: public static void sayWhat(javax.swing.event.TreeModelEvent e) {
176: System.out.println(e.getTreePath().getLastPathComponent());
177: int[] nIndex = e.getChildIndices();
178: for (int i = 0; i < nIndex.length; i++) {
179: System.out.println(i + ". " + nIndex[i]);
180: }
181: }
182:
183: /** Fornito un oggetto TreePath, torna il path sotto
184: * forma di stringa.
185: * @param asObjTreePath Oggetto TreePath di cui si vuole il path
186: * in formato stringa.
187: * @return String
188: */
189: public static String getStringFromTreePath(
190: javax.swing.tree.TreePath asObjTreePath) {
191: String path = asObjTreePath.getPathComponent(0).toString();
192: for (int i = 1; i < asObjTreePath.getPathCount(); i++) {
193: if (i == 1) {
194: path = path + asObjTreePath.getPathComponent(i);
195: } else {
196: path = path + "/" + asObjTreePath.getPathComponent(i);
197: }
198: }
199: return path;
200: }
201: }
|