Showing posts with label eclipse. Show all posts
Showing posts with label eclipse. Show all posts

eclipse ui activity to hide everything except for a view in a plugin

If you want to hide a view (or any other UI component in a plugin) from user, you can write a ui.activities extension as below :

<extension point="org.eclipse.ui.activities">
<activity description="Turn off My View"
id="com.myapp.myactivity" name="MyViewActivity">
</activity>
<activityPatternBinding activityId="com.myapp.myactivity"
pattern="com\.myapp\.mypluginid/com\.myapp\.myviewid">
</activityPatternBinding>
</extension>

This activity is by default disabled, so it hides the view by default. Now if you want to show the view programatically, you can do so by enabling this activity as below :


IWorkbenchActivitySupport workbenchActivitySupport =
PlatformUI.getWorkbench().getActivitySupport();

IActivityManager activityManager =
workbenchActivitySupport.getActivityManager();

Set<String> enabledActivityIds =
new HashSet<String>( activityManager.getEnabledActivityIds() );

enabledActivityIds.add( "MyViewActivity" ) );


.
So far so good. if you want to hide everything except a particular UI component, you can do so by specifying negative lookbehind ("?<!") regular expression in your activity pattern.
Lets say you want to hide all the ui components in your plugin execpt for 2 views in your plugin :

Plugin ID : com.myapp.myappid
View1 ID : com.myapp.myviewid1
View2 ID : com.myapp.myviewid2

Here is the snippet to Hide everything in plugin com.myapp.myappid except for view1 (com.myapp.myviewid1) & view2 (com.myapp.myviewid2).
Note that you have to use "?&lt;!" instead of "?<!" as in xthml '<' character is not allowed.

<extension point="org.eclipse.ui.activities">
<activity description="Turn off My View"
id="com.myapp.myactivity" name="MyViewActivity">
</activity>
<activityPatternBinding activityId="com.myapp.myactivity"
pattern="com\.myapp\.mypluginid/.*(?&lt;!myviewid1|myviewid2)">
</activityPatternBinding>
</extension>

toggle me

If you want to add toggle menu options ie. menu optoin with check mark, here is how can you do it:

Commands can have states associated with it. A state id known by its id and it can have any value, which is stored in a subclass of org.eclipse.core.commands.State. To know whether a command is checked or not, the Command Framework looks for a state with the id 'org.eclipse.ui.commands.toggleState'. Lets specify the default checked state of Bold to true and Italic to false:


< command
defaultHandler="com.eclipse_tips.commandstate.BoldHandler"
id="com.eclipse-tips.commandState.boldCommand"
name="Bold">
< state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
< /state>
< /command>
< command
defaultHandler="com.eclipse_tips.commandstate.ItalicHandler"
id="com.eclipse-tips.commandState.italicCommand"
name="Italic">
< state
class="org.eclipse.ui.handlers.RegistryToggleState:false"
id="org.eclipse.ui.commands.toggleState">
< /state>
< /command>


The Framework expects a Boolean value from the state. It doesn't care about which class that holds the state. So why should we use the RegistryToggleState instead of our own state class? Two reasons:

It implements IExecutableExtension. So you can specify the default values in the plugin.xml as I've done above.
It extends PersistentState, so it can remember the value between Eclipse sessions.
So if the user had checked/unchecked the menu, restarts Eclipse, he will get the same state as he left before - all this, you get without even your plugin being loaded.
So how should the handler work for this command?

public Object execute(ExecutionEvent event) throws ExecutionException
{
Command command = event.getCommand();
boolean oldValue = HandlerUtil.toggleCommandState(command);
// use the old value and perform the operation
return null;
}


Basically, its the Handler's responsibility to update the Command's state. The HandlerUtil has a convenient method which toggles the state and gives you the old value of the state. You can use the value to perform the operation.

To know more about Radio style menu optoins :

http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle-radio-menu.html

Eclipse : Using Standard Source Lookup Tab

If you are writing your own debugger in eclipse, you may want to allow users to provide source lookup paths. Eclipse framework provides a standard Source Tab ("org.eclipse.debug.ui.sourcelookup.SourceLookupTab") that you can add to your debug launch configration tab groups.
By default source tab include java classpath entries and allows users to add various java source paths such as Archive file etc. In your launch configuaration you may want to limit the kind of entries users can add to source tab and control default entries displayed in source tab.

To ensure that Source Tab allows users to add only specific types of entries, in your "source lookup director" class override following method.

public boolean supportsSourceContainerType(ISourceContainerType type);

Here is a sample implmenentaion of source lookup director that allows users to add only workspace projects , workspace folders and external directories.


