Thursday, May 28, 2015

How to specify to load a eclipse plugin only on 32 bit machine but not on 64 bit machine.

It’s simple, specify Eclipse-PlatformFilter flag in MANIFEST.MF file with the required criteria.

For example, I wanted to load my plugin only in 32 bit operating systems.
Eclipse-PlatformFilter: (osgi.os=win32)

for adding multiple conditions.
Eclipse-PlatformFilter: (& (osgi.ws=win32) (osgi.os=win32) (osgi.arch=x86))

The Framework supports filtering on the following system properties:
* osgi.nl - the platform language setting.
* osgi.os - the platform operating system.
* osgi.arch - the platform architecture.
* osgi.ws - the platform windowing system


Above example specify that, this bundle can only be resolved if the platform properties are osgi.ws=win32 and osgi.os=win32 and osgi.arch=x86. In other words a platform running on an x86 architecture, using a win32 operating system and the win32 windowing system.

We can find this property directly in plugin.xml file "Overview" tab.

Wednesday, May 13, 2015

Eclipse toolbar and menu bar group action id's

ICommonMenuConstants
IWorkbenchActionConstants
IIDEActionConstants


Few examples:
"group.new"
"group.goto"
"group.open"
"group.openWith"
"group.show"
"group.edit"
"group.reorganize"
"group.port"
"group.generate"
"group.search"
"group.build"
"additions"
"group.properties"

These will be used as a insertion points if you are contributing a new action/menu action/toolbar action.


Tuesday, May 12, 2015

Hiding certain UI elements from Eclipse RCP

We can achieve this by using "org.eclipse.ui.activities" extension point.

http://blog.vogella.com/2009/07/13/eclipse-activities/


We also use "Perspective Extensions" extension.
Example:
<extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="com.kk.StudioPerspective">
         <hiddenToolBarItem
               id="new.group">
         </hiddenToolBarItem>
      </perspectiveExtension>
   </extension>

Tuesday, May 5, 2015

Running multiple commands from single batch file in Windows

If your batch file is having multiple commands and those commands are trying to execute or invoke other batch commands something like this.

Example:
SET PATH=%PATH%;%PROGRAMFILES%\nodejs;%PROGRAMFILES(x86)%\nodejs;%APPDATA%\npm
npm install -g mypackageinstaller
mypackageinstaller use latest


If you run above commands from a batch file, only first 2 commands 
will be get executed and the last one will be ignored.

Reason being, here npm is a batch file from Node, once windows command process invokes npm process,after npm get executed it will not return to the original command process. Hence,third command won't be executed from a batch file.

To avoid these issues, we can invoke these kind of commands using 
"call".

Modified batch script:

SET PATH=%PATH%;%PROGRAMFILES%\nodejs;%PROGRAMFILES(x86)%\nodejs;%APPDATA%\npm
call npm install -g mypackageinstaller
call mypackageinstaller use latest

All these commands will be executed sequentially.

To make them parallel, we can use "start" command.

Listening to project explorer changes

If you want to control some actions based on the selection of an element from the project explorer, we can register a post selection changes through service registry.

//Register listener
ISelectionService ss = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();

ProjectExplorerSelectionListener selectionListener = new ProjectExplorerSelectionListener();
ss.addPostSelectionListener(IPageLayout.ID_PROJECT_EXPLORER, selectionListener);



//Listener
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;

class ProjectExplorerSelectionListener implements ISelectionListener
{
public void selectionChanged(IWorkbenchPart part, ISelection newSelection)
{
if (newSelection instanceof IStructuredSelection)
{
Object element = ((IStructuredSelection) newSelection).getFirstElement();
if (element instanceof IAdaptable)
{
IResource resource = (IResource) ((IAdaptable) element).getAdapter(IResource.class);
final IProject project = resource.getProject();
//do your action here!!
}
}

}

}

XML validation w.r.t to schema

                                                                                                         
import java.io.File;                                                                                      
import java.io.IOException;                                                                              
                                                                                                         
import javax.xml.XMLConstants;                                                                            
import javax.xml.transform.stream.StreamSource;                                                          
import javax.xml.validation.Schema;                                                                      
import javax.xml.validation.SchemaFactory;                                                                
import javax.xml.validation.Validator;                                                                    
                                                                                                         
import org.xml.sax.ErrorHandler;                                                                          
import org.xml.sax.SAXException;                                                                          
import org.xml.sax.SAXParseException;                                                                    
                                                                                                         
public class SchemaValidator                                                                              
{                                                                                                        
public static void main(String[] args)                                                                
{                                                                                                    
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);            
try                                                                                              
{                                                                                                
Schema schema = factory.newSchema(new File("C:\\myschema.xsd"));                              
Validator validator = schema.newValidator();                                                  
validator.setErrorHandler(new SimpleErrorHandler());                                          
try                                                                                          
{                                                                                            
validator.validate(new StreamSource(new File("C:\\myxmldata.xml")));                      
}                                                                                            
catch (SAXException e)                                                                        
{                                                                                            
e.printStackTrace();                                                                      
}                                                                                            
catch (IOException e)                                                                        
{                                                                                            
e.printStackTrace();                                                                      
}                                                                                            
}                                                                                                
catch (SAXException e)                                                                            
{                                                                                                
e.printStackTrace();                                                                          
}                                                                                                
}                                                                                                    
}                                                                                                        
                                                                                                         
class SimpleErrorHandler implements ErrorHandler                                                          
{                                                                                                        
public void warning(SAXParseException e) throws SAXException                                          
{                                                                                                    
System.out.println(e.getMessage());                                                              
// create problem marker                                                                          
}                                                                                                    
                                                                                                         
public void error(SAXParseException e) throws SAXException                                            
{                                                                                                    
System.out.println(e.getMessage());                                                              
// create problem marker                                                                          
}                                                                                                    
                                                                                                         
public void fatalError(SAXParseException e) throws SAXException                                      
{                                                                                                    
System.out.println(e.getMessage());                                                              
// create problem marker                                                                          
}                                                                                                    
}