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-2007 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: * RowSetSelection.java
043: *
044: * Created on June 5, 2005, 4:19 PM
045: */
046:
047: package org.netbeans.modules.visualweb.dataconnectivity.customizers;
048:
049: import org.netbeans.modules.visualweb.dataconnectivity.DataconnectivitySettings;
050: import org.netbeans.modules.visualweb.dataconnectivity.Log;
051: import com.sun.rave.designtime.Constants;
052: import com.sun.rave.designtime.DesignBean;
053: import com.sun.rave.designtime.DesignContext;
054: import com.sun.rave.designtime.DesignProject;
055: import com.sun.rave.designtime.DesignProperty;
056: import com.sun.rave.designtime.faces.FacesDesignProject;
057: import com.sun.sql.rowset.CachedRowSetX;
058: import java.awt.Color;
059: import java.awt.Component;
060: import java.awt.Container;
061: import java.awt.Dimension;
062: import java.awt.FocusTraversalPolicy;
063: import java.awt.GridBagConstraints;
064: import java.awt.GridLayout;
065: import java.awt.TextField;
066: import java.awt.event.ActionEvent;
067: import java.awt.event.ActionListener;
068: import java.util.ArrayList;
069: import javax.swing.AbstractButton;
070: import javax.swing.JButton;
071: import javax.swing.JComponent;
072: import javax.swing.JDialog;
073: import javax.swing.JLabel;
074: import javax.swing.JPanel;
075: import javax.swing.JRadioButton;
076: import javax.swing.JSeparator;
077: import javax.swing.JTextField;
078: import javax.swing.border.EmptyBorder;
079: import javax.swing.event.DocumentEvent;
080: import org.openide.DialogDescriptor;
081: import org.openide.DialogDisplayer;
082: import org.openide.util.NbBundle;
083:
084: /**
085: * This class determines the rowset to be used with a dataprovider.
086: * Based on the DesignContext and the tablename, we first compile a
087: * list of all matching rowsets (same tablename, etc) and also possible
088: * request/session/application beans.
089: * it also constructs a panel to allow picking one of the rowsets.
090: *
091: * @author jfbrown
092: */
093: public class RowSetSelection extends javax.swing.JPanel {
094:
095: private final String tableName;
096: private final String bareTableName;
097: private final String user;
098: private final String url;
099: private final String command;
100: private final DesignContext curContext;
101:
102: private String dataSourceName; // Short name
103: private String fullDataSourceName; // full name (with jfbe
104:
105: private static final String selectText = NbBundle.getMessage(
106: RowSetSelection.class, "RowSelectButton_label");
107: private static final String createText = NbBundle.getMessage(
108: RowSetSelection.class, "RowCreateButton_label");
109:
110: /****
111: * when something is selected by the user, one of these two will be set.
112: */
113: public DesignBean selectedDesignBean = null;
114: public DesignContext createContext = null;
115:
116: private static String javacomp = "java:comp/env/jdbc/";
117:
118: /**
119: * Create the thing. No UI is created by this constructor.
120: * Rather, we just search for rowsets and dataproviders.
121: */
122: public RowSetSelection(DesignContext currentContext,
123: String tableName, String dataSrcName, String user,
124: String url, String command) {
125:
126: this .curContext = currentContext;
127: this .tableName = tableName;
128:
129: int bt = tableName.lastIndexOf('.');
130: if (bt >= 0) {
131: this .bareTableName = tableName.substring(bt + 1);
132: } else {
133: this .bareTableName = tableName;
134: }
135:
136: if (dataSrcName != null) {
137: if (dataSrcName.startsWith(javacomp))
138: dataSrcName = dataSrcName.substring(javacomp.length());
139: }
140: this .dataSourceName = dataSrcName;
141: this .fullDataSourceName = "java:comp/env/jdbc/"
142: + dataSourceName; // NOI18N
143:
144: this .user = user;
145: this .url = url;
146: this .command = command;
147:
148: findRowSets();
149:
150: }
151:
152: // For performance improvement. No need to get all the contexts in the project
153: private DesignContext[] getDesignContexts(DesignContext context) {
154: DesignProject designProject = context.getProject();
155: DesignContext[] contexts;
156: if (designProject instanceof FacesDesignProject) {
157: contexts = ((FacesDesignProject) designProject)
158: .findDesignContexts(new String[] { "request",
159: "session", "application" });
160: } else {
161: contexts = new DesignContext[0];
162: }
163: DesignContext[] designContexts = new DesignContext[contexts.length + 1];
164: designContexts[0] = context;
165: System.arraycopy(contexts, 0, designContexts, 1,
166: contexts.length);
167: return designContexts;
168: }
169:
170: ArrayList mCommands = new ArrayList();
171: ArrayList mTables = new ArrayList();
172: ArrayList cBeans_this Page = new ArrayList();
173: ArrayList cBeans_request = new ArrayList();
174: ArrayList cBeans_session = new ArrayList();
175: ArrayList cBeans_application = new ArrayList();
176: ArrayList<DesignBean> alRowSets = new ArrayList<DesignBean>();
177:
178: private void findRowSets() {
179:
180: String this Scope = (String) curContext
181: .getContextData(Constants.ContextData.SCOPE);
182:
183: //DesignContext[] contexts = curContext.getProject().getDesignContexts() ;
184: DesignContext[] contexts = getDesignContexts(curContext);
185:
186: for (int i = 0; i < contexts.length; i++) {
187: String cScope = (String) contexts[i]
188: .getContextData(Constants.ContextData.SCOPE);
189: Log.log("RSS: examining " + contexts[i].getDisplayName()
190: + " , " + cScope);
191: if (compareScopes(this Scope, cScope) < 0) {
192: // ignore if "same" scope.
193: continue;
194: }
195: if (SCOPE_PAGE.equals(this Scope)
196: && contexts[i] != curContext) {
197: // ignore if PAGE and not self.
198: continue;
199: }
200: if (SCOPE_REQUEST.equals(this Scope)
201: && SCOPE_REQUEST.equals(cScope)) {
202: // If request Scope, loop context is also request
203: if (contexts[i] != curContext
204: && (contexts[i].getDisplayName().indexOf(
205: "RequestBean") < 0)) {
206: // ignore if REQUEST and (not self or not RequestBean)
207: continue;
208: }
209: if (contexts[i] == curContext
210: && contexts[i].getDisplayName().indexOf(
211: "RequestBean") < 0) {
212: cBeans_this Page.add(curContext);
213: }
214: }
215: DesignBean[] rowsets = contexts[i]
216: .getBeansOfType(CachedRowSetX.class);
217: for (int j = 0; j < rowsets.length; j++) {
218: if (alRowSets.contains(rowsets[j])) {
219: continue;
220: }
221:
222: alRowSets.add(rowsets[j]);
223: int matchVal = compareRowSet(rowsets[j]);
224: if (Log.isLoggable()) {
225: Log.log("RSS: " + contexts[i].getDisplayName()
226: + "." + rowsets[j].getInstanceName()
227: + " match=" + matchVal);
228: }
229: if (matchVal == 1) {
230: mTables.add(rowsets[j]);
231: } else if (matchVal == 2) {
232: mCommands.add(rowsets[j]);
233: }
234: }
235:
236: // add non-page "create" contexts
237: if (contexts[i].getDisplayName().indexOf("RequestBean") >= 0) {
238: if (!cBeans_request.contains(contexts[i])) {
239: cBeans_request.add(contexts[i]);
240: }
241: } else if (contexts[i].getDisplayName().indexOf(
242: "SessionBean") >= 0) {
243: if (!cBeans_session.contains(contexts[i])) {
244: cBeans_session.add(contexts[i]);
245: }
246: } else if (contexts[i].getDisplayName().indexOf(
247: "ApplicationBean") >= 0) {
248: if (!cBeans_application.contains(contexts[i])) {
249: cBeans_application.add(contexts[i]);
250: }
251: }
252: }
253: }
254:
255: public boolean hasMatchingRowSets() {
256: boolean nomatches = (mCommands.size() == 0 && mTables.size() == 0);
257: return !nomatches;
258: }
259:
260: /***
261: * returns an array of DesignContext objects of the given scope or higher.
262: */
263: public Object[] getCreateBeans(String scope) {
264: if (scope == null)
265: scope = SCOPE_SESSION; // default
266: if (scope.equals(SCOPE_REQUEST))
267: return cBeans_request.toArray();
268: if (scope.equals(SCOPE_SESSION))
269: return cBeans_session.toArray();
270: if (scope.equals(SCOPE_APPLICATION))
271: return cBeans_application.toArray();
272: throw new IllegalArgumentException("RSS arg eror: scope is '"
273: + scope + "'"); // NOI18N
274: }
275:
276: /****
277: * Now build the panel showing the matches.
278: * first, list the command matches.
279: * then, list the tableName matches.
280: * then, list the request/session/application context instances.
281: */
282: Color roBgColor = null;
283:
284: private void buildSelectionPanel() {
285: TextField tf = new TextField();
286: tf.setEditable(false);
287: roBgColor = tf.getBackground();
288:
289: initComponents();
290:
291: titleLabel.setBackground(roBgColor);
292:
293: GridBagConstraints constraints = new GridBagConstraints(
294: GridBagConstraints.RELATIVE, 0, 1, 1, // int gridx, int gridy, int gridwidth, int gridheight,
295: 0.0, 0.0, // double weightx, double weighty,
296: GridBagConstraints.CENTER, GridBagConstraints.NONE, // int anchor, int fill,
297: new java.awt.Insets(5, 5, 5, 5), 0, 0); // Insets insets, int ipadx, int ipady)
298: GridBagConstraints seperatorConstraints = new GridBagConstraints(
299: GridBagConstraints.RELATIVE, 0, 3,
300: 1, // int gridx, int gridy, int gridwidth, int gridheight,
301: 1.0,
302: 0.0, // double weightx, double weighty,
303: GridBagConstraints.NORTHWEST,
304: GridBagConstraints.HORIZONTAL, // int anchor, int fill,
305: new java.awt.Insets(5, 5, 5, 5), 0, 0); // Insets insets, int ipadx, int ipady)
306:
307: AbstractButton defaultButton = null;
308: BeanColumnPanel defaultBeanColumn = null;
309: int currentRow = -1;
310: int numberOfBeans = 0;
311: for (int i = 0; i < 7; i++) {
312: ArrayList curList = null;
313: boolean select = false;
314: boolean separator = false;
315: switch (i) {
316: case 0:
317: curList = mCommands;
318: select = true;
319: break;
320: case 1:
321: curList = mTables;
322: select = true;
323: break;
324: case 2:
325: separator = true;
326: break;
327: case 3:
328: curList = cBeans_this Page;
329: select = false;
330: break;
331: case 4:
332: curList = cBeans_request;
333: select = false;
334: break;
335: case 5:
336: curList = cBeans_session;
337: select = false;
338: break;
339: case 6:
340: curList = cBeans_application;
341: select = false;
342: break;
343: }
344: if (separator) {
345: // add a seperator
346: seperatorConstraints.gridy = ++currentRow;
347: selectionPanel.add(new JSeparator(),
348: seperatorConstraints);
349: continue;
350: }
351: if (curList == null) {
352: continue;
353: }
354: for (int j = 0, max = curList.size(); j < max; j++) {
355: constraints.gridy = ++currentRow;
356: constraints.weightx = 0;
357: constraints.fill = GridBagConstraints.NONE; // reset
358:
359: AbstractButton doButton = null;
360: if (select) {
361: doButton = makeButtonColumn(selectText);
362: } else {
363: doButton = makeButtonColumn(createText);
364: }
365:
366: BeanColumnPanel beanColumn = null;
367: JComponent commandColumn = null;
368: if (select) {
369: DesignBean db = (DesignBean) curList.get(j);
370: beanColumn = makeBeanColumn(db.getDesignContext(),
371: db, doButton);
372: try {
373: if (db.getInstance() instanceof CachedRowSetX) {
374: CachedRowSetX rs = (CachedRowSetX) db
375: .getInstance();
376: commandColumn = makeCommandColumn(rs
377: .getCommand());
378:
379: } else {
380: throw new RuntimeException(
381: "Internal error: not a CachedRowSetX."); // NOI18N
382: }
383: } catch (Exception se) { // catch runtime exceptions.
384: commandColumn = makeCommandColumn(se
385: .getLocalizedMessage());
386: doButton.setEnabled(false);
387: }
388: } else {
389: beanColumn = makeBeanColumn(
390: ((DesignContext) curList.get(j)), null,
391: doButton);
392: commandColumn = makeCommandColumn(command);
393: }
394:
395: if (doButton.isEnabled() && defaultButton == null) {
396: defaultButton = doButton;
397: defaultBeanColumn = beanColumn;
398: }
399: selectionPanel.add(doButton, constraints);
400: selectionPanel.add(beanColumn, constraints);
401: constraints.weightx = 1.0;
402: constraints.fill = GridBagConstraints.BOTH;
403: selectionPanel.add(commandColumn, constraints);
404: numberOfBeans += 1;
405: doButton.addActionListener(new SelectionListener(
406: beanColumn));
407: }
408: }
409:
410: if (numberOfBeans < 1) {
411: // nowhere to add it - Should never be here.
412: selectionPanel.add(new JLabel("No beans with rowsets.")); // NOI18N
413: } else {
414: // DataconnecitivityUIUtils.equalizeButtonPreferredSizes( (JComponent[])beanList.toArray(new JPanel[beanList.size()]) , 0 ,0 ) ;
415: defaultButton.setSelected(true);
416: setSelectedBean(defaultBeanColumn);
417: }
418: // add something to fill the bottom.
419: constraints.gridy = ++currentRow;
420: constraints.weightx = 0;
421: constraints.weighty = 1.0;
422: constraints.gridwidth = 4;
423: constraints.fill = GridBagConstraints.BOTH; // reset
424: selectionPanel.add(new JPanel(), constraints);
425:
426: }
427:
428: public class SelectionListener implements ActionListener {
429: BeanColumnPanel beanpanel;
430:
431: public SelectionListener(BeanColumnPanel bp) {
432: beanpanel = bp;
433: }
434:
435: public void actionPerformed(ActionEvent e) {
436: if (((AbstractButton) e.getSource()).isEnabled()) {
437: setSelectedBean(beanpanel);
438: }
439: }
440: }
441:
442: private BeanColumnPanel selectedPanel = null;
443:
444: private void setSelectedBean(BeanColumnPanel beanPanel) {
445: selectedPanel = beanPanel;
446: }
447:
448: public DesignContext getSelectedDesignContext() {
449: return selectedPanel.context;
450: }
451:
452: public DesignBean getSelectedRowSetBean() {
453: return selectedPanel.rowSetBean;
454: }
455:
456: public String getSelectedRowSetName() {
457: return selectedPanel.rowsetName.getText();
458: }
459:
460: /** This method is called from within the constructor to
461: * initialize the form.
462: * WARNING: Do NOT modify this code. The content of this method is
463: * always regenerated by the Form Editor.
464: */
465: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
466: private void initComponents() {
467: java.awt.GridBagConstraints gridBagConstraints;
468:
469: selections = new javax.swing.ButtonGroup();
470: titleLabel = new javax.swing.JTextArea();
471: jSeparator1 = new javax.swing.JSeparator();
472: rowsetPane = new javax.swing.JScrollPane();
473: selectionPanel = new javax.swing.JPanel();
474: bottomLabel = new javax.swing.JLabel();
475:
476: setLayout(new java.awt.GridBagLayout());
477:
478: getAccessibleContext().setAccessibleName(
479: org.openide.util.NbBundle.getMessage(
480: RowSetSelection.class, "RowSetDialogTitle"));
481: getAccessibleContext().setAccessibleDescription(
482: org.openide.util.NbBundle.getMessage(
483: RowSetSelection.class, "RowSetDialogTitle"));
484: titleLabel.setEditable(false);
485: titleLabel.setLineWrap(true);
486: titleLabel.setText(org.openide.util.NbBundle.getMessage(
487: RowSetSelection.class, "RowSetTitle1_label",
488: new Object[] { bareTableName }));
489: titleLabel.setWrapStyleWord(true);
490: gridBagConstraints = new java.awt.GridBagConstraints();
491: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
492: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
493: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
494: add(titleLabel, gridBagConstraints);
495:
496: gridBagConstraints = new java.awt.GridBagConstraints();
497: gridBagConstraints.gridx = 0;
498: gridBagConstraints.gridy = 2;
499: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
500: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
501: gridBagConstraints.weightx = 1.0;
502: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
503: add(jSeparator1, gridBagConstraints);
504:
505: selectionPanel.setLayout(new java.awt.GridBagLayout());
506:
507: rowsetPane.setViewportView(selectionPanel);
508:
509: gridBagConstraints = new java.awt.GridBagConstraints();
510: gridBagConstraints.gridx = 0;
511: gridBagConstraints.gridy = 3;
512: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
513: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
514: gridBagConstraints.weightx = 1.0;
515: gridBagConstraints.weighty = 1.0;
516: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
517: add(rowsetPane, gridBagConstraints);
518:
519: bottomLabel.setText(org.openide.util.NbBundle.getMessage(
520: RowSetSelection.class, "RowSetFooter1_label",
521: new Object[] {}));
522: gridBagConstraints = new java.awt.GridBagConstraints();
523: gridBagConstraints.gridx = 0;
524: gridBagConstraints.gridy = 4;
525: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
526: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
527: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
528: add(bottomLabel, gridBagConstraints);
529: bottomLabel.getAccessibleContext().setAccessibleDescription(
530: org.openide.util.NbBundle.getMessage(
531: RowSetSelection.class, "RowSetFooter1_label"));
532:
533: }
534:
535: // </editor-fold>//GEN-END:initComponents
536:
537: // Variables declaration - do not modify//GEN-BEGIN:variables
538: private javax.swing.JLabel bottomLabel;
539: private javax.swing.JSeparator jSeparator1;
540: private javax.swing.JScrollPane rowsetPane;
541: private javax.swing.JPanel selectionPanel;
542: private javax.swing.ButtonGroup selections;
543: private javax.swing.JTextArea titleLabel;
544:
545: // End of variables declaration//GEN-END:variables
546:
547: public AbstractButton makeButtonColumn(String text /*,ActionListener al)*/) {
548: JRadioButton xx = new JRadioButton(text);
549: xx.setBorder(new EmptyBorder(5, 5, 0, 0));
550: // xx.setMnemonic(text.charAt(0));
551: xx.getAccessibleContext()
552: .setAccessibleDescription(xx.getText());
553: selections.add(xx);
554: addToTabOrder(xx);
555: return xx;
556: }
557:
558: public BeanColumnPanel makeBeanColumn(DesignContext context,
559: DesignBean rowSetBean, AbstractButton columnButton) {
560: BeanColumnPanel jp = new BeanColumnPanel(context, rowSetBean);
561: JLabel xxLabel = new JLabel(context.getDisplayName());
562: xxLabel.getAccessibleContext().setAccessibleDescription(
563: xxLabel.getText());
564: xxLabel.getAccessibleContext().setAccessibleName(
565: xxLabel.getText());
566: jp.add(xxLabel);
567: JComponent xxField = null;
568: if (rowSetBean != null) {
569: xxField = new JLabel(rowSetBean.getInstanceName());
570: xxField.getAccessibleContext().setAccessibleDescription(
571: rowSetBean.getInstanceName());
572: xxField.getAccessibleContext().setAccessibleName(
573: rowSetBean.getInstanceName());
574: } else {
575: String defaultName = getRowSetNameForContext(context, null);
576: JTextField tf = new JTextField(defaultName);
577: tf.setColumns(15);
578:
579: // PlainDocument nameDoc = new PlainDocument() ;
580: tf.getDocument().addDocumentListener(
581: new DocListener(columnButton, jp));
582: // tf.setDocument(nameDoc) ;
583: xxLabel.setLabelFor(xxField);
584: addToTabOrder(tf);
585:
586: // either set the tooltip or the a11y description
587: tf.setName(context.getDisplayName());
588: tf.getAccessibleContext().setAccessibleDescription(
589: context.getDisplayName());
590:
591: xxField = (JComponent) tf;
592: jp.rowsetName = tf;
593: }
594: jp.add(xxField);
595: return jp;
596: }
597:
598: /***
599: * this class holds the file name (context) and bean name (rowset name)
600: */
601: public class BeanColumnPanel extends JPanel {
602: public DesignContext context = null; // never be null
603: public DesignBean rowSetBean = null; // may be null
604: public JTextField rowsetName = null;
605:
606: public BeanColumnPanel(DesignContext ctx, DesignBean designBean) {
607: super (new GridLayout(0, 1));
608: this .context = ctx;
609: this .rowSetBean = designBean;
610: String bStuff = "Bean to Use or Create"; //NOI18N
611: this .getAccessibleContext().setAccessibleName(bStuff);
612: this .getAccessibleContext()
613: .setAccessibleDescription(bStuff);
614: }
615: }
616:
617: /***
618: * Construct a valid rowset name (unique within the context if a context is
619: * provided.
620: * if tablename is not provided, start with "bareTableName"+DataconnectivitySettings.getRsSuffix(), otherwise
621: * start with provided tableName.
622: * Within the provided context, make sure there are no other rowsets with
623: * the same name
624: */
625: private String getRowSetNameForContext(DesignContext context,
626: String testTableName) {
627: DesignBean[] dbeans = null;
628: if (context != null) {
629: dbeans = context.getBeansOfType(CachedRowSetX.class);
630: }
631: if (testTableName == null) {
632: testTableName = bareTableName.toLowerCase().replaceAll(" ",
633: "")
634: + DataconnectivitySettings.getRsSuffix();
635: }
636: String lookForName = testTableName;
637: if (dbeans != null) {
638: for (int i = 0; i < 333; i++) {
639: if (i > 0)
640: lookForName = testTableName + Integer.toString(i);
641: boolean exists = false;
642: for (int j = 0; j < dbeans.length; j++) {
643: if (lookForName.equals(dbeans[j].getInstanceName())) {
644: exists = true;
645: break;
646: }
647: }
648: if (!exists) {
649: break;
650: }
651: }
652: }
653: return lookForName;
654: }
655:
656: public JComponent makeDataSourceColumn(String tableName, String ds,
657: String user, String url) {
658: java.awt.LayoutManager lm = new GridLayout(0, 1);
659: JPanel jp = new JPanel(lm);
660: jp.add(new JLabel(tableName));
661: jp.add(new JLabel(ds));
662: jp.add(new JLabel(user));
663: return jp;
664: }
665:
666: public JComponent makeCommandColumn(String text) {
667: return new RowSetSelectionQuery(text);
668: }
669:
670: private static final String SCOPE_PAGE = "page"; // ??unused??
671: private static final String SCOPE_REQUEST = "request";
672: private static final String SCOPE_SESSION = "session";
673: private static final String SCOPE_APPLICATION = "application";
674:
675: // navigate up the scope hierarchy
676: private static String getNextScope(String curScope) {
677: if (curScope.equals(SCOPE_PAGE))
678: return SCOPE_REQUEST;
679: if (curScope.equals(SCOPE_REQUEST))
680: return SCOPE_SESSION;
681: if (curScope.equals(SCOPE_SESSION))
682: return SCOPE_APPLICATION;
683: return null;
684: }
685:
686: /***
687: * compare scopes
688: * returns <0 if scope1 < scope2
689: * 0 if scope1 == scope2
690: * >0 if scope1 > scope2
691: * -99 if either scope1 or scope2 is "unknown".
692: **/
693: private static int compareScopes(String scope1, String scope2) {
694:
695: if (scope1.equals(SCOPE_PAGE)) {
696: if (scope2.equals(SCOPE_PAGE)) {
697: return 0;
698: } else if (scope2.equals(SCOPE_REQUEST)) {
699: return 1;
700: } else if (scope2.equals(SCOPE_SESSION)) {
701: return 2;
702: } else if (scope2.equals(SCOPE_APPLICATION)) {
703: return 3;
704: }
705: return -99;
706: }
707: if (scope1.equals(SCOPE_REQUEST)) {
708: if (scope2.equals(SCOPE_PAGE)) {
709: return -1;
710: } else if (scope2.equals(SCOPE_REQUEST)) {
711: return 0;
712: } else if (scope2.equals(SCOPE_SESSION)) {
713: return 1;
714: } else if (scope2.equals(SCOPE_APPLICATION)) {
715: return 2;
716: }
717: return -99;
718: }
719: if (scope1.equals(SCOPE_SESSION)) {
720: if (scope2.equals(SCOPE_PAGE)) {
721: return -2;
722: } else if (scope2.equals(SCOPE_REQUEST)) {
723: return -1;
724: } else if (scope2.equals(SCOPE_SESSION)) {
725: return 0;
726: } else if (scope2.equals(SCOPE_APPLICATION)) {
727: return 1;
728: }
729: return -99;
730: }
731: if (scope1.equals(SCOPE_APPLICATION)) {
732: if (scope2.equals(SCOPE_PAGE)) {
733: return -3;
734: } else if (scope2.equals(SCOPE_REQUEST)) {
735: return -2;
736: } else if (scope2.equals(SCOPE_SESSION)) {
737: return -1;
738: } else if (scope2.equals(SCOPE_APPLICATION)) {
739: return 0;
740: }
741: return -99;
742: }
743: // assume PAGE scope if scope1 is not known.
744: if (scope2.equals(SCOPE_PAGE)) {
745: return 0;
746: } else if (scope2.equals(SCOPE_REQUEST)) {
747: return 1;
748: } else if (scope2.equals(SCOPE_SESSION)) {
749: return 2;
750: } else if (scope2.equals(SCOPE_APPLICATION)) {
751: return 3;
752: }
753: return -99;
754: }
755:
756: /***
757: * compare a design bean to the table of this instance.
758: * return an int representing how well it matches.
759: * < 0 = bad, does not match dataSourceName, url, or username.
760: * 0 = bad, dataSourceName match, but no table or command match.
761: * 1 = good, tableName matches
762: * 2 = excellent, command matches
763: */
764: private int compareRowSet(DesignBean rowset) {
765:
766: if (!DataconnectivitySettings.checkRowsets())
767: return -3;
768:
769: // first compare dataSourceName
770: DesignProperty prop = rowset.getProperty("dataSourceName"); // NOI18N
771: if (prop == null || fullDataSourceName == null) {
772: return -2;
773: }
774: if (!fullDataSourceName.equals(prop.getValue())) {
775: return -2;
776: }
777:
778: // compare url - if not equal, return -1
779: // TODO: skip for EA
780:
781: // compare username - if not equal, return -1
782: // TODO: skip for EA
783:
784: // compare command - if equal, return 2
785: prop = rowset.getProperty("command"); // NOI18N
786: if (prop != null && command != null) {
787: if (command.equals(prop.getValue())) {
788: return 2;
789: } else {
790: if (prop.getValue() == null) {
791: // check for NULL command.
792: return -2;
793: } else if ("".equals((String) prop.getValue())) {
794: return -2;
795: }
796: }
797: }
798:
799: // compare tableName - if equal, return 1
800: prop = rowset.getProperty("tableName"); // NOI18N
801: if (prop != null && tableName != null) {
802: if (tableName.equals(prop.getValue())) {
803: return 1;
804: }
805: }
806: if (prop != null && bareTableName != null) {
807: if (bareTableName.equals(prop.getValue())) {
808: return 1;
809: }
810: }
811:
812: // bad, but at least dataSource stuff matches.
813: return 0;
814: }
815:
816: private JDialog dialog;
817: private static JButton okButton = new JButton(NbBundle.getMessage(
818: RowSetSelection.class, "RowSetDialogAcceptButton"));
819: private static JButton cancelButton = new JButton(NbBundle
820: .getMessage(RowSetSelection.class,
821: "RowSetDialogCancelButton"));
822:
823: // for calculating return value ;
824: private boolean makeStuff = false;
825:
826: public boolean showDialog() {
827: // Add a listener to the dialog's buttons
828: this .buildSelectionPanel();
829:
830: okButton.getAccessibleContext().setAccessibleDescription(
831: okButton.getText());
832: cancelButton.getAccessibleContext().setAccessibleDescription(
833: cancelButton.getText());
834: // listener to the dialog's buttons
835: ActionListener listener = new ActionListener() {
836: public void actionPerformed(ActionEvent evt) {
837:
838: if (evt.getSource() == okButton) {
839: makeStuff = true;
840: } else {
841: makeStuff = false;
842: }
843: dialog.dispose();
844: }
845: };
846: DialogDescriptor dlg = new DialogDescriptor(this , NbBundle
847: .getMessage(RowSetSelection.class, "RowSetDialogTitle",
848: bareTableName), true, listener);
849: dlg.setOptions(new Object[] { okButton, cancelButton });
850: dlg
851: .setHelpCtx(new org.openide.util.HelpCtx(
852: "projrave_ui_elements_server_nav_add_new_data_provider"));
853: dlg.setClosingOptions(null);
854:
855: dialog = (JDialog) DialogDisplayer.getDefault().createDialog(
856: dlg);
857:
858: dialog.setResizable(true);
859: dialog.pack();
860: Dimension dSize = dialog.getSize();
861: final double maxSize = 575;
862: if (dSize.getHeight() > maxSize) {
863: dSize.setSize(dSize.getWidth(), maxSize);
864: dialog.setSize(dSize);
865: }
866:
867: // HACK alert - fix tabbing order. The only way to get the
868: // help button is via looking up the children of the contentPane.
869: Tabbing tabber = new Tabbing();
870: Component[] comps = dialog.getContentPane().getComponents();
871: if (comps.length > 0) {
872: Component comp = comps[1]; /// JPanel with buttons
873: if (comp instanceof Container) { // should always be true
874: Component[] buttons = ((Container) comp)
875: .getComponents();
876: for (int j = 0; j < buttons.length; j++) {
877: if (buttons[j] instanceof JButton) { // should always be true
878: tabber.addToTab(buttons[j]);
879: }
880: }
881: }
882: }
883: tabber.addToTab(rowsetPane);
884: dialog.setFocusTraversalPolicy(tabber);
885:
886: dialog.setVisible(true);
887:
888: return makeStuff;
889: }
890:
891: /***
892: * listener for the "roset Name" field. When touched, select the
893: * appropriate button.
894: */
895: public class DocListener implements
896: javax.swing.event.DocumentListener {
897:
898: AbstractButton button = null;
899: BeanColumnPanel beanPanel = null;
900:
901: public DocListener(AbstractButton jb,
902: BeanColumnPanel beanColumnPanel) {
903: button = jb;
904: beanPanel = beanColumnPanel;
905: }
906:
907: public void insertUpdate(DocumentEvent ev) {
908: update(ev);
909: }
910:
911: public void changedUpdate(DocumentEvent ev) {
912: update(ev);
913: }
914:
915: public void removeUpdate(DocumentEvent ev) {
916: update(ev);
917: }
918:
919: private void update(DocumentEvent ev) {
920: selections.setSelected(button.getModel(), true);
921: setSelectedBean(beanPanel);
922: }
923: }
924:
925: ArrayList panelTabOrderList = new ArrayList(12);
926:
927: protected void addToTabOrder(Component o) {
928: panelTabOrderList.add(o);
929: }
930:
931: public class Tabbing extends FocusTraversalPolicy {
932: ArrayList tabOrderList = new ArrayList(15);
933:
934: public Tabbing() {
935: tabOrderList = (ArrayList) panelTabOrderList.clone();
936: }
937:
938: public void addToTab(Component o) {
939: tabOrderList.add(o);
940: }
941:
942: public java.awt.Component getComponentBefore(
943: java.awt.Container focusCycleRoot,
944: java.awt.Component aComponent) {
945: for (int i = 0; i < tabOrderList.size(); i++) {
946: if (aComponent == tabOrderList.get(i)) {
947: if (i == 0)
948: i = tabOrderList.size() - 1;
949: else
950: i = i - 1;
951: return (Component) tabOrderList.get(i);
952: }
953: }
954: return (Component) tabOrderList.get(0);
955: }
956:
957: public java.awt.Component getComponentAfter(
958: java.awt.Container focusCycleRoot,
959: java.awt.Component aComponent) {
960: for (int i = 0; i < tabOrderList.size(); i++) {
961: if (aComponent == tabOrderList.get(i)) {
962: if (i == tabOrderList.size() - 1)
963: i = 0;
964: else
965: i = i + 1;
966: return (Component) tabOrderList.get(i);
967: }
968: }
969: return (Component) tabOrderList
970: .get(tabOrderList.size() - 1);
971: }
972:
973: public java.awt.Component getLastComponent(
974: java.awt.Container focusCycleRoot) {
975: return (Component) tabOrderList
976: .get(tabOrderList.size() - 1);
977: }
978:
979: public java.awt.Component getFirstComponent(
980: java.awt.Container focusCycleRoot) {
981: return (Component) tabOrderList.get(0);
982: }
983:
984: public java.awt.Component getDefaultComponent(
985: java.awt.Container focusCycleRoot) {
986: return (Component) tabOrderList.get(0);
987: }
988:
989: }
990: }
|