include tag

This commit is contained in:
Alexander Lobas
2012-05-18 14:58:18 +04:00
parent 89be43e504
commit 00ffec389c
7 changed files with 191 additions and 54 deletions
@@ -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;
}
};
}
@@ -177,7 +177,8 @@ public class ModelParser extends XmlRecursiveElementVisitor {
addComponentTag(container.getTag(), newComponent, insertBefore == null ? null : insertBefore.getTag(), new Computable<String>() {
@Override
public String compute() {
return newComponent.getMetaModel().getCreation();
String creation = newComponent.getMetaModel().getCreation();
return creation == null ? newComponent.getCreationXml() : creation;
}
});
@@ -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<Property> myProperties = new ArrayList<Property>();
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 "<include layout=\"" + extractClientProperty(IncludeLayoutProperty.NAME) + "\"/>";
}
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<Property> properties) {
if (!properties.isEmpty()) {
properties = new ArrayList<Property>(properties);
properties.addAll(myProperties);
properties.add(LAYOUT_PROPERTY);
properties.add(ID_PROPERTY);
}
super.setProperties(properties);
}
@@ -61,6 +61,10 @@ public class RadViewComponent extends RadVisualComponent {
}
}
public String getCreationXml() {
throw new UnsupportedOperationException();
}
public ViewInfo getViewInfo() {
return myViewInfo;
}
@@ -19,11 +19,11 @@
<palette title="Device Screen (#merge)" icon="/com/intellij/android/designer/icons/DeviceScreen.png"/>
</meta>
<meta model="com.intellij.android.designer.model.RadIncludeComponent"
<meta model="com.intellij.android.designer.model.RadIncludeLayout"
class="java.lang.Object"
tag="include">
<presentation title=" - %layout%"/>
<presentation title=" - %layout:xml%"/>
<palette title="include" icon="/com/intellij/android/designer/icons/include.png"
tooltip="Lets you statically include XML layout inside other XML layouts."/>
@@ -1398,6 +1398,9 @@
<item tag="TextSwitcher"/>
<item tag="AdapterViewFlipper"/>
</group>
<group name="Custom">
<item tag="include"/>
</group>
</palette>
</meta-model>
@@ -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<RadViewComponent> {
public static final String NAME = "layout:xml";
public static ResourceType[] TYPES = new ResourceType[]{ResourceType.LAYOUT};
private static final Set<AttributeFormat> 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<RadViewComponent> 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<RadComponent> components) {
return false;
}
@NotNull
@Override
public PropertyRenderer getRenderer() {
return myRenderer;
}
@Override
public PropertyEditor getEditor() {
return myEditor;
}
}
@@ -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<AttributeFormat> formats, String[] values) {
Set<ResourceType> 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<AttributeFormat> 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<AttributeFormat> formats) {
Set<ResourceType> 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);
}
}