KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > JoinNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
20
21 import java.sql.SQLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import java.beans.PropertyEditorSupport JavaDoc;
26 import java.beans.PropertyEditor JavaDoc;
27
28 import org.openide.ErrorManager;
29
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Sheet;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.PropertySupport;
34 import org.openide.nodes.PropertySupport.Reflection;
35
36 import org.openide.util.NbBundle;
37
38 import org.netbeans.modules.db.sql.visualeditor.querymodel.JoinTable;
39 import org.netbeans.modules.db.sql.visualeditor.querymodel.Predicate;
40
41 // A class for providing properties to arcs (joins)
42

43 public class JoinNode extends AbstractNode
44 {
45     // Private variable
46

47     private String JavaDoc _table1=""; // NOI18N
48
private String JavaDoc _column1=""; // NOI18N
49
private String JavaDoc _table2=""; // NOI18N
50
private String JavaDoc _column2=""; // NOI18N
51
private String JavaDoc _type=""; // NOI18N
52
private QueryBuilder _queryBuilder;
53
54
55     // Constructors
56

57     JoinNode(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2, String JavaDoc type,
58              QueryBuilder queryBuilder)
59     {
60         super(Children.LEAF);
61         _table1 = table1;
62         _column1 = column1;
63         _table2 = table2;
64         _column2 = column2;
65         _type = type;
66         _queryBuilder = queryBuilder;
67     }
68
69     JoinNode(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2, QueryBuilder queryBuilder)
70     {
71         this(table1, column1, table2, column2, "INNER", queryBuilder); // NOI18N
72
}
73
74     // Accessors/mutators
75

76     public String JavaDoc getTable1() {
77         return _table1;
78     }
79
80     public String JavaDoc getColumn1() {
81         return _column1;
82     }
83
84     public void setColumn1(String JavaDoc column1) {
85         JoinTable jt = findJoinTable(_table1, _column1, _table2, _column2);
86         _column1 = column1;
87         updateModel(jt);
88     }
89
90     public String JavaDoc getTable2() {
91         return _table2;
92     }
93
94     public String JavaDoc getColumn2() {
95         return _column2;
96     }
97
98     public void setColumn2(String JavaDoc column2) {
99         JoinTable jt = findJoinTable(_table1, _column1, _table2, _column2);
100         _column2 = column2;
101         updateModel(jt);
102     }
103
104     public String JavaDoc getType() {
105         return _type;
106     }
107
108     public void setType(String JavaDoc type) {
109         JoinTable jt = findJoinTable(_table1, _column1, _table2, _column2);
110         _type = type;
111         updateModel(jt);
112     }
113
114     // Create minimal property sheet
115
// Join type and column names are editable, table names aren't
116

117     protected Sheet createSheet() {
118         Sheet s = Sheet.createDefault();
119         Sheet.Set ss = s.get(Sheet.PROPERTIES);
120         try {
121             PropertySupport.Reflection p;
122             p = new Reflection(this, String JavaDoc.class, "getTable1", null); // NOI18N
123
p.setName("table1"); // NOI18N
124
// p.setDisplayName("Table 1"); // NOI18N
125
// p.setShortDescription("first table for join"); // NOI18N
126

127             String JavaDoc table1 = NbBundle.getMessage(JoinNode.class, "TABLE_1"); // NOI18N
128
p.setDisplayName(table1);
129
130             String JavaDoc table1ShortDescription = NbBundle.getMessage(JoinNode.class, "TABLE_1_SHORT_DESCRIPTION"); // NOI18N
131
p.setShortDescription(table1ShortDescription);
132             ss.put(p);
133             p = new Reflection(this, String JavaDoc.class, "getColumn1", "setColumn1") {
134                     public PropertyEditor JavaDoc getPropertyEditor () {
135                         return new ColumnPropertyEditor1 ();
136                     }}; // NOI18N
137
p.setName("column1"); // NOI18N
138
// p.setDisplayName("Column 1"); // NOI18N
139
// p.setShortDescription("First column for join"); // NOI18N
140

141             String JavaDoc column1 = NbBundle.getMessage(JoinNode.class, "COLUMN_1"); // NOI18N
142
p.setDisplayName(column1);
143
144             String JavaDoc column1ShortDescription = NbBundle.getMessage(JoinNode.class, "COLUMN_1_SHORT_DESCRIPTION"); // NOI18N
145
p.setShortDescription(column1ShortDescription);
146 // p.setPropertyEditorClass(ColumnPropertyEditor.class);
147
ss.put(p);
148             p = new Reflection(this, String JavaDoc.class, "getTable2", null); // NOI18N
149
p.setName("table2"); // NOI18N
150
// p.setDisplayName("Table 2"); // NOI18N
151
// p.setShortDescription("second table for join"); // NOI18N
152
String JavaDoc table2 = NbBundle.getMessage(JoinNode.class, "TABLE_2"); // NOI18N
153
p.setDisplayName(table2);
154
155             String JavaDoc table2ShortDescription = NbBundle.getMessage(JoinNode.class, "TABLE_2_SHORT_DESCRIPTION"); // NOI18N
156
p.setShortDescription(table2ShortDescription);
157             ss.put(p);
158             p = new Reflection(this, String JavaDoc.class, "getColumn2", "setColumn2") {
159                     public PropertyEditor JavaDoc getPropertyEditor () {
160                         return new ColumnPropertyEditor2 ();
161                     }}; // NOI18N
162
p.setName("column2"); // NOI18N
163
// p.setDisplayName("Column 2"); // NOI18N
164
// p.setShortDescription("Second column for join"); // NOI18N
165

166             String JavaDoc column2 = NbBundle.getMessage(JoinNode.class, "COLUMN_2"); // NOI18N
167
p.setDisplayName(column2);
168
169             String JavaDoc column2ShortDescription = NbBundle.getMessage(JoinNode.class, "COLUMN_2_SHORT_DESCRIPTION"); // NOI18N
170
p.setShortDescription(column2ShortDescription);
171 // p.setPropertyEditorClass(ColumnPropertyEditor.class);
172
ss.put(p);
173             p = new Reflection(this, String JavaDoc.class, "getType", "setType") { // NOI18N
174
public PropertyEditor JavaDoc getPropertyEditor () {
175                         return new JoinTypePropertyEditor ();
176                     }}; // NOI18N
177
p.setName("type"); // NOI18N
178
// p.setDisplayName("Type"); // NOI18N
179
// p.setShortDescription("Type of join"); // NOI18N
180
String JavaDoc type = NbBundle.getMessage(JoinNode.class, "TYPE"); // NOI18N
181
p.setDisplayName(type);
182
183             String JavaDoc typeShortDescription = NbBundle.getMessage(JoinNode.class, "TYPE_SHORT_DESCRIPTION"); // NOI18N
184
p.setShortDescription(typeShortDescription);
185
186 // p.setPropertyEditorClass(JoinTypePropertyEditor.class);
187
ss.put(p);
188         } catch (NoSuchMethodException JavaDoc nsme) {
189             ErrorManager.getDefault().notify(nsme);
190         }
191         return s;
192     }
193
194     // Suppress label printing in graph
195

196     public String JavaDoc toString() {
197         return ""; // NOI18N
198
}
199
200     
201     // Find the JoinTable object behind this edge
202
// We may eventually add links from graph to model to simplify operations like this
203
JoinTable findJoinTable(String JavaDoc _table1, String JavaDoc _column1, String JavaDoc _table2, String JavaDoc _column2) {
204         return _queryBuilder._queryModel.findJoinTable(_table1, _column1, _table2, _column2);
205     }
206
207
208     private void updateModel(JoinTable jt) {
209
210         // Set the appropriate fields in this Join Expression
211
// Update the join type in the Join Table, and the fields of the condition
212
jt.setJoinType(_type);
213         if (_type.equals("CROSS")) // NOI18N
214
jt.setExpression(null);
215         else
216             ((Predicate)jt.getExpression()).setFields(_table1, _column1, _table2, _column2);
217
218         // And rebuild the text query
219
_queryBuilder.generateText();
220     }
221
222
223     // Property Editors
224

225     public class JoinTypePropertyEditor extends PropertyEditorSupport JavaDoc {
226
227         private String JavaDoc[] tags =
228             new String JavaDoc[] { "INNER", // NOI18N
229
"LEFT OUTER", // NOI18N
230
"RIGHT OUTER" // NOI18N
231
/*, "FULL OUTER" */ // Dropped b/c Pointbase doesn't support it
232
/*, "CROSS" */ };
233         public String JavaDoc[] getTags() {
234             return tags;
235         }
236     }
237
238     public class ColumnPropertyEditor1 extends PropertyEditorSupport JavaDoc {
239
240         public String JavaDoc[] getTags() {
241             List JavaDoc columnNames ;
242             try {
243                 columnNames = _queryBuilder.getColumnNames(_table1);
244             } catch(SQLException JavaDoc sqle) {
245                 return new String JavaDoc[0] ;
246             }
247             return (String JavaDoc[])columnNames.toArray(new String JavaDoc[columnNames.size()]);
248         }
249     }
250
251     public class ColumnPropertyEditor2 extends PropertyEditorSupport JavaDoc {
252
253         public String JavaDoc[] getTags() {
254
255             List JavaDoc columnNames ;
256             try {
257                 columnNames = _queryBuilder.getColumnNames(_table2);
258             } catch(SQLException JavaDoc sqle) {
259                 return new String JavaDoc[0] ;
260             }
261             return (String JavaDoc[])columnNames.toArray(new String JavaDoc[columnNames.size()]);
262         }
263     }
264 }
265
Popular Tags