Thursday, 22 June 2017

Command Parameter RCP (Eclipse RCP)

Command Parameter RCP (Eclipse RCP)

Open MANIFEST.MF file and right click on the Command and select "commandParameter" from "New".

After creating "commandParameter", enter the details, like "id", "name" and make optional as "false".




After adding the parameter in the command, in View (or Editor) at the time of calling the command set the parameter. Refer the following code to set the parameter at the time of execution.

Code in View (or Editor):


ICommandService commandService = (ICommandService) getSite().getService(ICommandService.class);
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);

// Enter the ID of the Command as the argument to getCommand() method in next line.
Command sysoComm = commandService.getCommand("ResumeParser.commands.sampleCommand");

HashMap<String, String> paramsForCommand = new HashMap<String, String>();
// Enter the Command Parameter id as the Key in the following line.
paramsForCommand.put("ResumeParser.commandParameter2"text.getText());

ParameterizedCommand paramCommand = ParameterizedCommand.generateCommand(sysoCommparamsForCommand);

try {
handlerService.executeCommand(paramCommandnull);
catch (Exception ee) {
ee.printStackTrace();
}


Following is the code of execute() method of the Handler (AbstractHandler) class.


/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
        // Provide the id of the "Command Parameter" as the argument of the getParameter method.
String commandValue = event.getParameter("ResumeParser.commandParameter2");
System.out.println(commandValue);
return null;

}

Friday, 16 June 2017

Execute Command from View/Editor in Eclipse RCP

Execute Command from View/Editor in Eclipse RCP

All Commands will be registered to IHandlerService. If we get the IHandlerService instance then we can call the desired command and then execute it.

// Get the Instance of IHandlerService from View
// Alternate way of getting site object is this also:
// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite()
// But we prefer  following so we get the appropriate IWorkbenchPartSite object.
IHandlerService service = (IHandlerService) getSite().getService(IHandlerService.class); 
try {
service.executeCommand("ResumeParser.commands.sampleCommand", null); 
} catch (ExecutionException e1) { 
e1.printStackTrace(); 
} catch (NotDefinedException e1) { 
e1.printStackTrace(); 
} catch (NotEnabledException e1) { 
e1.printStackTrace(); 
} catch (NotHandledException e1) { 
e1.printStackTrace(); 
}


Sunday, 11 June 2017

Fie Dialog and Folder Dialog in Eclipse RCP


Fie Dialog and Folder Dialog in Eclipse RCP

File Dialog in Eclipse:
- This will only allow you to select the files in the Dialog Box
FileDialog fileDialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); // Following line is Optional if you want to, enable filter for specific files. //fileDialog.setFilterExtensions(new String[] {"*.txt","*.csv"}); String filePath = fileDialog.open(); System.out.println(filePath);


Folder Dialog in Eclipse:
- This will allow you to select the folder in the Dialog box.
DirectoryDialog directoryDialog = new DirectoryDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); // Optional - if you want to open specific path //directoryDialog.setFilterPath("/home/hv185014/junk/"); String directoryPath = directoryDialog.open(); System.out.println(directoryPath);

Thursday, 6 April 2017

Builder Design Pattern - Java

Builder Design Pattern - Java

When the construction of the class contains a large number of argument then this Design Pattern is preferred.

Builder Pattern, as the name suggest, it helps to reduce the complexity in Building Java Object.


public class Person { private String firstname; private String lastname; private String address; private int age; private String city; private String state; public Person(String firstname, String lastname, String address, int age, String city, String state) { super(); this.firstname = firstname; this.lastname = lastname; this.address = address; this.age = age; this.city = city; this.state = state; } }

In the above example, you can see, that the construction of the Person class has 6 arguments and few of them can be optional.

So we can create a new PersonBuilder class which will help to create the Object of the Person class.


public class PersonBuilder { private String firstname; private String lastname; private String address; private int age; private String city; private String state; public PersonBuilder() { } public PersonBuilder setFirstname(String firstname) { this.firstname = firstname; return this; } public PersonBuilder setLastname(String lastname) { this.lastname = lastname; return this; } public PersonBuilder setAddress(String address) { this.address = address; return this; } public PersonBuilder setAge(int age) { this.age = age; return this; } public PersonBuilder setCity(String city) { this.city = city; return this; } public PersonBuilder setState(String state) { this.state = state; return this; } public Person build() { return new Person(firstname, lastname, address, age, city, state); } }

So, now the Person class can be created as below :

If Person is with address and name then it can created as following new PersonBuilder("firstname", "lastname").setAddress("my address").build();

If Person is with state and name then it can created as following new PersonBuilder("firstname", "lastname").setState("guj").build();

Friday, 8 January 2016

Open Eclipse RCP application as maximized

Open Eclipse RCP application as maximized

Open File :

ApplicationWorkbenchWindowAdvisor.java in you RCP application and override the method "postWindowOpen()" and write the code as mentioned below :

@Override
 public void postWindowOpen() {
  super.postWindowOpen();
  IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

  configurer.getWindow().getShell().setMaximized( true );
 }

Following Books are good to refer :


Wednesday, 30 December 2015

Show Perspective switch bar in Eclipse RCP application


Show Perspective switch bar in Eclipse RCP application

Open file ApplicationWorkbenchWindowAdvisor.java in your plug-in project.

Override the method, preWindowOpen and as shown below, setShowPerspectiveBar as True.


 @Override
    public void preWindowOpen ()
    {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setShowPerspectiveBar( true );
    }


Run your RCP application, and on Right side top, you will be able to see perspective Switch.

Following Books are good to refer :






Create (pulldown) Menu in Toolbar in Eclipse RCP

Create (pulldown) Menu in Toolbar


Step 1. Create Toolbar with PullDown
Step 2. Create MenuContribution 
Step 3. Add items to MenuContribution
Step 4. Run the application



Step 1. Create Toolbar with PullDown

Create your Eclipse RCP application ( with mail template).

Suppose open command id is "HarikrushnaVToolbar.open" then create a toolbar with command in it, 
as shown below:



now, convert the style to "pulldown".


Now, if you run the application, you will see a pulldown button next to toolbar icon as follow :


Step 2: Create MenuContribution

Now, create a MenuContribution, which will define the items for pulldown menu.

Define, locationURI to the same name as the menu:commandId , here we provide the LocationURI as "menu:HarikrushnaVToolbar.open"
Also Define Class name as "AllMenuItems".



Step 3. Add items to MenuContribution

Click on "class", you will see the dialogbox as shown below :


Click on Finish.
Now, go to MANIFEST.MF and add "org.eclipse.core.expressions" to "Required Plug-ins".

Enter code as follow in the AllMenuItems.java file


Code :
===========
public void createContributionItems(IServiceLocator serviceLocator,IContributionRoot additions) {

CommandContributionItemParameter commandContributionItemParameter = new CommandContributionItemParameter(
                serviceLocator, "",
                "org.eclipse.ui.file.exit",
                SWT.PUSH);
commandContributionItemParameter.label = "Exit the application";
commandContributionItemParameter.icon = Activator.getImageDescriptor("icons/alt_window_16.gif");

        CommandContributionItem commandContributionItem = new CommandContributionItem(commandContributionItemParameter);
        commandContributionItem.setVisible(true);
        additions.addContributionItem(commandContributionItem, null);
}


============



Step 4. Run the application

Run your application and your toolbar with Menu is ready.