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.tree.drag.*;
025: import it.frb.*;
026: import java.awt.Cursor;
027: import java.sql.SQLException;
028: import javax.swing.*;
029: import javax.swing.JOptionPane;
030: import javax.swing.tree.DefaultMutableTreeNode;
031: import javax.swing.tree.TreePath;
032:
033: /** TreeView
034: */
035: public class CustomTree extends javax.swing.JTree implements
036: InterfacciaDragDrop, javax.swing.event.TreeExpansionListener,
037: javax.swing.event.TreeModelListener,
038: javax.swing.event.TreeWillExpandListener {
039:
040: public DragDropTreeView dragDropTreeView;
041: public TreePath pathNodeSelected = null;
042:
043: private JTextArea sqlEditorDatapanel;
044:
045: private javax.swing.JPanel datapanel;
046: private javax.swing.JFrame parentFrame;
047: private CustomFileView fileView;
048: private CustomTreeUtil jtreeUtil;
049:
050: /** Costruttore di un treeview vuoto.
051: */
052: public CustomTree() {
053: super ();
054: this .addTreeExpansionListener(this );
055: this .addTreeWillExpandListener(this );
056: this .setMouseTreeViewListener();
057: fileView = new CustomFileView();
058: this .setFileExtension();
059: dragDropTreeView = new DragDropTreeView(this , this );
060: }
061:
062: /** Costruttore a partire da un sottoalbero.
063: * @param asRoot Root dell' albero da caricare
064: * ne treeview.
065: */
066: public CustomTree(DefaultMutableTreeNode asRoot) {
067: super (asRoot);
068: this .addTreeExpansionListener(this );
069: this .addTreeWillExpandListener(this );
070: this .setMouseTreeViewListener();
071: fileView = new CustomFileView();
072: this .setFileExtension();
073: dragDropTreeView = new DragDropTreeView(this , this );
074: }
075:
076: /** Costruttore di un treeview a partire
077: * da un oggetto TreeModel.
078: * @param asRoot Root dell' albero che verra'
079: * caricato nell treeview.
080: */
081: public CustomTree(javax.swing.tree.TreeModel asRoot,
082: JTextArea sqlEditorDatapanel) {
083: super (asRoot);
084: this .sqlEditorDatapanel = sqlEditorDatapanel;
085: this .addTreeExpansionListener(this );
086: this .addTreeWillExpandListener(this );
087: this .setMouseTreeViewListener();
088: fileView = new CustomFileView();
089: this .setFileExtension();
090: dragDropTreeView = new DragDropTreeView(this , this ,
091: sqlEditorDatapanel);
092: }
093:
094: private void setFileExtension() {
095: }
096:
097: /** Ritorna l' oggetto CustomFileView.
098: * @return CustomFileView
099: */
100: public CustomFileView getCustomFileView() {
101: return fileView;
102: }
103:
104: /** Ritorna l' oggetto EsploraRisorse.
105: * @return
106: */
107: public javax.swing.JFrame getParentFrame() {
108: return parentFrame;
109: }
110:
111: /** Comunica a treeview qual' è il
112: * frame che lo contiene.
113: * @param asParentFrame JFrame
114: */
115: public void setParentFrame(javax.swing.JFrame asParentFrame) {
116: parentFrame = asParentFrame;
117: }
118:
119: /** Setta sul treeview il Listener
120: * del mouse.
121: */
122: public void setMouseTreeViewListener() {
123: this .addMouseListener(new CustomMouseAdapterWithPopup(this ));
124: }
125:
126: public void setTreeUtil(CustomTreeUtil treeUtil) {
127: this .jtreeUtil = treeUtil;
128: }
129:
130: /** Metodo che scatta quando si è espanso
131: * un nodo.
132: *
133: * @param event TreeExpansionEvent
134: */
135: public void treeExpanded(javax.swing.event.TreeExpansionEvent event) {
136: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
137: TreePath treePathNodeExpand = event.getPath();
138: this .setSelectionPath(treePathNodeExpand);
139: }
140:
141: /** Metodo che scatta quando si è collassato
142: * un nodo.
143: * @param event TreeExpansionEvent
144: */
145: public void treeCollapsed(javax.swing.event.TreeExpansionEvent event) {
146: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
147: TreePath treePathNodeColl = event.getPath();
148: this .setSelectionPath(treePathNodeColl);
149: }
150:
151: /** Metodo che scatta quando si sta espandendo
152: * un nodo.
153: * @param e TreeExpansionEvent
154: */
155: public void treeWillExpand(javax.swing.event.TreeExpansionEvent e) {
156: java.sql.Connection conn = null;
157: TreePath path = e.getPath();
158: String nodeLabel = path.toString();
159:
160: int index = nodeLabel.lastIndexOf("Colonne");
161:
162: if (index != -1) {
163: String LabelTable = (String) e.getPath().getParentPath()
164: .toString();
165: String nodeLabelTable = LabelTable.substring(9,
166: LabelTable.length() - 1).trim();
167:
168: DefaultMutableTreeNode nColonne = (DefaultMutableTreeNode) e
169: .getPath().getLastPathComponent();
170:
171: try {
172: conn = Connessione.getConnessione();
173:
174: java.sql.DatabaseMetaData dbMD = conn.getMetaData();
175:
176: java.sql.ResultSet rsColonneTabella = dbMD.getColumns(
177: null, jtreeUtil.getSchema(), nodeLabelTable,
178: null);
179:
180: while (rsColonneTabella.next()) {
181: String sNameColoumn = rsColonneTabella
182: .getString("COLUMN_NAME");
183: String sTypeColoumn = rsColonneTabella
184: .getString("TYPE_NAME");
185: String sSizeColoumn = rsColonneTabella
186: .getString("COLUMN_SIZE");
187: String sDecimalDigit = rsColonneTabella
188: .getString("DECIMAL_DIGITS");
189: DefaultMutableTreeNode nNameColoumn = new DefaultMutableTreeNode(
190: sNameColoumn + " (" + sTypeColoumn + " "
191: + sSizeColoumn + "|"
192: + sDecimalDigit + ")");
193: nNameColoumn.setAllowsChildren(false);
194: nColonne.add(nNameColoumn);
195: }
196:
197: rsColonneTabella.close();
198:
199: boolean bMltStm = ConfigurationProperties.properties()
200: .getUseMultiTransaction(false);
201:
202: if (!bMltStm) {
203: conn.close();
204: }
205: } catch (SQLException s) {
206: JOptionPane.showMessageDialog(this , s.getMessage());
207: } finally {
208: try {
209: boolean bMltStm = ConfigurationProperties
210: .properties().getUseMultiTransaction(false);
211:
212: if (!bMltStm) {
213: conn.close();
214: }
215: } catch (SQLException s) {
216: JOptionPane.showMessageDialog(this , s.getMessage());
217: }
218: }
219: }
220:
221: index = nodeLabel.lastIndexOf("Primary Key");
222:
223: if (index != -1) {
224: String LabelTable = (String) e.getPath().getParentPath()
225: .toString();
226: String nodeLabelTable = LabelTable.substring(9,
227: LabelTable.length() - 1).trim();
228:
229: DefaultMutableTreeNode nColonne = (DefaultMutableTreeNode) e
230: .getPath().getLastPathComponent();
231:
232: try {
233: conn = Connessione.getConnessione();
234:
235: java.sql.DatabaseMetaData dbMD = conn.getMetaData();
236:
237: java.sql.ResultSet rsPrimaryKey = dbMD.getPrimaryKeys(
238: null, jtreeUtil.getSchema(), nodeLabelTable);
239:
240: while (rsPrimaryKey.next()) {
241: String sNamePRColumn = rsPrimaryKey
242: .getString("COLUMN_NAME");
243: String sNamePR = rsPrimaryKey.getString("PK_NAME");
244: DefaultMutableTreeNode nNameColoumn = new DefaultMutableTreeNode(
245: sNamePRColumn + " (" + sNamePR + ")");
246: nNameColoumn.setAllowsChildren(false);
247: nColonne.add(nNameColoumn);
248: }
249:
250: rsPrimaryKey.close();
251:
252: boolean bMltStm = ConfigurationProperties.properties()
253: .getUseMultiTransaction(false);
254:
255: if (!bMltStm) {
256: conn.close();
257: }
258:
259: } catch (SQLException s) {
260: JOptionPane.showMessageDialog(this , s.getMessage());
261:
262: } finally {
263: try {
264: boolean bMltStm = ConfigurationProperties
265: .properties().getUseMultiTransaction(false);
266:
267: if (!bMltStm) {
268: conn.close();
269: }
270:
271: } catch (SQLException s) {
272: JOptionPane.showMessageDialog(this , s.getMessage());
273: }
274: }
275: }
276:
277: index = nodeLabel.lastIndexOf("Primary Exported");
278:
279: if (index != -1) {
280: String LabelTable = (String) e.getPath().getParentPath()
281: .toString();
282: String nodeLabelTable = LabelTable.substring(9,
283: LabelTable.length() - 1).trim();
284:
285: DefaultMutableTreeNode nColonne = (DefaultMutableTreeNode) e
286: .getPath().getLastPathComponent();
287:
288: try {
289: conn = Connessione.getConnessione();
290:
291: java.sql.DatabaseMetaData dbMD = conn.getMetaData();
292:
293: java.sql.ResultSet rsPriKeyTabella = dbMD
294: .getExportedKeys(null, jtreeUtil.getSchema(),
295: nodeLabelTable);
296:
297: while (rsPriKeyTabella.next()) {
298: String sNamePRColumn = rsPriKeyTabella
299: .getString("PKCOLUMN_NAME");
300: String sNamePRTable = rsPriKeyTabella
301: .getString("FKTABLE_NAME");
302: String sNameFK = rsPriKeyTabella
303: .getString("FK_NAME");
304: DefaultMutableTreeNode nNameColoumn = new DefaultMutableTreeNode(
305: sNamePRColumn + " --> " + sNamePRTable
306: + " " + sNameFK);
307: nNameColoumn.setAllowsChildren(false);
308: nColonne.add(nNameColoumn);
309: }
310:
311: rsPriKeyTabella.close();
312:
313: boolean bMltStm = ConfigurationProperties.properties()
314: .getUseMultiTransaction(false);
315:
316: if (!bMltStm) {
317: conn.close();
318: }
319:
320: } catch (SQLException s) {
321: JOptionPane.showMessageDialog(this , s.getMessage());
322: } finally {
323: try {
324: boolean bMltStm = ConfigurationProperties
325: .properties().getUseMultiTransaction(false);
326:
327: if (!bMltStm) {
328: conn.close();
329: }
330:
331: } catch (SQLException s) {
332: JOptionPane.showMessageDialog(this , s.getMessage());
333: }
334: }
335: }
336:
337: index = nodeLabel.lastIndexOf("Foreign Key");
338:
339: if (index != -1) {
340: String LabelTable = (String) e.getPath().getParentPath()
341: .toString();
342: String nodeLabelTable = LabelTable.substring(9,
343: LabelTable.length() - 1).trim();
344:
345: DefaultMutableTreeNode nColonne = (DefaultMutableTreeNode) e
346: .getPath().getLastPathComponent();
347:
348: try {
349: conn = Connessione.getConnessione();
350:
351: java.sql.DatabaseMetaData dbMD = conn.getMetaData();
352:
353: java.sql.ResultSet rsForKeyTabella = dbMD
354: .getImportedKeys(null, jtreeUtil.getSchema(),
355: nodeLabelTable);
356:
357: while (rsForKeyTabella.next()) {
358: String sNameFKTable = rsForKeyTabella
359: .getString("PKTABLE_NAME");
360: String sNameFKColumn = rsForKeyTabella
361: .getString("FKCOLUMN_NAME");
362: String sNameFK = rsForKeyTabella
363: .getString("PK_NAME");
364: DefaultMutableTreeNode nNameColoumn = new DefaultMutableTreeNode(
365: sNameFKColumn + " <-- " + sNameFKTable
366: + " " + sNameFK);
367: nNameColoumn.setAllowsChildren(false);
368: nColonne.add(nNameColoumn);
369: }
370:
371: rsForKeyTabella.close();
372:
373: boolean bMltStm = ConfigurationProperties.properties()
374: .getUseMultiTransaction(false);
375:
376: if (!bMltStm) {
377: conn.close();
378: }
379:
380: } catch (SQLException s) {
381: JOptionPane.showMessageDialog(this , s.getMessage());
382: } finally {
383: try {
384: boolean bMltStm = ConfigurationProperties
385: .properties().getUseMultiTransaction(false);
386:
387: if (!bMltStm) {
388: conn.close();
389: }
390: } catch (SQLException s) {
391: JOptionPane.showMessageDialog(this , s.getMessage());
392: }
393: }
394: }
395:
396: index = nodeLabel.lastIndexOf("Index");
397:
398: if (index != -1) {
399: String LabelTable = (String) e.getPath().getParentPath()
400: .toString();
401: String nodeLabelTable = LabelTable.substring(9,
402: LabelTable.length() - 1).trim();
403:
404: DefaultMutableTreeNode nColonne = (DefaultMutableTreeNode) e
405: .getPath().getLastPathComponent();
406:
407: try {
408: conn = Connessione.getConnessione();
409:
410: java.sql.DatabaseMetaData dbMD = conn.getMetaData();
411:
412: java.sql.ResultSet rsIndexTabella = dbMD.getIndexInfo(
413: null, jtreeUtil.getSchema(), nodeLabelTable,
414: false, true);
415:
416: while (rsIndexTabella.next()) {
417: String sNameIndex = rsIndexTabella
418: .getString("INDEX_NAME");
419: String sNameIndexColumn = rsIndexTabella
420: .getString("COLUMN_NAME");
421: if (sNameIndex != null) {
422: DefaultMutableTreeNode nNameColoumn = new DefaultMutableTreeNode(
423: sNameIndex + " on " + sNameIndexColumn);
424: nNameColoumn.setAllowsChildren(false);
425: nColonne.add(nNameColoumn);
426: }
427: }
428:
429: rsIndexTabella.close();
430:
431: boolean bMltStm = ConfigurationProperties.properties()
432: .getUseMultiTransaction(false);
433:
434: if (!bMltStm) {
435: conn.close();
436: }
437:
438: } catch (SQLException s) {
439: JOptionPane.showMessageDialog(this , s.getMessage());
440: } finally {
441: try {
442: boolean bMltStm = ConfigurationProperties
443: .properties().getUseMultiTransaction(false);
444:
445: if (!bMltStm) {
446: conn.close();
447: }
448: } catch (SQLException s) {
449: JOptionPane.showMessageDialog(this , s.getMessage());
450: }
451: }
452: }
453: }
454:
455: public void treeWillCollapse(javax.swing.event.TreeExpansionEvent e) {
456: setCursor(new Cursor(Cursor.WAIT_CURSOR));
457: }
458:
459: public void dragEnter(java.awt.dnd.DropTargetDragEvent e) {
460: e.acceptDrag(e.getDropAction());
461: }
462:
463: public void dragExit(java.awt.dnd.DropTargetEvent e) {
464: }
465:
466: public void dragOver(java.awt.dnd.DropTargetDragEvent e) {
467: e.acceptDrag(e.getDropAction());
468: }
469:
470: public void dropActionChanged(java.awt.dnd.DropTargetDragEvent e) {
471: if (!isDragAcceptable(e))
472: e.rejectDrag();
473: else
474: e.acceptDrag(e.getDropAction());
475: }
476:
477: public void drop(java.awt.dnd.DropTargetDropEvent e,
478: TreePath pathSource, TreePath pathTarget) {
479: String nameTable = null;
480: String nameNode = null;
481:
482: int caretPosition = sqlEditorDatapanel.getCaretPosition();
483:
484: if (pathNodeSelected.getPathCount() == 2) {
485: String stringPath = pathNodeSelected.toString();
486: int indexlast = stringPath.lastIndexOf(',') + 2;
487: nameNode = stringPath.substring(indexlast, stringPath
488: .length() - 1)
489: + " ";
490:
491: if (sqlEditorDatapanel.getText().equals("")) {
492: sqlEditorDatapanel.setText("Select * From " + nameNode);
493: } else {
494: String sText = sqlEditorDatapanel.getText();
495: sText = sText.substring(0, caretPosition) + nameTable
496: + nameNode + sText.substring(caretPosition);
497: sqlEditorDatapanel.setText(sText);
498: }
499: }
500:
501: if (pathNodeSelected.getPathCount() == 4) {
502: String stringPath = pathNodeSelected.toString();
503: int indexlast = stringPath.lastIndexOf(',') + 2;
504: nameNode = stringPath.substring(indexlast, stringPath
505: .length() - 1);
506:
507: int indexlast2 = nameNode.lastIndexOf('(');
508: nameNode = nameNode.substring(0, indexlast2) + ",";
509:
510: stringPath = pathNodeSelected.getParentPath()
511: .getParentPath().toString();
512: indexlast = stringPath.lastIndexOf(',') + 1;
513: nameTable = stringPath.substring(indexlast, stringPath
514: .length() - 1)
515: + ".";
516:
517: sqlEditorDatapanel.setText(sqlEditorDatapanel.getText()
518: + " " + nameTable + nameNode);
519: }
520: }
521:
522: public boolean isDragAcceptable(java.awt.dnd.DropTargetDragEvent e) {
523: // Do this if you want to prohibit dropping onto the drag source...
524: return true;
525: }
526:
527: public boolean isDropAcceptable(java.awt.dnd.DropTargetDropEvent e) {
528: // Do this if you want to prohibit dropping onto the drag source...
529: return true;
530: }
531:
532: // TreeModelListener interface...
533: public void treeStructureChanged(
534: javax.swing.event.TreeModelEvent treeModelEvent) {
535: }
536:
537: public void treeNodesInserted(
538: javax.swing.event.TreeModelEvent treeModelEvent) {
539: }
540:
541: public void treeNodesRemoved(
542: javax.swing.event.TreeModelEvent treeModelEvent) {
543: }
544:
545: public void treeNodesChanged(
546: javax.swing.event.TreeModelEvent treeModelEvent) {
547: }
548:
549: public void dragEnter(java.awt.dnd.DragSourceDragEvent e) {
550: }
551:
552: public void dragOver(java.awt.dnd.DragSourceDragEvent e) {
553: }
554:
555: public void dragExit(java.awt.dnd.DragSourceEvent e) {
556: }
557:
558: public void dropActionChanged(java.awt.dnd.DragSourceDragEvent e) {
559: }
560:
561: public void dragDropEnd(java.awt.dnd.DragSourceDropEvent e) {
562: }
563:
564: public boolean isPossibleExpandedNode(java.awt.event.ActionEvent e) {
565: if (CustomTreeUtil.isRootPath(CustomTree.this , dragDropTreeView
566: .getLastPath())) {
567: return false;
568: } else {
569: return true;
570: }
571: }
572:
573: public void autoscroll(java.awt.Point pt) {
574:
575: int nRow = this .getRowForLocation(pt.x, pt.y);
576:
577: if (nRow < 0)
578: return;
579:
580: java.awt.Rectangle raOuter = this .getBounds();
581: nRow = (pt.y + raOuter.y <= InterfacciaDragDrop.AUTOSCROLL_MARGIN) ? (nRow <= 0 ? 0
582: : nRow - 1)
583: : (nRow < this .getRowCount() - 1 ? nRow + 1 : nRow);
584:
585: this .scrollRowToVisible(nRow);
586: }
587:
588: public java.awt.Insets getAutoscrollInsets() {
589: java.awt.Rectangle raOuter = this .getBounds();
590: java.awt.Rectangle raInner = this .getParent().getBounds();
591: return new java.awt.Insets(raInner.y - raOuter.y
592: + InterfacciaDragDrop.AUTOSCROLL_MARGIN, raInner.x
593: - raOuter.x + InterfacciaDragDrop.AUTOSCROLL_MARGIN,
594: raOuter.height - raInner.height - raInner.y + raOuter.y
595: + InterfacciaDragDrop.AUTOSCROLL_MARGIN,
596: raOuter.width - raInner.width - raInner.x + raOuter.x
597: + InterfacciaDragDrop.AUTOSCROLL_MARGIN);
598: }
599: }
|