Lines 39-51
Link Here
|
39 |
* |
39 |
* |
40 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
40 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
41 |
*/ |
41 |
*/ |
42 |
|
|
|
43 |
package org.netbeans.modules.db.explorer.node; |
42 |
package org.netbeans.modules.db.explorer.node; |
44 |
|
43 |
|
|
|
44 |
import java.beans.PropertyChangeEvent; |
45 |
import java.beans.PropertyChangeListener; |
45 |
import java.util.ArrayList; |
46 |
import java.util.ArrayList; |
|
|
47 |
import java.util.Arrays; |
46 |
import java.util.Collection; |
48 |
import java.util.Collection; |
47 |
import java.util.Comparator; |
49 |
import java.util.Comparator; |
48 |
import java.util.List; |
50 |
import java.util.List; |
|
|
51 |
import javax.swing.SwingWorker; |
49 |
import org.netbeans.api.db.explorer.node.NodeProvider; |
52 |
import org.netbeans.api.db.explorer.node.NodeProvider; |
50 |
import org.netbeans.api.db.explorer.node.NodeProviderFactory; |
53 |
import org.netbeans.api.db.explorer.node.NodeProviderFactory; |
51 |
import org.netbeans.modules.db.explorer.DatabaseConnection; |
54 |
import org.netbeans.modules.db.explorer.DatabaseConnection; |
Lines 56-69
Link Here
|
56 |
import org.netbeans.modules.db.metadata.model.api.MetadataModel; |
59 |
import org.netbeans.modules.db.metadata.model.api.MetadataModel; |
57 |
import org.netbeans.modules.db.metadata.model.api.MetadataModelException; |
60 |
import org.netbeans.modules.db.metadata.model.api.MetadataModelException; |
58 |
import org.netbeans.modules.db.metadata.model.api.Schema; |
61 |
import org.netbeans.modules.db.metadata.model.api.Schema; |
|
|
62 |
import org.openide.nodes.AbstractNode; |
63 |
import org.openide.nodes.ChildFactory; |
64 |
import org.openide.nodes.Children; |
59 |
import org.openide.nodes.Node; |
65 |
import org.openide.nodes.Node; |
60 |
import org.openide.util.Lookup; |
66 |
import org.openide.util.Lookup; |
|
|
67 |
import org.openide.util.NbBundle; |
68 |
import org.openide.util.WeakListeners; |
61 |
|
69 |
|
62 |
/** |
70 |
/** |
63 |
* |
71 |
* |
64 |
* @author Rob Englander |
72 |
* @author Rob Englander |
65 |
*/ |
73 |
*/ |
66 |
public class SchemaNodeProvider extends NodeProvider { |
74 |
public class SchemaNodeProvider extends NodeProvider implements PropertyChangeListener { |
67 |
|
75 |
|
68 |
// lazy initialization holder class idiom for static fields is used |
76 |
// lazy initialization holder class idiom for static fields is used |
69 |
// for retrieving the factory |
77 |
// for retrieving the factory |
Lines 72-77
Link Here
|
72 |
} |
80 |
} |
73 |
|
81 |
|
74 |
private static class FactoryHolder { |
82 |
private static class FactoryHolder { |
|
|
83 |
|
75 |
static final NodeProviderFactory FACTORY = new NodeProviderFactory() { |
84 |
static final NodeProviderFactory FACTORY = new NodeProviderFactory() { |
76 |
@Override |
85 |
@Override |
77 |
public SchemaNodeProvider createInstance(Lookup lookup) { |
86 |
public SchemaNodeProvider createInstance(Lookup lookup) { |
Lines 81-86
Link Here
|
81 |
}; |
90 |
}; |
82 |
} |
91 |
} |
83 |
|
92 |
|
|
|
93 |
private final List<Node> nodes = new ArrayList<Node>(); |
84 |
private final DatabaseConnection connection; |
94 |
private final DatabaseConnection connection; |
85 |
private final MetadataElementHandle<Catalog> catalogHandle; |
95 |
private final MetadataElementHandle<Catalog> catalogHandle; |
86 |
|
96 |
|
Lines 93-101
Link Here
|
93 |
@Override |
103 |
@Override |
94 |
protected synchronized void initialize() { |
104 |
protected synchronized void initialize() { |
95 |
final List<Node> newList = new ArrayList<Node>(); |
105 |
final List<Node> newList = new ArrayList<Node>(); |
|
|
106 |
final List<Node> otherList = new ArrayList<Node>(); |
96 |
|
107 |
|
97 |
boolean connected = !connection.getConnector().isDisconnected(); |
108 |
boolean connected = !connection.getConnector().isDisconnected(); |
98 |
MetadataModel metaDataModel = connection.getMetadataModel(); |
109 |
MetadataModel metaDataModel = connection.getMetadataModel(); |
|
|
110 |
|
111 |
nodes.clear(); |
112 |
|
99 |
if (connected && metaDataModel != null) { |
113 |
if (connected && metaDataModel != null) { |
100 |
try { |
114 |
try { |
101 |
metaDataModel.runReadAction( |
115 |
metaDataModel.runReadAction( |
Lines 111-120
Link Here
|
111 |
} else { |
125 |
} else { |
112 |
Collection<Schema> schemas = cat.getSchemas(); |
126 |
Collection<Schema> schemas = cat.getSchemas(); |
113 |
for (Schema schema : schemas) { |
127 |
for (Schema schema : schemas) { |
|
|
128 |
if (isDefaultSchema(schema, connection)) { |
114 |
updateNode(newList, schema); |
129 |
updateNode(newList, schema); |
|
|
130 |
} else { |
131 |
updateNode(otherList, schema); |
115 |
} |
132 |
} |
116 |
} |
133 |
} |
117 |
|
134 |
|
|
|
135 |
nodes.addAll(newList); |
136 |
nodes.addAll(otherList); |
137 |
|
138 |
if (!otherList.isEmpty()) { |
139 |
newList.add(new OtherSchemasNode( |
140 |
otherList)); |
141 |
} |
142 |
} |
143 |
|
118 |
if (syntheticSchema != null) { |
144 |
if (syntheticSchema != null) { |
119 |
setProxyNodes(newList); |
145 |
setProxyNodes(newList); |
120 |
} else { |
146 |
} else { |
Lines 122-141
Link Here
|
122 |
} |
148 |
} |
123 |
} |
149 |
} |
124 |
} |
150 |
} |
125 |
} |
151 |
}); |
126 |
); |
|
|
127 |
} catch (MetadataModelException e) { |
152 |
} catch (MetadataModelException e) { |
128 |
NodeRegistry.handleMetadataModelException(this.getClass(), connection, e, true); |
153 |
NodeRegistry.handleMetadataModelException(this.getClass(), connection, e, true); |
129 |
} |
154 |
} |
130 |
} else { |
155 |
} else { |
131 |
setNodes(newList); |
156 |
setNodes(newList); |
132 |
} |
157 |
} |
|
|
158 |
|
159 |
connection.addPropertyChangeListener(WeakListeners.propertyChange(this, connection)); |
133 |
} |
160 |
} |
134 |
|
161 |
|
|
|
162 |
private boolean isDefaultSchema(Schema schema, |
163 |
DatabaseConnection connection) { |
164 |
|
165 |
String def = connection.getDefaultSchema(); |
166 |
return (def == null && schema.isDefault()) |
167 |
|| (def != null && def.equals(schema.getName()) |
168 |
|| connection.isImportantSchema(schema.getName())); |
169 |
} |
170 |
|
135 |
private void updateNode(List<Node> newList, Schema schema) { |
171 |
private void updateNode(List<Node> newList, Schema schema) { |
136 |
MetadataElementHandle<Schema> schemaHandle = MetadataElementHandle.create(schema); |
172 |
MetadataElementHandle<Schema> schemaHandle = MetadataElementHandle.create(schema); |
137 |
Collection<Node> matches = getNodes(schemaHandle); |
173 |
Collection<Node> matches = getNodes(schemaHandle); |
138 |
if (matches.size() > 0) { |
174 |
if (matches != null && matches.size() > 0) { |
139 |
newList.addAll(matches); |
175 |
newList.addAll(matches); |
140 |
} else { |
176 |
} else { |
141 |
NodeDataLookup lookup = new NodeDataLookup(); |
177 |
NodeDataLookup lookup = new NodeDataLookup(); |
Lines 146-159
Link Here
|
146 |
} |
182 |
} |
147 |
} |
183 |
} |
148 |
|
184 |
|
|
|
185 |
@Override |
186 |
public void propertyChange(PropertyChangeEvent pce) { |
187 |
if (this.initialized && "importantSchemas".equals(pce.getPropertyName())) { |
188 |
final List<Node> mainList = new ArrayList<Node>(); |
189 |
final List<Node> otherList = new ArrayList<Node>(); |
190 |
|
191 |
for(Node node: new ArrayList<Node>(nodes)) { |
192 |
if(connection.isImportantSchema(node.getName())) { |
193 |
mainList.add(node.cloneNode()); |
194 |
} else { |
195 |
otherList.add(node.cloneNode()); |
196 |
} |
197 |
} |
198 |
|
199 |
if(! otherList.isEmpty()) { |
200 |
mainList.add(new OtherSchemasNode(otherList)); |
201 |
} |
202 |
|
203 |
setNodes(mainList); |
204 |
} |
205 |
} |
206 |
|
149 |
static class SchemaComparator implements Comparator<Node> { |
207 |
static class SchemaComparator implements Comparator<Node> { |
150 |
|
208 |
|
151 |
@Override |
209 |
@Override |
152 |
public int compare(Node node1, Node node2) { |
210 |
public int compare(Node node1, Node node2) { |
153 |
assert node1.getDisplayName() != null : node1 + " has display name."; |
211 |
assert node1.getDisplayName() != null : node1 + " has display name."; |
154 |
assert node2.getDisplayName() != null : node2 + " has display name."; |
212 |
assert node2.getDisplayName() != null : node2 + " has display name."; |
|
|
213 |
if(node1 instanceof OtherSchemasNode) { |
214 |
return 1; |
215 |
} |
216 |
if(node2 instanceof OtherSchemasNode) { |
217 |
return -1; |
218 |
} |
155 |
return node1.getDisplayName().compareToIgnoreCase(node2.getDisplayName()); |
219 |
return node1.getDisplayName().compareToIgnoreCase(node2.getDisplayName()); |
156 |
} |
220 |
} |
|
|
221 |
} |
157 |
|
222 |
|
|
|
223 |
@NbBundle.Messages({ |
224 |
"LBL_OtherSchemas=Other schemas" |
225 |
}) |
226 |
private static class OtherSchemasNode extends AbstractNode { |
227 |
|
228 |
private static final String ICON_BASE = |
229 |
"org/netbeans/modules/db/resources/schema.png"; //NOI18N |
230 |
|
231 |
public OtherSchemasNode(List<Node> otherList) { |
232 |
super(createChildren(otherList)); |
233 |
setDisplayName(Bundle.LBL_OtherSchemas()); |
234 |
setIconBaseWithExtension(ICON_BASE); |
158 |
} |
235 |
} |
|
|
236 |
|
237 |
@Override |
238 |
public String getName() { |
239 |
return "zzzzzz"; //NOI18N |
159 |
} |
240 |
} |
|
|
241 |
|
242 |
private static Children createChildren(final List<Node> otherList) { |
243 |
Children c = Children.create(new ChildFactory<Node>() { |
244 |
@Override |
245 |
protected boolean createKeys(final List<Node> toPopulate) { |
246 |
toPopulate.addAll(otherList); |
247 |
return true; |
248 |
} |
249 |
|
250 |
@Override |
251 |
protected Node createNodeForKey(Node key) { |
252 |
return key; |
253 |
} |
254 |
}, false); |
255 |
return c; |
256 |
} |
257 |
} |
258 |
} |