[forge-issues] [JBoss JIRA] (FORGE-2469) Getting a design pattern right for add commands
Antonio Goncalves (JIRA)
issues at jboss.org
Fri Sep 4 10:15:00 EDT 2015
[ https://issues.jboss.org/browse/FORGE-2469?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13105654#comment-13105654 ]
Antonio Goncalves commented on FORGE-2469:
------------------------------------------
https://gist.github.com/gastaldi/46b3f38bc38c5efcc0ba
> Getting a design pattern right for add commands
> -----------------------------------------------
>
> Key: FORGE-2469
> URL: https://issues.jboss.org/browse/FORGE-2469
> Project: Forge
> Issue Type: Sub-task
> Components: Java EE
> Affects Versions: 2.19.0.Final
> Reporter: Antonio Goncalves
> Fix For: 2.x Future
>
>
> The {{new}} commands are straight forward, we just create a new artifact (mostly classes in the Java EE addon). {{add}} commands are meant to add code to existing code. So all the add}} commands have a {{targetClass}} attribute. The {{targetClass}} points to the current class, or can point to a different one.
> The algorithm to get the {{targetClass}} is very different from command to command (see below). It would be very helpfull to find a generic design pattern that could be applicable to most of the {{add}} commands.
> {code:title=JavaEqualsHashcodeCommand}
> @Override
> public void initializeUI(UIBuilder builder) throws Exception
> {
> (...)
> UISelection<Object> initialSelection = uiContext.getInitialSelection();
> if (initialSelection.get() instanceof JavaResource)
> {
> targetClass.setValue((JavaResource) initialSelection.get());
> }
> targetClass.setValueChoices(projectOperations.getProjectClasses(project));
> @Override
> public Result execute(UIExecutionContext context) throws Exception
> {
> (...)
> if (targetClass.hasMethodSignature("equals", Object.class))
> {
> if (prompt.promptBoolean("Class already has an equals method. Would you like it to be overwritten?"))
> {code}
> {code:title=CDIAddInjectionPointCommand}
> private void setupTargetClasses(UIContext uiContext)
> {
> UISelection<FileResource<?>> selection = uiContext.getInitialSelection();
> Project project = getSelectedProject(uiContext);
> final List<JavaResource> classes = cdiOperations.getProjectInjectionPointBeans(project);
> targetClass.setValueChoices(classes);
> int idx = -1;
> if (!selection.isEmpty())
> {
> idx = classes.indexOf(selection.get());
> }
> if (idx == -1)
> {
> idx = classes.size() - 1;
> }
> if (idx != -1)
> {
> targetClass.setDefaultValue(classes.get(idx));
> }
> }
> {code}
> {code:title=CDIAddObserverMethodCommand}
> private void setupTargetClass(UIContext uiContext, Project project)
> {
> UISelection<Resource<?>> resource = uiContext.getInitialSelection();
> if (resource.get() instanceof JavaResource)
> {
> targetClass.setDefaultValue((JavaResource) resource.get());
> }
> targetClass.setValueChoices(projectOperations.getProjectClasses(project));
> }
> {code}
> {code:title=FacesNewValidatorMethodCommand}
> public void initializeUI(UIBuilder builder) throws Exception
> {
> Object selection = builder.getUIContext().getInitialSelection().get();
> if (selection instanceof JavaResource)
> {
> targetClass.setDefaultValue((JavaResource) selection);
> }
> builder.add(targetClass).add(named);
> }
> @Override
> public void validate(UIValidationContext validator)
> {
> try
> {
> JavaClassSource source = targetClass.getValue().reify(JavaResource.class).getJavaType();
> (...)
> }
> catch (FileNotFoundException e)
> {
> validator.addValidationError(targetClass, "Target Java class not found.");
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.4.11#64026)
More information about the forge-issues
mailing list