private void handleScreenCloseCancelled(SpoutPlayer player, ScreenCloseEvent e, boolean update) {
   this.action = (byte) ScreenAction.Close.getId();
   PopupScreen screen = player.getMainScreen().getActivePopup();
   if (screen != null) {
     if (update) {
       screen.onScreenClose(e);
     }
     if (!e.isCancelled() && ScreenType.getType(this.screen) == ScreenType.CUSTOM_SCREEN) {
       handleScreenClose(player, e, false);
       return;
     }
     screen.setDirty(true);
     player.sendImmediatePacket(new PacketWidget(screen, screen.getId()));
   }
 }
 private void handleScreenClose(SpoutPlayer player, ScreenCloseEvent e, boolean update) {
   PopupScreen p = player.getMainScreen().getActivePopup();
   if (update && p != null) {
     p.onScreenClose(e);
   }
   if (e.isCancelled()) {
     handleScreenCloseCancelled(player, e, false);
     return;
   }
   player.getMainScreen().closePopup();
   if (player.getItemOnCursor() != null && p != null) {
     p.handleItemOnCursor(player.getItemOnCursor());
     player.setItemOnCursor(null);
   }
 }
 @Override
 public void run(int playerId) {
   SpoutPlayer player = SpoutManager.getPlayerFromId(playerId);
   ScreenEvent event;
   switch (ScreenAction.getScreenActionFromId(action)) {
     case Close:
       event =
           new ScreenCloseEvent(
               player, player.getMainScreen().getActivePopup(), ScreenType.getType(this.screen));
       Bukkit.getServer().getPluginManager().callEvent(event);
       if (event.isCancelled()) {
         handleScreenCloseCancelled(player, (ScreenCloseEvent) event, true);
       } else if (ScreenType.getType(this.screen) == ScreenType.CUSTOM_SCREEN) {
         handleScreenClose(player, (ScreenCloseEvent) event, true);
       }
       if (!event.isCancelled()) {
         player.openScreen(ScreenType.GAME_SCREEN, false);
       }
       break;
     case Open:
       event =
           new ScreenOpenEvent(
               player, player.getMainScreen().getActivePopup(), ScreenType.getType(this.screen));
       Bukkit.getServer().getPluginManager().callEvent(event);
       if (event.isCancelled()) {
         PopupScreen screen = player.getMainScreen().getActivePopup();
         if (screen != null) {
           screen.setDirty(true);
           player.sendImmediatePacket(new PacketWidget(screen, screen.getId()));
         }
       } else {
         player.openScreen(ScreenType.getType(this.screen), false);
       }
       break;
   }
 }
  private void attachColorLabels(PopupScreen popup) {

    int y = 5;

    for (ChatColor color : ChatColor.values()) {

      GenericLabel label = new GenericLabel();
      label.setText(
          color
              + color.name().replace('_', ' ').toLowerCase()
              + ChatColor.RESET
              + ": &"
              + color.getChar());
      label.setHeight(25).setWidth(80).setX(300).setY(y);
      popup.attachWidget(plugin, label);
      y += 10;
    }
  }
  public void handleSpoutEditing(Player player, Sign sign) {

    if (!plugin.getSelectionHandler().checkForSelectionRights(player, sign.getBlock())) {

      QSUtil.tell(player, "You don't own this sign.");
      return;
    }

    if (!plugin.getBlackList().allows(sign, player)) {

      QSUtil.tell(player, "You cannot edit this sign: its contents are blacklisted.");
      return;
    }

    SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);

    PopupScreen popup = new GenericPopup();

    int y = 30;
    UUID[] widgets = new UUID[5];

    for (int i = 0; i < 4; i++) {

      GenericTextField textField = new GenericTextField();

      String line0 = sign.getLine(i).replaceAll("\u00A7([0-9a-fA-Fk-oK-OrR])", "&$1");
      textField
          .setText(line0)
          .setCursorPosition(line0.length() - 1)
          .setFieldColor(new Color(0, 0, 0, 1.0F));
      textField.setBorderColor(new Color(1.0F, 1.0F, 1.0F, 1.0F));
      textField.setMaximumCharacters(15).setHeight(20).setWidth(100);
      textField.setX(170).setY(y);
      textField.setAnchor(WidgetAnchor.SCALE);
      popup.attachWidget(plugin, textField);
      y += 30;
      widgets[i] = textField.getId();
    }

    GenericButton button = new GenericButton("Done");

    button.setColor(new Color(1.0F, 1.0F, 1.0F, 1.0F));
    button.setHoverColor(new Color(1.0F, 1.0F, 0, 1.0F));
    button.setX(170).setY(150);
    button.setWidth(100).setHeight(20);
    widgets[4] = button.getId();

    popup.attachWidget(plugin, button);

    attachColorLabels(popup);

    popup.setTransparent(true);

    spoutPlayer.getMainScreen().attachPopupScreen(popup);

    SpoutEditSession session = (SpoutEditSession) plugin.getSession(player);

    session.setPopup(popup);
    session.addSign(sign);
    session.setWidgets(widgets);
  }