GridLayout

This commit is contained in:
Alexander Lobas
2012-04-24 21:53:05 +04:00
parent 9ff35c1843
commit 2e3eb9951d
7 changed files with 152 additions and 23 deletions
@@ -16,8 +16,13 @@
package com.intellij.android.designer.designSurface.layout;
import com.intellij.android.designer.designSurface.layout.grid.GridOperation;
import com.intellij.android.designer.model.RadViewComponent;
import com.intellij.android.designer.model.grid.GridInfo;
import com.intellij.android.designer.model.grid.GridInsertType;
import com.intellij.android.designer.model.layout.grid.RadGridLayoutComponent;
import com.intellij.designer.designSurface.OperationContext;
import com.intellij.designer.model.RadComponent;
import org.jetbrains.annotations.Nullable;
/**
* @author Alexander Lobas
@@ -29,6 +34,57 @@ public class GridLayoutOperation extends GridOperation {
@Override
public void execute() throws Exception {
GridInfo gridInfo = getGridInfo();
RadViewComponent container = (RadViewComponent)myContainer;
RadComponent editComponent = myComponents.get(0);
if (myInsertType == GridInsertType.in_cell) {
if (myRow <= gridInfo.lastRow && myColumn <= gridInfo.lastColumn) {
RadComponent insertBefore = getNextComponent(myRow, myColumn);
if (editComponent != insertBefore) {
execute(myContext, container, myComponents, (RadViewComponent)insertBefore);
}
RadGridLayoutComponent.setCellIndex(editComponent, myRow, myColumn);
}
else {
execute(myContext, container, myComponents, null);
RadGridLayoutComponent.setCellIndex(editComponent, myRow, myColumn);
RadGridLayoutComponent.setGridSize(container,
Math.max(myRow + 1, gridInfo.lastRow + 1),
Math.max(myColumn + 1, gridInfo.lastColumn + 1));
}
}
// TODO: Auto-generated method stub
}
@Nullable
private RadComponent getNextComponent(int row, int column) {
GridInfo gridInfo = getGridInfo();
RadComponent[][] components = gridInfo.components;
RadComponent[] rowComponents = components[row];
for (int i = column + 1; i < rowComponents.length; i++) {
RadComponent component = rowComponents[i];
if (component != null) {
return component;
}
}
for (int i = row + 1; i < components.length; i++) {
rowComponents = components[i];
for (int j = 0; i < rowComponents.length; j++) {
RadComponent component = rowComponents[j];
if (component != null) {
return component;
}
}
}
return null;
}
}
@@ -184,7 +184,16 @@ public class TableLayoutOperation extends GridOperation {
RadComponent cellComponent = rowComponents[j];
if (cellComponent != null) {
RadTableLayoutComponent.setCellIndex(cellComponent, j + 1);
if (j == startColumn && startColumn > 0 && cellComponent == rowComponents[j - 1]) {
RadTableLayoutComponent.setCellSpan(cellComponent, RadTableLayoutComponent.getCellSpan(cellComponent) + 1);
}
else {
RadTableLayoutComponent.setCellIndex(cellComponent, j + 1);
}
while (j + 1 < rowComponents.length && cellComponent == rowComponents[j + 1]) {
j++;
}
}
}
}
@@ -150,6 +150,12 @@ public abstract class GridOperation extends AbstractEditOperation {
@Override
public abstract void execute() throws Exception;
//////////////////////////////////////////////////////////////////////////////////////////
//
// Grid
//
//////////////////////////////////////////////////////////////////////////////////////////
private static final int CROSS_SIZE = 10;
private void calculateGridInfo() {
@@ -16,11 +16,14 @@
package com.intellij.android.designer.model.layout.grid;
import com.android.ide.common.rendering.api.ViewInfo;
import com.intellij.android.designer.model.ModelParser;
import com.intellij.android.designer.model.RadViewComponent;
import com.intellij.android.designer.model.RadViewContainer;
import com.intellij.android.designer.model.grid.GridInfo;
import com.intellij.android.designer.model.grid.IGridProvider;
import com.intellij.designer.model.RadComponent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.xml.XmlTag;
import java.awt.*;
import java.lang.reflect.Field;
@@ -170,4 +173,28 @@ public class RadGridLayoutComponent extends RadViewContainer implements IGridPro
return cellInfo;
}
public static void setGridSize(final RadViewComponent container, final int rowCount, final int columnCount) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
XmlTag tag = container.getTag();
tag.setAttribute("android:rowCount", Integer.toString(rowCount));
tag.setAttribute("android:columnCount", Integer.toString(columnCount));
}
});
}
public static void setCellIndex(final RadComponent component, final int row, final int column) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
XmlTag tag = ((RadViewComponent)component).getTag();
tag.setAttribute("android:layout_row", Integer.toString(row));
tag.setAttribute("android:layout_column", Integer.toString(column));
ModelParser.deleteAttribute(tag, "android:layout_rowSpan");
ModelParser.deleteAttribute(tag, "android:layout_columnSpan");
}
});
}
}
@@ -19,6 +19,8 @@ import com.intellij.android.designer.model.grid.GridInfo;
import com.intellij.android.designer.model.grid.RadCaptionColumn;
import com.intellij.designer.model.RadComponent;
import java.util.Arrays;
/**
* @author Alexander Lobas
*/
@@ -34,16 +36,35 @@ public class RadCaptionTableColumn extends RadCaptionColumn<RadTableLayoutCompon
for (RadComponent[] rowComponents : components) {
RadComponent component = rowComponents[myIndex];
int nextIndex = myIndex + 1;
if (component != null) {
component.delete();
rowComponents[myIndex] = null;
while (nextIndex < rowComponents.length && component == rowComponents[nextIndex]) {
nextIndex++;
}
if (myIndex > 0 && component == rowComponents[myIndex - 1]) {
RadTableLayoutComponent.setCellSpan(component, RadTableLayoutComponent.getCellSpan(component) - 1);
}
else {
Arrays.fill(rowComponents, myIndex, nextIndex, null);
RadComponent parent = component.getParent();
component.delete();
if (parent.getChildren().isEmpty()) {
parent.delete();
}
}
}
for (int i = myIndex + 1; i < rowComponents.length; i++) {
for (int i = nextIndex; i < rowComponents.length; i++) {
RadComponent cellComponent = rowComponents[i];
if (cellComponent != null) {
RadTableLayoutComponent.setCellIndex(cellComponent, i - 1);
while (i + 1 < rowComponents.length && cellComponent == rowComponents[i + 1]) {
i++;
}
}
}
}
@@ -169,17 +169,6 @@ public class RadTableLayoutComponent extends RadViewContainer implements IGridPr
return components;
}
public static void setCellIndex(final RadComponent component, final int column) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
XmlTag tag = ((RadViewComponent)component).getTag();
tag.setAttribute("android:layout_column", Integer.toString(column));
ModelParser.deleteAttribute(tag, "android:layout_span");
}
});
}
private static int getCellIndex(RadComponent component) {
try {
String column = ((RadViewComponent)component).getTag().getAttributeValue("android:layout_column");
@@ -199,4 +188,25 @@ public class RadTableLayoutComponent extends RadViewContainer implements IGridPr
return 1;
}
}
public static void setCellIndex(final RadComponent component, final int column) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
XmlTag tag = ((RadViewComponent)component).getTag();
tag.setAttribute("android:layout_column", Integer.toString(column));
ModelParser.deleteAttribute(tag, "android:layout_span");
}
});
}
public static void setCellSpan(final RadComponent component, final int span) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
XmlTag tag = ((RadViewComponent)component).getTag();
tag.setAttribute("android:layout_span", Integer.toString(span));
}
});
}
}
@@ -1163,6 +1163,13 @@
<!-- ================= Palette ================= -->
<palette>
<group name="Layouts">
<item tag="FrameLayout"/>
<item tag="LinearLayout"/>
<item tag="TableLayout"/>
<item tag="TableRow"/>
<item tag="GridLayout"/>
</group>
<group name="Widgets">
<item tag="Button"/>
<item tag="RadioButton"/>
@@ -1218,13 +1225,6 @@
<item tag="TextSwitcher"/>
<item tag="AdapterViewFlipper"/>
</group>
<group name="Layouts">
<item tag="FrameLayout"/>
<item tag="LinearLayout"/>
<item tag="TableLayout"/>
<item tag="TableRow"/>
<item tag="GridLayout"/>
</group>
</palette>
</meta-model>