/**
 * Copyright 2010 Google 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 org.waveprotocol.wave.client.wavepanel.impl.menu;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.Window;

import org.waveprotocol.wave.client.wavepanel.WavePanel;
import org.waveprotocol.wave.client.wavepanel.event.WaveClickHandler;
import org.waveprotocol.wave.client.wavepanel.impl.edit.Actions;
import org.waveprotocol.wave.client.wavepanel.view.BlipMenuItemView;
import org.waveprotocol.wave.client.wavepanel.view.View.Type;
import org.waveprotocol.wave.client.wavepanel.view.dom.DomAsViewProvider;
import org.waveprotocol.wave.client.wavepanel.view.dom.full.TypeCodes;

/**
 * Interprets user gestures on blip menu items.
 *
 */
public final class MenuController implements WaveClickHandler {
  private final DomAsViewProvider panel;
  private final Actions actions;

  /**
   * Creates a manu handler.
   *
   * @param actions
   * @param panel
   */
  private MenuController(Actions actions, DomAsViewProvider panel) {
    this.actions = actions;
    this.panel = panel;
  }

  /**
   * Installs the focus-frame feature in a wave panel.
   */
  public static void install(Actions handler, WavePanel panel) {
    MenuController controller = new MenuController(handler, panel.getViewProvider());
    panel.getHandlers().registerClickHandler(TypeCodes.kind(Type.MENU_ITEM), controller);
  }

  @Override
  public boolean onClick(ClickEvent event, Element context) {
    if (event.getNativeButton() != NativeEvent.BUTTON_LEFT) {
      return false;
    }
    BlipMenuItemView item = panel.asBlipMenuItem(context);
    switch (item.getOption()) {
      case EDIT:
        actions.startEditing(item.getParent().getParent());
        break;
      case EDIT_DONE:
        actions.stopEditing();
        break;
      case REPLY:
        actions.reply(item.getParent().getParent());
        break;
      case DELETE:
        // We delete the blip without confirmation if shift key is pressed
        if (event.getNativeEvent().getShiftKey()
            || Window.confirm("Please confirm the deletion of this message"))
          actions.delete(item.getParent().getParent());
        break;
      case LINK:
        actions.popupLink(item.getParent().getParent());
        break;
      default:
        throw new AssertionError();
    }
    event.preventDefault();
    return true;
  }
}

@@ -779,21 +779,25 @@
 .client.
-Click
+MouseDown
 Event;%0Ai
@@ -947,29 +947,33 @@
 l.event.Wave
-Click
+MouseDown
 Handler;%0Aimp
@@ -1419,21 +1419,25 @@
 nts Wave
-Click
+MouseDown
 Handler 
@@ -1973,21 +1973,25 @@
 register
-Click
+MouseDown
 Handler(
@@ -2075,19 +2075,27 @@
 n on
-Click(Click
+MouseDown(MouseDown
 Even

Revert "Blip menu handles clicks instead of mouse down. By vjrj."