private static String getResourceElementValue(ResourceElement element) { String text = element.getRawText(); if (StringUtil.isEmpty(text)) { return element.getXmlTag().getText(); } return text; }
@Nullable public static ValidationInfo checkIfResourceAlreadyExists( @NotNull Module selectedModule, @NotNull String resourceName, @NotNull ResourceType resourceType, @NotNull List<String> dirNames, @NotNull String fileName) { if (resourceName.length() == 0 || dirNames.size() == 0 || fileName.length() == 0) { return null; } final AndroidFacet facet = AndroidFacet.getInstance(selectedModule); final VirtualFile resourceDir = facet != null ? AndroidRootUtil.getResourceDir(facet) : null; if (resourceDir == null) { return null; } for (String directoryName : dirNames) { final VirtualFile resourceSubdir = resourceDir.findChild(directoryName); if (resourceSubdir == null) { continue; } final VirtualFile resFile = resourceSubdir.findChild(fileName); if (resFile == null) { continue; } if (resFile.getFileType() != StdFileTypes.XML) { return new ValidationInfo( "File " + FileUtil.toSystemDependentName(resFile.getPath()) + " is not XML file"); } final Resources resources = AndroidUtils.loadDomElement(selectedModule, resFile, Resources.class); if (resources == null) { return new ValidationInfo( AndroidBundle.message( "not.resource.file.error", FileUtil.toSystemDependentName(resFile.getPath()))); } for (ResourceElement element : AndroidResourceUtil.getValueResourcesFromElement(resourceType.getName(), resources)) { if (resourceName.equals(element.getName().getValue())) { return new ValidationInfo( "resource '" + resourceName + "' already exists in " + FileUtil.toSystemDependentName(resFile.getPath())); } } } return null; }
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); } }