001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import infrastructure.logging.Log;
004: import java.awt.event.ActionListener;
005: import java.util.*;
006: import javax.swing.JMenu;
007: import javax.swing.JMenuItem;
008: import javax.swing.JPopupMenu;
009: import javax.swing.event.ChangeEvent;
010: import javax.swing.event.ChangeListener;
011: import org.dbbrowser.db.engine.exception.DBEngineException;
012: import org.dbbrowser.db.engine.model.ColumnInfo;
013: import org.dbbrowser.ui.UIControllerForQueries;
014:
015: public class CodeHelpBuilder extends Thread {
016: private UIControllerForQueries uiController = null;
017: private ActionListener actionListenerForPopupMenu = null;
018: private ChangeListener changeListener = null;
019: private ChangeListener processCompletedListener = null;
020:
021: private JPopupMenu codeHelp = null;
022: private int counter = 0;
023:
024: public CodeHelpBuilder(UIControllerForQueries uiController,
025: ActionListener actionListenerForPopupMenu,
026: ChangeListener changeListener,
027: ChangeListener processCompletedListener) {
028: this .uiController = uiController;
029: this .actionListenerForPopupMenu = actionListenerForPopupMenu;
030: this .changeListener = changeListener;
031: this .processCompletedListener = processCompletedListener;
032: }
033:
034: public JPopupMenu getJPopupMenu() {
035: return codeHelp;
036: }
037:
038: public void run() {
039:
040: Map mapOfSchemaNamesToListOfTableNames = null;
041: Map mapOfTableNamesToListOfColumnInfos = null;
042:
043: //Get the map of tablespaceNames to list of tablenames
044: try {
045: mapOfSchemaNamesToListOfTableNames = buildMapOfSchemaNamesToListOfTableNames();
046: mapOfTableNamesToListOfColumnInfos = buildMapOfTableNamesToListOfColumnInfos(mapOfSchemaNamesToListOfTableNames
047: .keySet());
048: } catch (DBEngineException exc) {
049: Log.getInstance().fatalMessage(
050: "*** Serious error while retrieving list of schema names *** - "
051: + exc.getMessage(),
052: this .getClass().getName());
053: //Dont do anything, use null values
054: }
055:
056: if (mapOfSchemaNamesToListOfTableNames != null) {
057: //Iterate through the map and build the pop up menu
058: codeHelp = new JPopupMenu();
059: Iterator i = mapOfSchemaNamesToListOfTableNames.keySet()
060: .iterator();
061: while (i.hasNext()) {
062: String schemaName = (String) i.next();
063: List listOfTableNames = (List) mapOfSchemaNamesToListOfTableNames
064: .get(schemaName);
065:
066: //Build the popup menu
067: JMenu jMenuForSchemaName = new JMenu(schemaName);
068: JMenuItem jMenuItemForSchemName = new JMenuItem(" - "
069: + schemaName + " - ");
070: jMenuItemForSchemName.setActionCommand(schemaName);
071: jMenuItemForSchemName
072: .addActionListener(actionListenerForPopupMenu);
073: jMenuForSchemaName.add(jMenuItemForSchemName);
074:
075: //Build a jmenu item for each table in the tablspace
076: Iterator iteratorForListOfTableNames = listOfTableNames
077: .iterator();
078: while (iteratorForListOfTableNames.hasNext()) {
079: String tablename = (String) iteratorForListOfTableNames
080: .next();
081: JMenu jMenuForTableName = new JMenu(tablename);
082: jMenuForTableName
083: .addActionListener(actionListenerForPopupMenu);
084: JMenuItem jMenuItemForTableName = new JMenuItem(
085: " - " + tablename + " - ");
086: jMenuItemForTableName.setActionCommand(tablename);
087: jMenuItemForTableName
088: .addActionListener(actionListenerForPopupMenu);
089: jMenuForTableName.add(jMenuItemForTableName);
090: jMenuForSchemaName.add(jMenuForTableName);
091:
092: //Build a jmenu item for each column in the table
093: Object o = mapOfTableNamesToListOfColumnInfos
094: .get(tablename);
095: if (o != null) {
096: List listOfColumnInfos = (List) o;
097: Iterator iteratorForListOfColumnInfos = listOfColumnInfos
098: .iterator();
099: while (iteratorForListOfColumnInfos.hasNext()) {
100: ColumnInfo ci = (ColumnInfo) iteratorForListOfColumnInfos
101: .next();
102: String columnInfoString = ci
103: .getColumnName()
104: + " - "
105: + ci.getColumnTypeName()
106: + " - "
107: + ci.getColumnDisplaySize()
108: .intValue();
109: JMenuItem jmenuItemForColumnName = new JMenuItem(
110: columnInfoString);
111: jmenuItemForColumnName.setActionCommand(ci
112: .getColumnName());
113: jmenuItemForColumnName
114: .addActionListener(actionListenerForPopupMenu);
115: jMenuForTableName
116: .add(jmenuItemForColumnName);
117:
118: counter++;
119: this .changeListener
120: .stateChanged(new ChangeEvent(
121: new Integer(counter)));
122: }
123: }
124: }
125:
126: codeHelp.add(jMenuForSchemaName);
127: }
128: }
129:
130: this .processCompletedListener.stateChanged(new ChangeEvent(
131: "Process complete"));
132: }
133:
134: private Map buildMapOfSchemaNamesToListOfTableNames()
135: throws DBEngineException {
136: Map mapOfSchemaNamesToListOfTableNames = new TreeMap();
137: List listOfSchemas = this .uiController.listSchemas();
138:
139: Iterator i = listOfSchemas.iterator();
140: while (i.hasNext()) {
141: String schemaName = (String) i.next();
142:
143: //Get all the tables in the tablespace
144: List listOfTablesInSchema = this .uiController
145: .listTablesInSchema(schemaName);
146:
147: //Add to map
148: mapOfSchemaNamesToListOfTableNames.put(schemaName,
149: listOfTablesInSchema);
150:
151: counter++;
152: this .changeListener.stateChanged(new ChangeEvent(
153: new Integer(counter)));
154: }
155:
156: return mapOfSchemaNamesToListOfTableNames;
157: }
158:
159: private Map buildMapOfTableNamesToListOfColumnInfos(Set setOfSchemas)
160: throws DBEngineException {
161: Map mapOfTableNamesToListOfColumnInfos = new TreeMap();
162: Iterator i = setOfSchemas.iterator();
163: while (i.hasNext()) {
164: String schemaName = (String) i.next();
165:
166: //Get all the tables in the tablespace
167: List listOfTablesInSchema = this .uiController
168: .listTablesInSchema(schemaName);
169:
170: //Get the list of columns in each table
171: Iterator ii = listOfTablesInSchema.iterator();
172: while (ii.hasNext()) {
173: String tableName = (String) ii.next();
174:
175: try {
176: List listOfColumnInfos = this .uiController
177: .listColumnsInATable(schemaName, tableName);
178: mapOfTableNamesToListOfColumnInfos.put(tableName,
179: listOfColumnInfos);
180: } catch (DBEngineException exc) {
181: Log.getInstance().infoMessage(
182: exc.getMessage() + " - " + schemaName + "."
183: + tableName,
184: this .getClass().getName());
185: }
186:
187: counter++;
188: this .changeListener.stateChanged(new ChangeEvent(
189: new Integer(counter)));
190: }
191: }
192:
193: return mapOfTableNamesToListOfColumnInfos;
194: }
195: }
|