@Override
  public void valueChanged(@Nullable TreeSelectionEvent e) {
    Component selectedComponent = myContentPanel.getSelectedComponent();

    if (selectedComponent == myColorPickerPanel) {
      Color color = myColorPicker.getColor();
      myNewResourceAction.setEnabled(false);
      myResultResourceName = ResourceHelper.colorToString(color);
      updateResourceNameStatus();
    } else {
      boolean isProjectPanel = selectedComponent == myProjectPanel.myComponent;
      ResourcePanel panel = isProjectPanel ? myProjectPanel : mySystemPanel;
      ResourceItem element = getSelectedElement(panel.myTreeBuilder, ResourceItem.class);
      setOKActionEnabled(element != null);
      myNewResourceAction.setEnabled(
          isProjectPanel && !panel.myTreeBuilder.getSelectedElements().isEmpty());

      if (element == null) {
        myResultResourceName = null;
      } else {
        String prefix = panel == myProjectPanel ? "@" : ANDROID;
        myResultResourceName = prefix + element.getName();
      }

      panel.showPreview(element);
    }
    notifyResourcePickerListeners(myResultResourceName);
  }
 @Override
 public Object getParentElement(Object element) {
   if (element instanceof ResourceItem) {
     ResourceItem resource = (ResourceItem) element;
     return resource.getGroup();
   }
   return null;
 }
 private void select(String type, String name) {
   for (ResourceGroup group : myGroups) {
     if (type.equalsIgnoreCase(group.getName())) {
       for (ResourceItem item : group.getItems()) {
         if (name.equals(item.toString())) {
           myTreeBuilder.select(item);
           return;
         }
       }
       return;
     }
   }
 }
    private void showComboPreview(ResourceItem element) {
      java.util.List<ResourceElement> resources = element.getPreviewResources();
      String selection = (String) myComboBox.getSelectedItem();
      if (selection == null) {
        selection = element.getPreviewComboDefaultSelection();
      }

      int index = element.getPreviewComboModel().getIndexOf(selection);
      if (index == -1) {
        index = 0;
      }

      myComboBox.setModel(element.getPreviewComboModel());
      myComboBox.putClientProperty(COMBO, resources);
      myComboBox.setSelectedIndex(index);
      myComboTextArea.setText(getResourceElementValue(resources.get(index)));

      CardLayout layout = (CardLayout) myPreviewPanel.getLayout();
      layout.show(myPreviewPanel, COMBO);
    }
    public void showPreview(@Nullable ResourceItem element) {
      CardLayout layout = (CardLayout) myPreviewPanel.getLayout();

      if (element == null || element.getGroup().getType() == ResourceType.ID) {
        layout.show(myPreviewPanel, NONE);
        return;
      }

      try {
        VirtualFile file = element.getFile();
        if (file == null) {
          String value = element.getPreviewString();
          if (value == null) {
            java.util.List<ResourceElement> resources = element.getPreviewResources();

            if (resources == null) {
              long time = System.currentTimeMillis();
              resources =
                  myManager.findValueResources(
                      element.getGroup().getType().getName(), element.toString());
              if (ApplicationManagerEx.getApplicationEx().isInternal()) {
                System.out.println("Time: " + (System.currentTimeMillis() - time)); // XXX
              }

              int size = resources.size();
              if (size == 1) {
                value = getResourceElementValue(resources.get(0));
                element.setPreviewString(value);
              } else if (size > 1) {
                resources = new ArrayList<ResourceElement>(resources);
                Collections.sort(
                    resources,
                    new Comparator<ResourceElement>() {
                      @Override
                      public int compare(ResourceElement element1, ResourceElement element2) {
                        PsiDirectory directory1 =
                            element1.getXmlTag().getContainingFile().getParent();
                        PsiDirectory directory2 =
                            element2.getXmlTag().getContainingFile().getParent();

                        if (directory1 == null && directory2 == null) {
                          return 0;
                        }
                        if (directory2 == null) {
                          return 1;
                        }
                        if (directory1 == null) {
                          return -1;
                        }

                        return directory1.getName().compareTo(directory2.getName());
                      }
                    });

                DefaultComboBoxModel model = new DefaultComboBoxModel();
                String defaultSelection = null;
                for (int i = 0; i < size; i++) {
                  ResourceElement resource = resources.get(i);
                  PsiDirectory directory = resource.getXmlTag().getContainingFile().getParent();
                  String name = directory == null ? "unknown-" + i : directory.getName();
                  model.addElement(name);
                  if (defaultSelection == null && "values".equalsIgnoreCase(name)) {
                    defaultSelection = name;
                  }
                }
                element.setPreviewResources(resources, model, defaultSelection);

                showComboPreview(element);
                return;
              } else {
                layout.show(myPreviewPanel, NONE);
                return;
              }
            } else {
              showComboPreview(element);
              return;
            }
          }
          if (value == null) {
            layout.show(myPreviewPanel, NONE);
            return;
          }

          myTextArea.setText(value);
          layout.show(myPreviewPanel, TEXT);
        } else if (ImageFileTypeManager.getInstance().isImage(file)) {
          Icon icon = element.getPreviewIcon();
          if (icon == null) {
            icon = new SizedIcon(100, 100, new ImageIcon(file.getPath()));
            element.setPreviewIcon(icon);
          }
          myImageComponent.setIcon(icon);
          layout.show(myPreviewPanel, IMAGE);
        } else if (file.getFileType() == XmlFileType.INSTANCE) {
          String value = element.getPreviewString();
          if (value == null) {
            value = new String(file.contentsToByteArray());
            element.setPreviewString(value);
          }
          myTextArea.setText(value);
          myTextArea.setEditable(false);
          layout.show(myPreviewPanel, TEXT);
        } else {
          layout.show(myPreviewPanel, NONE);
        }
      } catch (IOException e) {
        layout.show(myPreviewPanel, NONE);
      }
    }