alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
** Copyright (c) 1998 by Timothy Gerard Endres
** <mailto:time@ice.com>  
** 
** This program is free software.
** 
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included	with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE. 
** 
*/

package com.ice.sqlclient;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import com.ice.util.AWTUtilities;


public class
MainFrame extends JFrame
		implements ActionListener
	{
	private JMenu		dbMenu;
	private JMenuBar	menuBar;
	private MainPanel	mainPanel;


	public void
	hideDatabaseMenu()
		{
		this.menuBar.remove
			( this.menuBar.getComponentIndex( this.dbMenu ) );

		this.invalidate();
		this.validate();
		this.repaint();
		}

	public void
	showDatabaseMenu( String[] dbList, String currDb )
		{
System.err.println( "this.dbMenu NULL? " + (this.dbMenu==null) );
	//	this.dbMenu.removeAll();
		for ( ; this.dbMenu.getItemCount() > 0 ; )
			this.dbMenu.remove(0);

		ButtonGroup grp = new ButtonGroup();

		for ( int i = 0 ; i < dbList.length ; ++i )
			{
			JRadioButtonMenuItem item =
				new JRadioButtonMenuItem( dbList[i] );

			if ( currDb != null )
				{
				if ( currDb.equals( dbList[i] ) )
					item.setSelected( true );
				else
					item.setSelected( false );
				}

			item.addActionListener( this );
			item.setActionCommand( "NEWDB:" + dbList[i] );

			grp.add( item );

			this.dbMenu.add( item );
			}

		this.menuBar.add( this.dbMenu );
		this.invalidate();
		this.validate();
		this.repaint();
		}


	public
	MainFrame( String title )
		{
		super( title );

		this.mainPanel = new MainPanel( this );
		
		this.getContentPane().add( this.mainPanel );

		this.establishMenuBar();
		}

	public void
	actionPerformed( ActionEvent event )
		{
		String command = event.getActionCommand();

		if ( command.equals( "QUIT" ) )
			{
			System.exit(0);
			}
		else if ( command.startsWith( "NEWDB:" ) )
			{
			String dbName = command.substring(6);
			this.changeDatabase( dbName );
			}
		else if ( command.startsWith( "PLAF:" ) )
			{
			String plafClassName = command.substring(5);
			this.changeLookAndFeel( plafClassName );
			}
		else
			{
			System.err.println
				( "UNKNOWN Command '" + command + "'" );
			}
		}

	private boolean
	changeDatabase( String dbName )
		{
		boolean result = true;

		SQLClientHandler handler =
			SQLClientHandler.getCurrentHandler();

		if ( handler == null )
			{
			this.showMessage
				( "INTERNAL ERROR: current handler is null." );
			return false;
			}

		if ( ! handler.isOpen() )
			{
			this.showMessage
				( "INTERNAL ERROR: connection is not open." );
			return false;
			}

		try {
			handler.setCurrentDatabase( dbName );
			this.mainPanel.dbHasChanged( dbName );
			this.showMessage( "Database changed to '" + dbName + "'" );
			}
		catch ( SQLException ex )
			{
			result = false;
			this.showMessage
				( "SQLException: changing database to '"
					+ dbName + "':\n" + ex.getMessage() );
			}

		return result;
		}

	private void
	changeLookAndFeel( String plafClassName )
		{
		String plafName;
		int index = plafClassName.lastIndexOf( plafClassName );
		if ( index < 0 )
			plafName = plafClassName;
		else
			plafName = plafClassName.substring( index + 1 );

		if ( false )
		System.err.println
			( "New Look And Feel: " + plafClassName );

		try {
			UIManager.setLookAndFeel( plafClassName );
			SwingUtilities.updateComponentTreeUI
				( this.getRootPane() );
			}
		catch ( IllegalAccessException ex )
			{
			this.lookAndFeelExMessage
				( plafName, plafClassName, ex.getMessage(),
					"could not be accessed" );
			}
		catch ( InstantiationException ex )
			{
			this.lookAndFeelExMessage
				( plafName, plafClassName, ex.getMessage(),
					"could not be instantiated" );
			}
		catch ( ClassNotFoundException ex )
			{
			this.lookAndFeelExMessage
				( plafName, plafClassName, ex.getMessage(),
					"could not be located" );
			}
		catch ( UnsupportedLookAndFeelException ex )
			{
			this.lookAndFeelExMessage
				( plafName, plafClassName, ex.getMessage(),
					"is not supported on this platform" );
			}
		}

	private void
	showMessage( String msg )
		{
		System.err.println( msg );
		}

	private void
	lookAndFeelExMessage(
			String name, String className,
			String exMsg, String msg )
		{
		System.err.println
			( "Look and feel '" + name + "' " + msg + "." );
		
		if ( exMsg.length() > 0 )
			System.err.println( "\t" + exMsg );
		}

	private void
	createDBMenu()
		{
		this.dbMenu = new JMenu( "DB" );
		}

	private void
	establishMenuBar()
		{
		this.menuBar = new JMenuBar();

		JMenu menu = new JMenu( "File" );
		this.menuBar.add( menu );

		JMenuItem item = new JMenuItem( "Quit" );
		item.addActionListener( this );
		item.setActionCommand( "QUIT" );
		item.setAccelerator
			( KeyStroke.getKeyStroke
				( KeyEvent.VK_Q, Event.CTRL_MASK ) );
		menu.add( item );

		this.addLookAndFeelMenu( this.menuBar );

		this.createDBMenu();

		this.setJMenuBar( this.menuBar );
		}

	private void
	addLookAndFeelMenu( JMenuBar mbar )
		{
		JMenu menu = new JMenu( "Look" );
		mbar.add( menu );

		UIManager.LookAndFeelInfo[] info =
			UIManager.getInstalledLookAndFeels();

		for ( int idx = 0 ; idx < info.length ; ++idx )
			{
			JMenuItem item = new JMenuItem( info[idx].getName() );
			item.addActionListener( this );
			item.setActionCommand( "PLAF:" + info[idx].getClassName() );
			menu.add( item );
			}
		}
	
	}

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.