You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/03/24 21:34:51 UTC

svn commit: r758005 - in /incubator/pivot/trunk/wtk/src/pivot/wtk: Editor.java ListView.java content/ListViewItemEditor.java content/ListViewItemRenderer.java skin/MenuBarItemSkin.java skin/MenuButtonSkin.java skin/MenuItemSkin.java

Author: gbrown
Date: Tue Mar 24 20:34:51 2009
New Revision: 758005

URL: http://svn.apache.org/viewvc?rev=758005&view=rev
Log:
Complete ListViewItemEditor interface; minor fixes to MenuBarItemSkin, MenuButtonSkin, and MenuItemSkin.


Added:
    incubator/pivot/trunk/wtk/src/pivot/wtk/Editor.java
Modified:
    incubator/pivot/trunk/wtk/src/pivot/wtk/ListView.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemEditor.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemRenderer.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuBarItemSkin.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuButtonSkin.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuItemSkin.java

Added: incubator/pivot/trunk/wtk/src/pivot/wtk/Editor.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/Editor.java?rev=758005&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/Editor.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/Editor.java Tue Mar 24 20:34:51 2009
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2009 VMware, Inc.
+ *
+ * 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 pivot.wtk;
+
+/**
+ * Base interface for content editors.
+ *
+ * @author gbrown
+ */
+public interface Editor {
+    public boolean isEditing();
+    public void save();
+    public void cancel();
+}

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/ListView.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/ListView.java?rev=758005&r1=758004&r2=758005&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/ListView.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/ListView.java Tue Mar 24 20:34:51 2009
@@ -89,7 +89,7 @@
      *
      * @author gbrown
      */
