Skip to content

Commit

Permalink
Implement JavaClassChooserMultipleComponentBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jul 10, 2014
1 parent cc38662 commit fdfdc8e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.plugin.idea.ui.component.many.DirectoryChooserMultipleComponentBuilder;
import org.jboss.forge.plugin.idea.ui.component.many.FileChooserMultipleComponentBuilder;
import org.jboss.forge.plugin.idea.ui.component.many.JavaClassChooserMultipleComponentBuilder;
import org.jboss.forge.plugin.idea.ui.component.many.TextBoxMultipleComponentBuilder;

/**
Expand All @@ -34,6 +35,7 @@ public enum ComponentBuilderRegistry
new TextBoxMultipleComponentBuilder(),
new FileChooserMultipleComponentBuilder(),
new DirectoryChooserMultipleComponentBuilder(),
new JavaClassChooserMultipleComponentBuilder(),
new FallbackTextBoxComponentBuilder()};

public ComponentBuilder getBuilderFor(InputComponent<?, ?> input)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.plugin.idea.ui.component.many;

import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.plugin.idea.ui.component.ComponentBuilder;
import org.jboss.forge.plugin.idea.ui.component.ForgeComponent;
import org.jboss.forge.plugin.idea.util.ForgeProxies;
import org.jboss.forge.plugin.idea.util.IDEUtil;

/**
* @author Adam Wyłuda
*/
public class JavaClassChooserMultipleComponentBuilder extends ComponentBuilder
{
@Override
public ForgeComponent build(InputComponent<?, Object> input)
{
return new ListComponent(ForgeProxies.proxyTo(UIInputMany.class, input))
{
@Override
protected String editSelectedItem(String item)
{
return IDEUtil.chooseClass(context, item);
}

@Override
protected String findItemToAdd()
{
return IDEUtil.chooseClass(context, "");
}
};
}

@Override
protected Class<?> getProducedType()
{
return String.class;
}

@Override
protected String getSupportedInputType()
{
return InputType.JAVA_CLASS_PICKER;
}

@Override
protected Class<?>[] getSupportedInputComponentTypes()
{
return new Class<?>[]{UIInputMany.class};
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/jboss/forge/plugin/idea/util/IDEUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
*/
package org.jboss.forge.plugin.idea.util;

import com.intellij.ide.util.TreeClassChooser;
import com.intellij.ide.util.TreeClassChooserFactory;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiManager;
import com.intellij.psi.util.ClassUtil;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.furnace.proxy.Proxies;
Expand Down Expand Up @@ -73,4 +78,24 @@ public static String chooseFile(Project project, FileChooserDescriptor descripto
VirtualFile choosenFile = FileChooser.chooseFile(descriptor, project, initialFile);
return choosenFile != null ? choosenFile.getCanonicalPath() : (initialValue.isEmpty() ? null : initialValue);
}

public static String chooseClass(UIContext context, String initialValue)
{
return chooseClass(projectFromContext(context), initialValue);
}

public static String chooseClass(Project project, String initialValue)
{
PsiManager psiManager = PsiManager.getInstance(project);

TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project)
.createProjectScopeChooser(
"Select a Java class",
ClassUtil.findPsiClass(psiManager, initialValue));
chooser.showDialog();

PsiClass choosenPsiClass = chooser.getSelected();

return choosenPsiClass != null ? choosenPsiClass.getQualifiedName() : (initialValue.isEmpty() ? null : initialValue);
}
}

0 comments on commit fdfdc8e

Please sign in to comment.