[Japi-cvs] SF.net SVN: japi: [535] libs/swing-list/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-07-14 10:34:56
|
Revision: 535 http://svn.sourceforge.net/japi/?rev=535&view=rev Author: christianhujer Date: 2007-07-14 03:34:54 -0700 (Sat, 14 Jul 2007) Log Message: ----------- Added panel for controlling a MutableListModel's order. Added Paths: ----------- libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java libs/swing-list/trunk/src/net/sf/japi/swing/list/action.properties libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java Added: libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java (rev 0) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java 2007-07-14 10:34:54 UTC (rev 535) @@ -0,0 +1,144 @@ +/* + * 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.awt.GridLayout; +import javax.swing.Action; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JList; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import net.sf.japi.swing.ActionFactory; +import net.sf.japi.swing.ActionMethod; +import org.jetbrains.annotations.NotNull; + +/** A Panel for a {@link MutableListModel} rendered by a {@link JList}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @todo the selection should automatically change with the move. + */ +public class ListControlPanel extends JComponent implements ListSelectionListener { + + /** The ActionFactory for creating the actions. */ + @NotNull private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("net.sf.japi.swing.list"); + + /** Action for moving to top. */ + @NotNull private final Action moveToTop = ACTION_FACTORY.createAction(false, "moveToTop", this); + + /** Action for moving up. */ + @NotNull private final Action moveUp = ACTION_FACTORY.createAction(false, "moveUp", this); + + /** Action for moving down. */ + @NotNull private final Action moveDown = ACTION_FACTORY.createAction(false, "moveDown", this); + + /** Action for moving to bottom. */ + @NotNull private final Action moveToBottom = ACTION_FACTORY.createAction(false, "moveToBottom", this); + + /** The list that provides the information about the current selection. */ + @NotNull private JList list; + + /** The model to control. */ + @NotNull private MutableListModel model; + + /** Creates a ListControlPanel. + * @param list JList that provides the list selection information. + * @param model Model to control. + */ + public ListControlPanel(@NotNull final JList list, @NotNull final MutableListModel model) { + setLayout(new GridLayout(4, 1, 5, 5)); + setList(list); + setModel(model); + add(new JButton(moveToTop)); + add(new JButton(moveUp)); + add(new JButton(moveDown)); + add(new JButton(moveToBottom)); + updateActionStates(); + } + + /** Action for moving the currently selected element to top. */ + @ActionMethod public void moveToTop() { + model.moveToTop(list.getSelectedIndex()); + } + + /** Action for moving the currently selected element up. */ + @ActionMethod public void moveUp() { + model.moveUp(list.getSelectedIndex()); + } + + /** Action for moving the currently selected element down. */ + @ActionMethod public void moveDown() { + model.moveDown(list.getSelectedIndex()); + } + + /** Action for moving the currently selected element to bottom. */ + @ActionMethod public void moveToBottom() { + model.moveToBottom(list.getSelectedIndex()); + } + + /** Returns the list that provides the information about the current selection. + * @return JList queried for selection. + */ + @NotNull public JList getList() { + return list; + } + + /** Sets the list that provides the information about the current selection. + * @param list JList to query for selection. + */ + public void setList(@NotNull final JList list) { + if (this.list != null) { + this.list.removeListSelectionListener(this); + } + this.list = list; + list.addListSelectionListener(this); + } + + /** Returns the model that's controlled by this ListControlPanel. + * @return The controlled model. + */ + @NotNull public MutableListModel getModel() { + return model; + } + + /** Sets the model to control by this ListControlPanel. + * @param model The model to control. + */ + public void setModel(@NotNull final MutableListModel model) { + this.model = model; + } + + /** {@inheritDoc} */ + public void valueChanged(@NotNull final ListSelectionEvent e) { + if (!e.getValueIsAdjusting()) { + updateActionStates(); + } + } + + private void updateActionStates() { + assert list == null || list.getModel() == model; + final boolean top = list == null || list.getSelectedIndex() == 0; + final boolean bottom = list == null || list.getSelectedIndex() == model.getSize() - 1; + moveToTop.setEnabled(!top); + moveUp.setEnabled(!top); + moveDown.setEnabled(!bottom); + moveToBottom.setEnabled(!bottom); + } + +} // class ListControlPanel Property changes on: libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/net/sf/japi/swing/list/action.properties =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/action.properties (rev 0) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/action.properties 2007-07-14 10:34:54 UTC (rev 535) @@ -0,0 +1,23 @@ +# +# 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 +# + +moveToTop.text=Top +moveUp.text=Up +moveDown.text=Down +moveToBottom.text=Bottom Property changes on: libs/swing-list/trunk/src/net/sf/japi/swing/list/action.properties ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java =================================================================== --- libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java (rev 0) +++ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java 2007-07-14 10:34:54 UTC (rev 535) @@ -0,0 +1,56 @@ +/* + * 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 java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JList; +import javax.swing.JScrollPane; +import net.sf.japi.swing.list.ArrayListModel; +import net.sf.japi.swing.list.ListControlPanel; +import org.junit.Test; + +/** Test for {@link ListControlPanel}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class ListControlPanelTest { + + /** Tests that a ListControlPanel works. */ + @Test + public void testListControlPanel() { + final ArrayListModel<String> model = new ArrayListModel<String>(); + model.add("foo"); + model.add("bar"); + model.add("buzz"); + final JList list = new JList(model); + final JFrame f = new JFrame("Test"); + final ListControlPanel testling = new ListControlPanel(list, model); + f.add(new JScrollPane(list)); + f.add(testling, BorderLayout.LINE_END); + f.pack(); + f.setVisible(true); + try { + Thread.sleep(100000); + } catch (final InterruptedException ignore) { + // ignore + } + } + +} // class ListControlPanelTest Property changes on: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.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. |