use of com.android.tools.idea.gradle.parser.BuildFileKey in project android by JetBrains.
the class NamedObjectPanel method init.
@Override
public void init() {
super.init();
myPanelGroup.myPanes.add(myDetailsPane);
if (myGradleBuildFile == null) {
return;
}
List<NamedObject> namedObjects = (List<NamedObject>) myGradleBuildFile.getValue(myBuildFileKey);
if (namedObjects != null) {
for (NamedObject object : namedObjects) {
// consistent between the case where you've customized it and you haven't.
if (myBuildFileKey == BuildFileKey.BUILD_TYPES && HARDCODED_BUILD_TYPES.contains(object.getName())) {
object = new UndeletableNamedObject(object);
}
addElement(object);
}
}
// If this is a flavor panel, add a synthetic flavor entry for defaultConfig.
if (myBuildFileKey == BuildFileKey.FLAVORS) {
GrStatementOwner defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
NamedObject obj = new UndeletableNamedObject(DEFAULT_CONFIG);
if (defaultConfig != null) {
for (BuildFileKey key : DEFAULT_CONFIG_KEYS) {
Object value = myGradleBuildFile.getValue(defaultConfig, key);
obj.setValue(key, value);
}
}
addElement(obj);
}
NamedObject.Factory objectFactory = (NamedObject.Factory) myBuildFileKey.getValueFactory();
if (objectFactory == null) {
throw new IllegalArgumentException("Can't instantiate a NamedObjectPanel for BuildFileKey " + myBuildFileKey.toString());
}
Collection<BuildFileKey> properties = objectFactory.getProperties();
// Query the model for its view of the world, and merge it with the build file-based view.
for (NamedObject obj : getObjectsFromModel(properties)) {
boolean found = false;
for (NamedObject o : myListModel) {
if (o.getName().equals(obj.getName())) {
found = true;
}
}
if (!found) {
NamedObject namedObject = new UndeletableNamedObject(obj.getName());
addElement(namedObject);
// Keep track of objects that are only in the model and not in the build file. We want to avoid creating them in the build file
// unless some value in them is changed to non-default.
myModelOnlyObjects.add(namedObject);
}
myModelObjects.put(obj.getName(), obj.getValues());
}
myList.updateUI();
myDetailsPane.init(myGradleBuildFile, properties);
if (myListModel.getSize() > 0) {
myList.setSelectedIndex(0);
}
updateUiFromCurrentObject();
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
updatePanelGroup();
}
}, ModalityState.any());
}
use of com.android.tools.idea.gradle.parser.BuildFileKey in project android by JetBrains.
the class KeyValuePane method updateCurrentObjectFromUi.
/**
* Reads the state of the UI form objects and writes them into the currently selected object in the list, setting the dirty bit as
* appropriate.
*/
private void updateCurrentObjectFromUi() {
if (myIsUpdating || myCurrentBuildFileObject == null) {
return;
}
for (Map.Entry<BuildFileKey, JComponent> entry : myProperties.entrySet()) {
BuildFileKey key = entry.getKey();
JComponent component = entry.getValue();
Object currentValue = myCurrentBuildFileObject.get(key);
Object newValue;
BuildFileKeyType type = key.getType();
switch(type) {
case BOOLEAN:
{
ComboBox comboBox = (ComboBox) component;
JBTextField editorComponent = (JBTextField) comboBox.getEditor().getEditorComponent();
int index = comboBox.getSelectedIndex();
if (index == 2) {
newValue = Boolean.FALSE;
editorComponent.setForeground(JBColor.BLACK);
} else if (index == 1) {
newValue = Boolean.TRUE;
editorComponent.setForeground(JBColor.BLACK);
} else {
newValue = null;
editorComponent.setForeground(JBColor.GRAY);
}
break;
}
case FILE:
case FILE_AS_STRING:
{
newValue = ((TextFieldWithBrowseButton) component).getText();
if ("".equals(newValue)) {
newValue = null;
}
if (newValue != null) {
newValue = new File(newValue.toString());
}
break;
}
case INTEGER:
{
try {
if (hasKnownValues(key)) {
String newStringValue = ((ComboBox) component).getEditor().getItem().toString();
newStringValue = getMappedValue(myKeysWithKnownValues.get(key).inverse(), newStringValue);
newValue = Integer.valueOf(newStringValue);
} else {
newValue = Integer.valueOf(((JBTextField) component).getText());
}
} catch (Exception e) {
newValue = null;
}
break;
}
case REFERENCE:
{
newValue = ((ComboBox) component).getEditor().getItem();
String newStringValue = (String) newValue;
if (hasKnownValues(key)) {
newStringValue = getMappedValue(myKeysWithKnownValues.get(key).inverse(), newStringValue);
}
if (newStringValue != null && newStringValue.isEmpty()) {
newStringValue = null;
}
String prefix = getReferencePrefix(key);
if (newStringValue != null && !newStringValue.startsWith(prefix)) {
newStringValue = prefix + newStringValue;
}
newValue = newStringValue;
break;
}
case CLOSURE:
case STRING:
default:
{
if (hasKnownValues(key)) {
String newStringValue = ((ComboBox) component).getEditor().getItem().toString();
newStringValue = getMappedValue(myKeysWithKnownValues.get(key).inverse(), newStringValue);
if (newStringValue.isEmpty()) {
newStringValue = null;
}
newValue = newStringValue;
} else {
newValue = ((JBTextField) component).getText();
if ("".equals(newValue)) {
newValue = null;
}
}
if (type == BuildFileKeyType.CLOSURE && newValue != null) {
List newListValue = new ArrayList();
for (String s : Splitter.on(',').omitEmptyStrings().trimResults().split((String) newValue)) {
newListValue.add(key.getValueFactory().parse(s, myProject));
}
newValue = newListValue;
}
break;
}
}
if (!Objects.equal(currentValue, newValue)) {
if (newValue == null) {
myCurrentBuildFileObject.remove(key);
} else {
myCurrentBuildFileObject.put(key, newValue);
}
if (GradleBuildFile.shouldWriteValue(currentValue, newValue)) {
myListener.modified(key);
}
}
}
}
use of com.android.tools.idea.gradle.parser.BuildFileKey in project android by JetBrains.
the class KeyValuePane method getReferencePrefix.
@NotNull
private static String getReferencePrefix(@NotNull BuildFileKey key) {
BuildFileKey containerType = key.getContainerType();
if (containerType != null) {
String path = containerType.getPath();
String lastLeaf = path.substring(path.lastIndexOf('/') + 1);
return lastLeaf + ".";
} else {
return "";
}
}
use of com.android.tools.idea.gradle.parser.BuildFileKey in project android by JetBrains.
the class NamedObjectPanel method apply.
@Override
public void apply() {
if ((!myModified && myModifiedKeys.isEmpty()) || myGradleBuildFile == null) {
return;
}
List<NamedObject> objects = Lists.newArrayList();
for (NamedObject obj : myListModel) {
// Save the defaultConfig separately and don't write it out as a regular flavor.
if (myBuildFileKey == BuildFileKey.FLAVORS && obj.getName().equals(DEFAULT_CONFIG)) {
GrStatementOwner defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
if (defaultConfig == null) {
myGradleBuildFile.setValue(BuildFileKey.DEFAULT_CONFIG, "{}");
defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
}
assert defaultConfig != null;
for (BuildFileKey key : DEFAULT_CONFIG_KEYS) {
if (!isModified(obj, key)) {
continue;
}
Object value = obj.getValue(key);
if (value != null) {
myGradleBuildFile.setValue(defaultConfig, key, value);
} else {
myGradleBuildFile.removeValue(defaultConfig, key);
}
}
} else if (myModelOnlyObjects.contains(obj) && isObjectEmpty(obj)) {
// If this object wasn't in the build file to begin with and doesn't have non-default values, don't write it out.
continue;
} else {
objects.add(obj);
}
}
myGradleBuildFile.setValue(myBuildFileKey, objects, new ValueFactory.KeyFilter() {
@Override
public boolean shouldWriteKey(BuildFileKey key, Object object) {
return isModified((NamedObject) object, key);
}
});
myModified = false;
myModifiedKeys.clear();
}
use of com.android.tools.idea.gradle.parser.BuildFileKey in project android by JetBrains.
the class KeyValuePane method updateReferenceValues.
public void updateReferenceValues(@NotNull BuildFileKey containerProperty, @NotNull Iterable<String> values) {
BuildFileKey itemType = containerProperty.getItemType();
if (itemType == null) {
return;
}
ComboBox comboBox = (ComboBox) myProperties.get(itemType);
if (comboBox == null) {
return;
}
myIsUpdating = true;
try {
String currentValue = comboBox.getEditor().getItem().toString();
comboBox.removeAllItems();
for (String value : values) {
comboBox.addItem(value);
}
comboBox.setSelectedItem(currentValue);
} finally {
myIsUpdating = false;
}
}
Aggregations