001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog.ui.wizard;
018:
019: import java.sql.Connection;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import net.refractions.udig.catalog.ui.CatalogUIPlugin;
026: import net.refractions.udig.catalog.ui.internal.Messages;
027:
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.custom.CCombo;
030: import org.eclipse.swt.events.FocusEvent;
031: import org.eclipse.swt.events.FocusListener;
032: import org.eclipse.swt.events.ModifyEvent;
033: import org.eclipse.swt.events.ModifyListener;
034: import org.eclipse.swt.events.SelectionEvent;
035: import org.eclipse.swt.events.SelectionListener;
036: import org.eclipse.swt.graphics.Point;
037: import org.eclipse.swt.layout.GridData;
038: import org.eclipse.swt.layout.GridLayout;
039: import org.eclipse.swt.widgets.Button;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.Control;
042: import org.eclipse.swt.widgets.Group;
043: import org.eclipse.swt.widgets.Label;
044: import org.eclipse.swt.widgets.Text;
045:
046: /**
047: * David's magic superclass, seems to delegate to createAdvancedControl for anything cool.
048: *
049: * @author dzwiers
050: * @since 0.3
051: */
052: public abstract class DataBaseRegistryWizardPage extends
053: DataStoreWizardPage implements ModifyListener,
054: SelectionListener, FocusListener {
055:
056: private boolean focusing;
057:
058: public DataBaseRegistryWizardPage(String name) {
059: super (name);
060: }
061:
062: public DataBaseRegistryWizardPage() {
063: super (""); //$NON-NLS-1$
064: }
065:
066: /**
067: * gets the host name
068: */
069: protected abstract String getHostText();
070:
071: /**
072: * gets the port number
073: */
074: protected abstract String getPortText();
075:
076: /**
077: * gets the password
078: */
079: protected abstract String getPassText();
080:
081: /**
082: * gets the username
083: */
084: protected abstract String getUserText();
085:
086: /**
087: * Gets the database parameter
088: */
089: protected abstract String getDBText();
090:
091: /**
092: * TODO summary sentence for focusGained ...
093: *
094: * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
095: * @param e
096: */
097: public void focusGained(FocusEvent e) {
098: //set this flag to prevent further connections upon
099: // another focus
100: if (focusing || getHostText().trim().length() == 0
101: || getUserText().trim().length() == 0
102: || getPortText().trim().length() == 0)
103: return;
104:
105: focusing = true;
106: if (e.widget != null) {
107: if (e.widget.equals(database)) {
108: if (isDBCombo())
109: populateDB();
110: }
111:
112: if (hasSchema() && e.widget.equals(schema)) {
113: populateSchema();
114: }
115:
116: if (e.widget.equals(user) && user.getText().length() > 0) {
117: ((Text) user).setSelection(new Point(0, user.getText()
118: .length()));
119: }
120: if (e.widget.equals(pass) && pass.getText().length() > 0) {
121: ((Text) pass).setSelection(new Point(0, pass.getText()
122: .length()));
123: }
124: if (e.widget.equals(port) && port.getText().length() > 0) {
125: ((Text) port).setSelection(new Point(0, port.getText()
126: .length()));
127: }
128: }
129: getWizard().getContainer().updateButtons();
130: focusing = false;
131: }
132:
133: /**
134: * TODO summary sentence for focusLost ...
135: *
136: * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
137: * @param e
138: */
139: public void focusLost(FocusEvent e) {
140: // do nothing
141: getWizard().getContainer().updateButtons();
142: }
143:
144: // protected Text host = null;
145: protected Control host = null;
146: protected Text port = null;
147: protected Text user = null;
148: protected Text pass = null;
149: protected Control database = null;
150: protected CCombo schema = null;
151: protected Button advancedKey = null;
152: protected Group advanced = null;
153:
154: protected abstract boolean isDBCombo();
155:
156: protected abstract boolean isHostCombo();
157:
158: protected abstract boolean hasSchema();
159:
160: String hostStr = null;
161: String portStr = null;
162: String userStr = null;
163: String passStr = null;
164: String databaseStr = null;
165: String schemaStr = null;
166: private boolean fireEvents = true;
167:
168: /**
169: * TODO summary sentence for createControl ...
170: *
171: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
172: * @param parent
173: */
174: public void createControl(Composite arg0) {
175: Composite composite = new Group(arg0, SWT.NULL);
176: composite.setLayout(new GridLayout(5, true));
177: GridData data = new GridData(SWT.FILL, SWT.FILL, false, false);
178: data.widthHint = arg0.getClientArea().width;
179: composite.setLayoutData(data);
180:
181: // add host
182: Label label = new Label(composite, SWT.NONE);
183: label
184: .setText(Messages.DataBaseRegistryWizardPage_label_host_text);
185: label
186: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_host_tooltip);
187: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
188: false));
189:
190: if (isHostCombo()) {
191: CCombo host = new CCombo(composite, SWT.DROP_DOWN
192: | SWT.BORDER);
193: this .host = host;
194: host.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT,
195: true, false, 2, 1));
196: host.addModifyListener(this );
197: host
198: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_host_tooltip);
199: host.setEditable(true);
200: host.setLayoutDeferred(true);
201: } else {
202: Text host = new Text(composite, SWT.BORDER | SWT.SINGLE);
203: this .host = host;
204: host.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT,
205: true, false, 2, 1));
206: host.addModifyListener(this );
207: host
208: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_host_tooltip);
209: }
210: host.addFocusListener(this );
211: // host.setFocus();
212: // host = new Text( composite, SWT.BORDER | SWT.SINGLE );
213: // host.setLayoutData( new GridData(GridData.FILL, SWT.DEFAULT, true, false ,2,1) );
214: // host.addModifyListener(this);
215:
216: // add port
217: label = new Label(composite, SWT.NONE);
218: label
219: .setText(Messages.DataBaseRegistryWizardPage_label_port_text);
220: label
221: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_port_tooltip);
222: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
223: false));
224:
225: port = new Text(composite, SWT.BORDER | SWT.SINGLE);
226: port.setLayoutData(new GridData(GridData.FILL, SWT.DEFAULT,
227: true, false));
228: port.addModifyListener(this );
229: port.setTextLimit(6);
230: port.addFocusListener(this );
231:
232: // add user
233: label = new Label(composite, SWT.NONE);
234: label
235: .setText(Messages.DataBaseRegistryWizardPage_label_username_text);
236: label
237: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_username_tooltip);
238: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
239: false));
240:
241: user = new Text(composite, SWT.BORDER | SWT.SINGLE);
242: user.setLayoutData(new GridData(GridData.FILL, SWT.DEFAULT,
243: true, false, 2, 1));
244: user.addModifyListener(this );
245: user.addFocusListener(this );
246: //space
247: label = new Label(composite, SWT.NONE);
248: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
249: false, 2, 1));
250:
251: // add pass
252: label = new Label(composite, SWT.NONE);
253: label
254: .setText(Messages.DataBaseRegistryWizardPage_label_password_text);
255: label
256: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_password_tooltip);
257: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
258: false));
259:
260: pass = new Text(composite, SWT.BORDER | SWT.SINGLE
261: | SWT.PASSWORD);
262: pass.setLayoutData(new GridData(GridData.FILL, SWT.DEFAULT,
263: true, false, 2, 1));
264: pass.addModifyListener(this );
265: pass.addFocusListener(this );
266:
267: //space
268: label = new Label(composite, SWT.NONE);
269: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
270: false, 2, 1));
271:
272: // add spacer
273: label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
274: label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
275: false, 5, 3));
276:
277: // database
278: label = new Label(composite, SWT.NONE);
279: label
280: .setText(Messages.DataBaseRegistryWizardPage_label_database_text);
281: label
282: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_database_tooltip);
283: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
284: false));
285:
286: if (isDBCombo()) {
287: CCombo db = new CCombo(composite, SWT.DROP_DOWN
288: | SWT.BORDER);
289: this .database = db;
290: db.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true,
291: false, 2, 1));
292: db.addModifyListener(this );
293: db.addFocusListener(this );
294: db.addSelectionListener(this );
295: db
296: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_database_tooltip);
297: //db.setEditable(true);
298: } else {
299: Text db = new Text(composite, SWT.BORDER | SWT.SINGLE);
300: this .database = db;
301: db.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true,
302: false, 2, 1));
303: db.addModifyListener(this );
304: db.addFocusListener(this );
305: db
306: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_database_tooltip);
307: }
308:
309: label = new Label(composite, SWT.NONE);
310: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT, false,
311: false, 2, 1));
312:
313: if (hasSchema()) {
314: // schema
315: label = new Label(composite, SWT.NONE);
316: label
317: .setText(Messages.DataBaseRegistryWizardPage_label_schema_text);
318: label
319: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_schema_tooltip);
320: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT,
321: false, false));
322:
323: schema = new CCombo(composite, SWT.DROP_DOWN | SWT.BORDER);
324: schema.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT,
325: true, false, 2, 1));
326: schema.addModifyListener(this );
327: schema.addFocusListener(this );
328: schema
329: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_schema_tooltip);
330: schema.setEditable(true);
331:
332: label = new Label(composite, SWT.NONE);
333: label.setLayoutData(new GridData(SWT.END, SWT.DEFAULT,
334: false, false, 2, 1));
335: }
336:
337: // add spacer
338: label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
339: label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
340: false, 5, 3));
341:
342: // advanced
343: advancedKey = new Button(composite, SWT.CHECK);
344: advancedKey.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT,
345: false, false));
346: advancedKey.setSelection(false);
347: advancedKey.addSelectionListener(this );
348: advancedKey
349: .setText(Messages.DataBaseRegistryWizardPage_label_advanced_text);
350: advancedKey
351: .setToolTipText(Messages.DataBaseRegistryWizardPage_label_advanced_tooltip);
352:
353: label = new Label(composite, SWT.NONE); // advanced check row
354: label.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT,
355: false, false, 4, 1));
356: label = new Label(composite, SWT.NONE); // slot one of child row
357: label.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT,
358: false, false));
359:
360: advanced = createAdvancedControl(composite);
361: if (advanced == null) {
362: advancedKey.setVisible(false);// turn off - we don't want events
363: advancedKey.setEnabled(false);// turn off - we don't want events
364: } else {
365: data = new GridData(SWT.FILL, SWT.FILL, false, false);
366: data.horizontalSpan = 5;
367: advanced.setLayoutData(data);
368: advanced.setVisible(false);
369: }
370:
371: if (host.getData() == null && hostStr != null) {
372: if (isHostCombo()) {
373: ((CCombo) host).setText(hostStr);
374: ((CCombo) host).add(hostStr);
375: } else {
376: ((Text) host).setText(hostStr);
377: }
378: host.setData(hostStr);
379: port.setText(portStr);
380: user.setText(userStr);
381: pass.setText(passStr);
382: if (isDBCombo()) {
383: ((CCombo) database).setText(databaseStr);
384: } else {
385: ((Text) database).setText(databaseStr);
386: }
387: if (hasSchema())
388: schema.setText(schemaStr);
389: }
390: List<Control> tablist = new LinkedList<Control>();
391: tablist.add(host);
392: tablist.add(user);
393: tablist.add(pass);
394: tablist.add(database);
395: if (schema != null)
396: tablist.add(schema);
397: tablist.add(port);
398: tablist.add(advancedKey);
399: if (advanced != null)
400: tablist.add(advanced);
401:
402: composite.setTabList(tablist
403: .toArray(new Control[tablist.size()]));
404:
405: setControl(composite);
406: setPageComplete(true);
407: }
408:
409: protected abstract Group createAdvancedControl(Composite arg0);
410:
411: @Override
412: public void setVisible(boolean visible) {
413: super .setVisible(visible);
414: host.setFocus();
415: }
416:
417: @Override
418: public boolean isPageComplete() {
419: if (isHostCombo()) {
420: hostStr = ((CCombo) host).getText();
421: } else {
422: hostStr = ((Text) host).getText();
423: }
424: portStr = port.getText();
425: userStr = user.getText();
426: passStr = pass.getText();
427: if (isDBCombo()) {
428: databaseStr = ((CCombo) database).getText();
429: } else {
430: databaseStr = ((Text) database).getText();
431: }
432: if (hasSchema())
433: schemaStr = schema.getText();
434: return false;
435: }
436:
437: protected void setFireEvents(boolean fire) {
438: fireEvents = fire;
439: }
440:
441: protected boolean isFireEvents() {
442: return fireEvents;
443: }
444:
445: public void modifyText(ModifyEvent e) {
446: if (fireEvents) {
447: if (e.widget != null) {
448: if (e.widget instanceof Text) { //not a combo
449: ((Text) e.widget).setForeground(null);
450: //if the user/pass field is modified, clear the databases
451: if (isDBCombo()) {
452: ((CCombo) database).removeAll();
453: }
454: }
455: if (hasSchema() && e.widget.equals(database)
456: && schema.getItemCount() > 0) {
457: schema.removeAll();
458: }
459: }
460:
461: getWizard().getContainer().updateButtons();
462: }
463: }
464:
465: /**
466: * TODO summary sentence for widgetSelected ...
467: *
468: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
469: * @param e
470: */
471: public void widgetSelected(SelectionEvent e) {
472: if (e.widget != null && e.widget.equals(advancedKey)) {
473: advanced.setVisible(advancedKey.getSelection());
474: }
475:
476: widgetSelectedInternal(e);
477:
478: getWizard().getContainer().updateButtons();
479: }
480:
481: protected void widgetSelectedInternal(SelectionEvent e) {
482: //do nothing
483: }
484:
485: protected void populateDB() {
486: // will need a connection ...
487: CCombo db = (CCombo) this .database;
488:
489: //save current state
490: int selected = db.getSelectionIndex();
491: String string = null;
492: if (selected > -1) {
493: string = db.getItem(selected);
494: }
495:
496: db.removeAll();
497: db.setText(""); //$NON-NLS-1$
498:
499: Connection con = null;
500: try {
501: con = getConnection();
502: } catch (Exception e) {
503: CatalogUIPlugin.log(e.getLocalizedMessage(), e);
504: }
505:
506: if (con == null)
507: return;
508:
509: //connection ok, reset any previous error messages
510: setErrorMessage(null);
511:
512: ResultSet rs = null;
513: if (con != null)
514: try {
515: rs = con.getMetaData().getCatalogs();
516: while (rs.next()) {
517: String dbName = rs.getString(1);
518: if (!excludeDB(dbName)) {
519: ((CCombo) database).add(dbName);
520: }
521:
522: }
523: if (db.getItemCount() > 0)
524: db.select(0);
525:
526: } catch (SQLException e) {
527: setErrorMessage(e.getLocalizedMessage());
528: db.removeAll();
529: db.setText(""); //$NON-NLS-1$
530: return;
531: }
532:
533: if (string != null) {
534: String[] items = db.getItems();
535: for (int i = 0; items != null && i < items.length; i++) {
536: if (string.equals(items[i])) {
537: db.select(i);
538: return;
539: }
540: }
541: }
542: }
543:
544: protected void populateSchema() {
545: if (!hasSchema()) // error
546: return;
547: // will need a connection ...
548:
549: //save some state
550: String string = schema.getText();
551:
552: schema.removeAll();
553: schema.setText(""); //$NON-NLS-1$
554:
555: Connection con = null;
556: try {
557: con = getConnection();
558: } catch (Exception e) {
559: CatalogUIPlugin.log(e.getLocalizedMessage(), e);
560: setErrorMessage(e.getLocalizedMessage());
561: }
562:
563: if (con == null)
564: return;
565:
566: //connection ok, reset any previous error messages
567: setErrorMessage(null);
568:
569: ResultSet rs = null;
570: try {
571: rs = con.getMetaData().getSchemas();
572: while (rs.next()) {
573: String schemaName = rs.getString(1);
574: if (!excludeSchema(schemaName))
575: schema.add(schemaName);
576: }
577: if (schema.getItemCount() > 0)
578: schema.select(0);
579:
580: } catch (SQLException e) {
581: setMessage(Messages.DataBaseRegistryWizardPage_schemaMessage);
582: schema.removeAll();
583: if (string == null) {
584: schema.setText(""); //$NON-NLS-1$
585: } else { //pretend we didn't break it
586: schema.setText(string);
587: }
588: schema.setFocus();
589: return;
590: }
591:
592: setErrorMessage(null);
593: if (string != null) {
594: String[] items = schema.getItems();
595: for (int i = 0; items != null && i < items.length; i++) {
596: if (string.equals(items[i])) {
597: schema.select(i);
598: return;
599: }
600: }
601: }
602: }
603:
604: protected boolean excludeDB(String db) {
605: return false;
606: }
607:
608: protected boolean excludeSchema(String schema) {
609: return false;
610: }
611:
612: // to use for wizard polulation
613: protected abstract Connection getConnection() throws Exception;
614:
615: /**
616: * TODO summary sentence for widgetDefaultSelected ...
617: *
618: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
619: * @param e
620: */
621: public void widgetDefaultSelected(SelectionEvent e) {
622: // do nothing
623: }
624:
625: public static class DataBaseConnInfo {
626: private String host;
627: private String port;
628: private String user;
629: private String pass;
630: private String db;
631: private String schema;
632:
633: public DataBaseConnInfo(String host, String port, String user,
634: String pass, String db, String schema) {
635: this .host = host;
636: this .port = port;
637: this .user = user;
638: this .pass = pass;
639: this .db = db;
640: this .schema = schema;
641: }
642:
643: public DataBaseConnInfo(String dbEntry) {
644: String[] temp = dbEntry.split("\t"); //$NON-NLS-1$
645:
646: if (temp.length > 0)
647: this .host = temp[0];
648: if (temp.length > 1)
649: this .port = temp[1];
650: if (temp.length > 2)
651: this .user = temp[2];
652: if (temp.length > 3)
653: this .pass = temp[3];
654: if (temp.length > 4)
655: this .db = temp[4];
656: if (temp.length > 5)
657: this .schema = temp[5];
658: }
659:
660: public String getHost() {
661: return host == null ? "" : host; //$NON-NLS-1$
662: }
663:
664: public String getPort() {
665: return port == null ? "" : port; //$NON-NLS-1$
666: }
667:
668: public String getUser() {
669: return user == null ? "" : user; //$NON-NLS-1$
670: }
671:
672: public String getPass() {
673: return pass == null ? "" : pass; //$NON-NLS-1$
674: }
675:
676: public String getDb() {
677: return db == null ? "" : db; //$NON-NLS-1$
678: }
679:
680: public String getSchema() {
681: return schema == null ? "" : schema; //$NON-NLS-1$
682: }
683:
684: @Override
685: public String toString() {
686: return host
687: + "\t" + port + "\t" + user + "\t" + pass + "\t" + db + "\t" + schema; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
688: }
689:
690: @Override
691: public int hashCode() {
692: final int PRIME = 31;
693: int result = 1;
694: result = PRIME * result
695: + ((db == null) ? 0 : db.hashCode());
696: result = PRIME * result
697: + ((host == null) ? 0 : host.hashCode());
698: result = PRIME * result
699: + ((pass == null) ? 0 : pass.hashCode());
700: result = PRIME * result
701: + ((port == null) ? 0 : port.hashCode());
702: result = PRIME * result
703: + ((user == null) ? 0 : user.hashCode());
704: return result;
705: }
706:
707: @Override
708: public boolean equals(Object obj) {
709: if (this == obj)
710: return true;
711: if (obj == null)
712: return false;
713: if (getClass() != obj.getClass())
714: return false;
715: final DataBaseConnInfo other = (DataBaseConnInfo) obj;
716: if (db == null) {
717: if (other.db != null)
718: return false;
719: } else if (!db.equals(other.db))
720: return false;
721: if (host == null) {
722: if (other.host != null)
723: return false;
724: } else if (!host.equals(other.host))
725: return false;
726: if (pass == null) {
727: if (other.pass != null)
728: return false;
729: } else if (!pass.equals(other.pass))
730: return false;
731: if (port == null) {
732: if (other.port != null)
733: return false;
734: } else if (!port.equals(other.port))
735: return false;
736: if (user == null) {
737: if (other.user != null)
738: return false;
739: } else if (!user.equals(other.user))
740: return false;
741: return true;
742: }
743:
744: }
745:
746: }
|