From 00ffec389c6ab63fcccbf02f175328b8eec84b95 Mon Sep 17 00:00:00 2001 From: Alexander Lobas Date: Fri, 18 May 2012 14:58:18 +0400 Subject: [PATCH] include tag --- .../AndroidDesignerEditorPanel.java | 7 +- .../android/designer/model/ModelParser.java | 3 +- ...deComponent.java => RadIncludeLayout.java} | 36 +++++-- .../designer/model/RadViewComponent.java | 4 + .../designer/model/views-meta-model.xml | 7 +- .../propertyTable/IncludeLayoutProperty.java | 89 +++++++++++++++++ .../propertyTable/editors/ResourceEditor.java | 99 +++++++++++-------- 7 files changed, 191 insertions(+), 54 deletions(-) rename plugins/android-designer/src/com/intellij/android/designer/model/{RadIncludeComponent.java => RadIncludeLayout.java} (50%) create mode 100644 plugins/android-designer/src/com/intellij/android/designer/propertyTable/IncludeLayoutProperty.java diff --git a/plugins/android-designer/src/com/intellij/android/designer/designSurface/AndroidDesignerEditorPanel.java b/plugins/android-designer/src/com/intellij/android/designer/designSurface/AndroidDesignerEditorPanel.java index 676a3e3f970f..6255d57e7c42 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/designSurface/AndroidDesignerEditorPanel.java +++ b/plugins/android-designer/src/com/intellij/android/designer/designSurface/AndroidDesignerEditorPanel.java @@ -22,6 +22,7 @@ import com.intellij.android.designer.actions.ProfileAction; import com.intellij.android.designer.componentTree.AndroidTreeDecorator; import com.intellij.android.designer.model.ModelParser; import com.intellij.android.designer.model.PropertyParser; +import com.intellij.android.designer.model.RadIncludeLayout; import com.intellij.android.designer.model.RadViewComponent; import com.intellij.android.designer.profile.ProfileManager; import com.intellij.designer.DesignerToolWindowManager; @@ -441,7 +442,11 @@ public final class AndroidDesignerEditorPanel extends DesignerEditorPanel { @NotNull @Override public RadComponent create() throws Exception { - return ModelParser.createComponent(null, paletteItem.getMetaModel()); + RadViewComponent component = ModelParser.createComponent(null, paletteItem.getMetaModel()); + if (component instanceof RadIncludeLayout) { + ((RadIncludeLayout)component).configure(getModule()); + } + return component; } }; } diff --git a/plugins/android-designer/src/com/intellij/android/designer/model/ModelParser.java b/plugins/android-designer/src/com/intellij/android/designer/model/ModelParser.java index 82b217020b22..104585082d1d 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/model/ModelParser.java +++ b/plugins/android-designer/src/com/intellij/android/designer/model/ModelParser.java @@ -177,7 +177,8 @@ public class ModelParser extends XmlRecursiveElementVisitor { addComponentTag(container.getTag(), newComponent, insertBefore == null ? null : insertBefore.getTag(), new Computable() { @Override public String compute() { - return newComponent.getMetaModel().getCreation(); + String creation = newComponent.getMetaModel().getCreation(); + return creation == null ? newComponent.getCreationXml() : creation; } }); diff --git a/plugins/android-designer/src/com/intellij/android/designer/model/RadIncludeComponent.java b/plugins/android-designer/src/com/intellij/android/designer/model/RadIncludeLayout.java similarity index 50% rename from plugins/android-designer/src/com/intellij/android/designer/model/RadIncludeComponent.java rename to plugins/android-designer/src/com/intellij/android/designer/model/RadIncludeLayout.java index d8305c0db939..febad7a1c2ad 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/model/RadIncludeComponent.java +++ b/plugins/android-designer/src/com/intellij/android/designer/model/RadIncludeLayout.java @@ -16,7 +16,11 @@ package com.intellij.android.designer.model; import com.intellij.android.designer.propertyTable.IdProperty; +import com.intellij.android.designer.propertyTable.IncludeLayoutProperty; +import com.intellij.android.designer.propertyTable.editors.ResourceDialog; import com.intellij.designer.propertyTable.Property; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.ui.DialogWrapper; import org.jetbrains.android.dom.attrs.AttributeDefinition; import org.jetbrains.android.dom.attrs.AttributeFormat; @@ -27,20 +31,38 @@ import java.util.List; /** * @author Alexander Lobas */ -public class RadIncludeComponent extends RadViewComponent { - private final List myProperties = new ArrayList(); +public class RadIncludeLayout extends RadViewComponent { + private static final Property LAYOUT_PROPERTY = new IncludeLayoutProperty(); + private static final Property ID_PROPERTY = new IdProperty("id", new AttributeDefinition("id", Arrays.asList(AttributeFormat.Reference))); - public RadIncludeComponent() { - IdProperty idProperty = new IdProperty("id", new AttributeDefinition("id", Arrays.asList(AttributeFormat.Reference))); - idProperty.setImportant(true); - myProperties.add(idProperty); + static { + LAYOUT_PROPERTY.setImportant(true); + ID_PROPERTY.setImportant(true); + } + + @Override + public String getCreationXml() { + return ""; + } + + public void configure(Module module) throws Exception { + ResourceDialog dialog = new ResourceDialog(module, IncludeLayoutProperty.TYPES); + dialog.show(); + + if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) { + setClientProperty(IncludeLayoutProperty.NAME, dialog.getResourceName()); + } + else { + throw new Exception(); + } } @Override public void setProperties(List properties) { if (!properties.isEmpty()) { properties = new ArrayList(properties); - properties.addAll(myProperties); + properties.add(LAYOUT_PROPERTY); + properties.add(ID_PROPERTY); } super.setProperties(properties); } diff --git a/plugins/android-designer/src/com/intellij/android/designer/model/RadViewComponent.java b/plugins/android-designer/src/com/intellij/android/designer/model/RadViewComponent.java index 6e81f9f11c92..a472aa3aaf51 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/model/RadViewComponent.java +++ b/plugins/android-designer/src/com/intellij/android/designer/model/RadViewComponent.java @@ -61,6 +61,10 @@ public class RadViewComponent extends RadVisualComponent { } } + public String getCreationXml() { + throw new UnsupportedOperationException(); + } + public ViewInfo getViewInfo() { return myViewInfo; } diff --git a/plugins/android-designer/src/com/intellij/android/designer/model/views-meta-model.xml b/plugins/android-designer/src/com/intellij/android/designer/model/views-meta-model.xml index 421c4cc74d9e..3f765a78f624 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/model/views-meta-model.xml +++ b/plugins/android-designer/src/com/intellij/android/designer/model/views-meta-model.xml @@ -19,11 +19,11 @@ - - + @@ -1398,6 +1398,9 @@ + + + \ No newline at end of file diff --git a/plugins/android-designer/src/com/intellij/android/designer/propertyTable/IncludeLayoutProperty.java b/plugins/android-designer/src/com/intellij/android/designer/propertyTable/IncludeLayoutProperty.java new file mode 100644 index 000000000000..e6505db292d0 --- /dev/null +++ b/plugins/android-designer/src/com/intellij/android/designer/propertyTable/IncludeLayoutProperty.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.android.designer.propertyTable; + +import com.android.resources.ResourceType; +import com.intellij.android.designer.model.RadViewComponent; +import com.intellij.android.designer.propertyTable.editors.ResourceEditor; +import com.intellij.android.designer.propertyTable.renderers.ResourceRenderer; +import com.intellij.designer.model.RadComponent; +import com.intellij.designer.propertyTable.Property; +import com.intellij.designer.propertyTable.PropertyEditor; +import com.intellij.designer.propertyTable.PropertyRenderer; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.util.text.StringUtil; +import org.jetbrains.android.dom.attrs.AttributeFormat; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.EnumSet; +import java.util.List; +import java.util.Set; + +/** + * @author Alexander Lobas + */ +public class IncludeLayoutProperty extends Property { + public static final String NAME = "layout:xml"; + public static ResourceType[] TYPES = new ResourceType[]{ResourceType.LAYOUT}; + private static final Set FORMATS = EnumSet.of(AttributeFormat.Reference); + + private final PropertyRenderer myRenderer = new ResourceRenderer(FORMATS); + private final PropertyEditor myEditor = new ResourceEditor(TYPES, FORMATS, null); + + public IncludeLayoutProperty() { + super(null, NAME); + } + + @Override + public Property createForNewPresentation(@Nullable Property parent, @NotNull String name) { + return null; + } + + @Override + public Object getValue(RadViewComponent component) throws Exception { + String layout = component.getTag().getAttributeValue("layout"); + return layout == null ? "" : layout; + } + + @Override + public void setValue(final RadViewComponent component, final Object value) throws Exception { + if (!StringUtil.isEmpty((String)value)) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + component.getTag().setAttribute("layout", (String)value); + } + }); + } + } + + @Override + public boolean availableFor(List components) { + return false; + } + + @NotNull + @Override + public PropertyRenderer getRenderer() { + return myRenderer; + } + + @Override + public PropertyEditor getEditor() { + return myEditor; + } +} \ No newline at end of file diff --git a/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ResourceEditor.java b/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ResourceEditor.java index 835a98e3831f..f9ee6a52239c 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ResourceEditor.java +++ b/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ResourceEditor.java @@ -38,40 +38,16 @@ import java.util.Set; * @author Alexander Lobas */ public class ResourceEditor extends PropertyEditor { - private ResourceType[] myTypes; + private final ResourceType[] myTypes; private ComponentWithBrowseButton myEditor; private RadComponent myRootComponent; public ResourceEditor(Set formats, String[] values) { - Set types = EnumSet.noneOf(ResourceType.class); - for (AttributeFormat format : formats) { - switch (format) { - case Boolean: - types.add(ResourceType.BOOL); - break; - case Color: - types.add(ResourceType.COLOR); - types.add(ResourceType.DRAWABLE); - break; - case Dimension: - types.add(ResourceType.DIMEN); - break; - case Integer: - types.add(ResourceType.INTEGER); - break; - case String: - types.add(ResourceType.STRING); - break; - case Reference: - types.add(ResourceType.COLOR); - types.add(ResourceType.DRAWABLE); - types.add(ResourceType.STRING); - types.add(ResourceType.ID); - types.add(ResourceType.STYLE); - break; - } - } - myTypes = types.toArray(new ResourceType[types.size()]); + this(convertTypes(formats), formats, values); + } + + public ResourceEditor(ResourceType[] types, Set formats, String[] values) { + myTypes = types; if (formats.contains(AttributeFormat.Enum) || formats.contains(AttributeFormat.Boolean)) { ComboboxWithBrowseButton editor = new ComboboxWithBrowseButton(); @@ -118,14 +94,7 @@ public class ResourceEditor extends PropertyEditor { myEditor.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - PropertyParser parser = myRootComponent.getClientProperty(PropertyParser.KEY); - ResourceDialog dialog = parser.createResourceDialog(myTypes); - dialog.show(); - - if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) { - getComboText().setText(dialog.getResourceName()); - fireValueCommitted(true, true); - } + showDialog(); } }); myEditor.addFocusListener(new FocusAdapter() { @@ -136,6 +105,39 @@ public class ResourceEditor extends PropertyEditor { }); } + private static ResourceType[] convertTypes(Set formats) { + Set types = EnumSet.noneOf(ResourceType.class); + for (AttributeFormat format : formats) { + switch (format) { + case Boolean: + types.add(ResourceType.BOOL); + break; + case Color: + types.add(ResourceType.COLOR); + types.add(ResourceType.DRAWABLE); + break; + case Dimension: + types.add(ResourceType.DIMEN); + break; + case Integer: + types.add(ResourceType.INTEGER); + break; + case String: + types.add(ResourceType.STRING); + break; + case Reference: + types.add(ResourceType.COLOR); + types.add(ResourceType.DRAWABLE); + types.add(ResourceType.STRING); + types.add(ResourceType.ID); + types.add(ResourceType.STYLE); + break; + } + } + + return types.toArray(new ResourceType[types.size()]); + } + @NotNull @Override public JComponent getComponent(@NotNull RadComponent rootComponent, @Nullable RadComponent component, Object value) { @@ -151,6 +153,22 @@ public class ResourceEditor extends PropertyEditor { return value == StringsComboEditor.UNSET || StringUtil.isEmpty(value) ? null : value; } + @Override + public void updateUI() { + SwingUtilities.updateComponentTreeUI(myEditor); + } + + private void showDialog() { + PropertyParser parser = myRootComponent.getClientProperty(PropertyParser.KEY); + ResourceDialog dialog = parser.createResourceDialog(myTypes); + dialog.show(); + + if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) { + getComboText().setText(dialog.getResourceName()); + fireValueCommitted(true, true); + } + } + private JTextField getComboText() { JComponent component = myEditor.getChildComponent(); if (component instanceof JTextField) { @@ -159,9 +177,4 @@ public class ResourceEditor extends PropertyEditor { JComboBox combo = (JComboBox)component; return (JTextField)combo.getEditor().getEditorComponent(); } - - @Override - public void updateUI() { - SwingUtilities.updateComponentTreeUI(myEditor); - } } \ No newline at end of file