001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.infos;
043:
044: import java.io.IOException;
045: import java.sql.ResultSet;
046: import java.util.HashMap;
047: import java.util.Vector;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: import org.openide.DialogDisplayer;
052: import org.openide.NotifyDescriptor;
053: import org.openide.nodes.Node;
054:
055: import org.netbeans.lib.ddl.DDLException;
056: import org.netbeans.lib.ddl.impl.DriverSpecification;
057: import org.netbeans.lib.ddl.impl.DropIndex;
058: import org.netbeans.lib.ddl.impl.Specification;
059:
060: import org.netbeans.api.db.explorer.DatabaseException;
061: import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
062: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
063:
064: public class IndexNodeInfo extends TableNodeInfo {
065: static final long serialVersionUID = -8633867970381524742L;
066: static final Logger LOGGER = Logger.getLogger(IndexNodeInfo.class
067: .getName());
068:
069: public void initChildren(Vector children) throws DatabaseException {
070: try {
071: String table = (String) get(DatabaseNode.TABLE);
072:
073: DriverSpecification drvSpec = getDriverSpecification();
074: drvSpec.getIndexInfo(table, false, true);
075: ResultSet rs = drvSpec.getResultSet();
076: if (rs != null) {
077: HashMap rset = new HashMap();
078: DatabaseNodeInfo info;
079: while (rs.next()) {
080: rset = drvSpec.getRow();
081: String ixname = (String) get("index"); //NOI18N
082: info = DatabaseNodeInfo.createNodeInfo(this ,
083: DatabaseNode.INDEXCOLUMN, rset);
084: String newixname = (String) info.get("ixname"); //NOI18N
085: if (ixname != null && newixname != null
086: && newixname.equals(ixname)) {
087: String way;
088: if (info.get("ord") instanceof java.lang.Boolean) //NOI18N //HACK for PointBase
089: way = "A"; //NOI18N
090: else
091: way = (String) info.get("ord"); //NOI18N
092: if (way == null)
093: way = "A"; //NOI18N
094: info.put(DatabaseNodeInfo.ICONBASE, info
095: .get(DatabaseNodeInfo.ICONBASE + way));
096: if (info != null)
097: children.add(info);
098: else {
099: rs.close();
100: throw new Exception(bundle().getString(
101: "EXC_UnableToCreateIndexNodeInfo")); //NOI18N
102: }
103: }
104: rset.clear();
105: }
106: rs.close();
107: }
108: } catch (Exception e) {
109: throw new DatabaseException(e.getMessage());
110: }
111: }
112:
113: public void refreshChildren() throws DatabaseException {
114: // create list (infos)
115: Vector charr = new Vector();
116: put(DatabaseNodeInfo.CHILDREN, charr);
117: initChildren(charr);
118:
119: // create sub-tree (by infos)
120: try {
121: Node[] subTreeNodes = new Node[charr.size()];
122:
123: // current sub-tree
124: DatabaseNodeChildren children = (DatabaseNodeChildren) getNode()
125: .getChildren();
126:
127: // remove current sub-tree
128: children.remove(children.getNodes());
129:
130: // build refreshed sub-tree
131: for (int i = 0; i < charr.size(); i++)
132: subTreeNodes[i] = children
133: .createNode((DatabaseNodeInfo) charr
134: .elementAt(i));
135:
136: // add built sub-tree
137: children.add(subTreeNodes);
138: } catch (Exception ex) {
139: Logger.getLogger("global").log(Level.INFO, null, ex);
140: }
141: }
142:
143: public void delete() throws IOException {
144: try {
145: String table = (String) get(DatabaseNode.TABLE);
146: Specification spec = (Specification) getSpecification();
147: DDLHelper.deleteIndex(spec,
148: (String) get(DatabaseNodeInfo.SCHEMA), table,
149: getName());
150:
151: //refresh list of columns due to the column's icons
152: getParent(DatabaseNode.TABLE).refreshChildren();
153: } catch (DDLException e) {
154: DialogDisplayer.getDefault().notify(
155: new NotifyDescriptor.Message(e.getMessage(),
156: NotifyDescriptor.ERROR_MESSAGE));
157: } catch (Exception e) {
158: LOGGER.log(Level.WARNING, null, e);
159: throw new IOException(e.toString());
160: }
161: }
162: }
|