use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.
the class ViewEditorImpl method measureChildren.
@Nullable
@Override
public Map<NlComponent, Dimension> measureChildren(@NotNull NlComponent parent, @Nullable RenderTask.AttributeFilter filter) {
// TODO: Reuse snapshot!
Map<NlComponent, Dimension> unweightedSizes = Maps.newHashMap();
XmlTag parentTag = parent.getTag();
if (parentTag.isValid()) {
if (parent.getChildCount() == 0) {
return Collections.emptyMap();
}
Map<XmlTag, NlComponent> tagToComponent = Maps.newHashMapWithExpectedSize(parent.getChildCount());
for (NlComponent child : parent.getChildren()) {
tagToComponent.put(child.getTag(), child);
}
NlModel model = myScreen.getModel();
XmlFile xmlFile = model.getFile();
AndroidFacet facet = model.getFacet();
RenderService renderService = RenderService.get(facet);
RenderLogger logger = renderService.createLogger();
final RenderTask task = renderService.createTask(xmlFile, getConfiguration(), logger, null);
if (task == null) {
return null;
}
// Measure unweighted bounds
Map<XmlTag, ViewInfo> map = task.measureChildren(parentTag, filter);
task.dispose();
if (map != null) {
for (Map.Entry<XmlTag, ViewInfo> entry : map.entrySet()) {
ViewInfo viewInfo = entry.getValue();
viewInfo = RenderService.getSafeBounds(viewInfo);
Dimension size = new Dimension(viewInfo.getRight() - viewInfo.getLeft(), viewInfo.getBottom() - viewInfo.getTop());
NlComponent child = tagToComponent.get(entry.getKey());
if (child != null) {
unweightedSizes.put(child, size);
}
}
}
}
return unweightedSizes;
}
use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.
the class NlPreviewForm method setFile.
public boolean setFile(@Nullable PsiFile file) {
if (!isActive) {
// The form is not active so we just save the file to show once activate() is called
myInactiveFile = (XmlFile) file;
if (file != null) {
return false;
}
}
if (myPendingFile != null) {
if (file == myPendingFile.file) {
return false;
}
myPendingFile.invalidate();
// Set the model to null so the progressbar is displayed
mySurface.setModel(null);
} else if (file == myFile) {
return false;
}
AndroidFacet facet = file instanceof XmlFile ? AndroidFacet.getInstance(file) : null;
if (facet == null || file.getVirtualFile() == null) {
myPendingFile = null;
myFile = null;
setActiveModel(null);
} else {
XmlFile xmlFile = (XmlFile) file;
NlModel model = NlModel.create(mySurface, null, facet, xmlFile);
myPendingFile = new Pending(xmlFile, model);
}
return true;
}
use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.
the class ResizeTarget method mouseRelease.
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
NlComponent component = myComponent.getNlComponent();
AttributesTransaction attributes = component.startAttributeTransaction();
updateAttributes(attributes, x, y);
attributes.apply();
NlModel nlModel = component.getModel();
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
String label = "Constraint";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
myComponent.getScene().needsLayout(Scene.IMMEDIATE_LAYOUT);
}
use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.
the class DragTarget method mouseRelease.
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
if (myComponent.getParent() != null) {
boolean commitChanges = true;
if (Math.abs(x - myFirstMouseX) <= 1 && Math.abs(y - myFirstMouseY) <= 1) {
commitChanges = false;
}
NlComponent component = myComponent.getNlComponent();
AttributesTransaction attributes = component.startAttributeTransaction();
int dx = x - myOffsetX;
int dy = y - myOffsetY;
if (myCurrentNotchX != null) {
dx = myCurrentNotchX.apply(dx);
if (myComponent.allowsAutoConnect()) {
myCurrentNotchX.apply(attributes);
}
myCurrentNotchX = null;
}
if (myCurrentNotchY != null) {
dy = myCurrentNotchY.apply(dy);
if (myComponent.allowsAutoConnect()) {
myCurrentNotchY.apply(attributes);
}
myCurrentNotchY = null;
}
updateAttributes(attributes, dx, dy);
cleanup(attributes);
attributes.apply();
if (commitChanges) {
NlModel nlModel = component.getModel();
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
String label = "Component dragged";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
}
}
if (myChangedComponent) {
myComponent.getScene().needsLayout(Scene.IMMEDIATE_LAYOUT);
}
}
use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.
the class AnchorTarget method disconnectMe.
/**
* Disconnect the anchor
*
* @param component
*/
private void disconnectMe(NlComponent component) {
String label = "Constraint Disconnected";
NlModel nlModel = component.getModel();
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
AttributesTransaction attributes = component.startAttributeTransaction();
clearMe(attributes);
cleanup(attributes);
attributes.apply();
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
}
Aggregations