/** * Find a new location from a stream attribute. This is necessary because <code>Location</code> is * an interface. * * @param game The <code>Game</code> to look in. * @param attributeName The attribute to check. * @param make If true, try to make the location if it is not found. * @return The <code>Location</code> found. */ public Location getLocationAttribute(Game game, String attributeName, boolean make) throws XMLStreamException { if (attributeName == null) return null; final String attrib = // @compat 0.10.7 (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : // end @compat getAttribute(attributeName, (String) null); if (attrib != null) { FreeColObject fco = lookup(game, attrib); if (fco == null && make) { Class<? extends FreeColGameObject> c = game.getLocationClass(attrib); if (c != null) { fco = makeFreeColGameObject(game, attributeName, c, getReadScope() == ReadScope.SERVER); } } if (fco instanceof Location) return (Location) fco; logger.warning("Not a location: " + attrib); } return null; }
public void hyperlinkUpdate(HyperlinkEvent e) { HyperlinkEvent.EventType type = e.getEventType(); if (type == HyperlinkEvent.EventType.ACTIVATED) { String[] path = e.getURL().getPath().split("/"); if (FreeColObject.ID_ATTRIBUTE_TAG.equals(path[1])) { select(path[2]); } else if ("action".equals(path[1])) { getFreeColClient().getActionManager().getFreeColAction(path[2]).actionPerformed(null); } } }
/** * Get a FreeCol AI object from an attribute in a stream. * * @param aiMain The <code>AIMain</code> that contains the object. * @param attributeName The attribute name. * @param returnClass The <code>AIObject</code> type to expect. * @param defaultValue The default value. * @return The <code>AIObject</code> found, or the default value if not. */ public <T extends AIObject> T getAttribute( AIMain aiMain, String attributeName, Class<T> returnClass, T defaultValue) { final String attrib = // @compat 0.10.7 (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : // end @compat getAttribute(attributeName, (String) null); return (attrib == null) ? defaultValue : aiMain.getAIObject(attrib, returnClass); }
/** * Get a FreeColGameObjectType by identifier from a stream from a specification. * * @param spec The <code>Specification</code> to look in. * @param attributeName the name of the attribute identifying the <code>FreeColGameObjectType * </code>. * @param returnClass The expected class of the return value. * @param defaultValue A default value to return if the attributeName attribute is not present. * @return The <code>FreeColGameObjectType</code> found, or the <code>defaultValue</code>. */ public <T extends FreeColGameObjectType> T getType( Specification spec, String attributeName, Class<T> returnClass, T defaultValue) { final String attrib = // @compat 0.10.7 (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : // end @compat getAttribute(attributeName, (String) null); return (attrib == null) ? defaultValue : spec.getType(attrib, returnClass); }
// @compat 0.10.7 public <T extends FreeColGameObjectType> T getRole( Specification spec, String attributeName, Class<T> returnClass, T defaultValue) { String attrib = (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : getAttribute(attributeName, (String) null); if (attrib == null) { return defaultValue; } attrib = Role.fixRoleId(attrib); return spec.getType(attrib, returnClass); }
/** * Either get an existing <code>AIObject</code> from a stream attribute or create it if it does * not exist. * * <p>Use this routine when the object may not necessarily already be present in the game, but is * expected to be defined eventually. * * @param aiMain The <code>AIMain</code> that contains the object. * @param attributeName The attribute name. * @param returnClass The <code>AIObject</code> type to expect. * @param defaultValue The default value. * @exception XMLStreamException if there is problem reading the stream. * @return The <code>AIObject</code> found, or the default value if not. */ public <T extends AIObject> T makeAIObject( AIMain aiMain, String attributeName, Class<T> returnClass, T defaultValue, boolean required) throws XMLStreamException { final String id = // @compat 0.10.7 (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : // end @compat getAttribute(attributeName, (String) null); T ret = null; if (id == null) { if (required) { throw new XMLStreamException( "Missing " + attributeName + " for " + returnClass.getName() + ": " + currentTag()); } } else { ret = aiMain.getAIObject(id, returnClass); if (ret == null) { try { Constructor<T> c = returnClass.getConstructor(AIMain.class, String.class); ret = returnClass.cast(c.newInstance(aiMain, id)); if (required && ret == null) { throw new XMLStreamException( "Constructed null " + returnClass.getName() + " for " + id + ": " + currentTag()); } } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | XMLStreamException e) { if (required) { throw new XMLStreamException(e); } else { logger.log(Level.WARNING, "Failed to create AIObject: " + id, e); } } } } return ret; }
/** * Either get an existing <code>FreeColGameObject</code> from a stream attribute or create it if * it does not exist. * * <p>Use this routine when the object may not necessarily already be present in the game, but is * expected to be defined eventually. * * @param game The <code>Game</code> to look in. * @param attributeName The required attribute name. * @param returnClass The class of object. * @param required If true a null result should throw an exception. * @return The <code>FreeColGameObject</code> found or made, or null if the attribute was not * present. */ public <T extends FreeColGameObject> T makeFreeColGameObject( Game game, String attributeName, Class<T> returnClass, boolean required) throws XMLStreamException { final String id = // @compat 0.10.7 (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : // end @compat getAttribute(attributeName, (String) null); if (id == null) { if (required) { throw new XMLStreamException( "Missing " + attributeName + " for " + returnClass.getName() + ": " + currentTag()); } } else { FreeColObject fco = lookup(game, id); if (fco == null) { try { T ret = game.newInstance(returnClass, getReadScope() == ReadScope.SERVER); if (shouldIntern()) { ret.internId(id); } else { uninterned.put(id, ret); } return ret; } catch (IOException e) { if (required) { throw new XMLStreamException(e); } else { logger.log(Level.WARNING, "Failed to create FCGO: " + id, e); } } } else { try { return returnClass.cast(fco); } catch (ClassCastException cce) { throw new XMLStreamException(cce); } } } return null; }
/** * Gets a FreeCol object from an attribute in a stream. * * @param game The <code>Game</code> to look in. * @param attributeName The attribute name. * @param returnClass The <code>FreeColObject</code> type to expect. * @param defaultValue The default value. * @return The <code>FreeColObject</code> found, or the default value if not. * @exception XMLStreamException if the wrong class was passed. */ public <T extends FreeColObject> T getAttribute( Game game, String attributeName, Class<T> returnClass, T defaultValue) throws XMLStreamException { final String attrib = // @compat 0.10.7 (FreeColObject.ID_ATTRIBUTE_TAG.equals(attributeName)) ? readId() : // end @compat getAttribute(attributeName, (String) null); if (attrib == null) return defaultValue; FreeColObject fco = lookup(game, attrib); try { return returnClass.cast(fco); } catch (ClassCastException cce) { throw new XMLStreamException(cce); } }