svn commit: r14810 - trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java

[email protected]
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: bobtarling
Date: 2008-05-27 12:31:25-0700
New Revision: 14810

Modified:
   trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java

Log:
Left and right arrows will scroll the view

Modified: trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java?view=diff&rev=14810&p1=trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java&p2=trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java&r1=14809&r2=14810
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/ui/ScrollList.java	2008-05-27 12:31:25-0700
@@ -24,17 +24,34 @@
 
 package org.argouml.uml.ui;
 
+import java.awt.Component;
+import java.awt.Point;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
 import javax.swing.JScrollPane;
 import javax.swing.ListModel;
 import javax.swing.ScrollPaneConstants;
 
 /**
- * A scrollable list of items.
+ * A scrollable list of items. This makes sure that there is no horizontal
+ * scrollbar (which takes up too much screen real estate) and that sideways
+ * scrolling can be achieved instead with arrow keys.
  * @author Bob Tarling
  */
-public class ScrollList extends JScrollPane {
+public class ScrollList extends JScrollPane implements KeyListener {
+
+    /**
+     * The UID.
+     */
+    private static final long serialVersionUID = 6711776013279497682L;
 
     /**
+     * The Component that this scroll is wrapping.
+     */
+    private Component list;
+    
+    /**
      * Builds a JList from a given list model and wraps
      * in a scrollable view.
      * @param listModel The model from which to build the list
@@ -55,9 +72,43 @@
                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
         setViewportView(new UMLLinkedList(listModel, showIcon, showPath));
     }
-
+    
+    public void setViewportView(Component view) {
+        super.setViewportView(view);
+        list = view;
+    }
+    
     /**
-     * The UID.
+     * Examine key event to scroll left or right depending on key press
+     * @param e the key event to examine
      */
-    private static final long serialVersionUID = 6711776013279497682L;
+    public void keyPressed(KeyEvent e) {
+        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
+            final Point posn = getViewport().getViewPosition();
+            if (posn.x > 0) {
+                getViewport().setViewPosition(new Point(posn.x - 1, posn.y));
+            }
+        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
+            final Point posn = getViewport().getViewPosition();
+            if (list.getWidth() - posn.x > getViewport().getWidth()) {
+                getViewport().setViewPosition(new Point(posn.x + 1, posn.y));
+            }
+        }
+    }
+
+    public void keyReleased(KeyEvent arg0) {
+    }
+
+    public void keyTyped(KeyEvent arg0) {
+    }
+    
+    public void addNotify() {
+        super.addNotify();
+        list.addKeyListener(this);
+    }
+    
+    public void removeNotify() {
+        super.removeNotify();
+        list.removeKeyListener(this);
+    }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.