001: package net.sourceforge.squirrel_sql.client.session;
002:
003: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
004: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
005: import net.sourceforge.squirrel_sql.fw.util.StringManager;
006: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
007:
008: import javax.swing.*;
009: import java.util.ArrayList;
010:
011: /**
012: * Helps to locate Objects in the Object tree of a Session main window or an ObjectTreeInternalFrame
013: */
014: public class ObjectTreeSearch {
015: private static final StringManager s_stringMgr = StringManagerFactory
016: .getStringManager(ObjectTreeSearch.class);
017:
018: /**
019: * View the Object at cursor in the Object Tree
020: *
021: * @param evt Event being executed.
022: */
023: public synchronized void viewObjectInObjectTree(String objectName,
024: ISession session) {
025:
026: ObjectCandidates candidates = getObjectCandidates(objectName);
027: if (candidates.size() == 0) {
028: return;
029: }
030:
031: if (false == session.getActiveSessionWindow() instanceof SessionInternalFrame
032: && false == session.getActiveSessionWindow() instanceof ObjectTreeInternalFrame) {
033: return;
034: }
035:
036: IObjectTreeAPI objectTree = session
037: .getObjectTreeAPIOfActiveSessionWindow();
038:
039: boolean success = false;
040: while (candidates.hasNext()) {
041:
042: ArrayList<String> catSchemObj = candidates.next();
043: success = objectTree.selectInObjectTree(catSchemObj.get(0),
044: catSchemObj.get(1), catSchemObj.get(2));
045: if (success) {
046: session
047: .selectMainTab(ISession.IMainPanelTabIndexes.OBJECT_TREE_TAB);
048: break;
049: }
050:
051: }
052:
053: if (false == success) {
054: // i18n[ObjectTreeSearch.error.objectnotfound=Could not locate the database object ''{0}'' in Object tree]
055: String msg = s_stringMgr.getString(
056: "ObjectTreeSearch.error.objectnotfound", candidates
057: .getSearchString());
058: JOptionPane.showMessageDialog(session.getApplication()
059: .getMainFrame(), msg);
060: }
061:
062: }
063:
064: private ObjectCandidates getObjectCandidates(String objectName) {
065: ObjectCandidates ret = new ObjectCandidates(objectName);
066:
067: String[] splits = objectName.split("\\.");
068:
069: for (int i = splits.length - 1; i >= 0; i--) {
070: String object = null;
071: String schema = null;
072: String catalog = null;
073:
074: object = removeQuotes(splits[i]);
075:
076: if (i + 1 < splits.length) {
077: schema = splits[i + 1];
078: }
079:
080: if (i + 2 < splits.length) {
081: catalog = splits[i + 2];
082: }
083: if (catalog == null && schema == null && "".equals(object)) {
084: continue;
085: }
086: ret.add(catalog, schema, object);
087: }
088:
089: return ret;
090: }
091:
092: private String removeQuotes(String objectName) {
093: String ret = objectName.trim();
094:
095: while (ret.startsWith("\"") || ret.startsWith("/")) {
096: ret = ret.substring(1);
097: }
098:
099: while (ret.endsWith("\"") || ret.endsWith("/")) {
100: ret = ret.substring(0, ret.length() - 1);
101: }
102:
103: return ret;
104: }
105:
106: private static class ObjectCandidates {
107: ArrayList<ArrayList<String>> _candidates = new ArrayList<ArrayList<String>>();
108:
109: int _curIndex = 0;
110: private String _searchString;
111:
112: public ObjectCandidates(String searchString) {
113: _searchString = searchString;
114: }
115:
116: public boolean hasNext() {
117: return _curIndex < _candidates.size();
118: }
119:
120: public ArrayList<String> next() {
121: return _candidates.get(_curIndex++);
122: }
123:
124: public String getSearchString() {
125: return _searchString;
126: }
127:
128: public void add(String catalog, String schema, String object) {
129: ArrayList<String> candidate = new ArrayList<String>(3);
130: candidate.add(catalog);
131: candidate.add(schema);
132: candidate.add(object);
133: _candidates.add(candidate);
134: }
135:
136: public int size() {
137: return _candidates.size();
138: }
139: }
140:
141: }
|