2010-02-22 2 views
0

Il est juste une simple application TESTVaadin et DataModel. Comment puis-je obtenir la valeur stockée par une implémentation Property DataModel utilisée par un champ Label? Vaadin

import br.com.elf.ui.IndexApplication; 

public class IndexApplication extends Application { 

    public void init() { 
     setMainWindow(getStartUpWindow()); 
    } 

    private Window getStartUpWindow() { 
     Window mainWindow = new Window(); 

     mainWindow.addComponent(
      new Label(new Property() { 
       public Object getValue() { 
        return "DataModel Example"; 
       } 

       public void setValue(Object value) throws ReadOnlyException, ConversionException { 
        throw new ReadOnlyException(); 
       } 

       public Class<?> getType() { 
        return String.class; 
       } 

       public boolean isReadOnly() { 
        return true; 
       } 

       public void setReadOnly(boolean readyOnly) { 
        // Empty body 
       } 
      )); 
     } 

     return mainWindow; 
    } 

} 

Avis i ai i plain champ Label. Je sais que je peux simplement appeler

mainWindow.addComponent(new Label("DataModel Example")); 

à la place. Mais afin de voir comment Property DataModel fonctionne derrière les scènes, j'ai ajouté une implémentation de propriété. Mais au lieu de voir la production

DataModel Exemple

-je obtenir

[email protected]

Pourquoi ???

Et quel est le véritable but de la méthode Object getType() définie dans l'interface Property ??? Si le HTML montre sa sortie dans la chaîne normale, donc je pense qu'il n'y a aucune raison d'implémenter un Object getType(), non ???

salutations,

Répondre

2

pourquoi je trouve,

La méthode utilisée pour montrer sa valeur en format textuel humain redable est toString. Comme indiqué dans l'API de propriété

renvoie la valeur de la propriété dans un format textuel lisible par l'homme.

Comme le montre ci-dessous

mainWindow.addComponent(new Label(new Property() { 
     public Object getValue() { 
      return "Wellcome to Vaadin!"; 
     } 

     public void setValue(Object newValue) throws ReadOnlyException, ConversionException { 
      throw new ReadOnlyException(); 
     } 

     public Class<?> getType() { 
      return String.class; 
     } 

     public boolean isReadOnly() { 
      return true; 
     } 

     public void setReadOnly(boolean newStatus) { 
      throw new UnsupportedOperationException(); 
     } 

     @Override 
     public String toString() { 
      return (String) getValue(); 
     } 
    })); 

Et getType vous indique le type stocké par cette propriété, rien d'autre. Cela peut être n'importe quoi, même une classe de compte, par exemple. La valeur affichée par le composant lui-même est toujours dérivée de la méthode toString.

salutations,

Questions connexes