public void applyTo(Token token) {
   for (com.t3.model.campaign.TokenProperty property : getPropertyList()) {
     Object value = getPropertyMap().get(property.getName());
     if (property.getDefaultValue() != null && property.getDefaultValue().equals(value)) {
       token.setProperty(property.getName(), null); // Clear original value
       continue;
     }
     token.setProperty(property.getName(), value);
   }
 }
 public void setSizeCombo(Token token) {
   JComboBox size = getSizeCombo();
   Grid grid = TabletopTool.getFrame().getCurrentZoneRenderer().getZone().getGrid();
   DefaultComboBoxModel model = new DefaultComboBoxModel(grid.getFootprints().toArray());
   model.insertElementAt(token.getLayer() == Layer.TOKEN ? "Native Size" : "Free Size", 0);
   size.setModel(model);
   if (token.isSnapToScale()) {
     size.setSelectedItem(token.getFootprint(grid));
   } else {
     size.setSelectedIndex(0);
   }
 }
 public SpeechTableModel(Token token) {
   List<Association<String, String>> rowList = new ArrayList<Association<String, String>>();
   for (String speechName : token.getSpeechNames()) {
     rowList.add(new Association<String, String>(speechName, token.getSpeech(speechName)));
   }
   Collections.sort(
       rowList,
       new Comparator<Association<String, String>>() {
         @Override
         public int compare(Association<String, String> o1, Association<String, String> o2) {
           return o1.getLeft().compareToIgnoreCase(o2.getLeft());
         }
       });
   init(rowList);
 }
    private Map<String, Object> getPropertyMap() {
      Token token = getModel();

      if (propertyMap == null) {
        propertyMap = new HashMap<String, Object>();

        List<com.t3.model.campaign.TokenProperty> propertyList = getPropertyList();
        for (com.t3.model.campaign.TokenProperty property : propertyList) {
          Object value = token.getProperty(property.getName());
          if (value == null) {
            value = property.getDefaultValue();
          }
          propertyMap.put(property.getName(), value);
        }
      }
      return propertyMap;
    }
  @Override
  public boolean commit() {
    Token token = getModel();

    if (getNameField().getText().equals("")) {
      TabletopTool.showError("msg.error.emptyTokenName");
      return false;
    }
    if (getSpeechTable().isEditing()) {
      getSpeechTable().getCellEditor().stopCellEditing();
    }
    if (getPropertyTable().isEditing()) {
      getPropertyTable().getCellEditor().stopCellEditing();
    }
    // Commit the changes to the token properties
    if (!super.commit()) {
      return false;
    }
    // SIZE
    token.setSnapToScale(getSizeCombo().getSelectedIndex() != 0);
    if (getSizeCombo().getSelectedIndex() > 0) {
      Grid grid = TabletopTool.getFrame().getCurrentZoneRenderer().getZone().getGrid();
      token.setFootprint(grid, (TokenFootprint) getSizeCombo().getSelectedItem());
    }
    // Other
    token.setPropertyType((String) getPropertyTypeCombo().getSelectedItem());
    token.setSightType((String) getSightTypeCombo().getSelectedItem());

    // Get the states
    Component[] stateComponents = getStatesPanel().getComponents();
    Component barPanel = null;
    for (int j = 0; j < stateComponents.length; j++) {
      if ("bar".equals(stateComponents[j].getName())) {
        barPanel = stateComponents[j];
        continue;
      }
      Component[] components = ((Container) stateComponents[j]).getComponents();
      for (int i = 0; i < components.length; i++) {
        JCheckBox cb = (JCheckBox) components[i];
        String state = cb.getText();
        token.setState(state, cb.isSelected());
      }
    } // endfor

    // BARS
    if (barPanel != null) {
      Component[] bars = ((Container) barPanel).getComponents();
      for (int i = 0; i < bars.length; i += 2) {
        JCheckBox cb = (JCheckBox) ((Container) bars[i]).getComponent(1);
        JSlider bar = (JSlider) bars[i + 1];
        Float value = cb.isSelected() ? null : new Float(bar.getValue() / 100f);
        token.setBar(bar.getName(), value);
      }
    }
    // Ownership
    // If the token is owned by all and we are a player don't alter the ownership list.
    if (TabletopTool.getPlayer().isGM() || !token.isOwnedByAll()) {
      token.clearAllOwners();

      for (int i = 0; i < getOwnerList().getModel().getSize(); i++) {
        DefaultSelectable selectable =
            (DefaultSelectable) getOwnerList().getModel().getElementAt(i);
        if (selectable.isSelected()) {
          token.addOwner((String) selectable.getObject());
        }
      }
      // If we are not a GM and the only non GM owner make sure we can't
      // take our selves off of the owners list
      if (!TabletopTool.getPlayer().isGM()) {
        boolean hasPlayer = false;
        Set<String> owners = token.getOwners();
        if (owners != null) {
          Iterator<Player> playerIter = TabletopTool.getPlayerList().iterator();
          while (playerIter.hasNext()) {
            Player pl = playerIter.next();
            if (!pl.isGM() && owners.contains(pl.getName())) {
              hasPlayer = true;
            }
          }
        }
        if (!hasPlayer) {
          token.addOwner(TabletopTool.getPlayer().getName());
        }
      }
    }
    // SHAPE
    token.setShape((Token.TokenShape) getShapeCombo().getSelectedItem());

    // Macros
    token.setSpeechMap(((KeyValueTableModel) getSpeechTable().getModel()).getMap());

    // Properties
    ((TokenPropertyTableModel) getPropertyTable().getModel()).applyTo(token);

    // Charsheet
    if (getCharSheetPanel().getImageId() != null) {
      T3Util.uploadAsset(AssetManager.getAsset(getCharSheetPanel().getImageId()));
    }
    token.setCharsheetImage(getCharSheetPanel().getImageId());

    // IMAGE
    if (!token.getImageAssetId().equals(getTokenIconPanel().getImageId())) {
      BufferedImage image = ImageManager.getImageAndWait(getTokenIconPanel().getImageId());
      T3Util.uploadAsset(AssetManager.getAsset(getTokenIconPanel().getImageId()));
      token.setImageAsset(null, getTokenIconPanel().getImageId()); // Default image for now
      token.setWidth(image.getWidth(null));
      token.setHeight(image.getHeight(null));
    }
    // PORTRAIT
    if (getPortraitPanel().getImageId() != null) {
      // Make sure the server has the image
      if (!TabletopTool.getCampaign().containsAsset(getPortraitPanel().getImageId())) {
        TabletopTool.serverCommand()
            .putAsset(AssetManager.getAsset(getPortraitPanel().getImageId()));
      }
    }
    token.setPortraitImage(getPortraitPanel().getImageId());

    // LAYOUT
    token.setSizeScale(getTokenLayoutPanel().getSizeScale());
    token.setAnchor(getTokenLayoutPanel().getAnchorX(), getTokenLayoutPanel().getAnchorY());

    // OTHER
    tokenSaved = true;

    // Character Sheet
    //		Map<String, Object> properties = controller.getData();
    //		for (String prop : token.getPropertyNames())
    //			token.setProperty(prop, properties.get(prop));

    // Update UI
    TabletopTool.getFrame().updateTokenTree();
    TabletopTool.getFrame().resetTokenPanels();

    return true;
  }
  @Override
  public void bind(final Token token) {
    // ICON
    getTokenIconPanel().setImageId(token.getImageAssetId());

    // PROPERTIES
    updatePropertyTypeCombo();
    updatePropertiesTable(token.getPropertyType());

    // SIGHT
    updateSightTypeCombo();

    // STATES
    Component barPanel = null;
    updateStatesPanel();
    Component[] statePanels = getStatesPanel().getComponents();
    for (int j = 0; j < statePanels.length; j++) {
      if ("bar".equals(statePanels[j].getName())) {
        barPanel = statePanels[j];
        continue;
      }
      Component[] states = ((Container) statePanels[j]).getComponents();
      for (int i = 0; i < states.length; i++) {
        JCheckBox state = (JCheckBox) states[i];
        state.setSelected(token.hasState(state.getText()));
      }
    }

    // BARS
    if (barPanel != null) {
      Component[] bars = ((Container) barPanel).getComponents();
      for (int i = 0; i < bars.length; i += 2) {
        JCheckBox cb = (JCheckBox) ((Container) bars[i]).getComponent(1);
        JSlider bar = (JSlider) bars[i + 1];
        if (token.getBar(bar.getName()) == null) {
          cb.setSelected(true);
          bar.setEnabled(false);
          bar.setValue(100);
        } else {
          cb.setSelected(false);
          bar.setEnabled(true);
          bar.setValue((int) (token.getBar(bar.getName()) * 100));
        }
      }
    }

    // OWNER LIST
    EventQueue.invokeLater(
        new Runnable() {
          @Override
          public void run() {
            getOwnerList().setModel(new OwnerListModel());
          }
        });

    // SPEECH TABLE
    EventQueue.invokeLater(
        new Runnable() {
          @Override
          public void run() {
            getSpeechTable().setModel(new SpeechTableModel(token));
          }
        });

    //		Player player = TabletopTool.getPlayer();
    //		boolean editable = player.isGM() ||
    // !TabletopTool.getServerPolicy().useStrictTokenManagement() ||
    // token.isOwner(player.getName());
    //		getAllPlayersCheckBox().setSelected(token.isOwnedByAll());

    // OTHER
    getShapeCombo().setSelectedItem(token.getShape());
    setSizeCombo(token);

    getPropertyTypeCombo().setSelectedItem(token.getPropertyType());
    getSightTypeCombo()
        .setSelectedItem(
            token.getSightType() != null
                ? token.getSightType()
                : TabletopTool.getCampaign().getCampaignProperties().getDefaultSightType());
    getCharSheetPanel().setImageId(token.getCharsheetImage());
    getPortraitPanel().setImageId(token.getPortraitImage());
    getTokenLayoutPanel().setToken(token);

    // we will disable the Owner only visible check box if the token is not
    // visible to players to signify the relationship
    ActionListener tokenVisibleActionListener =
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            getVisibleOnlyToOwnerCheckBox().setEnabled(selected);
            getVisibleOnlyToOwnerLabel().setEnabled(selected);
          }
        };
    getVisibleCheckBox().addActionListener(tokenVisibleActionListener);

    // Character Sheets
    //		controller = null;
    //		String form = TabletopTool.getCampaign().getCharacterSheets().get(token.getPropertyType());
    //		if (form == null)
    //			return;
    //		URL formUrl = getClass().getClassLoader().getResource(form);
    //		if (formUrl == null)
    //			return;
    //		controller = new CharSheetController(formUrl, null);
    //		HashMap<String, Object> properties = new HashMap<String, Object>();
    //		for (String prop : token.getPropertyNames())
    //			properties.put(prop, token.getProperty(prop));
    //		controller.setData(properties);
    //		controller.getPanel().setName("characterSheet");
    //		replaceComponent("sheetPanel", "characterSheet", controller.getPanel());

    super.bind(token);
  }