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                                                                          
}                                                                                                    
}                                                                                                        
                                                                                                            

Checking XML structure validation

To check the syntax validation for XML files, we can use below piece of code.Implement an error handler to handle the syntax errors.

This does not do any schema validation. 

//DOM model
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);


DocumentBuilder xmlBuilder = factory.newDocumentBuilder();
xmlBuilder.setErrorHandler(new SimpleErrorHandler());
xmlBuilder.parse(new ByteArrayInputStream(myXMLString.getBytes(IOUtil.UTF_8)));


//Handler
public 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
}
}