[Japi-cvs] SF.net SVN: japi: [534] libs
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-07-14 09:49:50
|
Revision: 534 http://svn.sourceforge.net/japi/?rev=534&view=rev Author: christianhujer Date: 2007-07-14 02:49:47 -0700 (Sat, 14 Jul 2007) Log Message: ----------- Added swing-list. Added Paths: ----------- libs/swing-list/ libs/swing-list/trunk/ libs/swing-list/trunk/libs-swing-list.iml libs/swing-list/trunk/src/ libs/swing-list/trunk/src/net/ libs/swing-list/trunk/src/net/sf/ libs/swing-list/trunk/src/net/sf/japi/ libs/swing-list/trunk/src/net/sf/japi/swing/ libs/swing-list/trunk/src/net/sf/japi/swing/list/ libs/swing-list/trunk/src/net/sf/japi/swing/list/AbstractMutableListModel.java libs/swing-list/trunk/src/net/sf/japi/swing/list/ArrayListModel.java libs/swing-list/trunk/src/net/sf/japi/swing/list/MutableListModel.java libs/swing-list/trunk/src/net/sf/japi/swing/list/package-info.java libs/swing-list/trunk/src/test/ libs/swing-list/trunk/src/test/net/ libs/swing-list/trunk/src/test/net/sf/ libs/swing-list/trunk/src/test/net/sf/japi/ libs/swing-list/trunk/src/test/net/sf/japi/swing/ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ArrayListModelTest.java libs/swing-list/trunk/src/test/net/sf/japi/swing/list/MockListDataListener.java libs/swing-list/trunk/src/test/net/sf/japi/swing/list/package-info.java Property changes on: libs/swing-list/trunk ___________________________________________________________________ Name: svn:ignore + classes dest developer.properties dist docs Name: svn:externals + common https://japi.svn.sourceforge.net/svnroot/japi/common/trunk Added: libs/swing-list/trunk/libs-swing-list.iml =================================================================== --- libs/swing-list/trunk/libs-swing-list.iml (rev 0) +++ libs/swing-list/trunk/libs-swing-list.iml 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,191 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module relativePaths="true" type="JAVA_MODULE" version="4"> + <component name="ModuleRootManager" /> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" packagePrefix="test" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="library" name="annotations" level="project" /> + <orderEntry type="library" name="junit" level="project" /> + <orderEntry type="module" module-name="libs-swing-action" /> + <orderEntryProperties /> + </component> + <component name="copyright"> + <Base> + <setting name="state" value="0" /> + </Base> + <LanguageOptions name="$TEMPLATE$"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="JAPI libs-swing-list is a library for lists in swing. Copyright (C) &#36;today.year Christian Hujer. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA" /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="4" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="CSS"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="HTML"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="JAVA"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="JSP"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="JavaScript"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="Properties"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + <LanguageOptions name="XML"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value=" " /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="2" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> + </component> +</module> + Property changes on: libs/swing-list/trunk/libs-swing-list.iml ___________________________________________________________________ Name: svn:mime-type + text/xml Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/net/sf/japi/swing/list/AbstractMutableListModel.java =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/AbstractMutableListModel.java (rev 0) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/AbstractMutableListModel.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,28 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.swing.list; + +import javax.swing.AbstractListModel; + +/** The abstract definition for the data model that provides a List with its contents in a mutable fashion. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public abstract class AbstractMutableListModel<E> extends AbstractListModel implements MutableListModel<E> { +} // class AbstractMutableListModel Property changes on: libs/swing-list/trunk/src/net/sf/japi/swing/list/AbstractMutableListModel.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/net/sf/japi/swing/list/ArrayListModel.java =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/ArrayListModel.java (rev 0) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/ArrayListModel.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,174 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.swing.list; + +import java.util.List; +import java.util.ArrayList; +import org.jetbrains.annotations.Nullable; + +/** + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class ArrayListModel<E> extends AbstractMutableListModel<E> { + + /** The ArrayList that backs this ArrayListModel. */ + private final List<E> list = new ArrayList<E>(); + + /** Creates a new ArrayListModel. + */ + public ArrayListModel() { + } + + /** {@inheritDoc} */ + public int getSize() { + return list.size(); + } + + /** {@inheritDoc} */ + public E getElementAt(final int index) { + return list.get(index); + } + + /** {@inheritDoc} */ + public boolean add(final E e) { + if (list.contains(e)) { + return false; + } + final int index; + final boolean retVal; + synchronized (list) { + index = list.size(); + retVal = list.add(e); + } + fireIntervalAdded(this, index, index); + return retVal; + } + + /** {@inheritDoc} */ + public boolean remove(final E e) { + if (!list.contains(e)) { + return false; + } + final int index; + final boolean retVal; + synchronized (list) { + index = list.indexOf(e); + retVal = list.remove(e); + } + fireIntervalRemoved(this, index, index); + return retVal; + } + + /** {@inheritDoc} */ + public boolean moveToTop(final E e) { + synchronized (list) { + if (!list.contains(e)) { + throw new IllegalArgumentException("Element " + e + " not part of " + this); + } + final int oldIndex = list.indexOf(e); + final int newIndex = 0; + return move(e, oldIndex, newIndex); + } + + } + + /** {@inheritDoc} */ + public boolean moveToTop(final int index) { + synchronized (list) { + return move(null, index, 0); + } + } + + /** {@inheritDoc} */ + public boolean moveUp(final E e) { + synchronized (list) { + if (!list.contains(e)) { + throw new IllegalArgumentException("Element " + e + " not part of " + this); + } + final int oldIndex = list.indexOf(e); + final int newIndex = oldIndex - 1; + return move(e, oldIndex, newIndex); + } + } + + /** {@inheritDoc} */ + public boolean moveUp(final int index) { + synchronized (list) { + return move(null, index, index - 1); + } + } + + /** {@inheritDoc} */ + public boolean moveDown(final E e) { + synchronized (list) { + if (!list.contains(e)) { + throw new IllegalArgumentException("Element " + e + " not part of " + this); + } + final int oldIndex = list.indexOf(e); + final int newIndex = oldIndex + 1; + return move(e, oldIndex, newIndex); + } + } + + /** {@inheritDoc} */ + public boolean moveDown(final int index) { + synchronized (list) { + return move(null, index, index + 1); + } + } + + /** {@inheritDoc} */ + public boolean moveToBottom(final E e) { + synchronized (list) { + if (!list.contains(e)) { + throw new IllegalArgumentException("Element " + e + " not part of " + this); + } + final int oldIndex = list.indexOf(e); + final int newIndex = list.size() - 1; + return move(e, oldIndex, newIndex); + } + } + + /** {@inheritDoc} */ + public boolean moveToBottom(final int index) { + synchronized (list) { + return move(null, index, list.size() - 1); + } + } + + /** Performs the move operation of an element. + * @param e Element to move + * @param oldIndex old index of the element + * @param newIndex new index of the element + * @return <code>true</code> if the element was moved and the event was fired, otherwise <code>false</code>. + */ + private boolean move(@Nullable final E e, final int oldIndex, final int newIndex) { + if (oldIndex == newIndex || oldIndex < 0 || newIndex < 0 || newIndex >= list.size()) { + return false; + } + assert e == null || list.indexOf(e) == oldIndex; + final E moving = list.remove(oldIndex); + list.add(newIndex, moving); + assert e == null || list.indexOf(e) == newIndex; + fireContentsChanged(this, oldIndex, newIndex); + return true; + } + +} // class ArrayListModel Property changes on: libs/swing-list/trunk/src/net/sf/japi/swing/list/ArrayListModel.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/net/sf/japi/swing/list/MutableListModel.java =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/MutableListModel.java (rev 0) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/MutableListModel.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,92 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.swing.list; + +import javax.swing.ListModel; + +/** A MutableListModel is a list model that additionally provides operations for adding and moving the list's elements. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public interface MutableListModel<E> extends ListModel { + + /** {@inheritDoc} */ + E getElementAt(final int index); + + /** Addes the specified element to this model. + * @param e Element to add. + * @return <code>true</code> if the element was successfully added, otherwise <code>false</code>. + */ + boolean add(E e); + + /** Removes the specified element from this model. + * @param e Element to remove. + * @return <code>true</code> if the element was successfully removed, otherwise <code>false</code>. + */ + boolean remove(E e); + + /** Moves the specified element to top. + * @param e Element to move to top. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveToTop(E e); + + /** Moves the element with the specified index to top. + * @param index Index of the element to move to top. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveToTop(int index); + + /** Moves the specified element up. + * @param e Element to move up. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveUp(E e); + + /** Moves the element with the specified index up. + * @param index Index of the element to move up. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveUp(int index); + + /** Moves the specified element down. + * @param e Element to move down. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveDown(E e); + + /** Moves the element with the specified index down. + * @param index Index of the element to move down. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveDown(int index); + + /** Moves the specified element to bottom. + * @param e Element to move to bottom. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveToBottom(E e); + + /** Moves the element with the specified index to bottom. + * @param index Index of the element to move to bottom. + * @return <code>true</code> if the move was successful, otherwise <code>false</code>. + */ + boolean moveToBottom(int index); + +} // interface MutableListModel Property changes on: libs/swing-list/trunk/src/net/sf/japi/swing/list/MutableListModel.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/net/sf/japi/swing/list/package-info.java =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/package-info.java (rev 0) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/package-info.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,20 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.swing.list; Property changes on: libs/swing-list/trunk/src/net/sf/japi/swing/list/package-info.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ArrayListModelTest.java =================================================================== --- libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ArrayListModelTest.java (rev 0) +++ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ArrayListModelTest.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,242 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.swing.list; + +import javax.swing.event.ListDataEvent; +import net.sf.japi.swing.list.ArrayListModel; +import org.jetbrains.annotations.NotNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +/** Test for {@link ArrayListModel}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class ArrayListModelTest { + + /** Tests that an empty ArrayListModel behaves as expected. */ + @Test + public void testEmpty() { + final ArrayListModel testling = new ArrayListModel(); + assertEquals("Newly created ArrayListModel must contain no elements.", 0, testling.getSize()); + } + + /** Tests that an ArrayListModel with one element behaves as expected. */ + @Test + public void testSingle() { + final ArrayListModel<String> testling = new ArrayListModel<String>(); + final String foo = "foo"; + testling.add(foo); + assertEquals("ArrayListModel now must contain one element.", 1, testling.getSize()); + assertSame("ArrayListModel must return the stored element.", foo, testling.getElementAt(0)); + } + + /** Tests that an ArrayListModel with one element does not accept duplicates. */ + @Test + public void testSingleDuplicate() { + final ArrayListModel<String> testling = new ArrayListModel<String>(); + final String foo = "foo"; + testling.add(foo); + testling.add(foo); + assertEquals("ArrayListModel now must contain one element.", 1, testling.getSize()); + assertSame("ArrayListModel must return the stored element.", foo, testling.getElementAt(0)); + } + + /** Tests that the move operations with elements work as expected for non-exception cases. */ + @Test + public void testMoveElement() { + final ArrayListModel<String> testling = new ArrayListModel<String>(); + final String test1 = "Test1"; + final String test2 = "Test2"; + final String test3 = "Test3"; + final String test4 = "Test4"; + final String test5 = "Test5"; + assertTrue("Adding " + test1 + " must work.", testling.add(test1)); + assertTrue("Adding " + test1 + " must work.", testling.add(test2)); + assertTrue("Adding " + test1 + " must work.", testling.add(test3)); + assertTrue("Adding " + test1 + " must work.", testling.add(test4)); + assertTrue("Adding " + test1 + " must work.", testling.add(test5)); + assertMove("Original order must match.", testling, test1, test2, test3, test4, test5); + assertTrue("Moving " + test5 + " to top must work.", testling.moveToTop(test5)); + assertMove("Test5 now must be first.", testling, test5, test1, test2, test3, test4); + assertFalse("Moving " + test5 + " to top must fail.", testling.moveToTop(test5)); + assertMove("Test5 now must still be first.", testling, test5, test1, test2, test3, test4); + assertTrue("Moving " + test5 + " down must work.", testling.moveDown(test5)); + assertMove("Test5 now must be second.", testling, test1, test5, test2, test3, test4); + assertTrue("Moving " + test5 + " down must work.", testling.moveDown(test5)); + assertMove("Test5 now must be third.", testling, test1, test2, test5, test3, test4); + assertTrue("Moving " + test5 + " down must work.", testling.moveDown(test5)); + assertMove("Test5 now must be fourth.", testling, test1, test2, test3, test5, test4); + assertTrue("Moving " + test5 + " down must work.", testling.moveDown(test5)); + assertMove("Test5 now must be last.", testling, test1, test2, test3, test4, test5); + assertFalse("Moving " + test5 + " down must work.", testling.moveDown(test5)); + assertMove("Test5 now still must be last.", testling, test1, test2, test3, test4, test5); + assertTrue("Moving " + test5 + " up must work.", testling.moveUp(test5)); + assertMove("Test5 now must be fourth.", testling, test1, test2, test3, test5, test4); + assertTrue("Moving " + test5 + " up must work.", testling.moveUp(test5)); + assertMove("Test5 now must be third.", testling, test1, test2, test5, test3, test4); + assertTrue("Moving " + test5 + " up must work.", testling.moveUp(test5)); + assertMove("Test5 now must be second.", testling, test1, test5, test2, test3, test4); + assertTrue("Moving " + test5 + " up must work.", testling.moveUp(test5)); + assertMove("Test5 now must be first.", testling, test5, test1, test2, test3, test4); + assertFalse("Moving " + test5 + " up must fail.", testling.moveUp(test5)); + assertMove("Test5 now still must be first.", testling, test5, test1, test2, test3, test4); + assertTrue("Moving " + test5 + " to bottom must work.", testling.moveToBottom(test5)); + assertMove("Test5 now must be last.", testling, test1, test2, test3, test4, test5); + assertFalse("Moving " + test5 + " to bottom must fail.", testling.moveToBottom(test5)); + assertMove("Test5 now must still be last.", testling, test1, test2, test3, test4, test5); + } + + /** Tests that the move operations with indices work as expected for non-exception cases. */ + @Test + public void testMoveIndex() { + final ArrayListModel<String> testling = new ArrayListModel<String>(); + final String test1 = "Test1"; + final String test2 = "Test2"; + final String test3 = "Test3"; + final String test4 = "Test4"; + final String test5 = "Test5"; + testling.add(test1); + testling.add(test2); + testling.add(test3); + testling.add(test4); + testling.add(test5); + assertMove("Original order must match.", testling, test1, test2, test3, test4, test5); + assertTrue("Moving >0 to top must work.", testling.moveToTop(4)); + assertMove("Test5 now must be first.", testling, test5, test1, test2, test3, test4); + assertFalse("Moving 0 to top must fail.", testling.moveToTop(0)); + assertMove("Test5 now must still be first.", testling, test5, test1, test2, test3, test4); + assertTrue("Moving <end down must work.", testling.moveDown(0)); + assertMove("Test5 now must be second.", testling, test1, test5, test2, test3, test4); + assertTrue("Moving <end down must work.", testling.moveDown(1)); + assertMove("Test5 now must be third.", testling, test1, test2, test5, test3, test4); + assertTrue("Moving <end down must work.", testling.moveDown(2)); + assertMove("Test5 now must be fourth.", testling, test1, test2, test3, test5, test4); + assertTrue("Moving <end down must work.", testling.moveDown(3)); + assertMove("Test5 now must be last.", testling, test1, test2, test3, test4, test5); + assertFalse("Moving end down must fail.", testling.moveDown(4)); + assertMove("Test5 now still must be last.", testling, test1, test2, test3, test4, test5); + assertTrue("Moving >0 up must work.", testling.moveUp(4)); + assertMove("Test5 now must be fourth.", testling, test1, test2, test3, test5, test4); + assertTrue("Moving >0 up must work.", testling.moveUp(3)); + assertMove("Test5 now must be third.", testling, test1, test2, test5, test3, test4); + assertTrue("Moving >0 up must work.", testling.moveUp(2)); + assertMove("Test5 now must be second.", testling, test1, test5, test2, test3, test4); + assertTrue("Moving >0 up must work.", testling.moveUp(1)); + assertMove("Test5 now must be first.", testling, test5, test1, test2, test3, test4); + assertFalse("Moving 0 up must fail.", testling.moveUp(0)); + assertMove("Test5 now still must be first.", testling, test5, test1, test2, test3, test4); + assertTrue("Moving <end to bottom must work.", testling.moveToBottom(0)); + assertMove("Test5 now must be last.", testling, test1, test2, test3, test4, test5); + assertFalse("Moving end to bottom must fail.", testling.moveToBottom(4)); + assertMove("Test5 now must still be last.", testling, test1, test2, test3, test4, test5); + } + + /** Tests that add and remove operations fire the correct events. */ + @Test + public void testAddRemoveEvents() { + final ArrayListModel<String> testling = new ArrayListModel<String>(); + final String test1 = "Test1"; + final String test2 = "Test2"; + final String test3 = "Test3"; + final String test4 = "Test4"; + final MockListDataListener listenerMock = new MockListDataListener(); + testling.addListDataListener(listenerMock); + testling.add(test1); + listenerMock.assertEvent(ListDataEvent.INTERVAL_ADDED, 0, 0); + testling.add(test2); + listenerMock.assertEvent(ListDataEvent.INTERVAL_ADDED, 1, 1); + testling.add(test3); + listenerMock.assertEvent(ListDataEvent.INTERVAL_ADDED, 2, 2); + testling.removeListDataListener(listenerMock); + testling.add(test4); + listenerMock.assertNoEvent(); + testling.addListDataListener(listenerMock); + testling.remove(test1); + listenerMock.assertEvent(ListDataEvent.INTERVAL_REMOVED, 0, 0); + testling.remove(test4); + listenerMock.assertEvent(ListDataEvent.INTERVAL_REMOVED, 2, 2); + testling.removeListDataListener(listenerMock); + testling.remove(test3); + listenerMock.assertNoEvent(); + } + + /** Tests that move element operations fire the correct events. */ + @Test + public void testMoveElementEvents() { + final ArrayListModel<String> testling = new ArrayListModel<String>(); + final String test1 = "Test1"; + final String test2 = "Test2"; + final String test3 = "Test3"; + final String test4 = "Test4"; + final String test5 = "Test5"; + final MockListDataListener listenerMock = new MockListDataListener(); + testling.add(test1); + testling.add(test2); + testling.add(test3); + testling.add(test4); + testling.add(test5); + testling.addListDataListener(listenerMock); + testling.moveToTop(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 0, 4); + testling.moveToTop(test5); + listenerMock.assertNoEvent(); + testling.moveDown(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 0, 1); + testling.moveDown(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 1, 2); + testling.moveDown(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 2, 3); + testling.moveDown(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 3, 4); + testling.moveDown(test5); + listenerMock.assertNoEvent(); + testling.moveUp(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 3, 4); + testling.moveUp(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 2, 3); + testling.moveUp(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 1, 2); + testling.moveUp(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 0, 1); + testling.moveUp(test5); + listenerMock.assertNoEvent(); + testling.moveToBottom(test5); + listenerMock.assertEvent(ListDataEvent.CONTENTS_CHANGED, 0, 4); + testling.moveToBottom(test5); + listenerMock.assertNoEvent(); + testling.removeListDataListener(listenerMock); + } + + /** Asserts a certain size and order after a move. + * @param message Message to issue in case the size and order are not as expected. + * @param model Testling to check. + * @param elements Elements in expected order. + */ + public static <E> void assertMove(@NotNull final String message, @NotNull final ArrayListModel<E> model, @NotNull final E... elements) { + assertEquals("ArrayListModel now must contain " + elements.length + " elements.", elements.length, model.getSize()); + for (int i = 0; i < elements.length; i++) { + assertSame(message + " / ArrayList must contain element " + elements[i] + " at index " + i + ".", elements[i], model.getElementAt(i)); + } + } + +} // class ArrayListModelTest Property changes on: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ArrayListModelTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/MockListDataListener.java =================================================================== --- libs/swing-list/trunk/src/test/net/sf/japi/swing/list/MockListDataListener.java (rev 0) +++ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/MockListDataListener.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,95 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.swing.list; + +import javax.swing.event.ListDataEvent; +import javax.swing.event.ListDataListener; +import net.sf.japi.swing.list.ArrayListModel; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +/** A ListDataListener useful for testing that the event system in {@link ArrayListModel} works. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class MockListDataListener implements ListDataListener { + + /** Last event. */ + @Nullable + private ListDataEvent lastEvent; + + /** {@inheritDoc} */ + public void intervalAdded(@NotNull final ListDataEvent e) { + lastEvent = e; + assertEquals("intervalAdded() must be invoked with a ListDataEvent of type INTERVAL_ADDED.", ListDataEvent.INTERVAL_ADDED, e.getType()); + } + + /** {@inheritDoc} */ + public void intervalRemoved(@NotNull final ListDataEvent e) { + lastEvent = e; + assertEquals("intervalRemoved() must be invoked with a ListDataEvent of type INTERVAL_REMOVED.", ListDataEvent.INTERVAL_REMOVED, e.getType()); + } + + /** {@inheritDoc} */ + public void contentsChanged(@NotNull final ListDataEvent e) { + lastEvent = e; + assertEquals("contentsChanged() must be invoked with a ListDataEvent of type CONTENTS_CHANGED.", ListDataEvent.CONTENTS_CHANGED, e.getType()); + } + + /** Asserts that there was an event and it has the expected properties. + * @param type Type of the event to assert. + * @param index0 Lower bound of the event's range. + * @param index1 Upper bound of the event's range. + */ + public void assertEvent(final int type, final int index0, final int index1) { + @Nullable final ListDataEvent lastEvent = this.lastEvent; + assertNotNull("Expected to have an event.", lastEvent); + assert lastEvent != null; + assertEquals("Expecting last event to be of type " + getTypeName(type) + ".", type, lastEvent.getType()); + assertEquals("Expecting last event to have correct index0.", index0, lastEvent.getIndex0()); + assertEquals("Expecting last event to have correct index1.", index1, lastEvent.getIndex1()); + this.lastEvent = null; + } + + /** Asserts that there was no event. */ + public void assertNoEvent() { + assertNull("Expected to not have an event.", lastEvent); + } + + /** Returns the name of an event type. + * @param type Type to return name for. + * @return Name for <var>type</var> + */ + private static String getTypeName(final int type) { + switch (type) { + case 0: return "CONTENTS_CHANGED"; + case 1: return "INTERVAL_ADDED"; + case 2: return "INTERVAL_REMOVED"; + default: + fail("Internal test error: unexpected event type for check " + type); + assert false; + return ""; // Never reaches this statement, but the compiler expects a return. + } + } + +} // class MockListDataListener Property changes on: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/MockListDataListener.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/package-info.java =================================================================== --- libs/swing-list/trunk/src/test/net/sf/japi/swing/list/package-info.java (rev 0) +++ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/package-info.java 2007-07-14 09:49:47 UTC (rev 534) @@ -0,0 +1,20 @@ +/* + * JAPI libs-swing-list is a library for lists in swing. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.swing.list; Property changes on: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/package-info.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |