001 package com.sptci.rwt.webui;
002
003 import java.util.ArrayList;
004 import java.util.Collection;
005
006 import nextapp.echo2.app.Component;
007 import nextapp.echo2.app.event.ActionEvent;
008
009 import com.sptci.ReflectionUtility;
010 import com.sptci.echo2.Configuration;
011 import com.sptci.echo2.ErrorPane;
012 import com.sptci.echo2.Listener;
013 import com.sptci.rwt.ConnectionManager;
014 import com.sptci.rwt.ConnectionParameters;
015
016 /**
017 * The dialogue used to initiate a new JDBC {@link java.sql.Connection}.
018 * Also used to save/edit pre-configured connections.
019 *
020 * <p>© Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
021 * @author Rakesh Vidyadharan 2007-10-08
022 * @version $Id: ConnectListener.java 4123 2008-05-25 21:49:01Z rakesh $
023 */
024 public class ConnectListener extends Listener<MainController>
025 {
026 /**
027 * Create a new instance of the listener using the specified controller.
028 *
029 * @param controller The controller to use to interact with the
030 * application.
031 */
032 public ConnectListener( final MainController controller )
033 {
034 super( controller );
035 }
036
037 /**
038 * The action listener implementation. Initiates a connection to the
039 * database using the specified parameters.
040 *
041 * @see #checkView
042 * @see #displayMessages
043 * @see MainController#getParameters
044 * @param event The event that was triggered.
045 */
046 public void actionPerformed( ActionEvent event )
047 {
048 final ConnectionDialogue dialogue = (ConnectionDialogue)
049 controller.getParentView( (Component) event.getSource() );
050
051 Collection<String> messages = checkView( dialogue );
052 if ( messages.isEmpty() )
053 {
054 final ConnectionParameters parameters =
055 controller.getParameters( dialogue );
056
057 final ConnectionManager manager = new ConnectionManager( parameters );
058 controller.setConnectionManager( manager );
059 dialogue.userClose();
060 }
061 else
062 {
063 displayMessages( messages );
064 }
065 }
066
067 /**
068 * Check the {@link ConnectionDialogue} to ensure that all the required
069 * information has been entered to fetch a connection.
070 *
071 * @see #checkString
072 * @return Collection<String> Return a collection of error messages.
073 * Returns an empty collection if no errors were found.
074 */
075 protected Collection<String> checkView( final ConnectionDialogue dialogue )
076 {
077 Collection<String> messages = new ArrayList<String>();
078
079 if ( dialogue.getDatabaseType() == null )
080 {
081 messages.add( Configuration.getString( this, "noDatabaseType" ) );
082 }
083
084 checkString( "Host", dialogue, messages );
085
086 if ( dialogue.getPort() == 0 )
087 {
088 messages.add( Configuration.getString( this, "noPort" ) );
089 }
090
091 checkString( "Database", dialogue, messages );
092 checkString( "UserName", dialogue, messages );
093
094 return messages;
095 }
096
097 /**
098 * Check the field represented by the specified name.
099 *
100 * @param name The name to use to find the appropriate field to test.
101 * @param dialogue The view component that is to be tested.
102 * @param messages The collection of messages that is to be updated as
103 * appropriate.
104 */
105 protected void checkString( final String name,
106 final ConnectionDialogue dialogue, final Collection<String> messages )
107 {
108 final String method = "get" + name;
109
110 try
111 {
112 String value = (String) ReflectionUtility.execute( dialogue, method );
113
114 if ( value.length() == 0 )
115 {
116 messages.add( Configuration.getString( this, "no" + name ) );
117 }
118 }
119 catch ( Throwable t )
120 {
121 String message = Configuration.getString( this, "methodError" );
122 message = message.replaceAll( "\\$method\\$", method );
123 message = message.replaceAll( "\\$class\\$", dialogue.getClass().getName() );
124 controller.processFatalException( message, t );
125 }
126 }
127
128 /**
129 * Display an {@link com.sptci.echo2.ErrorPane} with the contents of the
130 * specified collection of messages.
131 *
132 * @param messages The collection of messages to display.
133 */
134 protected void displayMessages( final Collection<String> messages )
135 {
136 StringBuilder builder = new StringBuilder();
137 for ( String message : messages )
138 {
139 builder.append( message ).append( "<br/>" );
140 }
141
142 ErrorPane pane = new ErrorPane(
143 Configuration.getString( this, "title" ),
144 builder.toString() );
145 controller.addPane( pane );
146 }
147 }