home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-24 | 9.8 KB | 302 lines |
-
- /* Copyright (C) Microsoft Corporation, 1996-1998. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Visual J++ 6.0. See this product's on-line documentation for detailed
- information regarding Microsoft code samples.
-
- */
-
- import wfc.app.*;
- import wfc.core.*;
- import wfc.ui.*;
- import wfc.util.*;
- import wfc.data.ui.*;
- import wfc.data.*;
- import splash.*;
-
-
- /**BindPubs creates a simple form with text
- * boxes bound to the authors table in the
- * pubs database and a set of navigation buttons
- */
- public class BindPubs extends Form implements ISplashable
- {
- /**Default constructor creates form
- */
- public BindPubs()
- {
- SplashScreen splash = (new SplashScreen(this));
- Label splashLabel = splash.getSplashLabel();
- splashLabel.setFont(new Font("MS Sans Serif", 12));
- splashLabel.setText("Initializing Main Form");
- //call initForm() to set up form
- initForm();
- //create buttons with which to navigate the recordset
- splashLabel.setText("Creating Navigation Buttons");
- //ensure there'll be enough width for the buttons
- int minButtonWidth = 40;
- for(int i=0;i<buttonLabels.length;i++)
- {
- Button nav = new Button();
- nav.setText(buttonLabels[i]);
- nav.addOnClick(new EventHandler(this.nav_click));
- add(nav);
- navButtons.addItem(nav);
- nav.setWidth(buttonWidth);
- minButtonWidth += nav.getWidth() + buttonPad;
- }
- /*Create a new connection. There must be a dsn named pubs that logs on to a SQL server,
- and defaults to a database that contains a table names authors
- */
- splashLabel.setText("Opening Connection to DataBase");
- Connection pubs = new Connection("dsn=Pubs");
- System.err.println(pubs.getConnectionString());
- System.err.println(pubs.getProvider());
- AdoProperties props = pubs.getProperties();
- System.err.println(props);
- for(int i=0;i<props.getCount();i++)
- System.err.println(props.getItem(i).getName()+": "+props.getItem(i).getString());
- //pubs.setDefaultDatabase("Pubs");
- try
- {
- pubs.open();
- }
- catch(AdoException e)
- {
- splashLabel.setText("Connection Failed");
- Label error = new Label();
- error.setText("Please check your connection to the database and DSN definition");
- error.setBounds(getClientRect());
- add(error);
- return;
- }
- splashLabel.setText("Preparing for user query");
- //Create and open a recordset that contains all of the authors table
- authors = new Recordset();
- authors.setCursorLocation(AdoEnums.CursorLocation.CLIENT);
- authors.setActiveConnection(pubs);
-
-
- //get the query string from the user (default to "select * from authors")
- String query = "select * from authors";
- InputBox queryBox = new InputBox("Enter a Structured Query Language (SQL)"+
- "string with which to query the database",
- "Input Query String", query);
- queryBox.bringToFront();
- //make the splash screen invisible while the input box is up
- splash.setVisible(false);
- if(queryBox.showDialog()==DialogResult.OK)
- {
- query = queryBox.getResponse();
- }
- splash.setVisible(true);
- splash.update();
- splashLabel.setText("Retrieving Data");
- try
- {
- authors.open(query);
- }
- catch(AdoException e)
- {
- String message = "Bad Query; Please Try Again\r"+e.getMessage();
- String title = "Incorrect SQL Syntax or no table found";
- int options = MessageBox.ICONWARNING;
- new MessageBox().show(message, title, options);
- dispose();
- }
- //keep the query that has been sent in the form's title bar
- setText(query);
- //append the number of records returned to the window title
- setText(getText()+": "+String.valueOf(authors.getRecordCount())+" records");
- int fieldCount=0;
- try
- {
- fieldCount = authors.getFields().getCount();
- //TODO: warn the user if no fields were returned
- }
- catch(Throwable t)
- {
- t.printStackTrace();
- System.err.println(t.getMessage());
- }
-
- splashLabel.setText("Binding Data to Edit Boxes");
- //create a DataBinding array
- DataBinding[] bindings = new DataBinding[fieldCount];
- //create an edit for each field and bind it thereto
- //ensure that the form is wide enough for the edit boxes
- int minEditWidth = 80;
- int editPad = 10;
- for(int i=0;i<fieldCount;i++)
- {
- CaptionedEdit edit = new CaptionedEdit();
- DataBinding binding = new DataBinding(edit, "text", authors.getField(i).getName());
- bindings[i]=binding;
- edit.setCaption(authors.getField(i).getName());
- add(edit);
- //keep a list of all the display edits
- dispEdits.addItem(edit);
- //the edit boxes should be at least 80 pixels wide
- minEditWidth += 80 + editPad;
- }
- //lay all of the display edits side by side
- int editCount = dispEdits.getSize();
-
- //set the width to be wide enough to hold the buttons and the edits
- int formWidth = getWidth();
- if(formWidth<minButtonWidth)
- formWidth=minButtonWidth;
- if(formWidth<minEditWidth)
- formWidth = minEditWidth;
- setWidth(formWidth);
-
- //TIP: use this code if your form is wider than your screen
- setAutoScroll(true);
- setAutoScrollMargin(new Point(editMargin, 0));
- //determine the spacing between edit boxes
- int width = (getClientRect().width-(2*editMargin)-(editCount-1)*editPad)/editCount;
- for(int i=0;i<editCount;i++)
- {
- Edit edit = ((Edit)(dispEdits.getItem(i)));
- edit.setSize(width, 40);
- edit.setLocation(editMargin+(width+editPad)*i, 20);
- }
-
- //bind the edits to the recordset
- DataBinder binder = new DataBinder(authors, bindings);
- try
- {
- authors.moveFirst();
- }
- catch(Throwable t)
- {
- System.err.println(t.getMessage());
- t.printStackTrace();
- }
- bringToFront();
- }
-
- /**
- * The main entry point for the application.
- * Calls constructor and runs form
- */
- public static void main(String args[])
- {
- //Do the dataprocessing in the form's constructor, after the splashScreen is created
- Application.run(new BindPubs());
- }
-
-
- int buttonPad = 20;
- int buttonWidth = 80;
- Recordset authors;
- //create labels for the navigation buttons
- String[] buttonLabels = {"First","Previous","Next","Last"};//TODO: Add Update Functionality
- List navButtons = new List();
- List dispEdits = new List();
- int editMargin = 40;
- private static int callLayout=0;
- Container components = new Container();
- /*TIP: creating a variable to hold the event handler allows you to remove it
- or assign it to more than one element
- */
- private EventHandler layButtons = new EventHandler(this.form_resize);
- private ThreadExceptionEventHandler ignoreGlobalException = new ThreadExceptionEventHandler(this.application_threadExcept);
- /**Creates and places UI elements and properties of form
- */
- private void initForm()
- {
- this.setSize(100, 150);
- this.setText ("BindPubs");
- this.addOnResize(layButtons);
- this.addOnClosed(new EventHandler(this.form_closed));
- }
- // NOTE: End of form designer support code
- protected void form_closed(Object source, Event event)
- {
- //close the recordset
- authors.close();
- authors.getActiveConnection().close();
- //release the recordset
- authors = null;
- //From here on in, I don't care about any exception
- Application.addOnThreadException(ignoreGlobalException);
- }
- /**Called by any exception on the Application that happens after the form closes. The empty message body suppresses default behavior of the libraries
- */
- private void application_threadExcept(Object source, ThreadExceptionEvent event)
- {
- //Global Exceptions can take care of themselves
- }
- /**Called by clicking a navigation button. Moves around the recordset
- */
- protected void nav_click(Object source, Event event) throws RuntimeException
- {
- String label = ((Button)source).getText();
- if(label.equals(buttonLabels[0]))//First
- authors.moveFirst();
- else if(label.equals(buttonLabels[1]))//Previous
- {
- //the data binding will cause movePrevious to throw uncatchable exceptions on BOF
- //Application.addOnThreadException(ignoreGlobalException);
- try
- {
- authors.movePrevious();
- }
- catch (AdoException e){}
- //Application.removeOnThreadException(ignoreGlobalException);
- if(authors.getBOF())
- authors.moveFirst();
- }
- else if(label.equals(buttonLabels[2]))//Next
- {
- //the data binding will cause moveNext to throw exceptions on EOF
- try
- {
- authors.moveNext();
- }
- catch(AdoException e){}
- if(authors.getEOF())
- authors.moveLast();
- }
- else if(label.equals(buttonLabels[3]))//Last
- authors.moveLast();
- else
- new MessageBox().show("Visual J++ 6.0 Sample","Unknown button -- command not implemented", MessageBox.ICONWARNING + MessageBox.OK);
- }
- /**Called when the constructor resizes the form to make room for the edits and buttons
- * this code places the buttons along the bottom of the form, then removes itself as an
- * event handler
- */
- public void form_resize(Object source, Event event)
- {
- //position the buttons given their number & width , the padding between them, and the margin
- int buttonCount = buttonLabels.length;
- int buttonMargin = (getSize().x-(buttonCount*buttonWidth + (buttonCount-1)*buttonPad))/2;
- for(int i=0;i<navButtons.getSize();i++)
- {
- Button button = ((Button)(navButtons.getItem(i)));
- button.setLocation(buttonMargin+i*(buttonWidth+buttonPad), getClientRect().height-50);
- button.setSize(buttonWidth, 30);
- //I need only do this once
- removeOnResize(layButtons);
- layButtons = null;
- }
- }
- public void initSplash(SplashScreen splash)
- {
- /*the SplashScreen.gif in the project director is bigger than the default one
- the default is 349*171
- this one is 509*340
- */
- splash.initSplash(splash);
- try
- {
- splash.setSplashPicture("SplashScreen.gif");
- }
- //If the file is not found, leave the default picture in place
- catch(wfc.io.IOException e){}
- }
- }
-