This commit is contained in:
Alexander Lobas
2012-07-27 19:53:10 +04:00
parent 9b430f7f2d
commit 7e0cee5506
4 changed files with 156 additions and 15 deletions
@@ -83,6 +83,7 @@ public final class AndroidDesignerEditorPanel extends DesignerEditorPanel {
private final ExternalPSIChangeListener myPSIChangeListener;
private final ProfileAction myProfileAction;
private final Alarm mySessionAlarm = new Alarm();
private FolderConfiguration myLastRenderedConfiguration;
private volatile RenderSession mySession;
private boolean myParseTime;
private int myProfileLastVersion;
@@ -181,7 +182,7 @@ public final class AndroidDesignerEditorPanel extends DesignerEditorPanel {
public void consume(RenderSession session) throws Throwable {
RootView rootView = new RootView(30, 20, session.getImage());
try {
parser.updateRootComponent(session, rootView);
parser.updateRootComponent(myLastRenderedConfiguration, session, rootView);
}
catch (Throwable e) {
myRootComponent = parser.getRootComponent();
@@ -252,17 +253,17 @@ public final class AndroidDesignerEditorPanel extends DesignerEditorPanel {
throw new RenderingException("Device is not specified");
}
FolderConfiguration config = new FolderConfiguration();
config.set(deviceConfiguration.getConfiguration());
config.setUiModeQualifier(new UiModeQualifier(manager.getSelectedDockMode()));
config.setNightModeQualifier(new NightModeQualifier(manager.getSelectedNightMode()));
myLastRenderedConfiguration = new FolderConfiguration();
myLastRenderedConfiguration.set(deviceConfiguration.getConfiguration());
myLastRenderedConfiguration.setUiModeQualifier(new UiModeQualifier(manager.getSelectedDockMode()));
myLastRenderedConfiguration.setNightModeQualifier(new NightModeQualifier(manager.getSelectedNightMode()));
LocaleData locale = manager.getSelectedLocale();
if (locale == null) {
throw new RenderingException("Locale is not specified");
}
config.setLanguageQualifier(new LanguageQualifier(locale.getLanguage()));
config.setRegionQualifier(new RegionQualifier(locale.getRegion()));
myLastRenderedConfiguration.setLanguageQualifier(new LanguageQualifier(locale.getLanguage()));
myLastRenderedConfiguration.setRegionQualifier(new RegionQualifier(locale.getRegion()));
float xdpi = deviceConfiguration.getDevice().getXDpi();
float ydpi = deviceConfiguration.getDevice().getYDpi();
@@ -274,8 +275,18 @@ public final class AndroidDesignerEditorPanel extends DesignerEditorPanel {
throw new RenderingException();
}
RenderingResult result =
RenderUtil.renderLayout(getModule(), layoutXmlText, myFile, null, target, facet, config, xdpi, ydpi, theme, 10000, true);
RenderingResult result = RenderUtil.renderLayout(getModule(),
layoutXmlText,
myFile,
null,
target,
facet,
myLastRenderedConfiguration,
xdpi,
ydpi,
theme,
10000,
true);
if (ApplicationManagerEx.getApplicationEx().isInternal()) {
System.out.println("Render time: " + (System.currentTimeMillis() - time)); // XXX
@@ -346,7 +357,7 @@ public final class AndroidDesignerEditorPanel extends DesignerEditorPanel {
RadViewComponent rootComponent = (RadViewComponent)myRootComponent;
RootView rootView = (RootView)rootComponent.getNativeComponent();
rootView.setImage(session.getImage());
ModelParser.updateRootComponent(rootComponent, session, rootView);
ModelParser.updateRootComponent(myLastRenderedConfiguration, rootComponent, session, rootView);
myParseTime = false;
@@ -17,6 +17,7 @@ package com.intellij.android.designer.model;
import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.ViewInfo;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.sdklib.SdkConstants;
import com.intellij.android.designer.designSurface.AndroidPasteFactory;
import com.intellij.android.designer.designSurface.RootView;
@@ -41,6 +42,7 @@ import org.jdom.Attribute;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.List;
/**
@@ -52,6 +54,7 @@ public class ModelParser extends XmlRecursiveElementVisitor {
public static final String XML_FILE_KEY = "XML_FILE";
public static final String MODULE_KEY = "MODULE";
public static final String FOLDER_CONFIG_KEY = "FOLDER_CONFIG";
private static final int EMPTY_COMPONENT_SIZE = 5;
@@ -379,7 +382,7 @@ public class ModelParser extends XmlRecursiveElementVisitor {
return myLayoutXmlText;
}
public void updateRootComponent(RenderSession session, RootView nativeComponent) throws Exception {
public void updateRootComponent(FolderConfiguration configuration, RenderSession session, RootView nativeComponent) throws Exception {
if (myRootComponent == null) {
myRootComponent = createComponent(myXmlFile.getRootTag(), myMetaManager.getModelByTag("<root>"));
}
@@ -389,13 +392,16 @@ public class ModelParser extends XmlRecursiveElementVisitor {
myRootComponent.add(rootComponent, null);
}
updateRootComponent(myRootComponent, session, nativeComponent);
updateRootComponent(configuration, myRootComponent, session, nativeComponent);
myRootComponent.setClientProperty(IdManager.KEY, myIdManager);
}
public static void updateRootComponent(RadViewComponent rootComponent,
public static void updateRootComponent(FolderConfiguration configuration,
RadViewComponent rootComponent,
RenderSession session,
RootView nativeComponent) {
rootComponent.setClientProperty(FOLDER_CONFIG_KEY, configuration);
updateChildren(rootComponent, session.getRootViews(), nativeComponent, 0, 0);
rootComponent.setNativeComponent(nativeComponent);
@@ -442,7 +448,23 @@ public class ModelParser extends XmlRecursiveElementVisitor {
updateComponent(childComponent, views.get(viewIndex), nativeComponent, left, top);
}
else {
// XXX
Rectangle bounds = null;
for (int subViewIndex = 0; subViewIndex < childViewCount; subViewIndex++) {
ViewInfo view = views.get(viewIndex + subViewIndex);
Rectangle viewBounds =
new Rectangle(view.getLeft(), view.getTop(), view.getRight() - view.getLeft(), view.getBottom() - view.getTop());
if (bounds == null) {
bounds = viewBounds;
}
else {
bounds = bounds.union(viewBounds);
}
}
updateComponent(childComponent,
new ViewInfo(childComponent.getMetaModel().getTarget(), null, bounds.x, bounds.y, bounds.x + bounds.width,
bounds.y + bounds.height),
nativeComponent, left, top);
}
viewIndex += childViewCount;
@@ -15,6 +15,9 @@
*/
package com.intellij.android.designer.model;
import com.android.AndroidConstants;
import com.android.ide.common.resources.configuration.Configurable;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.intellij.android.designer.propertyTable.IdProperty;
import com.intellij.android.designer.propertyTable.IncludeLayoutProperty;
import com.intellij.android.designer.propertyTable.editors.ResourceDialog;
@@ -22,6 +25,15 @@ import com.intellij.designer.ModuleProvider;
import com.intellij.designer.model.RadComponent;
import com.intellij.designer.propertyTable.Property;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import org.jetbrains.android.dom.converters.AndroidResourceReference;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@@ -30,6 +42,9 @@ import java.util.List;
* @author Alexander Lobas
*/
public class RadIncludeLayout extends RadViewComponent implements IConfigurableComponent {
private int myViewInfoCount = -1;
private FolderConfiguration myConfiguration;
@Override
public String getCreationXml() {
return "<include android:layout_width=\"wrap_content\"\n" +
@@ -62,8 +77,99 @@ public class RadIncludeLayout extends RadViewComponent implements IConfigurableC
super.setProperties(properties);
}
public void clearViewInfoCount() {
myViewInfoCount = -1;
}
@Override
public int getViewInfoCount() {
return super.getViewInfoCount(); // TODO: Auto-generated method stub
FolderConfiguration configuration = getRoot().getClientProperty(ModelParser.FOLDER_CONFIG_KEY);
if (myViewInfoCount != -1 && !configuration.isMatchFor(myConfiguration)) {
myViewInfoCount = -1;
}
if (myViewInfoCount == -1) {
myConfiguration = configuration;
XmlAttribute layout = getTag().getAttribute("layout");
XmlAttributeValue value = layout == null ? null : layout.getValueElement();
if (value == null) {
myViewInfoCount = 0;
}
else {
PsiReference reference = value.getReference();
if (reference == null) {
myViewInfoCount = 0;
}
else {
XmlFile xmlFile = findFile(configuration, (AndroidResourceReference)reference);
XmlTag tag = xmlFile == null ? null : xmlFile.getRootTag();
if (tag == null) {
myViewInfoCount = 0;
}
else if ("merge".equalsIgnoreCase(tag.getName())) {
myViewInfoCount = tag.getSubTags().length;
}
else {
myViewInfoCount = 1;
}
}
}
}
return myViewInfoCount;
}
@Nullable
private static XmlFile findFile(FolderConfiguration configuration, AndroidResourceReference reference) {
PsiElement[] elements = reference.computeTargetElements();
if (elements.length == 0) {
return null;
}
if (elements.length == 1) {
return (XmlFile)elements[0];
}
List<MyConfigurable> configurables = new ArrayList<MyConfigurable>();
for (PsiElement element : elements) {
XmlFile xmlFile = (XmlFile)element;
VirtualFile file = xmlFile.getVirtualFile();
if (file == null) {
continue;
}
VirtualFile folder = file.getParent();
if (folder == null) {
continue;
}
FolderConfiguration includeConfig = FolderConfiguration.getConfig(folder.getName().split(AndroidConstants.RES_QUALIFIER_SEP));
configurables.add(new MyConfigurable(xmlFile, includeConfig));
}
MyConfigurable configurable = (MyConfigurable)configuration.findMatchingConfigurable(configurables);
return configurable == null ? null : configurable.getFile();
}
private static class MyConfigurable implements Configurable {
private final XmlFile myFile;
private final FolderConfiguration myConfig;
private MyConfigurable(XmlFile file, FolderConfiguration config) {
myFile = file;
myConfig = config;
}
@Override
public FolderConfiguration getConfiguration() {
return myConfig;
}
public XmlFile getFile() {
return myFile;
}
}
}
@@ -16,6 +16,7 @@
package com.intellij.android.designer.propertyTable;
import com.android.resources.ResourceType;
import com.intellij.android.designer.model.RadIncludeLayout;
import com.intellij.android.designer.model.RadViewComponent;
import com.intellij.android.designer.propertyTable.editors.ResourceEditor;
import com.intellij.android.designer.propertyTable.renderers.ResourceRenderer;
@@ -69,6 +70,7 @@ public class IncludeLayoutProperty extends Property<RadViewComponent> implements
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
((RadIncludeLayout)component).clearViewInfoCount();
component.getTag().setAttribute("layout", (String)value);
}
});