001/* 002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. 003 * Copyright (C) 2000-2010 The Gridarta Developers. 004 * 005 * This program is free software; you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation; either version 2 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License along 016 * with this program; if not, write to the Free Software Foundation, Inc., 017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 018 */ 019 020package net.sf.gridarta.plugin.parameter; 021 022import net.sf.gridarta.model.archetype.Archetype; 023import net.sf.gridarta.model.archetype.UndefinedArchetypeException; 024import net.sf.gridarta.model.archetypeset.ArchetypeSet; 025import net.sf.gridarta.model.gameobject.GameObject; 026import net.sf.gridarta.model.maparchobject.MapArchObject; 027import org.jetbrains.annotations.NotNull; 028import org.jetbrains.annotations.Nullable; 029 030/** 031 * A {@link PluginParameter} that holds an {@link Archetype} value. 032 */ 033public class ArchParameter<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractPluginParameter<G, A, R, Archetype<G, A, R>> { 034 035 /** 036 * The string representation of this parameter type. 037 */ 038 @NotNull 039 public static final String PARAMETER_TYPE = Archetype.class.getName(); 040 041 /** 042 * The {@link ArchetypeSet} for looking up archetypes. 043 */ 044 @NotNull 045 private final ArchetypeSet<G, A, R> archetypeSet; 046 047 private String valueString; 048 049 /** 050 * Creates a new instance. 051 * @param archetypeSet the archetype set for looking up archetypes 052 */ 053 public ArchParameter(@NotNull final ArchetypeSet<G, A, R> archetypeSet) { 054 this.archetypeSet = archetypeSet; 055 } 056 057 /** 058 * {@inheritDoc} 059 */ 060 @NotNull 061 @Override 062 public <T> T visit(@NotNull final PluginParameterVisitor<G, A, R, T> visitor) { 063 return visitor.visit(this); 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Nullable 070 @Override 071 public Archetype<G, A, R> getValue() { 072 if (super.getValue() == null) { 073 setValue(archetypeSet.getOrCreateArchetype(valueString)); 074 } 075 return super.getValue(); 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 @Override 082 public boolean setStringValue(@NotNull final String value) { 083 final Archetype<G, A, R> archetype; 084 try { 085 archetype = archetypeSet.getArchetype(value); 086 } catch (final UndefinedArchetypeException ignored) { 087 return false; 088 } 089 setValue(archetype); 090 return true; 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 @NotNull 097 @Override 098 public String getParameterType() { 099 return PARAMETER_TYPE; 100 } 101 102 @Nullable 103 public String getValueString() { 104 return valueString; 105 } 106 107}