-    public interface ItemEditor {
+    public interface ItemEditor extends Editor {
         /**
          * Notifies the editor that editing should begin.
          *
@@ -1113,6 +1113,17 @@
     }
 
     /**
+     * Returns the item indent.
+     *
+     * @return
+     * The horizontal space preceding items in the list.
+     */
+    public int getItemIndent() {
+        ListView.Skin listViewSkin = (ListView.Skin)getSkin();
+        return listViewSkin.getItemIndent();
+    }
+
+    /**
      * Returns the list view listener list.
      */
     public ListenerList<ListViewListener> getListViewListeners() {

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemEditor.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemEditor.java?rev=758005&r1=758004&r2=758005&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemEditor.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemEditor.java Tue Mar 24 20:34:51 2009
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 VMware, Inc.
+ * Copyright (c) 2009 VMware, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,7 +15,24 @@
  */
 package pivot.wtk.content;
 
+import pivot.collections.List;
+import pivot.util.Vote;
+import pivot.wtk.Bounds;
+import pivot.wtk.Component;
+import pivot.wtk.ComponentKeyListener;
+import pivot.wtk.Container;
+import pivot.wtk.ContainerMouseListener;
+import pivot.wtk.Display;
+import pivot.wtk.Insets;
+import pivot.wtk.Keyboard;
 import pivot.wtk.ListView;
+import pivot.wtk.ListViewItemListener;
+import pivot.wtk.ListViewListener;
+import pivot.wtk.Mouse;
+import pivot.wtk.Point;
+import pivot.wtk.TextInput;
+import pivot.wtk.Window;
+import pivot.wtk.WindowStateListener;
 
 /**
  * Default list view item editor.
@@ -23,14 +40,230 @@
  * @author gbrown
  */
 public class ListViewItemEditor implements ListView.ItemEditor {
+    private ListView listView = null;
+    private int index = -1;
+
+    private TextInput textInput = null;
+    private Window popup = null;
+
+    private ListViewListener listViewListener = new ListViewListener() {
+        public void listDataChanged(ListView listView, List<?> previousListData) {
+            cancel();
+        }
+
+        public void itemRendererChanged(ListView listView, ListView.ItemRenderer previousItemRenderer) {
+            // No-op
+        }
+
+        public void itemEditorChanged(ListView listView, ListView.ItemEditor previousItemEditor) {
+            cancel();
+        }
+
+        public void selectModeChanged(ListView listView, ListView.SelectMode previousSelectMode) {
+            // No-op
+        }
+
+        public void checkmarksEnabledChanged(ListView listView) {
+            // No-op
+        }
+
+        public void selectedItemKeyChanged(ListView listView, String previousSelectedItemKey) {
+            // No-op
+        }
+
+        public void selectedItemsKeyChanged(ListView listView, String previousSelectedItemsKey) {
+            // No-op
+        }
+    };
+
+    private ListViewItemListener listViewItemListener = new ListViewItemListener() {
+        public void itemInserted(ListView listView, int index) {
+            cancel();
+        }
+
+        public void itemsRemoved(ListView listView, int index, int count) {
+            cancel();
+        }
+
+        public void itemUpdated(ListView listView, int index) {
+            cancel();
+        }
+
+        public void itemsSorted(ListView listView) {
+            cancel();
+        }
+    };
+
+    private ComponentKeyListener textInputKeyHandler = new ComponentKeyListener() {
+        @SuppressWarnings("unchecked")
+        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
+            if (keyCode == Keyboard.KeyCode.ENTER) {
+                save();
+            } else if (keyCode == Keyboard.KeyCode.ESCAPE) {
+                cancel();
+            }
+
+            return false;
+        }
+
+        public boolean keyReleased(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
+            return false;
+        }
+
+        public boolean keyTyped(Component component, char character) {
+            return false;
+        }
+    };
+
+    private WindowStateListener popupWindowStateHandler = new WindowStateListener() {
+        public Vote previewWindowOpen(Window window, Display display) {
+            return Vote.APPROVE;
+        }
+
+        public void windowOpenVetoed(Window window, Vote reason) {
+        }
+
+        public void windowOpened(Window window) {
+            Display display = window.getDisplay();
+            display.getContainerMouseListeners().add(displayMouseHandler);
+
+            listView.getListViewListeners().add(listViewListener);
+            listView.getListViewItemListeners().add(listViewItemListener);
+        }
+
+        public Vote previewWindowClose(Window window) {
+            return Vote.APPROVE;
+        }
+
+        public void windowCloseVetoed(Window window, Vote reason) {
+        }
+
+        public void windowClosed(Window window, Display display) {
+            display.getContainerMouseListeners().remove(displayMouseHandler);
+
+            listView.getListViewListeners().remove(listViewListener);
+            listView.getListViewItemListeners().remove(listViewItemListener);
+
+            listView.requestFocus();
+
+            listView = null;
+            index = -1;
+            textInput = null;
+            popup = null;
+        }
+    };
+
+    private ContainerMouseListener displayMouseHandler = new ContainerMouseListener() {
+        public boolean mouseMove(Container container, int x, int y) {
+            return false;
+        }
+
+        public boolean mouseDown(Container container, Mouse.Button button, int x, int y) {
+            Display display = (Display)container;
+            Window window = (Window)display.getComponentAt(x, y);
+            if (popup != window) {
+                save();
+            }
+
+            return false;
+        }
+
+        public boolean mouseUp(Container container, Mouse.Button button, int x, int y) {
+            return false;
+        }
+
+        public boolean mouseWheel(Container container, Mouse.ScrollType scrollType,
+            int scrollAmount, int wheelRotation, int x, int y) {
+            return true;
+        }
+    };
+
     public void edit(ListView listView, int index) {
-        // TODO Get item bounds
+        if (this.listView != null) {
+            throw new IllegalStateException("Currently editing.");
+        }
+
+        this.listView = listView;
+        this.index = index;
+
+        // Get the data being edited
+        List<?> listData = listView.getListData();
+        ListItem listItem = (ListItem)listData.get(index);
+
+        // Get the item bounds
+        Bounds itemBounds = listView.getItemBounds(index);
+        int itemIndent = listView.getItemIndent();
+        itemBounds.x += itemIndent;
+        itemBounds.width -= itemIndent;
+
+        // Render the item data
+        ListViewItemRenderer itemRenderer = (ListViewItemRenderer)listView.getItemRenderer();
+        itemRenderer.render(listItem, listView, false, false, false, false);
+        itemRenderer.setSize(itemBounds.width, itemBounds.height);
+
+        // Calculate the text bounds
+        Bounds textBounds = itemRenderer.getTextBounds();
+
+        if (textBounds != null) {
+            textInput = new TextInput();
+            Insets padding = (Insets)textInput.getStyles().get("padding");
+
+            // Calculate the bounds of what we're editing
+            Bounds editBounds = new Bounds(itemBounds);
+            editBounds.x += textBounds.x - (padding.left + 1);
+            editBounds.width -= textBounds.x;
+            editBounds.width += (padding.left + 1);
 
-        // TODO Constrain bounds to viewports
+            // Scroll to make the text as visible as possible
+            listView.scrollAreaToVisible(editBounds.x, editBounds.y,
+                textBounds.width, editBounds.height);
+
+            // Constrain the bounds by what is visible through Viewport ancestors
+            listView.constrainToViewportBounds(editBounds);
+
+            textInput.setText(listItem.getText());
+            textInput.setPreferredWidth(editBounds.width);
+            textInput.getComponentKeyListeners().add(textInputKeyHandler);
+
+            // Create and open the popup
+            popup = new Window(textInput);
+            popup.getWindowStateListeners().add(popupWindowStateHandler);
+
+            Point location = listView.mapPointToAncestor(listView.getDisplay(), 0, 0);
+            popup.setLocation(location.x + editBounds.x,
+                location.y + editBounds.y + (editBounds.height - textInput.getPreferredHeight(-1)) / 2);
+            popup.open(listView.getWindow());
+
+            textInput.requestFocus();
+        }
+    }
+
+    public boolean isEditing() {
+        return (listView != null);
+    }
+
+    @SuppressWarnings("unchecked")
+    public void save() {
+        if (!isEditing()) {
+            throw new IllegalStateException();
+        }
+
+        List<Object> listData = (List<Object>)listView.getListData();
+        ListItem listItem = (ListItem)listData.get(index);
+
+        // Update the item data
+        String text = textInput.getText();
+        listItem.setText(text);
+
+        // Notifying the parent will close the popup
+        listData.update(index, listItem);
+    }
 
-        // TODO Add listener for mouse events on listView so we can close the
-        // popup when the affiliate is clicked
+    public void cancel() {
+        if (!isEditing()) {
+            throw new IllegalStateException();
+        }
 
-        System.out.println("BEGIN EDIT " + index);
+        popup.close();
     }
 }

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemRenderer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemRenderer.java?rev=758005&r1=758004&r2=758005&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemRenderer.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/content/ListViewItemRenderer.java Tue Mar 24 20:34:51 2009
@@ -18,6 +18,7 @@
 import java.awt.Color;
 import java.awt.Font;
 
+import pivot.wtk.Bounds;
 import pivot.wtk.Component;
 import pivot.wtk.FlowPane;
 import pivot.wtk.HorizontalAlignment;
@@ -155,4 +156,15 @@
     public void setShowIcon(boolean showIcon) {
         imageView.setDisplayable(showIcon);
     }
+
+    /**
+     * Gets the bounds of the text that is rendered by this renderer.
+     *
+     * @return
+     * The bounds of the rendered text, or <tt>null</tt> if this renderer did
+     * not render any text.
+     */
+    public Bounds getTextBounds() {
+        return (label.isVisible() ? label.getBounds() : null);
+    }
 }

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuBarItemSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuBarItemSkin.java?rev=758005&r1=758004&r2=758005&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuBarItemSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuBarItemSkin.java Tue Mar 24 20:34:51 2009
@@ -87,9 +87,11 @@
         public boolean mouseDown(Container container, Mouse.Button button, int x, int y) {
             Display display = (Display)container;
             Component descendant = display.getDescendantAt(x, y);
+            Window window = descendant.getWindow();
 
             if (!menuPopup.isAncestor(descendant)
-                && !menuPopup.isOwner(descendant.getWindow())
+                && (window == null
+                    || !menuPopup.isOwner(window))
                 && descendant != MenuBarItemSkin.this.getComponent()) {
                 menuPopup.close();
             }
@@ -109,7 +111,8 @@
             Window window = (Window)display.getComponentAt(x, y);
 
             if (window != menuPopup
-                && !menuPopup.isOwner(window)) {
+                && (window == null
+                    || !menuPopup.isOwner(window))) {
                 consumed = true;
             }
 

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuButtonSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuButtonSkin.java?rev=758005&r1=758004&r2=758005&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuButtonSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuButtonSkin.java Tue Mar 24 20:34:51 2009
@@ -85,9 +85,11 @@
         public boolean mouseDown(Container container, Mouse.Button button, int x, int y) {
             Display display = (Display)container;
             Component descendant = display.getDescendantAt(x, y);
+            Window window = descendant.getWindow();
 
             if (!menuPopup.isAncestor(descendant)
-                && !menuPopup.isOwner(descendant.getWindow())
+                && (window == null
+                    || !menuPopup.isOwner(window))
                 && descendant != MenuButtonSkin.this.getComponent()) {
                 menuPopup.close();
             }
@@ -107,7 +109,8 @@
             Window window = (Window)display.getComponentAt(x, y);
 
             if (window != menuPopup
-                && !menuPopup.isOwner(window)) {
+                && (window == null
+                    || !menuPopup.isOwner(window))) {
                 consumed = true;
             }
 

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuItemSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuItemSkin.java?rev=758005&r1=758004&r2=758005&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuItemSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MenuItemSkin.java Tue Mar 24 20:34:51 2009
@@ -84,9 +84,11 @@
         public boolean mouseDown(Container container, Mouse.Button button, int x, int y) {
             Display display = (Display)container;
             Component descendant = display.getDescendantAt(x, y);
+            Window window = descendant.getWindow();
 
             if (!menuPopup.isAncestor(descendant)
-                && !menuPopup.isOwner(descendant.getWindow())
+                && (window == null
+                    || !menuPopup.isOwner(window))
                 && descendant != MenuItemSkin.this.getComponent()) {
                 menuPopup.close();
             }
@@ -106,7 +108,8 @@
             Window window = (Window)display.getComponentAt(x, y);
 
             if (window != menuPopup
-                && !menuPopup.isOwner(window)) {
+                && (window == null
+                    || !menuPopup.isOwner(window))) {
                 consumed = true;
             }