use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class ThemeEditorUtils method createNewStyle.
/**
* Creates a new style
* @param project the project where the new style is being created
* @param resourceDir the res/ directory where the new style is being created
* @param newStyleName the new style name
* @param parentStyleName the name of the new style parent
* @param fileName name of the xml file where the style will be added (usually "styles.xml")
* @param folderNames folder names where the style will be added
* @return true if the style was created or false otherwise
*/
public static boolean createNewStyle(@NotNull final Project project, @NotNull final VirtualFile resourceDir, @NotNull final String newStyleName, @Nullable final String parentStyleName, @NotNull final String fileName, @NotNull final List<String> folderNames) {
return new WriteCommandAction<Boolean>(project, "Create new style " + newStyleName) {
@Override
protected void run(@NotNull Result<Boolean> result) {
CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
result.setResult(AndroidResourceUtil.createValueResource(project, resourceDir, newStyleName, null, ResourceType.STYLE, fileName, folderNames, new Processor<ResourceElement>() {
@Override
public boolean process(ResourceElement element) {
assert element instanceof Style;
final Style style = (Style) element;
if (parentStyleName != null) {
style.getParentStyle().setStringValue(parentStyleName);
}
return true;
}
}));
}
}.execute().getResultObject();
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class AndroidFindUsagesHandlerFactory method correctResourceElement.
@Nullable
private static PsiElement correctResourceElement(PsiElement element) {
if (element instanceof XmlElement && !(element instanceof XmlFile)) {
XmlTag tag = element instanceof XmlTag ? (XmlTag) element : PsiTreeUtil.getParentOfType(element, XmlTag.class);
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement) {
return tag;
}
return null;
}
return element;
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class ValueResourceElementWrapper method setName.
@Override
@Nullable
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
if (AndroidResourceUtil.isIdDeclaration(myWrappee)) {
XmlAttribute attribute = (XmlAttribute) myWrappee.getParent();
attribute.setValue(name);
} else {
// then it is a value resource
XmlTag tag = PsiTreeUtil.getParentOfType(myWrappee, XmlTag.class);
DomElement domElement = DomManager.getDomManager(getProject()).getDomElement(tag);
assert domElement instanceof ResourceElement;
ResourceElement resElement = (ResourceElement) domElement;
resElement.getName().setValue(name);
}
return null;
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class ValueResourceInfoImpl method computeDomElement.
@Nullable
public ResourceElement computeDomElement() {
final PsiFile file = PsiManager.getInstance(myProject).findFile(myFile);
if (!(file instanceof XmlFile)) {
return null;
}
final XmlTag tag = PsiTreeUtil.findElementOfClassAtOffset(file, myOffset, XmlTag.class, true);
if (tag == null) {
return null;
}
final DomElement domElement = DomManager.getDomManager(myProject).getDomElement(tag);
if (!(domElement instanceof ResourceElement)) {
return null;
}
final String resType = domElement instanceof Item ? ((Item) domElement).getType().getStringValue() : AndroidCommonUtils.getResourceTypeByTagName(tag.getName());
if (!myType.getName().equals(resType)) {
return null;
}
final ResourceElement resDomElement = (ResourceElement) domElement;
final String resName = ((ResourceElement) domElement).getName().getStringValue();
return myName.equals(resName) ? resDomElement : null;
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class OverrideResourceAction method createValueResource.
private static void createValueResource(@NotNull final Project project, @NotNull final PsiDirectory resDir, @NotNull PsiFile file, @NotNull PsiDirectory resourceSubdir, @NotNull final String resName, @NotNull final String value, @NotNull final ResourceType type, @NotNull final String oldTagText, boolean open) {
final String filename = file.getName();
final List<String> dirNames = Collections.singletonList(resourceSubdir.getName());
final AtomicReference<PsiElement> openAfter = new AtomicReference<PsiElement>();
final WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, "Override Resource " + resName, file) {
@Override
protected void run(@NotNull Result<Void> result) {
List<ResourceElement> elements = Lists.newArrayListWithExpectedSize(1);
// AndroidResourceUtil.createValueResource will create a new resource value in the given resource
// folder (and record the corresponding tags added in the elements list passed into it).
// However, it only creates a new element and sets the name attribute on it; it does not
// transfer attributes, child content etc. Therefore, we use this utility method first to
// create the corresponding tag, and then *afterwards* we will replace the tag with a text copy
// from the resource tag we are overriding. We do this all under a single write lock such
// that it becomes a single atomic operation.
AndroidResourceUtil.createValueResource(project, resDir.getVirtualFile(), resName, type, filename, dirNames, value, elements);
if (elements.size() == 1) {
final XmlTag tag = elements.get(0).getXmlTag();
if (tag != null && tag.isValid()) {
try {
XmlTag tagFromText = XmlElementFactory.getInstance(tag.getProject()).createTagFromText(oldTagText);
PsiElement replaced = tag.replace(tagFromText);
openAfter.set(replaced);
} catch (IncorrectOperationException e) {
// The user tried to override an invalid XML fragment: don't attempt to do a replacement in that case
openAfter.set(tag);
}
}
}
}
};
action.execute();
PsiElement tag = openAfter.get();
if (open && tag != null) {
NavigationUtil.openFileWithPsiElement(tag, true, true);
}
}
Aggregations