001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import java.awt.Cursor;
024: import java.util.Hashtable;
025: import java.util.Vector;
026:
027: import javax.swing.JOptionPane;
028:
029: import nickyb.sqleonardo.common.jdbc.ConnectionAssistant;
030: import nickyb.sqleonardo.common.jdbc.ConnectionHandler;
031: import nickyb.sqleonardo.environment.Application;
032: import nickyb.sqleonardo.environment.ctrl.ContentPane;
033: import nickyb.sqleonardo.environment.ctrl.define.TableMetaData;
034: import nickyb.sqleonardo.querybuilder.QueryModel;
035: import nickyb.sqleonardo.querybuilder.syntax.QueryTokens;
036: import nickyb.sqleonardo.querybuilder.syntax.SQLFormatter;
037: import nickyb.sqleonardo.environment.mdi.ClientContent;
038:
039: public class JumpManager implements Runnable {
040: private ContentPane owner;
041:
042: private Hashtable h = new Hashtable();
043: private Vector v = new Vector();
044:
045: public static void perform(ContentPane control) {
046: JumpManager j = new JumpManager();
047: j.owner = control;
048:
049: new Thread(j).start();
050: }
051:
052: public void run() {
053: int row = owner.getView().getRow();
054: int col = owner.getView().getColumn();
055:
056: if (!(owner.getQueryModel().getQueryExpression()
057: .getQuerySpecification().getSelectList()[col] instanceof QueryTokens.Column))
058: return;
059: QueryTokens.Column column = (QueryTokens.Column) owner
060: .getQueryModel().getQueryExpression()
061: .getQuerySpecification().getSelectList()[col];
062:
063: owner.setCursor(new Cursor(Cursor.WAIT_CURSOR));
064:
065: TableMetaData tmd = new TableMetaData(owner.getHandlerKey(),
066: column.getTable().getSchema(), column.getTable()
067: .getName());
068: for (int i = 0; i < tmd.getImportedKeys().size(); i++) {
069: if (tmd.getImportedKeyProperty(i,
070: TableMetaData.IDX_REL_FKCOLUMN_NAME).equals(
071: column.getName())) {
072: TableMetaData tmdPK = new TableMetaData(tmd
073: .getHandlerKey(), tmd.getImportedKeyProperty(i,
074: TableMetaData.IDX_REL_PKTABLE_SCHEM), tmd
075: .getImportedKeyProperty(i,
076: TableMetaData.IDX_REL_PKTABLE_NAME));
077:
078: String pk = tmd.getImportedKeyProperty(i,
079: TableMetaData.IDX_REL_PKCOLUMN_NAME);
080: String id = ">> " + tmdPK + " (" + pk + ")";
081:
082: h.put(id, tmdPK);
083:
084: if (!v.contains(id))
085: v.addElement(id);
086: }
087: }
088:
089: if (tmd.isPrimaryKey(column.getName())) {
090: for (int i = 0; i < tmd.getExportedKeys().size(); i++) {
091: if (tmd.getExportedKeyProperty(i,
092: TableMetaData.IDX_REL_PKCOLUMN_NAME).equals(
093: column.getName())) {
094: TableMetaData tmdFK = new TableMetaData(
095: tmd.getHandlerKey(),
096: tmd
097: .getExportedKeyProperty(
098: i,
099: TableMetaData.IDX_REL_FKTABLE_SCHEM),
100: tmd.getExportedKeyProperty(i,
101: TableMetaData.IDX_REL_FKTABLE_NAME));
102:
103: String fk = tmd.getExportedKeyProperty(i,
104: TableMetaData.IDX_REL_FKCOLUMN_NAME);
105: String id = "<< " + tmdFK + " (" + fk + ")";
106:
107: h.put(id, tmdFK);
108:
109: if (!v.contains(id))
110: v.addElement(id);
111: }
112: }
113: }
114:
115: owner.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
116:
117: Object jumpTo = null;
118: if (v.size() == 1) {
119: if (JOptionPane
120: .showConfirmDialog(Application.window,
121: "jump from '" + column + "' to:\n"
122: + v.elementAt(0),
123: Application.PROGRAM,
124: JOptionPane.OK_CANCEL_OPTION,
125: JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION)
126: jumpTo = v.elementAt(0);
127: } else if (v.size() > 1) {
128: jumpTo = JOptionPane.showInputDialog(Application.window,
129: "jump from '" + column + "' to:",
130: Application.PROGRAM, JOptionPane.PLAIN_MESSAGE,
131: null, v.toArray(), null);
132: } else {
133: JOptionPane.showMessageDialog(Application.window,
134: "column '" + column + "' has no references",
135: Application.PROGRAM,
136: JOptionPane.INFORMATION_MESSAGE);
137: return;
138: }
139:
140: if (jumpTo != null) {
141: owner.setCursor(new Cursor(Cursor.WAIT_CURSOR));
142:
143: TableMetaData tmdJ = (TableMetaData) h.get(jumpTo);
144:
145: int pos = jumpTo.toString().indexOf('(');
146: String filter = jumpTo.toString().substring(pos + 1,
147: jumpTo.toString().length() - 1);
148:
149: int sqltype = 0;
150: for (int i = 0; i < tmdJ.getColumns().size(); i++) {
151: if (tmdJ.getColumnProperty(i,
152: TableMetaData.IDX_COL_COLUMN_NAME).equals(
153: filter))
154: sqltype = Integer.valueOf(
155: tmdJ.getColumnProperty(i,
156: TableMetaData.IDX_COL_DATA_TYPE))
157: .intValue();
158: }
159:
160: ConnectionHandler ch = ConnectionAssistant.getHandler(tmdJ
161: .getHandlerKey());
162: filter = SQLFormatter.ensureQuotes(filter, ch.getObject(
163: "$identifierQuoteString").toString(), false);
164:
165: Object value = owner.getView().getValueAt(row, col);
166: QueryTokens.Condition condition = new QueryTokens.Condition(
167: new QueryTokens.DefaultExpression(filter), "=",
168: new QueryTokens.DefaultExpression(SQLFormatter
169: .toJdbcValue(value, sqltype)));
170:
171: QueryModel qm = tmdJ.createQueryModel();
172: qm.getQueryExpression().getQuerySpecification()
173: .addWhereClause(condition);
174:
175: ClientContent client = new ClientContent(tmdJ
176: .getHandlerKey(), qm, null);
177: client.setTitle("CONTENT : " + tmdJ + " : "
178: + tmdJ.getHandlerKey());
179: Application.window.add(client);
180:
181: owner.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
182: }
183: }
184: }
|