기본 콘텐츠로 건너뛰기
검색
이 블로그 검색
불친절한자수씨
자수씨의 보물창고
페이지
홈
더보기…
공유
공유 링크 만들기
Facebook
X
Pinterest
이메일
기타 앱
태그
Eclipse
9월 27, 2008
[GEF] Annotation 으로 EditPartFactory 자동화하기
GMF 는 모르겠지만 GEF 로 할 경우 모델이 늘어나게 되면 여러군데에서 추가해주어야 할 부분들이 많습니다.
이런것들을 줄이기 위해 어노테이션을 간간히 적용하고 있습니다.
그들 중에 하나를 소개합니다.
더보기
ㅁ 기존의 EditPartFactory
모델이 하나 늘어날 때 마다 if 문이 추가가 됩니다.
public class ElementEditPartFactory implements EditPartFactory {
@Override
public EditPart createEditPart(EditPart context, Object model) {
EditPart part = null;
if (model instanceof BodyElement) {
part = new BodyElementPart();
} else if (model instanceof RectangleElement) {
part = new RectangleElementPart();
} else if (model instanceof TextFieldElement) {
part = new TextFieldElementPart();
} else if (model instanceof NumberFieldElement) {
part = new NumberFieldElementPart();
} else if (model instanceof CurrencyFieldElement) {
part = new CurrencyFieldElementPart();
} else if (model instanceof DateFieldElement) {
part = new DateFieldElementPart();
} else if (model instanceof FormatFieldElement) {
part = new FormatFieldElementPart();
} else if (model instanceof GridElement) {
part = new GridElementPart();
} else if (model instanceof GridColumn) {
part = new GridColumnPart();
}
part.setModel(model);
return part;
}
}
ㅁ 자동화를 위한 AnnotatedEditPartFactory
package kr.co.vicki.gef.model;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
public class AnnotatedEditPartFactory implements EditPartFactory {
@Override
public EditPart createEditPart(EditPart context, Object model) {
EditPart part = ModelHelper.createEditPart(model.getClass());
if (part != null)
part.setModel(model);
return part;
}
}
모델에
@GEFEditPart
어노테이션을 정의하면 모델 추가시에 바로 처리가 가능합니다.
@GEFEditPart(editPartType=BodyElementPart.class)
@GEFTreeEditPart(editPartType=BodyElementTreePart.class)
public class BodyElement extends BaseElement implements IBeanConvertable {
...
}
어노테이션 처리를 위한 클래스는 다음과 같습니다. -
ModelHelper
package kr.co.vicki.gef.model;
import java.lang.annotation.Annotation;
import kr.co.vicki.gef.model.annotations.GEFEditPart;
import kr.co.vicki.gef.model.annotations.GEFModelIcon;
import kr.co.vicki.gef.model.annotations.GEFTreeEditPart;
import org.eclipse.gef.EditPart;
public class ModelHelper {
/**
* 상속 구조에서 어노테이션을 찾습니다.
* @param modelClass
* @param annotationClass
* @return
*/
@SuppressWarnings("unchecked")
private static Annotation getHierachyAnnotation(Class modelClass, Class annotationClass) {
Annotation result = null;
result = modelClass.getAnnotation(annotationClass);
if (result == null) {
Class superClass = modelClass.getSuperclass();
if (superClass != null) {
return getHierachyAnnotation(superClass, annotationClass);
}
}
return result;
}
public static EditPart createEditPart(Class modelClass) {
GEFEditPart editPart = (GEFEditPart)
getHierachyAnnotation(modelClass, GEFEditPart.class);
try {
if (editPart != null)
return editPart.editPartType().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static EditPart createTreeEditPart(Class modelClass) {
GEFTreeEditPart treeEditPart = (GEFTreeEditPart)
getHierachyAnnotation(modelClass, GEFTreeEditPart.class);
try {
if (treeEditPart != null)
return treeEditPart.editPartType().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getModelIconImagePath(Class clazz) {
GEFModelIcon modelIcon = (GEFModelIcon) clazz.getAnnotation(GEFModelIcon.class);
if (modelIcon != null)
return modelIcon.imageFilePath();
return null;
}
}
사용된 라이브러리
GMF 도 슬슬 확인해봐야 하는건지.. .끄으응;;;
댓글
가장 많이 본 글
1월 17, 2013
[for신규가입자] 카페 가입시 30%할인쿠폰 전원 지급!
댓글
댓글 쓰기