public class MySourceLookupDirector extends AbstractSourceLookupDirector {

@Override
public boolean supportsSourceContainerType(ISourceContainerType type) {
if(type.getId().equals(ProjectSourceContainer.TYPE_ID))
return true;
if(type.getId().equals(FolderSourceContainer.TYPE_ID))
return true;
if(type.getId().equals(DirectorySourceContainer.TYPE_ID))
return true;
return false;
}
}


To control default entries in Source Tab, make sure that you provide implementaion for ISourcePathComputerDelegate interface. Here is a sample implementation that displays all workspace project as default entries in source Tab:


public class MySourcePathComputerDelegate implements
ISourcePathComputerDelegate {

public ISourceContainer[] computeSourceContainers(ILaunchConfiguration configuration,
IProgressMonitor monitor)throws CoreException
{
return createSourceContainers(configuration);
}

protected ISourceContainer[] createSourceContainers(ILaunchConfiguration configuration)
throws CoreException {

List containers = new ArrayList();
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

for (IProject prj : projects) {
if(prj.isOpen())
containers.add(new ProjectSourceContainer(prj, true));
}
return (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]);
}
}


To perform the source lookup, implement ISourceLookupParticipant or extend from AbstractSourceLookupParticipant as below:


//used to when source is not found in specified source lookup paths
public class SampleSourceNotFound {

private Object artifact;
private String message;

public SampleSourceNotFound(Object artifact) {
this.artifact = artifact;
}

public SampleSourceNotFound(Object artifact,String message) {
this.artifact = artifact;
this.message = message;
}

public Object getArtifact() {
return artifact;
}

public String getMessage(){
return message;
}
}

public class MySourceLookupParticipant extends AbstractSourceLookupParticipant {

@Override
public Object[] findSourceElements(Object object) throws CoreException {

String name = getSourceName(object);
Object[] result = super.findSourceElements(name);
// super implmentaion will lookup using SourceContainers added by user and /
// or default provided by ISourcePathComputerDelegate

if (result == null || result.lenght == 0) {
result = new SampleSourceNotFound[1];
result[0] = new SampleSourceNotFound(object);
}
return result;
}
// object is generally stack frame or breakpoint instance.
// get the name of source file from object.
public String getSourceName(Object object) {
if (object instanceof MyStackFrame) {
return ((MyStackFrame)object).getSourceName();
}
return null;
}

}


For the returned result, eclipse debug framework will get the editor from debug model prensentation.
Eclipse provides a standard "source not found" editor (org.eclipse.debug.ui.sourcelookup.CommonSourceNotFoundEditorInput) that you can show when file is not found in user specified source lookup paths. e.g.


public class MyDebugModelPresentation implements IDebugModelPresentation {

public IEditorInput getEditorInput(Object element){
// you may want to add additional conditions based on type of sorce
// entries / containers supported by your source tab.

if (element instanceof IPath) {
IPath path = (IPath) element;
return getEditorInputFromPath(path);
}
if (element instanceof IFile) {
return new FileEditorInput((IFile) element);
}
if (element instanceof SampleSourceNotFound) {
return new CommonSourceNotFoundEditorInput(element);
}
}
}


You may want to show custom source not found editor (for various reasons such as displaying different message than the default one). In your debug model presentation, check if
result is instnace of SampleSourceNotFound and if so, return the id of your custom editor input.
Here is a sample implentation:


public class MySourceNotFoundEditorInput extends CommonSourceNotFoundEditorInput {

// The message to display in editor page instead of "Source not found.".
private String fMessage;

public MySourceNotFoundEditorInput(SampleSourceNotFound object) {

super(object.getArtifact());

// add getMessage() to SampleSourceNot Found or just use hardcode string
fMessage = object.getMessage();

//Or use something like :
fMessage = "Can not find" + object.getArtifact();
}

@Override
public String getToolTipText() {
// custom message to be displayed in the editor before Edit source lookup Button.
return fMessage;
}
}


To use this editor input, just modify your debug model presentation as below:


public class MyDebugModelPresentation implements IDebugModelPresentation {

public IEditorInput getEditorInput(Object element){
// :
// other conditions shown above
// :
if (element instanceof SampleSourceNotFound) {
return new MySourceNotFoundEditorInput(element);
}
}
}

Eclipse : Switching Perspectives Programatically

Here is how you can switch to a Perspective programatically:


Display.getDefault().asyncExec(new Runnable(){
public void run(){
IWorkbenchWindow w = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IPerspectiveRegistry reg = PlatformUI.getWorkbench().getPerspectiveRegistry();
String perspId = "id of perspective you want to switch to";
w.getActivePage().setPerspective(reg.findPerspectiveWithId(perspId));
}
});


If you want to for example swithc to debug perspective when your custom launch configuration is launched, put this code at the end of your launch delegate.