mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
android: add device configurator to "create resource" action
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.android.actions.CreateResourceDialog">
|
||||
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="5" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="407" height="170"/>
|
||||
@@ -22,11 +22,6 @@
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<vspacer id="5d2d6">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="6bfe" class="javax.swing.JTextField" binding="myFileNameField">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
@@ -57,6 +52,39 @@
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<grid id="8653" binding="myDeviceConfiguratorWrapper" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<component id="f676e" class="com.intellij.ui.components.JBLabel" binding="myErrorLabel">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="fda57" class="com.intellij.ui.components.JBLabel">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<labelFor value="90507"/>
|
||||
<text value="Director&y name:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="90507" class="javax.swing.JTextField" binding="myDirectoryNameTextField">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="false"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
|
||||
@@ -16,15 +16,23 @@
|
||||
|
||||
package org.jetbrains.android.actions;
|
||||
|
||||
import com.android.ide.common.resources.configuration.FolderConfiguration;
|
||||
import com.android.resources.ResourceFolderType;
|
||||
import com.intellij.ide.actions.TemplateKindCombo;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.InputValidator;
|
||||
import com.intellij.ui.components.JBLabel;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import org.jetbrains.android.uipreview.DeviceConfiguratorPanel;
|
||||
import org.jetbrains.android.uipreview.InvalidOptionValueException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -41,9 +49,13 @@ public abstract class CreateResourceDialog extends DialogWrapper {
|
||||
private JPanel myPanel;
|
||||
private JLabel myUpDownHint;
|
||||
private JLabel myResTypeLabel;
|
||||
private JPanel myDeviceConfiguratorWrapper;
|
||||
private JBLabel myErrorLabel;
|
||||
private JTextField myDirectoryNameTextField;
|
||||
private InputValidator myValidator;
|
||||
|
||||
private final Map<String, CreateTypedResourceFileAction> myResType2ActionMap = new HashMap<String, CreateTypedResourceFileAction>();
|
||||
private final DeviceConfiguratorPanel myDeviceConfiguratorPanel;
|
||||
|
||||
public CreateResourceDialog(Project project, Collection<CreateTypedResourceFileAction> actions, CreateTypedResourceFileAction selected) {
|
||||
super(project);
|
||||
@@ -57,16 +69,59 @@ public abstract class CreateResourceDialog extends DialogWrapper {
|
||||
myResType2ActionMap.put(resType, action);
|
||||
myResourceTypeCombo.addItem(action.toString(), null, resType);
|
||||
}
|
||||
|
||||
myDeviceConfiguratorPanel = new DeviceConfiguratorPanel(null) {
|
||||
@Override
|
||||
public void applyEditors() {
|
||||
try {
|
||||
doApplyEditors();
|
||||
final FolderConfiguration config = myDeviceConfiguratorPanel.getConfiguration();
|
||||
final CreateTypedResourceFileAction selectedAction = getSelectedAction();
|
||||
myErrorLabel.setText("");
|
||||
myDirectoryNameTextField.setText("");
|
||||
if (selectedAction != null) {
|
||||
final String resTypeStr = selectedAction.getResourceType();
|
||||
if (resTypeStr != null) {
|
||||
final ResourceFolderType resFolderType = ResourceFolderType.getTypeByName(resTypeStr);
|
||||
if (resFolderType != null) {
|
||||
myDirectoryNameTextField.setText(config.getFolderName(resFolderType));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InvalidOptionValueException e) {
|
||||
myErrorLabel.setText("<html><body><font color=\"red\">" + e.getMessage() + "</font></body></html>");
|
||||
myDirectoryNameTextField.setText("");
|
||||
}
|
||||
setOKActionEnabled(myDirectoryNameTextField.getText().length() > 0);
|
||||
}
|
||||
};
|
||||
|
||||
myResourceTypeCombo.getComboBox().addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
myDeviceConfiguratorPanel.applyEditors();
|
||||
}
|
||||
});
|
||||
|
||||
myDeviceConfiguratorPanel.updateAll();
|
||||
myDeviceConfiguratorWrapper.add(myDeviceConfiguratorPanel, BorderLayout.CENTER);
|
||||
setOKActionEnabled(myDirectoryNameTextField.getText().length() > 0);
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
protected abstract InputValidator createValidator(@NotNull String resourceType);
|
||||
protected abstract InputValidator createValidator(@NotNull String subdirName);
|
||||
|
||||
@Override
|
||||
protected void doOKAction() {
|
||||
String fileName = myFileNameField.getText();
|
||||
assert getSelectedAction() != null;
|
||||
myValidator = createValidator(getSelectedAction().getResourceType());
|
||||
|
||||
final String subdirName = myDirectoryNameTextField.getText();
|
||||
assert subdirName != null && subdirName.length() > 0;
|
||||
|
||||
myValidator = createValidator(subdirName);
|
||||
if (myValidator.checkInput(fileName) && myValidator.canClose(fileName)) {
|
||||
super.doOKAction();
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ public abstract class CreateResourceDirectoryDialog extends DialogWrapper {
|
||||
});
|
||||
|
||||
myDeviceConfiguratorPanel.updateAll();
|
||||
setOKActionEnabled(myDirectoryNameTextField.getText().length() > 0);
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.android.actions;
|
||||
|
||||
import com.android.AndroidConstants;
|
||||
import com.intellij.CommonBundle;
|
||||
import com.intellij.ide.actions.CreateElementActionBase;
|
||||
import com.intellij.openapi.actionSystem.ActionManager;
|
||||
@@ -106,8 +107,8 @@ public class CreateResourceFileAction extends CreateElementActionBase {
|
||||
CreateTypedResourceFileAction createLayoutAction = mySubactions.get("layout");
|
||||
CreateResourceDialog dialog = new CreateResourceDialog(project, mySubactions.values(), createLayoutAction) {
|
||||
@Override
|
||||
protected InputValidator createValidator(@NotNull String resourceType) {
|
||||
return CreateResourceFileAction.this.createValidator(project, directory, resourceType);
|
||||
protected InputValidator createValidator(@NotNull String subdirName) {
|
||||
return CreateResourceFileAction.this.createValidator(project, directory, subdirName);
|
||||
}
|
||||
};
|
||||
dialog.setTitle(getCommandName());
|
||||
@@ -120,12 +121,12 @@ public class CreateResourceFileAction extends CreateElementActionBase {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private MyInputValidator createValidator(Project project, final PsiDirectory resDir, final String resType) {
|
||||
PsiDirectory resSubdir = resDir.findSubdirectory(resType);
|
||||
private MyInputValidator createValidator(Project project, final PsiDirectory resDir, final String subdirName) {
|
||||
PsiDirectory resSubdir = resDir.findSubdirectory(subdirName);
|
||||
if (resSubdir == null) {
|
||||
resSubdir = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
|
||||
public PsiDirectory compute() {
|
||||
return resDir.createSubdirectory(resType);
|
||||
return resDir.createSubdirectory(subdirName);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -145,7 +146,12 @@ public class CreateResourceFileAction extends CreateElementActionBase {
|
||||
}
|
||||
|
||||
private CreateTypedResourceFileAction getActionByDir(PsiDirectory directory) {
|
||||
return mySubactions.get(directory.getName());
|
||||
String baseDirName = directory.getName();
|
||||
final int index = baseDirName.indexOf(AndroidConstants.RES_QUALIFIER_SEP);
|
||||
if (index >= 0) {
|
||||
baseDirName = baseDirName.substring(0, index);
|
||||
}
|
||||
return mySubactions.get(baseDirName);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user