[Schmitzm-commits] r2325 - in trunk/schmitzm-core: . src/main/java src/main/java/de/schmitzm/swing src/main/java/de/schmitzm/swing/list

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Jun 6 11:30:15 CEST 2013


Author: mojays
Date: 2013-06-06 11:30:15 +0200 (Thu, 06 Jun 2013)
New Revision: 2325

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/list/
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/list/ReferencedListModel.java
Removed:
   trunk/schmitzm-core/src/main/java/javax/
Modified:
   trunk/schmitzm-core/
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
Log:
ReferencedListModel moved (from temporary package javax.swing) to de.schmitzm.swing.list


Property changes on: trunk/schmitzm-core
___________________________________________________________________
Modified: svn:ignore
   - target
.classpath
*.project
.settings

   + target
.classpath
*.project
.settings
lib


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java	2013-06-06 07:46:13 UTC (rev 2324)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java	2013-06-06 09:30:15 UTC (rev 2325)
@@ -14,12 +14,12 @@
 import javax.swing.JButton;
 import javax.swing.JList;
 import javax.swing.JScrollPane;
-import javax.swing.ReferencedListModel;
 import javax.swing.event.ListDataListener;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 
 import net.miginfocom.swing.MigLayout;
+import de.schmitzm.swing.list.ReferencedListModel;
 
 /**
  * Panel showing a {@link JList} which allows to modify the list.

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/list/ReferencedListModel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/list/ReferencedListModel.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/list/ReferencedListModel.java	2013-06-06 09:30:15 UTC (rev 2325)
@@ -0,0 +1,388 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program 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 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.swing.list;
+
+import java.util.List;
+import java.util.Vector;
+
+import javax.swing.AbstractListModel;
+
+/**
+ * This {@link ListModel} is a reduced version of {@link javax.swing.DefaultListModel}
+ * which uses a {@link List} as that the {@link #delegate} field (which contains the data).
+ * Furthermore the field is {@code protected} so that sub-classes can override the implementation.<br>
+ * <br>
+ * In contrast to {@link javax.swing.DefaultListModel} this model does not hold a copy
+ * of the list data, but changes an underlying {@link List} directly (to avoid unnecessary copy
+ * processes). However "external" inserts and deletes on the list data should be avoided because
+ * no corresponding events are fired!   
+ * @author Martin O.J. Schmitz
+ *
+ */
+public class ReferencedListModel<E> extends AbstractListModel<E>
+{
+    /** Hold the list data. */
+    protected List<E> delegate;
+
+    
+    /**
+     * Creates a new model with a new empty {@link Vector} as data source.
+     */
+    public ReferencedListModel() {
+      this(null);
+    }
+
+    /**
+     * Creates a new model.
+     */
+    public ReferencedListModel(List<E> data) {
+      super();
+      if ( data == null )
+        data = new Vector<E>();
+      this.delegate = data;
+    }
+    
+    /**
+     * Resets the data source.
+     */
+    public void setDataSource(List<E> data) {
+      // first remove all elements (and fire event)
+      removeAllElements();
+      // redefine the data
+      if ( data == null )
+        data = new Vector<E>();
+      this.delegate = data;
+
+      int index = delegate.size()-1;
+      if (index >= 0) {
+          fireIntervalAdded(this, 0, index);
+      }
+    }
+
+    /**
+     * Returns the data source. Modifications of this list will be effect the
+     * model data, but not immediately the GUI because no events are fired!
+     */
+    public List<E> getDataSource() {
+      return delegate;
+    }
+    
+    ////////////////////////////////////////////////////////////////////////
+    // The following methods are copied from javax.swing.DefaultListModel //
+    // and only lightly modified to respect the List type of delegate (   //
+    // instead of Vector)                                                 //
+    ////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Returns the number of components in this list.
+     * <p>
+     * This method is identical to <code>size</code>, which implements the
+     * <code>List</code> interface defined in the 1.2 Collections framework.
+     * This method exists in conjunction with <code>setSize</code> so that
+     * <code>size</code> is identifiable as a JavaBean property.
+     *
+     * @return  the number of components in this list
+     * @see #size()
+     */
+    public int getSize() {
+        return delegate.size();
+    }
+
+    /**
+     * Returns the component at the specified index.
+     * <blockquote>
+     * <b>Note:</b> Although this method is not deprecated, the preferred
+     *    method to use is <code>get(int)</code>, which implements the
+     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     * </blockquote>
+     * @param      index   an index into this list
+     * @return     the component at the specified index
+     * @exception  ArrayIndexOutOfBoundsException  if the <code>index</code>
+     *             is negative or greater than the current size of this
+     *             list
+     * @see #get(int)
+     */
+    public E getElementAt(int index) {
+        return delegate.get(index);
+    }
+
+    /**
+     * Returns the number of components in this list.
+     *
+     * @return  the number of components in this list
+     * @see Vector#size()
+     */
+    public int size() {
+        return delegate.size();
+    }
+
+    /**
+     * Tests whether this list has any components.
+     *
+     * @return  <code>true</code> if and only if this list has
+     *          no components, that is, its size is zero;
+     *          <code>false</code> otherwise
+     * @see Vector#isEmpty()
+     */
+    public boolean isEmpty() {
+        return delegate.isEmpty();
+    }
+
+    /**
+     * Tests whether the specified object is a component in this list.
+     *
+     * @param   elem   an object
+     * @return  <code>true</code> if the specified object
+     *          is the same as a component in this list
+     * @see Vector#contains(Object)
+     */
+    public boolean contains(Object elem) {
+        return delegate.contains(elem);
+    }
+
+    /**
+     * Searches for the first occurrence of <code>elem</code>.
+     *
+     * @param   elem   an object
+     * @return  the index of the first occurrence of the argument in this
+     *          list; returns <code>-1</code> if the object is not found
+     * @see Vector#indexOf(Object)
+     */
+    public int indexOf(Object elem) {
+        return delegate.indexOf(elem);
+    }
+
+    /**
+     * Returns the index of the last occurrence of <code>elem</code>.
+     *
+     * @param   elem   the desired component
+     * @return  the index of the last occurrence of <code>elem</code>
+     *          in the list; returns <code>-1</code> if the object is not found
+     * @see Vector#lastIndexOf(Object)
+     */
+    public int lastIndexOf(Object elem) {
+        return delegate.lastIndexOf(elem);
+    }
+
+    /**
+     * Returns the component at the specified index.
+     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
+     * is negative or not less than the size of the list.
+     * <blockquote>
+     * <b>Note:</b> Although this method is not deprecated, the preferred
+     *    method to use is <code>get(int)</code>, which implements the
+     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     * </blockquote>
+     *
+     * @param      index   an index into this list
+     * @return     the component at the specified index
+     * @see #get(int)
+     * @see Vector#elementAt(int)
+     */
+    public E elementAt(int index) {
+        return delegate.get(index);
+    }
+
+    /**
+     * Returns the first component of this list.
+     * Throws a <code>NoSuchElementException</code> if this
+     * vector has no components.
+     * @return     the first component of this list
+     * @see Vector#firstElement()
+     */
+    public E firstElement() {
+        return delegate.get(0);
+    }
+
+    /**
+     * Returns the last component of the list.
+     * Throws a <code>NoSuchElementException</code> if this vector
+     * has no components.
+     *
+     * @return  the last component of the list
+     * @see Vector#lastElement()
+     */
+    public E lastElement() {
+        return delegate.get( getSize()-1 );
+    }
+
+    /**
+     * Sets the component at the specified <code>index</code> of this
+     * list to be the specified element. The previous component at that
+     * position is discarded.
+     * <p>
+     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
+     * is invalid.
+     * <blockquote>
+     * <b>Note:</b> Although this method is not deprecated, the preferred
+     *    method to use is <code>set(int,Object)</code>, which implements the
+     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     * </blockquote>
+     *
+     * @param      element what the component is to be set to
+     * @param      index   the specified index
+     * @see #set(int,Object)
+     * @see Vector#setElementAt(Object,int)
+     */
+    public void setElementAt(E element, int index) {
+        delegate.set(index,element);
+        fireContentsChanged(this, index, index);
+    }
+
+    /**
+     * Deletes the component at the specified index.
+     * <p>
+     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
+     * is invalid.
+     * <blockquote>
+     * <b>Note:</b> Although this method is not deprecated, the preferred
+     *    method to use is <code>remove(int)</code>, which implements the
+     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     * </blockquote>
+     *
+     * @param      index   the index of the object to remove
+     * @see #remove(int)
+     * @see Vector#removeElementAt(int)
+     */
+    public void removeElementAt(int index) {
+        delegate.remove(index);
+        fireIntervalRemoved(this, index, index);
+    }
+
+    /**
+     * Inserts the specified element as a component in this list at the
+     * specified <code>index</code>.
+     * <p>
+     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
+     * is invalid.
+     * <blockquote>
+     * <b>Note:</b> Although this method is not deprecated, the preferred
+     *    method to use is <code>add(int,Object)</code>, which implements the
+     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     * </blockquote>
+     *
+     * @param      element the component to insert
+     * @param      index   where to insert the new component
+     * @exception  ArrayIndexOutOfBoundsException  if the index was invalid
+     * @see #add(int,Object)
+     * @see Vector#insertElementAt(Object,int)
+     */
+    public void insertElementAt(E element, int index) {
+        delegate.add(index,element);
+        fireIntervalAdded(this, index, index);
+    }
+
+    /**
+     * Adds the specified component to the end of this list.
+     *
+     * @param   element   the component to be added
+     * @see Vector#addElement(Object)
+     */
+    public void addElement(E element) {
+        int index = delegate.size();
+        delegate.add(element);
+        fireIntervalAdded(this, index, index);
+    }
+
+    /**
+     * Removes the first (lowest-indexed) occurrence of the argument
+     * from this list.
+     *
+     * @param   obj   the component to be removed
+     * @return  <code>true</code> if the argument was a component of this
+     *          list; <code>false</code> otherwise
+     * @see Vector#removeElement(Object)
+     */
+    public boolean removeElement(Object obj) {
+        int index = indexOf(obj);
+        boolean rv = delegate.remove(obj);
+        if (index >= 0) {
+            fireIntervalRemoved(this, index, index);
+        }
+        return rv;
+    }
+
+
+    /**
+     * Removes all components from this list and sets its size to zero.
+     * <blockquote>
+     * <b>Note:</b> Although this method is not deprecated, the preferred
+     *    method to use is <code>clear</code>, which implements the
+     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     * </blockquote>
+     *
+     * @see #clear()
+     * @see Vector#removeAllElements()
+     */
+    public void removeAllElements() {
+        int index1 = delegate.size()-1;
+        delegate.clear();
+        if (index1 >= 0) {
+            fireIntervalRemoved(this, 0, index1);
+        }
+    }
+
+
+    /**
+     * Deletes the components at the specified range of indexes.
+     * The removal is inclusive, so specifying a range of (1,5)
+     * removes the component at index 1 and the component at index 5,
+     * as well as all components in between.
+     * <p>
+     * Throws an <code>ArrayIndexOutOfBoundsException</code>
+     * if the index was invalid.
+     * Throws an <code>IllegalArgumentException</code> if
+     * <code>fromIndex > toIndex</code>.
+     *
+     * @param      fromIndex the index of the lower end of the range
+     * @param      toIndex   the index of the upper end of the range
+     * @see        #remove(int)
+     */
+    public void removeRange(int fromIndex, int toIndex) {
+        if (fromIndex > toIndex) {
+            throw new IllegalArgumentException("fromIndex must be <= toIndex");
+        }
+        for(int i = toIndex; i >= fromIndex; i--) {
+            delegate.remove(i);
+        }
+        fireIntervalRemoved(this, fromIndex, toIndex);
+    }
+
+    /**
+     * Returns a string that displays and identifies this
+     * object's properties.
+     *
+     * @return a String representation of this object
+     */
+   public String toString() {
+        return delegate.toString();
+    }
+}
+



More information about the Schmitzm-commits mailing list