void CharCreateDialog::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "create")
    {
        if (getName().length() >= 4)
        {
            // Attempt to create the character
            mCreateButton->setEnabled(false);

            std::vector<int> atts;
//            for (unsigned i = 0; i < mAttributeSlider.size(); i++)
//            {
//                atts.push_back((int) mAttributeSlider[i]->getValue());
//            }
            atts.push_back(9);
            atts.push_back(9);
            atts.push_back(9);
            atts.push_back(1);
            atts.push_back(1);
            atts.push_back(1);
            Net::getCharHandler()->newCharacter(getName(), mSlot,
                                                mFemale->isSelected(), mHairStyle,
                                                mHairColor, atts);
        }
        else
        {
            new OkDialog(_("Error"),
                         _("Your name needs to be at least 4 characters."),
                         this);
        }
    }
    else if (event.getId() == "cancel")
        scheduleDelete();
    else if (event.getId() == "nextcolor")
    {
        mHairColor++;
        updateHair();
    }
    else if (event.getId() == "prevcolor")
    {
        mHairColor--;
        updateHair();
    }
    else if (event.getId() == "nextstyle")
    {
        mHairStyle++;
        updateHair();
    }
    else if (event.getId() == "prevstyle")
    {
        mHairStyle--;
        updateHair();
    }
    else if (event.getId() == "statslider") {
        updateSliders();
    }
    else if (event.getId() == "gender") {
        if (mMale->isSelected()) {
            mPlayer->setGender(GENDER_MALE);
        } else {
            mPlayer->setGender(GENDER_FEMALE);
        }
    }
}
void TradeWindow::action(const gcn::ActionEvent &event)
{
    if (!inventoryWindow)
        return;

    Item *const item = inventoryWindow->getSelectedItem();
    const std::string &eventId = event.getId();

    if (eventId == "add")
    {
        if (mStatus != PREPARING)
            return;

        if (!inventoryWindow->isWindowVisible())
        {
            inventoryWindow->setVisible(true);
            return;
        }

        if (!item)
            return;

        if (mMyInventory->getFreeSlot() == -1)
            return;


        if (!checkItem(item))
            return;

        // Choose amount of items to trade
        ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, this, item);

        setStatus(PREPARING);
    }
    else if (eventId == "cancel")
    {
        setVisible(false);
        reset();
        PlayerInfo::setTrading(false);
        Net::getTradeHandler()->cancel();
    }
    else if (eventId == "ok")
    {
        mMoneyField->setEnabled(false);
        mAddButton->setEnabled(false);
        mMoneyChangeButton->setEnabled(false);
        receivedOk(true);
        setStatus(PROPOSING);
        Net::getTradeHandler()->confirm();
    }
    else if (eventId == "trade")
    {
        receivedOk(true);
        setStatus(ACCEPTED);
        Net::getTradeHandler()->finish();
    }
    else if (eventId == "money")
    {
        if (mStatus != PREPARING)
            return;

        int v = atoi(mMoneyField->getText().c_str());
        const int curMoney = PlayerInfo::getAttribute(PlayerInfo::MONEY);
        if (v > curMoney)
        {
            if (localChatTab)
            {
                // TRANSLATORS: trade error
                localChatTab->chatLog(_("You don't have enough money."),
                                      BY_SERVER);
            }
            v = curMoney;
        }
        Net::getTradeHandler()->setMoney(v);
        mMoneyField->setText(strprintf("%d", v));
    }
}
void SellDialog::action(const gcn::ActionEvent &event)
{
    const std::string &eventId = event.getId();

    if (eventId == "quit")
    {
        close();
        return;
    }

    const int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid item selection
    if (selectedItem == -1
        || selectedItem >= mShopItems->getNumberOfElements())
    {
        return;
    }

    if (eventId == "slider")
    {
        mAmountItems = static_cast<int>(mSlider->getValue());
        updateButtonsAndLabels();
    }
    else if (eventId == "inc" && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "dec" && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "max")
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if ((eventId == "presell" || eventId == "sell" || eventId == "yes")
             && mAmountItems > 0 && mAmountItems <= mMaxItems)
    {
        if (mNpcId != -1)
        {
            ShopItem *const item = mShopItems->at(selectedItem);
            if (PlayerInfo::isItemProtected(item->getId()))
                return;
            if (eventId == "presell")
            {
                const ItemInfo &info = ItemDB::get(item->getId());
                if (info.isProtected())
                {
                    ConfirmDialog *const dialog = new ConfirmDialog(
                        // TRANSLATORS: sell confirmation header
                        _("sell item"),
                        // TRANSLATORS: sell confirmation message
                        strprintf(_("Do you really want to sell %s?"),
                        info.getName().c_str()), SOUND_REQUEST, false, true);
                    dialog->postInit();
                    dialog->addActionListener(this);
                    return;
                }
            }
            // Attempt sell
            mPlayerMoney +=
                mAmountItems * mShopItems->at(selectedItem)->getPrice();
            mMaxItems -= mAmountItems;
            while (mAmountItems > 0)
            {
#ifdef MANASERV_SUPPORT
                // This order is important, item->getCurrentInvIndex() would
                // return the inventory index of the next Duplicate otherwise.
                int itemIndex = item->getCurrentInvIndex();
                const int sellCount = item->sellCurrentDuplicate(mAmountItems);
                // For Manaserv, the Item id is to be given as index.
                if ((Net::getNetworkType() == ServerInfo::MANASERV))
                    itemIndex = item->getId();
#else
                // This order is important, item->getCurrentInvIndex() would
                // return the inventory index of the next Duplicate otherwise.
                const int itemIndex = item->getCurrentInvIndex();
                const int sellCount = item->sellCurrentDuplicate(mAmountItems);
#endif
                Net::getNpcHandler()->sellItem(mNpcId, itemIndex, sellCount);
                mAmountItems -= sellCount;
            }

            mPlayerMoney +=
                mAmountItems * mShopItems->at(selectedItem)->getPrice();
            mAmountItems = 1;
            mSlider->setValue(0);

            if (mMaxItems)
            {
                updateButtonsAndLabels();
            }
            else
            {
                // All were sold
                mShopItemList->setSelected(-1);
                delete mShopItems->at(selectedItem);
                mShopItems->erase(selectedItem);

                gcn::Rectangle scroll;
                scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
                scroll.height = mShopItemList->getRowHeight();
                mShopItemList->showPart(scroll);
            }
        }
        else
        {
            ShopItem *const item = mShopItems->at(selectedItem);
            Net::getBuySellHandler()->sendSellRequest(mNick,
                    item, mAmountItems);

            if (tradeWindow)
                tradeWindow->addAutoItem(mNick, item, mAmountItems);
        }
    }
}