/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This 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 software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.gwt.wysiwyg.client.plugin.table.feature;

import org.xwiki.gwt.user.client.ui.rta.cmd.Command;
import org.xwiki.gwt.wysiwyg.client.Strings;
import org.xwiki.gwt.wysiwyg.client.plugin.table.TablePlugin;
import org.xwiki.gwt.wysiwyg.client.plugin.table.util.TableUtils;

import com.google.gwt.dom.client.TableElement;

/**
 * Feature allowing to remove a table from the editor. It is disabled when the caret is positioned outside of a table
 * element.
 * 
 * @version $Id$
 */
public class DeleteTable extends AbstractTableFeature
{
    /**
     * Feature name.
     */
    public static final String NAME = "deletetable";

    /**
     * Initialize the feature. Table features needs to be aware of the plug-in (here the ClickListener) since they hold
     * their own PushButton.
     * 
     * @param plugin table plug-in.
     */
    public DeleteTable(TablePlugin plugin)
    {
        super(NAME, new Command(NAME), Strings.INSTANCE.deleteTable(), plugin);
    }

    /**
     * {@inheritDoc}
     * 
     * @see AbstractTableFeature#execute(String)
     */
    public boolean execute(String parameter)
    {
        TableElement table =
            TableUtils.getInstance().getTable(TableUtils.getInstance().getCaretNode(rta.getDocument()));
        table.getParentNode().removeChild(table);
        // FIXME : the table editor rulers are still visible, find a way to clean midas state after deletion.
        return true;
    }

    /**
     * {@inheritDoc}
     * 
     * @see AbstractTableFeature#isEnabled()
     */
    public boolean isEnabled()
    {
        return super.isEnabled()
            && TableUtils.getInstance().getTable(TableUtils.getInstance().getCaretNode(rta.getDocument())) != null;
    }
}

@@ -972,20 +972,19 @@
 gwt.
-user
+dom
 .client.
 ui.r
@@ -983,26 +983,54 @@
 ent.
-ui.rta.cmd.Command
+Element;%0Aimport org.xwiki.gwt.dom.client.Range
 ;%0Aim
@@ -1040,39 +1040,35 @@
 t org.xwiki.gwt.
-wysiwyg
+dom
 .client.Strings;
@@ -1060,22 +1060,77 @@
 client.S
-trings
+election;%0Aimport org.xwiki.gwt.user.client.ui.rta.cmd.Command
 ;%0Aimport
@@ -1163,32 +1163,15 @@
 ent.
-plugin.table.TablePlugin
+Strings
 ;%0Aim
@@ -1221,23 +1221,19 @@
 ble.
-util.TableUtils
+TablePlugin
 ;%0A%0Ai
@@ -1264,28 +1264,20 @@
 .client.
-TableElement
+Node
 ;%0A%0A/**%0A 
@@ -1577,16 +1577,125 @@
 able%22;%0A%0A
+    /**%0A     * The name of the table HTML element.%0A     */%0A    private static final String TABLE = %22table%22;%0A%0A
     /**%0A
@@ -2188,46 +2188,88 @@
     
-TableElement table =%0A            Table
+Selection selection = rta.getDocument().getSelection();%0A        Node table = dom
 Util
@@ -2277,116 +2277,242 @@
 .get
-Instance().getTable(TableUtils.getInstance().getCaretNode(rta.getDocument()));%0A        table.getParentNode()
+FirstAncestor(selection.getRangeAt(0).getCommonAncestorContainer(), TABLE);%0A        Node previousLeaf = domUtils.getPreviousLeaf(table);%0A        Node parent = table.getParentNode();%0A%0A        selection.removeAllRanges();%0A        parent
 .rem
@@ -2528,24 +2528,25 @@
 table);%0A
+%0A
         
 // FIXME
@@ -2541,109 +2541,470 @@
     
-// FIXME : the table editor rulers are still visible, find a way to clean midas state after d
+Range range = rta.getDocument().createRange();%0A        if (previousLeaf != null) %7B%0A            if (previousLeaf.getNodeType() == Node.ELEMENT_NODE && Element.as(previousLeaf).canHaveChildren()) %7B%0A                range.setStart(previousLeaf, 0);%0A            %7D else %7B%0A                range.setStartAfter(previousLeaf);%0A            %7D%0A        %7D else %7B%0A            range.setStart(parent, 0);%0A        %7D%0A        range.collapse(true);%0A        s
 ele
+c
 tion.
+addRange(range);%0A
 %0A   
@@ -3203,21 +3203,19 @@
      && 
-Table
+dom
 Utils.ge
@@ -3219,84 +3219,120 @@
 .get
-Instance().getTable(TableUtils.getInstance().getCaretNode(rta.getDocument())
+FirstAncestor(rta.getDocument().getSelection().getRangeAt(0).getCommonAncestorContainer(),%0A                TABLE
 ) !=

Add support for Safari browser * Delete table blurs the rich text area