I have an RCP Project. I currently want to modify the Project Explorer. I wrote an addition ContentProvider(implements ICommonContentProvider) and an additional LabelProvider (implements ICommonLabelProvider). In my plugin.xml I added the following:
<extension
id="navigator-viewbinding"
point="org.eclipse.ui.navigator.viewer">
<viewerContentBinding
viewerId="org.eclipse.ui.navigator.ProjectExplorer">
<includes>
<contentExtension
isRoot="true"
pattern="de.myapp.application.EditorResourceContent">
</contentExtension>
</includes>
</viewerContentBinding>
</extension>
And:
<extension
id="navigator-content"
point="org.eclipse.ui.navigator.navigatorContent">
<navigatorContent
activeByDefault="true"
contentProvider="de.myapp.application.ProjectExplorerContentProvider"
icon="icon.gif"
id="de.myapp.application.EditorResourceContent"
labelProvider="de.myapp.application.ProjectExplorerLabelProvider"
name="Editor Decoration"
priority="highest">
<triggerPoints>
<or>
<instanceof
value="org.eclipse.core.resources.IFile">
</instanceof>
<instanceof
value="org.eclipse.core.resources.IFolder">
</instanceof>
<instanceof
value="org.eclipse.jdt.internal.core.PackageFragment">
</instanceof>
<instanceof
value="org.eclipse.core.resources.IWorkspaceRoot" />
<instanceof
value="org.eclipse.core.resources.IProject" />
</or>
</triggerPoints>
<possibleChildren>
<or>
<instanceof
value="org.eclipse.core.resources.IWorkspaceRoot" />
<instanceof
value="org.eclipse.core.resources.IProject" />
<instanceof
value="org.eclipse.core.resources.IResource" />
<instanceof
value="org.eclipse.core.resources.IFolder" />
<instanceof
value="org.eclipse.core.resources.IFile" />
<instanceof
value="org.eclipse.jdt.internal.core.PackageFragment" />
</or>
</possibleChildren>
</navigatorContent>
</extension>
When I start the Editor, the Project Explorer won't show any projects, until I make a right mouse click. Then all projects are loaded. When I open the tree, I see no error markers on my modified icons, but the icons themself are shown. Also I see the error markers on the packages(unmodified). I even see the egit decorators, but not the red markers for the errors.
Also I provide both of the Providers, maybe it will help give me some hints for both of my problems.
ContentProvider:
public class ProjectExplorerContentProvider implements
ICommonContentProvider
{
private static final Object[] NO_CHILDREN = {};
#Override
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
#Override
public Object[] getChildren(Object parentElement) {
Object[] children = null;
if(IWorkspaceRoot.class.isInstance(parentElement))
{
IProject[] projects = ((IWorkspaceRoot)parentElement).getProjects();
children = createParents(projects);
}
else
{
children = NO_CHILDREN;
}
return children;
}
private Object[] createParents(IProject[] projects)
{
Object[] result;
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < projects.length; i++) {
try {
if(projects[i].hasNature("org.eclipse.xtext.ui.shared.xtextNature"))
list.add(projects[i]);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
result = new Object[list.size()];
list.toArray(result);
return result;
}
#Override
public Object getParent(Object element) {
Object parent = null;
if(IProject.class.isInstance(element))
{
parent = ((IProject)element).getWorkspace().getRoot();
}
return parent;
}
#Override
public boolean hasChildren(Object element) {
boolean hasChildren = false;
if(IWorkspaceRoot.class.isInstance(element))
{
hasChildren = ((IWorkspaceRoot)element).getProjects().length > 0;
}
return hasChildren;
}
#Override
public void restoreState(IMemento aMemento) {
}
#Override
public void saveState(IMemento aMemento) {
}
#Override
public void init(ICommonContentExtensionSite aConfig) {
}
}
And here the LabelProvider:
public class ProjectExplorerLabelProvider implements ICommonLabelProvider{
/****/
#Override
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
#Override
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
#Override
public Image getImage(Object anElement) {
if (anElement instanceof File) {
File fi = (File) anElement;
if (fi.getFileExtension().equalsIgnoreCase("mydsl")) {
return Activator.getImage("icons/img1.png");
} else {
try {
InputStream inputStream = fi.getContents();
String content =
ContentFactory.getInstance().toStringInputStream(inputStream);
inputStream.close();
if (content.contains("some string")) {
return Activator.getImage("icons/img2.png");
} else if (content.contains("some other string")) {
return Activator.getImage("icons/img3.png");
} else if (content.contains("some other string")) {
return Activator.getImage("icons/img4.png");
}
} catch (CoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (anElement instanceof Folder) {
Folder fo = (Folder) anElement;
}
else if (anElement instanceof PackageFragment) {
PackageFragment pf = (PackageFragment) anElement;
}
}
return null;
}
#Override
public String getText(Object element) {
// TODO Auto-generated method stub
return null;
}
#Override
public void restoreState(IMemento aMemento) {
// TODO Auto-generated method stub
}
#Override
public void saveState(IMemento aMemento) {
// TODO Auto-generated method stub
}
#Override
public String getDescription(Object anElement) {
// TODO Auto-generated method stub
return null;
}
#Override
public void init(ICommonContentExtensionSite aConfig) {
}
}
When I start the Editor, the Project Explorer won't show any projects,
until I make a right mouse click. Then all projects are loaded.
For this problem you can try this:
Update 2015-08-24: if in the final application the ProjectExplorer
content is not visible by its own (but only after you forced an
updated, e.g., via opening the context menu with a right-click), try
this: override the getDefaultPageInput() method of the
ApplicationWorkbenchAdvisor and add the following line:
return ResourcesPlugin.getWorkspace().getRoot();
Source:
https://dirksmetric.wordpress.com/2012/08/01/tutorial-eclipse-rcp-e4-with-3-x-views-like-project-explorer-properties-etc/
When I open the tree, I see no error markers on my modified icons, but
the icons themself are shown.
I think you override the Icons with the decoration? So its clear that there are no decoration on it.
Related
Recently I am working on a Flutter plugin for my project. My plugin requires startActivityForResult but can't figure out how to use it according to Flutter plugin development. I have given my code below.
public class MyPlugin implements FlutterPlugin, MethodCallHandler {
private static final int REQUEST_CODE = 1;
Result result;
#Override
public void onAttachedToEngine(#NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "my_plugin");
channel.setMethodCallHandler(this);
}
#Override
public void onDetachedFromEngine(#NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
#Override
public void onMethodCall(#NonNull MethodCall call, #NonNull Result result) {
this.result = result;
if (call.method.equals("myMethod")) {
myMethod();
} else {
result.notImplemented();
}
}
private void myMethod() {
// my intent instance will be here
startActivityForResult(intent, REQUEST_CODE); // Cannot resolve method 'startActivityForResult'
}
#Override
public boolean onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
try {
if (requestCode == REQUEST_CODE) {
result.success("done");
} else {
result.error("Something went wrong", null, null);
}
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return false;
}
}
Try this solution to get the Context. After, call the intent using the Context:
private void myMethod() {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
I have a requirement where the AEM pathfield component will show only the child(cq:Page
) resources/nodes of the current node(cq:Page) in the options picker. I went through the documentation https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/granite-ui/api/jcr_root/libs/granite/ui/components/coral/foundation/form/pathfield/index.html , not sure the filter option can be of use.
#Component(
service = Predicate.class,
property = {
"predicate.name= myPredicateName"
}
)
public class MyPredicate extends AbstractNodePredicate {
private static final LocalisedLogger LOG = LocalisedLoggerFactory.getLogger(MyPredicate.class);
#Override
public boolean evaluate(final Node node) {
LOG.log(Messages.DEBUG, "testing" + node.toString());
try {
return isInPredicate(node);
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
private boolean isInPredicate(final Node node) throws RepositoryException {
if (node.hasProperty("jcr:content/dog")) {
return true;
}
return false;
}
}
content.xml
<path
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/pathfield"
fieldLabel="Page Path"
predicate="[myPredicateName]"
emptyText="Select the carousels page"
name="./carouselsPagePath"
forceSelection="{Boolean}true"
required="{Boolean}true"/>
I have a class for giving extension to newly created file.
public class MyNewFileWizard extends BasicNewFileResourceWizard
{
#Override
public void addPages()
{
super.addPages();
MyWizardNewFileCreationPage page = (MyWizardNewFileCreationPage )getPage("newFilePage1");
page.setFileExtension("css");
addPage(page);
}
#Override
public void init(IWorkbench workbench, IStructuredSelection currentSelection)
{
super.init(workbench, currentSelection);
setNeedsProgressMonitor(true);
}}
Also I have a class to give that created class's context
public class MyWizardNewFileCreationPage extends WizardNewFileCreationPage
{
...
#Override
protected InputStream getInitialContents()
{
//to give same strings context for every generated css file
StringBuilder sb = new StringBuilder();
sb.append("SAMPLE_CSS_FILE"); //$NON-NLS-1$
sb.append("SECTION_1"); //$NON-NLS-1$
sb.append("SECTION_1_BODY_1"); //$NON-NLS-1$
return new ByteArrayInputStream(sb.toString().getBytes());
}
plugin.xml
..
<wizard
category="ui.category"
id="ui.wizard.MyNewFileWizard"
name="Create a new app.test File"
icon="icons/project.png"
class="MyNewFileWizard"
project="false"
>
</wizard>
My goal is to create a css class with already defined context in it. So somehow combining these two class.
You can't call super.addPages because that will add the normal WizardNewFileCreationPage. You have to add only your own page:
#Override
public void addPages()
{
MyWizardNewFileCreationPage page = new MyWizardNewFileCreationPage("newFilePage1", getSelection());
page.setFileExtension("css");
addPage(page);
}
But doing this means that the standard BasicNewFileResourceWizard.performFinish won't work so you will have to override that as well:
#Override
public boolean performFinish() {
MyWizardNewFileCreationPage mainPage = (MyWizardNewFileCreationPage )getPage("newFilePage1");
IFile file = mainPage.createNewFile();
if (file == null) {
return false;
}
selectAndReveal(file);
// Open editor on new file.
IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
try {
if (dw != null) {
IWorkbenchPage page = dw.getActivePage();
if (page != null) {
IDE.openEditor(page, file, true);
}
}
} catch (final PartInitException e) {
// Show error
}
return true;
}
public class MyNewFileWizard extends BasicNewFileResourceWizard
{
WizardNewFileCreationPage mainPage;
public static final String WIZARD_ID = "ui.wizard.MyNewFileWizard";
public MyNewFileWizard()
{
super();
}
#Override
public void addPages()
{
mainPage = new WizardNewFileCreationPage("New File Page", getSelection())
{
#Override
protected InputStream getInitialContents()
{
StringBuilder sb = new StringBuilder();
sb.append("SAMPLE_README_FILE");
sb.append("SECTION_1");
sb.append("SECTION_1_BODY_1");
return new ByteArrayInputStream(sb.toString().getBytes());
}
};
mainPage.setFileExtension("css");
addPage(mainPage);
}
#Override
protected void initializeDefaultPageImageDescriptor()
{
//setDefaultPageImageDescriptor(imageHelper.getImageDescriptor("icon_48x48.png"));
}
#Override
public void init(IWorkbench workbench, IStructuredSelection currentSelection)
{
super.init(workbench, currentSelection);
setWindowTitle("New Solidity file");
setNeedsProgressMonitor(true);
}
#Override
public boolean performFinish()
{
IFile file = mainPage.createNewFile();
if (file == null) {
return false;
}
selectAndReveal(file);
// Open editor on new file.
IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
try {
if (dw != null) {
IWorkbenchPage page = dw.getActivePage();
if (page != null) {
IDE.openEditor(page, file, true);
}
}
}
catch (PartInitException e) {
//openError(dw.getShell(), "Problems opening editor", e.getMessage(), e);
}
return true;
}
}
I'm trying uses the converter in p:autocomplete, but the arg2 of getAsObject Method always is "".
below are my codes:
page xhtml:
<p:autoComplete id="Cliente" value="#{beanPedido.cliente}"
converter="converterCliente"
completeMethod="#{beanPedido.completarCliente}" var="cliente"
itemLabel="#{cliente.login}" itemValue="#{cliente}"
>
<p:ajax event="itemSelect"
listener="#{beanPedido.onClienteSelecionado}" />
</p:autoComplete>
Bean:
private Cliente cliente;
private List<Cliente> clientes;
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public List<Cliente> completarCliente(String query) {
clientes = f.buscarTodosClientes();
List<Cliente> clientesFiltrados = new ArrayList<Cliente>();
for (int i = 0; i < clientes.size(); i++) {
Cliente cli = clientes.get(i);
if (cli.getLogin().toLowerCase().startsWith(query)) {
clientesFiltrados.add(cli);
}
}
return clientesFiltrados;
}
public void onClienteSelecionado(SelectEvent e) {
cliente = (Cliente)e.getObject();
}
Converter:
#FacesConverter("converterCliente")
public class ConverterCliente implements Converter {
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if (value!= null && value.trim().length() > 0) {
try {
Fachada f = Fachada.getInstancia();
return (Cliente) f.buscarClienteLogin(value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
#Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
// TODO Auto-generated method stub
return null;
}
}
When I debug it always return value=""
Thank you very much in advance.
How to display only the contents of the workspace in my TreeViewer?
This is my main view class:
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
public class MainView extends ViewPart {
public static final String ID = "com.MainView";
public MainView() {
// TODO Auto-generated constructor stub
}
#Override
public void createPartControl(Composite parent) {
TreeViewer tv = new TreeViewer(parent);
tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
tv.setContentProvider(new FileTreeContentProvider());
tv.setLabelProvider(new FileTreeLabelProvider());
tv.setInput(ResourcesPlugin.getWorkspace());
}
#Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
This is my content provider class:
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class FileTreeContentProvider implements ITreeContentProvider {
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
public Object[] getElements(Object inputElement) {
return null;
}
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof IProject) {
IProject projects = (IProject) parentElement;
try {
return projects.members();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (parentElement instanceof IFolder) {
IFolder ifolder = (IFolder) parentElement;
try {
return ifolder.members();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public Object getParent(Object element) {
if (element instanceof IProject) {
IProject projects = (IProject) element;
return projects.getParent();
}
if (element instanceof IFolder) {
IFolder folder = (IFolder) element;
return folder.getParent();
}
if (element instanceof IFile) {
IFile file = (IFile) element;
return file.getParent();
}
return null;
}
public boolean hasChildren(Object element) {
if (element instanceof IProject) {
IProject projects = (IProject) element;
try {
return projects.members().length > 0;
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (element instanceof IFolder) {
IFolder folder = (IFolder) element;
try {
return folder.members().length > 0;
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}
This is my label provider class:
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
public class FileTreeLabelProvider implements ILabelProvider {
public void addListener(ILabelProviderListener listener) {
}
public void dispose() {
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {
}
public Image getImage(Object element) {
return null;
}
public String getText(Object element) {
if (element instanceof IProject) {
String text = ((IProject) element).getName();
return text;
}
if (element instanceof IFolder) {
String text = ((IFolder) element).getName();
return text;
}
if (element instanceof IFile) {
String text = ((IFile) element).getName();
return text;
}
return null;
}
}
public Object[] getElements(Object inputElement) {
return ResourcesPlugin.getWorkspace().getRoot().getProjects();
}
I am able to get projects now.my code is working
A content provider and a label provider for the workspace objects are already present in the Eclipse framework:
org.eclipse.ui.model.WorkbenchLabelProvider
org.eclipse.ui.model.WorkbenchContentProvider
Using them, your code will look like this:
TreeViewer treeViewer = new TreeViewer(parent);
treeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
treeViewer.setContentProvider(new WorkbenchContentProvider());
treeViewer.setLabelProvider(new WorkbenchLabelProvider());
treeViewer.setInput(ResourcesPlugin.getWorkspace().getRoot());