package org.orienteer.core.component.meta;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.WicketTag;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.parser.filter.WicketTagIdentifier;
import org.apache.wicket.markup.resolver.IComponentResolver;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.Strings;
import org.orienteer.core.component.property.DisplayMode;

import ru.ydn.wicket.wicketorientdb.model.OPropertyModel;

import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * {@link IComponentResolver} to be able to handle wicket:property tags
 */
public class WicketPropertyResolver implements IComponentResolver {
	
	public static final String PROPERTY = "property";
	
	static {
		WicketTagIdentifier.registerWellKnownTagName(PROPERTY);
	}

	@Override
	public Component resolve(MarkupContainer container,
			MarkupStream markupStream, ComponentTag tag) {
		if (tag instanceof WicketTag)
		{
			final WicketTag wTag = (WicketTag)tag;

			// If <wicket:property ...>
			if (PROPERTY.equalsIgnoreCase(wTag.getName()))
			{
				wTag.setAutoComponentTag(false);
				String oClassName = tag.getAttribute("class");
				IModel<?> model = container.getPage().getDefaultModel();
				if(Strings.isEmpty(oClassName)) {
					Object object = model.getObject();
					if(object instanceof OIdentifiable) {
						ODocument doc = ((OIdentifiable)object).getRecord();
						oClassName = doc.getSchemaClass().getName();
					}
				}
				
				return new ODocumentMetaPanel<Object>(wTag.getId(), DisplayMode.VIEW.asModel(), 
										(IModel<ODocument>)model, 
										new OPropertyModel(oClassName, wTag.getId()));
			}
		}

		// We were not able to handle the tag
		return null;
	}

}

@@ -111,24 +111,73 @@
 pContainer;%0A
+import org.apache.wicket.WicketRuntimeException;%0A
 import org.a
@@ -622,16 +622,82 @@
 yMode;%0A%0A
+import ru.ydn.wicket.wicketorientdb.model.ODocumentPropertyModel;%0A
 import r
@@ -813,16 +813,84 @@
 fiable;%0A
+import com.orientechnologies.orient.core.metadata.schema.OProperty;%0A
 import c
@@ -1588,16 +1588,79 @@
 lass%22);%0A
+%09%09%09%09%0A%09%09%09%09String objectExpression = tag.getAttribute(%22object%22);%0A
 %09%09%09%09IMod
@@ -1708,24 +1708,265 @@
 ultModel();%0A
+%09%09%09%09if(!Strings.isEmpty(objectExpression)) model = new ODocumentPropertyModel%3CODocument%3E(model, objectExpression);%0A%09%09%09%09%0A%09%09%09%09String property = tag.getAttribute(%22property%22);%0A%09%09%09%09if(Strings.isEmpty(property)) property = wTag.getId();%0A%09%09%09%09%0A%09%09%09%09%0A
 %09%09%09%09if(Strin
@@ -2201,21 +2201,252 @@
 %09%09%7D%0A%09%09%09%09
-%0A%09%09%09%09
+IModel%3COProperty%3E propertyModel = new OPropertyModel(oClassName, wTag.getId());%0A%09%09%09%09if(propertyModel.getObject()==null) throw new WicketRuntimeException(%22No such property '%22+wTag.getId()+%22' defined on class '%22+oClassName+%22'%22);%0A%09%09%09%09else 
 return n
@@ -2565,22 +2565,17 @@
 %09%09%09%09%09%09%09%09
-new OP
+p
 ropertyM
@@ -2582,34 +2582,8 @@
 odel
-(oClassName, wTag.getId())
 );%0A%09

Allow to specify object in wicket:property