Package org.waveprotocol.wave.client.wavepanel.impl.edit

Source Code of org.waveprotocol.wave.client.wavepanel.impl.edit.ParticipantController

/**
* 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.edit;

import javax.annotation.Nullable;

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

import org.waveprotocol.wave.client.account.Profile;
import org.waveprotocol.wave.client.account.ProfileManager;
import org.waveprotocol.wave.client.common.safehtml.EscapeUtils;
import org.waveprotocol.wave.client.wavepanel.WavePanel;
import org.waveprotocol.wave.client.wavepanel.event.EventHandlerRegistry;
import org.waveprotocol.wave.client.wavepanel.event.WaveClickHandler;
import org.waveprotocol.wave.client.wavepanel.view.ParticipantView;
import org.waveprotocol.wave.client.wavepanel.view.ParticipantsView;
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.ModelAsViewProvider;
import org.waveprotocol.wave.client.wavepanel.view.dom.full.TypeCodes;
import org.waveprotocol.wave.client.widget.profile.ProfilePopupPresenter;
import org.waveprotocol.wave.client.widget.profile.ProfilePopupView;
import org.waveprotocol.wave.model.conversation.Conversation;
import org.waveprotocol.wave.model.util.Pair;
import org.waveprotocol.wave.model.util.Preconditions;
import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
import org.waveprotocol.wave.model.wave.ParticipantId;

/**
* Installs the add/remove participant controls.
*
*/
public final class ParticipantController {
  private final DomAsViewProvider views;
  private final ModelAsViewProvider models;
  private final ProfileManager profiles;
  private final String localDomain;

  /**
   * @param localDomain nullable. if provided, automatic suffixing will occur.
   */
  ParticipantController(
      DomAsViewProvider views, ModelAsViewProvider models, ProfileManager profiles,
      String localDomain) {
    this.views = views;
    this.models = models;
    this.profiles = profiles;
    this.localDomain = localDomain;
  }

  /**
   * Builds and installs the participant control feature.
   */
  public static void install(WavePanel panel, ModelAsViewProvider models, ProfileManager profiles,
      String localDomain) {
    ParticipantController controller =
        new ParticipantController(panel.getViewProvider(), models, profiles, localDomain);
    controller.install(panel.getHandlers());
  }

  private void install(EventHandlerRegistry handlers) {
    handlers.registerClickHandler(TypeCodes.kind(Type.ADD_PARTICIPANT), new WaveClickHandler() {
      @Override
      public boolean onClick(ClickEvent event, Element context) {
        handleAddButtonClicked(context);
        return true;
      }
    });
    handlers.registerClickHandler(TypeCodes.kind(Type.PARTICIPANT), new WaveClickHandler() {
      @Override
      public boolean onClick(ClickEvent event, Element context) {
        handleParticipantClicked(context);
        return true;
      }
    });
  }

  /**
   * Constructs a list of {@link ParticipantId} with the supplied string with comma
   * separated participant addresses. The method will only succeed if all addresses
   * is valid.
   *
   * @param localDomain if provided, automatic suffixing will occur.
   * @param addresses string with comma separated participant addresses
   * @return the array of {@link ParticipantId} instances constructed using the given
   *         addresses string
   * @throws InvalidParticipantAddress if at least one of the addresses failed validation.
   */
  public static ParticipantId[] buildParticipantList(
      @Nullable String localDomain, String addresses) throws InvalidParticipantAddress {
    Preconditions.checkNotNull(addresses, "Expected non-null address");

    String[] addressList = addresses.split(",");
    ParticipantId[] participants = new ParticipantId[addressList.length];

    for (int i = 0; i < addressList.length; i++) {
      String address = addressList[i].trim();

      if (localDomain != null) {
        if (!address.isEmpty() && address.indexOf("@") == -1) {
          // If no domain was specified, assume that the participant is from the local domain.
          address = address + "@" + localDomain;
        } else if (address.equals("@")) {
          // "@" is a shortcut for the shared domain participant.
          address = address + localDomain;
        }
      }

      // Will throw InvalidParticipantAddress if address is not valid
      participants[i] = ParticipantId.of(address);
    }
    return participants;
  }

  /**
   * Shows an add-participant popup.
   */
  private void handleAddButtonClicked(Element context) {
    String addressString = Window.prompt("Add a participant(s) (separate with comma ','): ", "");
    if (addressString == null) {
      return;
    }

    ParticipantId[] participants;

    try {
      participants = buildParticipantList(localDomain, addressString);
    } catch (InvalidParticipantAddress e) {
      Window.alert(e.getMessage());
      return;
    }

    ParticipantsView participantsUi = views.fromAddButton(context);
    Conversation conversation = models.getParticipants(participantsUi);
    for (ParticipantId participant : participants) {
      conversation.addParticipant(participant);
    }
  }

  /**
   * Shows a participation popup for the clicked participant.
   */
  private void handleParticipantClicked(Element context) {
    ParticipantView participantView = views.asParticipant(context);
    final Pair<Conversation, ParticipantId> participation = models.getParticipant(participantView);
    Profile profile = profiles.getProfile(participation.second);

    // Summon a popup view from a participant, and attach profile-popup logic to
    // it.
    final ProfilePopupView profileView = participantView.showParticipation();
    ProfilePopupPresenter profileUi = ProfilePopupPresenter.create(profile, profileView, profiles);
    profileUi.addControl(EscapeUtils.fromSafeConstant("Remove"), new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        participation.first.removeParticipant(participation.second);
        // The presenter is configured to destroy itself on view hide.
        profileView.hide();
      }
    });
    profileUi.show();
  }
}
TOP

Related Classes of org.waveprotocol.wave.client.wavepanel.impl.edit.ParticipantController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.