protected void refreshProperties() {
    patternList.removeAllComponents();

    for (String patternID : detail.getTypes()) {
      PatternPanel dp = new PatternPanel(app.getType(patternID));
      patternList.addComponent(dp);
    }
  }
 public int getPropertiesNotPresent(NType p) {
   int count = 0;
   for (Property pid : app.getProperties(p, true, true)) {
     if (!definedProperty(pid.getID())) {
       count++;
     }
   }
   return count;
 }
  public void addNewPattern(String patternID) {
    if (!detail.getTypes().contains(patternID)) {
      NType p = app.getType(patternID);
      if (p != null) {
        detail.addType(patternID);

        for (Property pr : app.getAvailableProperties(detail).keySet()) {
          if (pr.getCardinalityMin() > 0) {
            for (int i = getPropertiesPresent(pr.getID()); i < pr.getCardinalityMin(); i++) {
              addProperty(pr.getID());
            }
          }
        }
      }

      refreshPatternBar();
      refreshProperties();
    }
  }
  public void addProperty(String id) {
    Property p = app.getPropertyByID(id);
    PropertyValue propertyValue = p.newDefaultValue(detail.getMode());
    propertyValue.setProperty(id);

    detail.getValues().add(propertyValue);

    refreshPatternBar();
    refreshProperties();
  }
  public boolean supportsAnotherProperty(String propertyID) {
    Property p = app.getPropertyByID(propertyID);
    if (p.getCardinalityMax() == -1) {
      return true;
    }

    int present = getPropertiesPresent(propertyID);
    if (present < p.getCardinalityMax()) {
      return true;
    }

    return false;
  }
  protected void refreshPatternBar() {
    patternBar.removeAllComponents();

    final Collection<NType> allPatterns = app.getTypes(null);

    patternBar.setColumns(patternColumns);
    patternBar.setRows((int) Math.ceil(allPatterns.size() / patternColumns));

    // 1. add all non-present patterns to 'patternMenu'
    for (final NType pattern : allPatterns) {
      String pName = pattern.getName();

      final PushButton pButton =
          new PushButton(pName, detail.getTypes().contains(pattern)) {

            @Override
            public void setPushed(boolean b) {
              super.setPushed(b);
              if (isPushed()) {
                addNewPattern(pattern.getId());
              } else {
                removePattern(pattern.getId());
              }
            }
          };
      pButton.setWidth("100%");
      pButton.setHeight("100%");
      // pButton.setIcon(new ThemeResource(pattern.getIconURL()));

      patternBar.addComponent(pButton);
    }
    //        patternMenu.addSeparator();
    //        patternMenu.addItem("New Pattern...", new Command() {
    //
    //            @Override
    //            public void menuSelected(MenuItem selectedItem) {
    //                createNewPattern();
    //            }
    //        });

    //        //2. add all present patterns to 'patternBar' as menus
    //        for (final String p : presentPatterns) {
    //            Pattern pattern = schema.getPatterns().get(p);
    //            int total = getPropertiesTotal(pattern);
    //            MenuItem i = patternBar.addItem(pattern.getName() + " (" + (total -
    // getPropertiesNotPresent(pattern)) + "/" + total + ")", null, null);
    //
    //            for (String property : pattern.keySet()) {
    //                if (supportsAnotherProperty(property)) {
    //                    final Property pr = schema.getProperty(property);
    //                    i.addItem(pr.getName(), new Command() {
    //
    //                        @Override
    //                        public void menuSelected(MenuItem selectedItem) {
    //                            addProperty(pr.getID());
    //                        }
    //                    });
    //                }
    //            }
    //
    //            i.addSeparator();
    //
    //            //move left (more important)
    //            //move right (less important)
    //
    //            i.addItem("Remove", new Command() {
    //
    //                @Override
    //                public void menuSelected(MenuItem selectedItem) {
    //                    removePattern(p);
    //                }
    //            });
    //        }
  }