Revision: 1132
Author:   peterdb
Date:     2006-05-10 12:59:17 -0700 (Wed, 10 May 2006)
ViewCVS:  http://svn.sourceforge.net/spring-rich-c/?rev=1132&view=rev

Log Message:
-----------
Initial checkin for jdbc settings support

Added Paths:
-----------
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/AbstractFormUIProvider.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/DefaultFormUIProvider.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettings.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettingsFactory.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/StaticUserNameProvider.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/UserNameProvider.java
    
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/schema.sql
    
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/
    
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/DefaultFormUIProviderTests.java
    
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/SimplePanel.java
    
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/TestableBindingFactory.java
    
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/settings/jdbc/
    
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/settings/jdbc/JdbcSettingsTests.java
Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/AbstractFormUIProvider.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/AbstractFormUIProvider.java
                               (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/AbstractFormUIProvider.java
       2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ *
+ */
+
+package org.springframework.richclient.form;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.springframework.richclient.factory.AbstractControlFactory;
+import org.springframework.richclient.form.binding.BindingFactory;
+import org.springframework.util.Assert;
+
+/**
+ * Abstract <code>FormUIProvider</code> implementation. Extenders only need to 
implement
+ * the <code>createControl()</code> and <code>getComponent(String id)</code> 
methods.
+ * 
+ * @author Peter De Bruycker
+ */
+public abstract class AbstractFormUIProvider extends AbstractControlFactory 
implements FormUIProvider {
+    private boolean bound = false;
+    private String[] properties;
+    private Map contextMap = new HashMap();
+
+    public void bind(BindingFactory factory) {
+        Assert.state(properties != null && properties.length > 0, "Properties 
must be set");
+
+        bound = true;
+
+        for (int i = 0; i < properties.length; i++) {
+            factory.bindControl(getComponent(properties[i]), properties[i], 
getContext(properties[i]));
+        }
+    }
+
+    public Map getContext(String propertyPath) {
+        return contextMap.containsKey(propertyPath) ? 
(Map)contextMap.get(propertyPath) : Collections.EMPTY_MAP;
+    }
+
+    public void setContext(String propertyPath, Map context) {
+        contextMap.put(propertyPath, context);
+    }
+
+    public void setProperties(String[] properties) {
+        Assert.state(!bound, "You cannot set the form properties after the 
binding");
+
+        this.properties = properties;
+    }
+
+    public String[] getProperties() {
+        return properties;
+    }
+
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/DefaultFormUIProvider.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/DefaultFormUIProvider.java
                                (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/form/DefaultFormUIProvider.java
        2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ *
+ */
+
+package org.springframework.richclient.form;
+
+import java.awt.Component;
+import java.awt.Container;
+
+import javax.swing.JComponent;
+
+/**
+ * Default <code>FormUIProvider</code> implementation.
+ * 
+ * @author Peter De Bruycker
+ */
+public class DefaultFormUIProvider extends AbstractFormUIProvider {
+    private JComponent formComponent;
+
+    public DefaultFormUIProvider() {
+
+    }
+
+    public DefaultFormUIProvider(JComponent component) {
+        formComponent = component;
+    }
+
+    public JComponent getComponent(String propertyPath) {
+        return getComponent(formComponent, propertyPath);
+    }
+
+    private static JComponent getComponent(Container parent, String id) {
+        Component[] children = parent.getComponents();
+        for (int i = 0; i < children.length; i++) {
+            Component child = children[i];
+            if (child instanceof JComponent && 
id.equals(children[i].getName())) {
+                return (JComponent)child;
+            }
+
+            if (child instanceof Container) {
+                Container container = (Container)child;
+                JComponent result = getComponent(container, id);
+                if (result != null) {
+                    return result;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    public void setControl(JComponent control) {
+        formComponent = control;
+    }
+
+    protected JComponent createControl() {
+        return formComponent;
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettings.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettings.java
                                (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettings.java
        2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,178 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.settings.jdbc;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.sql.DataSource;
+
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.richclient.settings.AbstractSettings;
+import org.springframework.richclient.settings.Settings;
+
+/**
+ * 
+ * @author Peter De Bruycker
+ */
+public class JdbcSettings extends AbstractSettings {
+    private DataSource dataSource;
+
+    private Integer id;
+    private String user;
+
+    private Map values = new HashMap();
+    private Set remove = new HashSet();
+    private Set add = new HashSet();
+    private Set update = new HashSet();
+
+    private String[] childKeys;
+
+    public JdbcSettings( DataSource ds, String user, Integer id, String key ) {
+        this( null, ds, user, id, key );
+    }
+
+    public JdbcSettings( JdbcSettings parent, DataSource ds, String user, 
Integer id, String key ) {
+        super( parent, key );
+        this.id = id;
+
+        // TODO assert dataSource not null
+        dataSource = ds;
+
+        // TODO assert user not empty
+        this.user = user;
+    }
+
+    protected boolean internalContains( String key ) {
+        return values.containsKey( key );
+    }
+
+    protected String[] internalGetChildSettings() {
+        if( childKeys == null ) {
+            loadChildKeys();
+        }
+        return childKeys;
+    }
+
+    protected Settings internalCreateChild( String key ) {
+        return new JdbcSettings( this, dataSource, user, null, key );
+    }
+
+    protected void internalSet( String key, String value ) {
+        boolean isNew = !values.containsKey( key ) || add.contains( key );
+
+        values.put( key, value );
+
+        if( isNew ) {
+            add.add( key );
+        } else {
+            update.add( key );
+        }
+        remove.remove( key );
+    }
+
+    protected String internalGet( String key ) {
+        return (String) values.get( key );
+    }
+
+    protected void internalRemove( String key ) {
+        values.remove( key );
+
+        if( !add.contains( key ) ) {
+            remove.add( key );
+        }
+
+        update.remove( key );
+        add.remove( key );
+    }
+
+    public String[] getKeys() {
+        return (String[]) values.keySet().toArray( new String[0] );
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void save() throws IOException {
+        if( getParent() != null ) {
+            getParent().save();
+        }
+
+        JdbcTemplate template = new JdbcTemplate( dataSource );
+
+        // if this is a new node, insert it
+        if( id == null ) {
+            JdbcSettings parent = (JdbcSettings) getParent();
+            template.update( "INSERT INTO SETTINGS (KEY, PARENT, USER) VALUES 
(?, ?, ?)", new Object[] { getName(),
+                    parent == null ? null : parent.getId(), user } );
+            id = Integer.valueOf( template.queryForInt( "SELECT MAX(ID) FROM 
SETTINGS" ) );
+        } else {
+            for( Iterator iter = remove.iterator(); iter.hasNext(); ) {
+                String key = (String) iter.next();
+                template.update( "REMOVE FROM SETTINGS_VALUES WHERE 
SETTINGS_ID=? AND KEY=?", new Object[] { id, key } );
+            }
+            for( Iterator iter = update.iterator(); iter.hasNext(); ) {
+                String key = (String) iter.next();
+                template.update( "UPDATE SETTINGS_VALUES SET VALUE=? WHERE 
SETTINGS_ID=? AND KEY=?", new Object[] {
+                        values.get( key ), id, key } );
+            }
+        }
+
+        for( Iterator iter = add.iterator(); iter.hasNext(); ) {
+            String key = (String) iter.next();
+            template.update( "INSERT INTO SETTINGS_VALUES (SETTINGS_ID, KEY, 
VALUE) VALUES (?, ?, ?)", new Object[] {
+                    id, key, values.get( key ) } );
+        }
+
+        remove.clear();
+        update.clear();
+        add.clear();
+    }
+
+    public void load() throws IOException {
+        if( id == null ) {
+            return;
+        }
+
+        JdbcTemplate template = new JdbcTemplate( dataSource );
+        List entries = template.queryForList( "SELECT KEY, VALUE FROM 
SETTINGS_VALUES WHERE SETTINGS_ID=?",
+                new Object[] { id } );
+        for( Iterator iter = entries.iterator(); iter.hasNext(); ) {
+            Map entry = (Map) iter.next();
+            values.put( (String) entry.get( "KEY" ), (String) entry.get( 
"VALUE" ) );
+        }
+    }
+
+    private void loadChildKeys() {
+        JdbcTemplate template = new JdbcTemplate( dataSource );
+        List keys = template.queryForList( "SELECT KEY FROM SETTINGS WHERE 
PARENT=" + id, String.class );
+
+        childKeys = (String[]) keys.toArray( new String[keys.size()] );
+    }
+
+    protected void internalRemoveChild( String key ) {
+    }
+
+    public String getUser() {
+        return user;
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettingsFactory.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettingsFactory.java
                         (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/JdbcSettingsFactory.java
 2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.settings.jdbc;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.richclient.settings.Settings;
+import org.springframework.richclient.settings.SettingsException;
+import org.springframework.richclient.settings.SettingsFactory;
+import org.springframework.util.Assert;
+
+/**
+ * 
+ * @author Peter De Bruycker
+ */
+public class JdbcSettingsFactory implements SettingsFactory, InitializingBean {
+    private DataSource dataSource;
+    private UserNameProvider userNameProvider;
+
+    public JdbcSettingsFactory() {
+    }
+
+    public void setDataSource( DataSource dataSource ) {
+        this.dataSource = dataSource;
+    }
+
+    public DataSource getDataSource() {
+        return dataSource;
+    }
+
+    /**
+     * TODO: somehow make the key unique by adding a user name or login or 
something
+     */
+    public Settings createSettings( String key ) throws SettingsException {
+        try {
+            JdbcTemplate template = new JdbcTemplate( dataSource );
+            Map result = template.queryForMap( "SELECT * FROM SETTINGS WHERE 
KEY=? AND USER=?", new Object[] { key,
+                    userNameProvider.getUser() } );
+
+            JdbcSettings settings = new JdbcSettings( dataSource, 
userNameProvider.getUser(), (Integer) result
+                    .get( "ID" ), key );
+            settings.load();
+            return settings;
+        } catch( IncorrectResultSizeDataAccessException e ) {
+            return new JdbcSettings( dataSource, userNameProvider.getUser(), 
null, key );
+        } catch( IOException e ) {
+            throw new SettingsException( "Unable to create settings with name 
" + key, e );
+        }
+    }
+
+    public void setUserNameProvider( UserNameProvider userNameProvider ) {
+        this.userNameProvider = userNameProvider;
+    }
+
+    public UserNameProvider getUserNameProvider() {
+        return userNameProvider;
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        Assert.notNull( userNameProvider, "UserNameProvider must be set" );
+        Assert.notNull( dataSource, "DataSource must be set" );
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/StaticUserNameProvider.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/StaticUserNameProvider.java
                              (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/StaticUserNameProvider.java
      2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.settings.jdbc;
+
+/**
+ * 
+ * @author Peter De Bruycker
+ */
+public class StaticUserNameProvider implements UserNameProvider {
+    private String user;
+
+    public StaticUserNameProvider( String user ) {
+        this.user = user;
+    }
+
+    public String getUser() {
+        return user;
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/UserNameProvider.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/UserNameProvider.java
                            (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/UserNameProvider.java
    2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.settings.jdbc;
+
+/**
+ * 
+ * @author Peter De Bruycker
+ */
+public interface UserNameProvider {
+    String getUser();
+}

Added: 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/schema.sql
===================================================================
--- 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/schema.sql
                               (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/main/java/org/springframework/richclient/settings/jdbc/schema.sql
       2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,21 @@
+/* for hsqldb */
+
+CREATE TABLE
+    SETTINGS
+    (
+        ID INTEGER IDENTITY,
+        KEY VARCHAR(250) NOT NULL,
+        PARENT INTEGER,
+        USER VARCHAR(250) NOT NULL,
+        CONSTRAINT SYS_CT_52 UNIQUE(KEY,USER)
+    )
+    
+CREATE TABLE
+    SETTINGS_VALUES
+    (
+        SETTINGS_ID INTEGER NOT NULL,
+        KEY VARCHAR(250) NOT NULL,
+        VALUE VARCHAR(250),
+        PRIMARY KEY(SETTINGS_ID,KEY),
+        CONSTRAINT SYS_FK_48 FOREIGN KEY(SETTINGS_ID) REFERENCES SETTINGS(ID)
+    )

Added: 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/DefaultFormUIProviderTests.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/DefaultFormUIProviderTests.java
                           (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/DefaultFormUIProviderTests.java
   2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,74 @@
+package org.springframework.richclient.form;
+
+import java.text.Collator;
+
+import java.util.Collections;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.springframework.richclient.form.binding.swing.ComboBoxBinder;
+
+public class DefaultFormUIProviderTests extends TestCase {
+    private SimplePanel panel;
+    private Map context;
+
+    public void testGetComponent() {
+        DefaultFormUIProvider formUIProvider = new 
DefaultFormUIProvider(panel);
+
+        assertSame(panel, formUIProvider.getControl());
+        assertEquals(panel.getStringField(), 
formUIProvider.getComponent("stringProperty"));
+        assertEquals(panel.getComboBox(), 
formUIProvider.getComponent("comboProperty"));
+        assertEquals(panel.getCheckBox(), 
formUIProvider.getComponent("booleanProperty"));
+        
+        // find nested components
+        assertEquals(panel.getNestedField(), 
formUIProvider.getComponent("nestedField"));
+    }
+
+    public void testBind() {
+        DefaultFormUIProvider formUIProvider = new 
DefaultFormUIProvider(panel);
+
+        TestableBindingFactory bindingFactory = new TestableBindingFactory();
+
+        String[] properties = { "stringProperty", "comboProperty", 
"booleanProperty" };
+        formUIProvider.setProperties(properties);
+
+        formUIProvider.setContext("comboProperty", context);
+
+        formUIProvider.bind(bindingFactory);
+        assertEquals(3, bindingFactory.getBindControlCount());
+
+        // string property
+        assertEquals("stringProperty", 
bindingFactory.getPropertyPaths().get(0));
+        assertEquals(panel.getStringField(), 
bindingFactory.getControls().get(0));
+        assertEquals(Collections.EMPTY_MAP, 
bindingFactory.getContexts().get(0));
+
+        // combo property
+        assertEquals("comboProperty", 
bindingFactory.getPropertyPaths().get(1));
+        assertEquals(panel.getComboBox(), bindingFactory.getControls().get(1));
+        assertEquals(context, bindingFactory.getContexts().get(1));
+
+        // boolean property
+        assertEquals("booleanProperty", 
bindingFactory.getPropertyPaths().get(2));
+        assertEquals(panel.getCheckBox(), bindingFactory.getControls().get(2));
+        assertEquals(Collections.EMPTY_MAP, 
bindingFactory.getContexts().get(2));
+    }
+
+    protected void setUp() throws Exception {
+        panel = new SimplePanel();
+
+        context = Collections.singletonMap(ComboBoxBinder.COMPARATOR_KEY, 
Collator.getInstance());
+    }
+
+    public void testSetAndGetContext() {
+        DefaultFormUIProvider formUIProvider = new 
DefaultFormUIProvider(panel);
+
+        formUIProvider.setContext("comboProperty", context);
+
+        assertEquals(context, formUIProvider.getContext("comboProperty"));
+
+        assertNotNull("if no context provided, must return empty map", 
formUIProvider.getContext("stringProperty"));
+        assertTrue("if no context provided, must return empty map", 
+                   formUIProvider.getContext("stringProperty").isEmpty());
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/SimplePanel.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/SimplePanel.java
                          (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/SimplePanel.java
  2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,81 @@
+package org.springframework.richclient.form;
+
+import javax.swing.JCheckBox;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import org.springframework.richclient.layout.TableLayoutBuilder;
+
+/**
+ * Simple Panel that mimics a panel created by a visual designer.
+ * @author Peter De Bruycker
+ */
+public class SimplePanel extends JPanel {
+    private JTextField stringField;
+    private JComboBox comboBox;
+    private JCheckBox checkBox;
+    private JTextField nestedField;
+
+    public SimplePanel() {
+        TableLayoutBuilder builder = new TableLayoutBuilder(this);
+
+        stringField = new JTextField(10);
+        stringField.setName("stringProperty");
+
+        comboBox = new JComboBox(new String[] { "item 0", "item 1", "item 2" 
});
+        comboBox.setName("comboProperty");
+
+        checkBox = new JCheckBox("checkbox");
+        checkBox.setName("booleanProperty");
+
+        builder.cell(new JLabel("string"));
+        builder.gapCol();
+        builder.cell(stringField);
+        builder.relatedGapRow();
+        builder.cell(new JLabel("combo"));
+        builder.gapCol();
+        builder.cell(comboBox);
+        builder.relatedGapRow();
+        builder.cell(checkBox);
+        builder.relatedGapRow();
+        
+        JPanel nestedPanel =new JPanel();
+        nestedField = new JTextField("test");
+        nestedField.setName("nestedField");
+        nestedPanel.add(nestedField);
+        
+        builder.cell(nestedPanel);
+
+        builder.getPanel();
+    }
+
+    public static void main(String[] args) {
+        JFrame frame = new JFrame("test");
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        frame.add(new SimplePanel());
+
+        frame.pack();
+        frame.setLocationRelativeTo(null);
+        frame.setVisible(true);
+    }
+
+    public JTextField getStringField() {
+        return stringField;
+    }
+
+    public JComboBox getComboBox() {
+        return comboBox;
+    }
+
+    public JCheckBox getCheckBox() {
+        return checkBox;
+    }
+    
+    public JTextField getNestedField() {
+        return nestedField;
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/TestableBindingFactory.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/TestableBindingFactory.java
                               (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/form/TestableBindingFactory.java
       2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,69 @@
+package org.springframework.richclient.form;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.JComponent;
+
+import org.springframework.binding.form.FormModel;
+import org.springframework.richclient.form.binding.Binding;
+import org.springframework.richclient.form.binding.BindingFactory;
+
+public class TestableBindingFactory implements BindingFactory {
+
+    private int bindControlCount;
+    private List controls = new ArrayList();
+    private List contexts = new ArrayList();
+    private List propertyPaths = new ArrayList();
+
+    public FormModel getFormModel() {
+        return null;
+    }
+
+    public Binding createBinding( String formPropertyPath ) {
+        return null;
+    }
+
+    public Binding createBinding( String formPropertyPath, Map context ) {
+        return null;
+    }
+
+    public Binding createBinding( Class controlType, String formPropertyPath ) 
{
+        return null;
+    }
+
+    public Binding createBinding( Class controlType, String formPropertyPath, 
Map context ) {
+        return null;
+    }
+
+    public Binding bindControl( JComponent control, String formPropertyPath ) {
+        return null;
+    }
+
+    public Binding bindControl( JComponent control, String formPropertyPath, 
Map context ) {
+        bindControlCount++;
+
+        controls.add( control );
+        propertyPaths.add( formPropertyPath );
+        contexts.add( context );
+
+        return null;
+    }
+
+    public int getBindControlCount() {
+        return bindControlCount;
+    }
+
+    public List getPropertyPaths() {
+        return propertyPaths;
+    }
+
+    public List getControls() {
+        return controls;
+    }
+
+    public List getContexts() {
+        return contexts;
+    }
+}

Added: 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/settings/jdbc/JdbcSettingsTests.java
===================================================================
--- 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/settings/jdbc/JdbcSettingsTests.java
                           (rev 0)
+++ 
trunk/spring-richclient/sandbox/src/test/java/org/springframework/richclient/settings/jdbc/JdbcSettingsTests.java
   2006-05-10 19:59:17 UTC (rev 1132)
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.springframework.richclient.settings.jdbc;
+
+import java.util.List;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import org.springframework.richclient.settings.Settings;
+import org.springframework.richclient.settings.SettingsAbstractTest;
+
+/**
+ * @author Peter De Bruycker
+ */
+public class JdbcSettingsTests extends SettingsAbstractTest {
+    private DataSource dataSource;
+    private JdbcTemplate jdbcTemplate;
+
+    public JdbcSettingsTests() {
+    }
+
+    protected Settings createSettings() {
+        return new JdbcSettings(dataSource, "user", Integer.valueOf(5), 
"test");
+    }
+
+    protected void doSetUp() throws Exception {
+        dataSource = createDataSource();
+        jdbcTemplate = new JdbcTemplate(dataSource);
+
+        // setup the schema
+        jdbcTemplate.execute("DROP TABLE SETTINGS_VALUES IF EXISTS");
+        jdbcTemplate.execute("DROP TABLE SETTINGS IF EXISTS");
+
+        jdbcTemplate.execute("CREATE TABLE SETTINGS (ID INTEGER IDENTITY, KEY 
VARCHAR(250) NOT NULL, PARENT INTEGER, USER VARCHAR(250) NOT NULL, CONSTRAINT 
SYS_CT_52 UNIQUE(KEY,USER))");
+        jdbcTemplate.execute("CREATE TABLE SETTINGS_VALUES (SETTINGS_ID 
INTEGER NOT NULL, KEY VARCHAR(250) NOT NULL, VALUE VARCHAR(250), PRIMARY 
KEY(SETTINGS_ID,KEY), CONSTRAINT SYS_FK_48 FOREIGN KEY(SETTINGS_ID) REFERENCES 
SETTINGS(ID))");
+    }
+
+    /**
+     * Creates a <code>DataSource</code> using hsqldb in memory-only mode
+     * @return the <code>DataSource</code>
+     */
+    private static DataSource createDataSource() {
+        DriverManagerDataSource ds = new DriverManagerDataSource();
+        ds.setDriverClassName("org.hsqldb.jdbcDriver");
+        ds.setUrl("jdbc:hsqldb:mem:test-database");
+        ds.setUsername("sa");
+
+        return ds;
+    }
+
+    public void testLoadExistingSettings() throws Exception {
+        jdbcTemplate.execute("INSERT INTO SETTINGS (ID, KEY, USER) VALUES (55, 
'test-key', 'test-user')");
+        jdbcTemplate.execute("INSERT INTO SETTINGS_VALUES (SETTINGS_ID, KEY, 
VALUE) VALUES (55, 'key0', 'true')");
+        jdbcTemplate.execute("INSERT INTO SETTINGS_VALUES (SETTINGS_ID, KEY, 
VALUE) VALUES (55, 'key1', '25')");
+
+        JdbcSettings settings = new JdbcSettings(dataSource, "test-user", 
Integer.valueOf(55), "test-key");
+        settings.load();
+    }
+
+    public void testLoadHierarchy() throws Exception {
+
+    }
+
+    public void testSaveHierarchy() throws Exception {
+        JdbcSettings settings = new JdbcSettings(dataSource, "test-user", 
null, "test-key");
+        settings.setBoolean("boolean-value", true);
+
+        JdbcSettings childSettings = 
(JdbcSettings)settings.getSettings("child");
+        childSettings.setString("string", "test");
+        childSettings.save();
+        
+        assertEquals(Integer.valueOf(0), settings.getId());
+        assertEquals(Integer.valueOf(1), childSettings.getId());
+    }
+
+    public void testSaveNewSettings() throws Exception {
+        JdbcSettings settings = new JdbcSettings(dataSource, "test-user", 
null, "test-key");
+
+        assertEquals("name not set", "test-key", settings.getName());
+        assertEquals("user not set", "test-user", settings.getUser());
+        assertNull("id must be null until first save", settings.getId());
+
+        settings.setBoolean("boolean-value", true);
+        settings.setString("string-value", "value");
+
+        settings.save();
+
+        assertEquals(Integer.valueOf(0), settings.getId());
+
+        assertEquals(1, jdbcTemplate.queryForInt("SELECT count(*) FROM 
SETTINGS"));
+        Map map = jdbcTemplate.queryForMap("SELECT * FROM SETTINGS WHERE ID = 
0");
+        assertEquals(Integer.valueOf(0), map.get("ID"));
+        assertEquals("test-key", map.get("KEY"));
+        assertEquals(null, map.get("PARENT"));
+        assertEquals("test-user", map.get("USER"));
+
+        assertEquals(2, jdbcTemplate.queryForInt("SELECT count(*) FROM 
SETTINGS_VALUES"));
+        List values = jdbcTemplate.queryForList("SELECT * FROM 
SETTINGS_VALUES");
+        assertEquals(2, values.size());
+        Map first = (Map) values.get(0);
+        Map second = (Map) values.get(1);
+
+        assertEquals(Integer.valueOf(0), first.get("SETTINGS_ID"));
+        assertEquals(Integer.valueOf(0), second.get("SETTINGS_ID"));
+
+        assertEquals("boolean-value", first.get("KEY"));
+        assertEquals("true", first.get("VALUE"));
+
+        assertEquals("string-value", second.get("KEY"));
+        assertEquals("value", second.get("VALUE"));
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to