Wednesday, March 30, 2016

How to select an item in a Jface TableViewer after a dialog becomes activated

Scenario : We have a Jface Dialog and a TableViewer or a TreeViewer on that dialog. after the dialog become active we want to select an item and want to fire the TableViewers selection listener.


Step 1. Add a selection listener to the TableViewer :

Note : You should do this in createDialogArea() method.

  getTableViewer().addSelectionChangedListener(new ISelectionChangedListener() {  
   @Override  
   public void selectionChanged(SelectionChangedEvent event) {  
   final Object selection = getSingleSelection(event.getSelection());  
   if (selection instanceof SomeObject) {  
    DoSomething();  
   }  
   }  
  });  
  }  


Step 2. Add a Shell Listner :(again in createDialogArea() method)

  getShell().addShellListener(new ShellAdapter()  
  {  
   @Override  
   public void shellActivated(ShellEvent e) {    
   super.shellActivated(e);  
   if (getLines().size()>0) {  
    getViewer().getControl().setFocus();  
    getTableViewer().setSelection(new StructuredSelection(get.getElementAt(0)),true);  
   }  
   }  
  });  



You are done ! now after the shell become active the first item of the TableViews will be selected and a selection listner will be fired . Enjoy!


Sunday, October 4, 2015

Css Stying in E4 Compatibility Layer

I can apply Css Styling  in my Eclipse 3.x plugin using following steps:


Step 1. Configuring CSS Stylesheets:


1.1 add a css file





1.2 add a theme using theme extension point






1.3






Step 2. add css id/class to your swt control:
final Composite composite = new Composite(parent, SWT.NONE);
composite.setData("org.eclipse.e4.ui.css.id","MyCSSTagForLabel");

Step 3. add the folowing block to your default.css file:

#MyCSSTagForLabel{
  color: black;
  background: #99b4d1;
  swt-corner-radius: 20;
}

Done!

Thursday, September 10, 2015

Eclipse RCP : How to check state(toggle) of a menu items or toolbar items inside handlers

In Eclipse RCP there are at-least two different ways we  can check the state of a menu item or  a toolbar item inside a handlers execute method :

Option 1 :


ICommandService service =(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service.getCommand("org.eclipse.example.command.toggle");
State state = command.getState("org.eclipse.example.command.toggleState");
System.out.println(state.getValue());

//state.setValue(!(Boolean) state.getValue());



Option 2:


   @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
   
   
        Event selEvent = (Event) event.getTrigger();
        Widget widget =  selEvent.widget;
        if (widget instanceof ToolItem) {        
        isSelected = ((ToolItem)widget).getSelection();
}else {
isSelected = ((MenuItem)widget).getSelection();
}
}


Wednesday, September 2, 2015

Eclipse RCP : How to check a view is currenlty visible


If we need to check whether a view currently available or visible we can use following code :

Boolean result = false;

IViewReference viewReferene =  PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findViewReference("my_view_id");


if (viewReferene != null) {
result = true;
}





How to Enable and Disable VNC server password authentication in cent os

You can switch between password and non password mode by:

# vi /etc/X11/xorg.conf  (enable password)
[..]
    #Option "SecurityTypes" "None"
    Option "SecurityTypes" "VncAuth"
    Option "UserPasswdVerifier" "VncAuth"
    Option "PasswordFile" "/root/.vnc/passwd"
[..]

# vi /etc/X11/xorg.conf  (disable password)
[..]
    Option "SecurityTypes" "None"
    #Option "SecurityTypes" "VncAuth"
    #Option "UserPasswdVerifier" "VncAuth"
    #Option "PasswordFile" "/root/.vnc/passwd"
[..]

Restart X for change to take effect:
# init 3   (wait a few seconds - )

# init 5



after enabling the password: 

You should be able to type ”vncpasswd” from the terminal as root. This will enable you to type in a new password for the VNC connection.

Sunday, August 30, 2015

Configure log4j properties file for logging both in file system and console



# Root logger option
log4j.rootLogger= DEBUG,errorLogger,stdout,



# Direct log messages to a log file
log4j.appender.errorLogger=org.apache.log4j.RollingFileAppender
log4j.appender.errorLogger.Threshold = ERROR
log4j.appender.errorLogger.File=.\\Log\\logging.log
log4j.appender.errorLogger.MaxFileSize=1MB
log4j.appender.errorLogger.MaxBackupIndex=1
log4j.appender.errorLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.errorLogger.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n






The above configuration will write only error log to file system and all the other log in console.
log4j.appender.errorLogger.Threshold = ERROR   does the trick.

Tuesday, January 13, 2015

Eclipse Jobs API : Perform something when job is done

When we need to do something for example notify user or cleaup someting after a JOB is finished we can user the  JobChangeListener to accomplish this. see the folowing code:


     final Job job = new Job("Long Running Job") {
        protected IStatus run(IProgressMonitor monitor) {
           try {
              while(hasMoreWorkToDo()) {
                 // do some work
                 // ...
              if (monitor.isCanceled()) return Status.CANCEL_STATUS;
             }
              return Status.OK_STATUS;
           } finally {
              schedule(60000); // start again in an hour
           }
        }
     };
  job.addJobChangeListener(new JobChangeAdapter() {
        public void done(IJobChangeEvent event) {
        if (event.getResult().isOK())
           postMessage("Job completed successfully");
           else
              postError("Job did not complete successfully");
        }
     });
  job.setSystem(true);
     job.schedule(); // start as soon as possible


See the details in following website:
https://eclipse.org/articles/Article-Concurrency/jobs-api.html





5 Strategies for Getting More Work Done in Less Time

Summary.    You’ve got more to do than could possibly get done with your current work style. You’ve prioritized. You’ve planned. You’ve dele...