mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Work with resources
This commit is contained in:
+1
-2
@@ -25,7 +25,6 @@ import com.intellij.designer.model.MetaModel;
|
||||
import com.intellij.designer.model.RadComponent;
|
||||
import com.intellij.designer.propertyTable.Property;
|
||||
import com.intellij.designer.propertyTable.PropertyTable;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
@@ -71,7 +70,7 @@ public class RadCustomViewComponent extends RadViewComponent implements IConfigu
|
||||
new ChooseClassDialog(moduleProvider.getModule(), "Views", false, "android.view.View");
|
||||
dialog.show();
|
||||
|
||||
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
|
||||
if (dialog.isOK()) {
|
||||
return dialog.getClassName();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import com.intellij.designer.ModuleProvider;
|
||||
import com.intellij.designer.model.RadComponent;
|
||||
import com.intellij.designer.propertyTable.Property;
|
||||
import com.intellij.designer.propertyTable.editors.TextEditor;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import org.jetbrains.android.dom.attrs.AttributeFormat;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -86,7 +85,7 @@ public class RadFragment extends RadViewComponent implements IConfigurableCompon
|
||||
new ChooseClassDialog(moduleProvider.getModule(), "Fragments", true, "android.app.Fragment", "android.support.v4.app.Fragment");
|
||||
dialog.show();
|
||||
|
||||
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
|
||||
if (dialog.isOK()) {
|
||||
return dialog.getClassName();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ public class RadIncludeLayout extends RadViewComponent implements IConfigurableC
|
||||
|
||||
public void configure(RadComponent rootComponent) throws Exception {
|
||||
ModuleProvider moduleProvider = rootComponent.getClientProperty(ModelParser.MODULE_KEY);
|
||||
ResourceDialog dialog = new ResourceDialog(moduleProvider.getModule(), IncludeLayoutProperty.TYPES, null);
|
||||
ResourceDialog dialog = new ResourceDialog(moduleProvider.getModule(), IncludeLayoutProperty.TYPES, null, null);
|
||||
dialog.show();
|
||||
|
||||
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
|
||||
|
||||
+2
-2
@@ -80,7 +80,7 @@ public class ChooseClassDialog extends DialogWrapper implements ListSelectionLis
|
||||
};
|
||||
|
||||
setTitle(title);
|
||||
getOKAction().setEnabled(false);
|
||||
setOKActionEnabled(false);
|
||||
|
||||
init();
|
||||
}
|
||||
@@ -119,7 +119,7 @@ public class ChooseClassDialog extends DialogWrapper implements ListSelectionLis
|
||||
@Override
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
PsiClass psiClass = (PsiClass)myList.getSelectedValue();
|
||||
getOKAction().setEnabled(psiClass != null);
|
||||
setOKActionEnabled(psiClass != null);
|
||||
myResultClassName = psiClass == null ? null : psiClass.getQualifiedName();
|
||||
}
|
||||
}
|
||||
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.android.designer.propertyTable.editors;
|
||||
|
||||
import com.android.resources.ResourceType;
|
||||
import com.intellij.openapi.actionSystem.ActionToolbarPosition;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.ValidationInfo;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.ui.*;
|
||||
import com.intellij.uiDesigner.core.GridConstraints;
|
||||
import com.intellij.uiDesigner.core.GridLayoutManager;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.containers.Convertor;
|
||||
import com.intellij.util.ui.tree.TreeUtil;
|
||||
import org.jetbrains.android.actions.CreateXmlResourceDialog;
|
||||
import org.jetbrains.android.util.AndroidResourceUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.tree.TreePath;
|
||||
import javax.swing.tree.TreeSelectionModel;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Alexander Lobas
|
||||
*/
|
||||
public class ExtractStyleDialog extends DialogWrapper {
|
||||
private final Module myModule;
|
||||
private final String myFileName;
|
||||
private final List<String> myDirNames;
|
||||
|
||||
private final JPanel myPanel;
|
||||
private final JTextField myNameText;
|
||||
private final CheckboxTree myTree;
|
||||
|
||||
private final CheckedTreeNode myRootNode;
|
||||
|
||||
public ExtractStyleDialog(Module module, String fileName, List<String> dirNames, XmlTag tag) {
|
||||
super(module.getProject());
|
||||
myModule = module;
|
||||
myFileName = fileName;
|
||||
myDirNames = dirNames;
|
||||
|
||||
myPanel = new JPanel(new GridLayoutManager(3, 2));
|
||||
|
||||
JLabel nameLabel = new JLabel("Style name:");
|
||||
nameLabel.setDisplayedMnemonic('n');
|
||||
myPanel.add(nameLabel,
|
||||
new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, 0, 0, null, null, null));
|
||||
|
||||
myNameText = new JTextField();
|
||||
myPanel.add(myNameText, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
|
||||
GridConstraints.SIZEPOLICY_CAN_GROW, 0, null, null, null));
|
||||
|
||||
nameLabel.setLabelFor(myNameText);
|
||||
|
||||
myNameText.getDocument().addDocumentListener(new DocumentListener() {
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
checkFinish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
checkFinish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
checkFinish();
|
||||
}
|
||||
});
|
||||
|
||||
JLabel attributesLabel = new JLabel("Attributes:");
|
||||
attributesLabel.setDisplayedMnemonic('A');
|
||||
myPanel.add(attributesLabel,
|
||||
new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, 0, 0, null, null, null));
|
||||
|
||||
myRootNode = new CheckedTreeNode(null);
|
||||
for (XmlAttribute attribute : tag.getAttributes()) {
|
||||
String name = attribute.getName();
|
||||
if (!"style".equalsIgnoreCase(name)) {
|
||||
CheckedTreeNode treeNode = new CheckedTreeNode(attribute);
|
||||
treeNode.setChecked(!"android:layout_width".equalsIgnoreCase(name) && !"android:layout_height".equalsIgnoreCase(name));
|
||||
myRootNode.add(treeNode);
|
||||
}
|
||||
}
|
||||
|
||||
CheckboxTree.CheckboxTreeCellRenderer renderer = new CheckboxTree.CheckboxTreeCellRenderer() {
|
||||
@Override
|
||||
public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
|
||||
if (value instanceof CheckedTreeNode) {
|
||||
XmlAttribute attribute = (XmlAttribute)((CheckedTreeNode)value).getUserObject();
|
||||
if (attribute != null) {
|
||||
getTextRenderer().append(attribute.getLocalName());
|
||||
getTextRenderer().append(" [" + attribute.getValue() + "]", SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
myTree = new CheckboxTree(renderer, myRootNode) {
|
||||
@Override
|
||||
protected void onNodeStateChanged(CheckedTreeNode node) {
|
||||
checkFinish();
|
||||
}
|
||||
|
||||
protected void installSpeedSearch() {
|
||||
new TreeSpeedSearch(this, new Convertor<TreePath, String>() {
|
||||
public String convert(TreePath path) {
|
||||
Object object = path.getLastPathComponent();
|
||||
if (object instanceof CheckedTreeNode) {
|
||||
XmlAttribute attribute = (XmlAttribute)((CheckedTreeNode)object).getUserObject();
|
||||
if (attribute != null) {
|
||||
return attribute.getLocalName();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||
myTree.setRootVisible(false);
|
||||
myTree.setShowsRootHandles(false);
|
||||
TreeUtil.expandAll(myTree);
|
||||
|
||||
attributesLabel.setLabelFor(myTree);
|
||||
|
||||
ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myTree);
|
||||
decorator.setToolbarPosition(ActionToolbarPosition.RIGHT);
|
||||
decorator.setEditAction(null);
|
||||
decorator.disableUpDownActions();
|
||||
|
||||
AnActionButton selectAll = new AnActionButton("Select All", null, PlatformIcons.SELECT_ALL_ICON) {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
setChecked(true);
|
||||
}
|
||||
};
|
||||
decorator.addExtraAction(selectAll);
|
||||
|
||||
AnActionButton unselectAll = new AnActionButton("Unselect All", null, PlatformIcons.UNSELECT_ALL_ICON) {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
setChecked(false);
|
||||
}
|
||||
};
|
||||
decorator.addExtraAction(unselectAll);
|
||||
|
||||
|
||||
myPanel.add(decorator.createPanel(),
|
||||
new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
|
||||
GridConstraints.SIZEPOLICY_CAN_GROW, 0, null, null, null));
|
||||
|
||||
myPanel.setPreferredSize(new Dimension(400, -1));
|
||||
|
||||
setTitle("Extract Style");
|
||||
checkFinish();
|
||||
init();
|
||||
}
|
||||
|
||||
private void checkFinish() {
|
||||
if (AndroidResourceUtil.isCorrectAndroidResourceName(myNameText.getText().trim())) {
|
||||
int count = myRootNode.getChildCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
CheckedTreeNode treeNode = (CheckedTreeNode)myRootNode.getChildAt(i);
|
||||
if (treeNode.isChecked()) {
|
||||
setOKActionEnabled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
setOKActionEnabled(false);
|
||||
}
|
||||
|
||||
private void setChecked(boolean value) {
|
||||
int count = myRootNode.getChildCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
((CheckedTreeNode)myRootNode.getChildAt(i)).setChecked(value);
|
||||
}
|
||||
myTree.repaint();
|
||||
checkFinish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JComponent createCenterPanel() {
|
||||
return myPanel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent getPreferredFocusedComponent() {
|
||||
return myNameText;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ValidationInfo doValidate() {
|
||||
return CreateXmlResourceDialog.checkIfResourceAlreadyExists(myModule, getStyleName(), ResourceType.STYLE, myDirNames, myFileName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getStyleName() {
|
||||
return myNameText.getText();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<XmlAttribute> getStyledAttributes() {
|
||||
List<XmlAttribute> attributes = new ArrayList<XmlAttribute>();
|
||||
|
||||
int count = myRootNode.getChildCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
CheckedTreeNode treeNode = (CheckedTreeNode)myRootNode.getChildAt(i);
|
||||
if (treeNode.isChecked()) {
|
||||
attributes.add((XmlAttribute)treeNode.getUserObject());
|
||||
}
|
||||
}
|
||||
|
||||
return attributes;
|
||||
}
|
||||
}
|
||||
+181
-12
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package com.intellij.android.designer.propertyTable.editors;
|
||||
|
||||
import com.android.resources.ResourceFolderType;
|
||||
import com.android.resources.ResourceType;
|
||||
import com.intellij.android.designer.model.RadViewComponent;
|
||||
import com.intellij.android.designer.propertyTable.renderers.ResourceRenderer;
|
||||
import com.intellij.designer.componentTree.TreeNodeDescriptor;
|
||||
import com.intellij.icons.AllIcons;
|
||||
@@ -23,19 +25,30 @@ import com.intellij.ide.util.treeView.AbstractTreeBuilder;
|
||||
import com.intellij.ide.util.treeView.AbstractTreeStructure;
|
||||
import com.intellij.ide.util.treeView.NodeDescriptor;
|
||||
import com.intellij.ide.util.treeView.NodeRenderer;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.ui.*;
|
||||
import com.intellij.ui.components.JBTabbedPane;
|
||||
import com.intellij.ui.speedSearch.SpeedSearchUtil;
|
||||
import com.intellij.ui.treeStructure.Tree;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.ui.tree.TreeUtil;
|
||||
import org.jetbrains.android.actions.CreateResourceFileAction;
|
||||
import org.jetbrains.android.actions.CreateXmlResourceDialog;
|
||||
import org.jetbrains.android.facet.AndroidFacet;
|
||||
import org.jetbrains.android.facet.AndroidRootUtil;
|
||||
import org.jetbrains.android.resourceManagers.ResourceManager;
|
||||
import org.jetbrains.android.util.AndroidCommonUtils;
|
||||
import org.jetbrains.android.util.AndroidResourceUtil;
|
||||
import org.jetbrains.android.util.AndroidUtils;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -60,23 +73,53 @@ import java.util.List;
|
||||
*/
|
||||
public class ResourceDialog extends DialogWrapper implements TreeSelectionListener {
|
||||
private static final String ANDROID = "@android:";
|
||||
private static final String TYPE_KEY = "ResourceType";
|
||||
|
||||
private static final Icon RESOURCE_ITEM_ICON = AllIcons.Css.Property;
|
||||
|
||||
private final Module myModule;
|
||||
private final RadViewComponent myComponent;
|
||||
|
||||
private final JBTabbedPane myContentPanel;
|
||||
private final ResourcePanel myProjectPanel;
|
||||
private final ResourcePanel mySystemPanel;
|
||||
private ColorPicker myColorPicker;
|
||||
private final Action myNewResourceAction = new AbstractAction("New Resource...") {
|
||||
|
||||
private final Action myNewResourceAction = new AbstractAction("New Resource", AllIcons.General.ComboArrowDown) {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// TODO: Auto-generated method stub
|
||||
JComponent component = (JComponent)e.getSource();
|
||||
ActionPopupMenu popupMenu = createNewResourcePopupMenu();
|
||||
popupMenu.getComponent().show(component, 0, component.getHeight());
|
||||
}
|
||||
};
|
||||
private final AnAction myNewResourceValueAction = new AnAction() {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
ResourceType type = (ResourceType)getTemplatePresentation().getClientProperty(TYPE_KEY);
|
||||
createNewResourceValue(type);
|
||||
}
|
||||
};
|
||||
private final AnAction myNewResourceFileAction = new AnAction() {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
ResourceType type = (ResourceType)getTemplatePresentation().getClientProperty(TYPE_KEY);
|
||||
createNewResourceFile(type);
|
||||
}
|
||||
};
|
||||
private final AnAction myExtractStyleAction = new AnAction("Extract Style...") {
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
extractStyle();
|
||||
}
|
||||
};
|
||||
|
||||
private String myResultResourceName;
|
||||
|
||||
public ResourceDialog(Module module, ResourceType[] types, String value) {
|
||||
public ResourceDialog(Module module, ResourceType[] types, String value, RadViewComponent component) {
|
||||
super(module.getProject());
|
||||
myModule = module;
|
||||
myComponent = component;
|
||||
|
||||
setTitle("Resources");
|
||||
|
||||
@@ -123,13 +166,126 @@ public class ResourceDialog extends DialogWrapper implements TreeSelectionListen
|
||||
myContentPanel.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
myNewResourceAction.setEnabled(myContentPanel.getSelectedComponent() == myProjectPanel.myComponent);
|
||||
valueChanged(null);
|
||||
}
|
||||
});
|
||||
|
||||
init();
|
||||
valueChanged(null);
|
||||
init();
|
||||
}
|
||||
|
||||
private ActionPopupMenu createNewResourcePopupMenu() {
|
||||
ActionManager actionManager = ActionManager.getInstance();
|
||||
DefaultActionGroup actionGroup = new DefaultActionGroup();
|
||||
|
||||
ResourceGroup resourceGroup = getSelectedElement(myProjectPanel.myTreeBuilder, ResourceGroup.class);
|
||||
if (resourceGroup == null) {
|
||||
resourceGroup = getSelectedElement(myProjectPanel.myTreeBuilder, ResourceItem.class).getGroup();
|
||||
}
|
||||
|
||||
if (AndroidResourceUtil.VALUE_RESOURCE_TYPES.contains(resourceGroup.getType())) {
|
||||
myNewResourceValueAction.getTemplatePresentation().setText("New " + resourceGroup + " Value...");
|
||||
myNewResourceValueAction.getTemplatePresentation().putClientProperty(TYPE_KEY, resourceGroup.getType());
|
||||
actionGroup.add(myNewResourceValueAction);
|
||||
}
|
||||
if (AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.contains(resourceGroup.getType())) {
|
||||
myNewResourceFileAction.getTemplatePresentation().setText("New " + resourceGroup + " File...");
|
||||
myNewResourceFileAction.getTemplatePresentation().putClientProperty(TYPE_KEY, resourceGroup.getType());
|
||||
actionGroup.add(myNewResourceFileAction);
|
||||
}
|
||||
if (myComponent != null && ResourceType.STYLE.equals(resourceGroup.getType()) && myComponent.getTag().getAttributes().length > 0) {
|
||||
// TODO: add StyleItem
|
||||
//actionGroup.add(myExtractStyleAction);
|
||||
}
|
||||
|
||||
return actionManager.createActionPopupMenu(ActionPlaces.UNKNOWN, actionGroup);
|
||||
}
|
||||
|
||||
private void createNewResourceValue(ResourceType resourceType) {
|
||||
CreateXmlResourceDialog dialog = new CreateXmlResourceDialog(myModule, resourceType, null, null, true);
|
||||
dialog.setTitle("New " + StringUtil.capitalize(resourceType.getDisplayName()) + " Value Resource");
|
||||
dialog.show();
|
||||
|
||||
if (!dialog.isOK()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Module moduleToPlaceResource = dialog.getModule();
|
||||
if (moduleToPlaceResource == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String fileName = dialog.getFileName();
|
||||
List<String> dirNames = dialog.getDirNames();
|
||||
String resValue = dialog.getValue();
|
||||
String resName = dialog.getResourceName();
|
||||
if (!AndroidResourceUtil.createValueResource(moduleToPlaceResource, resName, resourceType, fileName, dirNames, resValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
PsiDocumentManager.getInstance(myModule.getProject()).commitAllDocuments();
|
||||
|
||||
myResultResourceName = "@" + resourceType.getName() + "/" + resName;
|
||||
close(OK_EXIT_CODE);
|
||||
}
|
||||
|
||||
private void createNewResourceFile(ResourceType resourceType) {
|
||||
AndroidFacet facet = AndroidFacet.getInstance(myModule);
|
||||
PsiElement[] elements = CreateResourceFileAction.createFileResource(facet, resourceType, null, true);
|
||||
|
||||
if (elements.length == 1) {
|
||||
String name = ((PsiFile)elements[0]).getName();
|
||||
int index = name.lastIndexOf('.');
|
||||
if (index != -1) {
|
||||
name = name.substring(0, index);
|
||||
}
|
||||
myResultResourceName = "@" + resourceType.getName() + "/" + name;
|
||||
close(OK_EXIT_CODE);
|
||||
}
|
||||
}
|
||||
|
||||
private void extractStyle() {
|
||||
String fileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STYLE);
|
||||
|
||||
VirtualFile[] dirs = {AndroidRootUtil.getResourceDir(AndroidFacet.getInstance(myModule))};
|
||||
List<VirtualFile> subDirs = AndroidResourceUtil.getResourceSubdirs(ResourceFolderType.VALUES.getName(), dirs);
|
||||
List<String> dirNames = new ArrayList<String>();
|
||||
for (VirtualFile dir : subDirs) {
|
||||
dirNames.add(dir.getName());
|
||||
}
|
||||
|
||||
ExtractStyleDialog dialog = new ExtractStyleDialog(myModule, fileName, dirNames, myComponent.getTag());
|
||||
dialog.show();
|
||||
|
||||
if (!dialog.isOK()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<XmlAttribute> attributes = dialog.getStyledAttributes();
|
||||
|
||||
StringBuilder value = new StringBuilder();
|
||||
for (XmlAttribute attribute : attributes) {
|
||||
value.append("<item name=\"").append(attribute.getName()).append("\">").append(attribute.getValue()).append("</item>\n");
|
||||
}
|
||||
|
||||
String resName = dialog.getStyleName();
|
||||
if (!AndroidResourceUtil.createValueResource(myModule, resName, ResourceType.STYLE, fileName, dirNames, value.toString())) {
|
||||
return;
|
||||
}
|
||||
|
||||
PsiDocumentManager.getInstance(myModule.getProject()).commitAllDocuments();
|
||||
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (XmlAttribute attribute : attributes) {
|
||||
attribute.delete();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
myResultResourceName = "@style/" + resName;
|
||||
close(OK_EXIT_CODE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -144,7 +300,7 @@ public class ResourceDialog extends DialogWrapper implements TreeSelectionListen
|
||||
|
||||
@Override
|
||||
protected Action[] createLeftSideActions() {
|
||||
return super.createLeftSideActions();//new Action[]{myNewResourceAction};
|
||||
return new Action[]{myNewResourceAction};
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -164,26 +320,35 @@ public class ResourceDialog extends DialogWrapper implements TreeSelectionListen
|
||||
super.doOKAction();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static <T> T getSelectedElement(AbstractTreeBuilder treeBuilder, Class<T> elementClass) {
|
||||
Set<T> elements = treeBuilder.getSelectedElements(elementClass);
|
||||
return elements.isEmpty() ? null : elements.iterator().next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void valueChanged(@Nullable TreeSelectionEvent e) {
|
||||
Component selectedComponent = myContentPanel.getSelectedComponent();
|
||||
|
||||
if (selectedComponent == myColorPicker) {
|
||||
Color color = myColorPicker.getColor();
|
||||
getOKAction().setEnabled(color != null);
|
||||
setOKActionEnabled(color != null);
|
||||
myNewResourceAction.setEnabled(false);
|
||||
myResultResourceName = color == null ? null : "#" + toHex(color.getRed()) + toHex(color.getGreen()) + toHex(color.getBlue());
|
||||
}
|
||||
else {
|
||||
ResourcePanel panel = selectedComponent == myProjectPanel.myComponent ? myProjectPanel : mySystemPanel;
|
||||
Set<ResourceItem> elements = panel.myTreeBuilder.getSelectedElements(ResourceItem.class);
|
||||
getOKAction().setEnabled(!elements.isEmpty());
|
||||
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 (elements.isEmpty()) {
|
||||
if (element == null) {
|
||||
myResultResourceName = null;
|
||||
}
|
||||
else {
|
||||
String prefix = panel == myProjectPanel ? "@" : ANDROID;
|
||||
myResultResourceName = prefix + elements.iterator().next().getName();
|
||||
myResultResourceName = prefix + element.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,6 +481,10 @@ public class ResourceDialog extends DialogWrapper implements TreeSelectionListen
|
||||
});
|
||||
}
|
||||
|
||||
public ResourceType getType() {
|
||||
return myType;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return myType.getName();
|
||||
}
|
||||
|
||||
+5
-3
@@ -17,6 +17,7 @@ package com.intellij.android.designer.propertyTable.editors;
|
||||
|
||||
import com.android.resources.ResourceType;
|
||||
import com.intellij.android.designer.model.ModelParser;
|
||||
import com.intellij.android.designer.model.RadViewComponent;
|
||||
import com.intellij.designer.ModuleProvider;
|
||||
import com.intellij.designer.model.RadComponent;
|
||||
import com.intellij.designer.propertyTable.InplaceContext;
|
||||
@@ -26,7 +27,6 @@ import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.ComponentWithBrowseButton;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.ui.ComboboxWithBrowseButton;
|
||||
@@ -50,6 +50,7 @@ public class ResourceEditor extends PropertyEditor {
|
||||
private final ResourceType[] myTypes;
|
||||
private ComponentWithBrowseButton myEditor;
|
||||
protected RadComponent myRootComponent;
|
||||
private RadComponent myComponent;
|
||||
private JCheckBox myCheckBox;
|
||||
private final Border myCheckBoxBorder = new JTextField().getBorder();
|
||||
private boolean myIgnoreCheckBoxValue;
|
||||
@@ -207,6 +208,7 @@ public class ResourceEditor extends PropertyEditor {
|
||||
Object object,
|
||||
@Nullable InplaceContext inplaceContext) {
|
||||
myRootComponent = rootComponent;
|
||||
myComponent = component;
|
||||
String value = (String)object;
|
||||
JTextField text = getComboText();
|
||||
|
||||
@@ -270,10 +272,10 @@ public class ResourceEditor extends PropertyEditor {
|
||||
|
||||
protected void showDialog() {
|
||||
ModuleProvider moduleProvider = myRootComponent.getClientProperty(ModelParser.MODULE_KEY);
|
||||
ResourceDialog dialog = new ResourceDialog(moduleProvider.getModule(), myTypes, (String)getValue());
|
||||
ResourceDialog dialog = new ResourceDialog(moduleProvider.getModule(), myTypes, (String)getValue(), (RadViewComponent)myComponent);
|
||||
dialog.show();
|
||||
|
||||
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
|
||||
if (dialog.isOK()) {
|
||||
setValue(dialog.getResourceName());
|
||||
}
|
||||
else if (myBooleanResourceValue != null) {
|
||||
|
||||
@@ -94,7 +94,7 @@ public class CreateResourceFileAction extends CreateElementActionBase {
|
||||
@NotNull
|
||||
public static PsiElement[] createFileResource(@NotNull AndroidFacet facet,
|
||||
@NotNull final ResourceType resType,
|
||||
@NotNull String resName,
|
||||
@Nullable String resName,
|
||||
boolean chooseResName) {
|
||||
final CreateResourceFileAction action = getInstance();
|
||||
|
||||
|
||||
@@ -344,7 +344,7 @@ public class CreateXmlResourceDialog extends DialogWrapper {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ValidationInfo checkIfResourceAlreadyExists(@NotNull Module selectedModule,
|
||||
public static ValidationInfo checkIfResourceAlreadyExists(@NotNull Module selectedModule,
|
||||
@NotNull String resourceName,
|
||||
@NotNull ResourceType resourceType,
|
||||
@NotNull List<String> dirNames,
|
||||
|
||||
Reference in New Issue
Block a user