Showing posts with label swt. Show all posts
Showing posts with label swt. Show all posts

Monday, March 12, 2018

Setting a WorkbenchWindowControlContribution as a SelectionProvider

Leave a Comment

I added a control in the toolbar which extends WorkbenchWindowControlContribution and implements ISelectionProvider. This control contains a Combo and when I change the selection I want to notify another view. Because it's not a ViewPart I can't call getSite().setSelectionProvider directly. I tried to set it like this:

        PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {         PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().setSelectionProvider(MyControl.this);     }); 

If I add this to the protected Control createControl(Composite parent) method the getActivePart would always be null. I also tried registering it when the Combo's value changes for the first time, but that way my other View wasn't notified about the change event.

Here is the ISelectionProvider methods:

@Override public void addSelectionChangedListener(ISelectionChangedListener listener) {     listeners.add(listener); }  @Override public ISelection getSelection() {     return new StructuredSelection(comboBox.getText()); }  @Override public void removeSelectionChangedListener(ISelectionChangedListener listener) {     listeners.remove(listener); }  @Override public void setSelection(ISelection selection) {     for (ISelectionChangedListener listener : listeners) {         listener.selectionChanged(new SelectionChangedEvent(this, selection));     }  } 

The View that should be getting notified implements ISelectionListener. I register it in the public void createPartControl(Composite parent) with getSite().getPage().addSelectionListener(this);

And here is the override of the SelectionChanged too:

@Override public void selectionChanged(IWorkbenchPart part, ISelection selection) {     if (part instanceof MyControl) {         String selectedProtocol = ((StructuredSelection) selection).getFirstElement().toString();                } } 

Can it be done?
Thanks!

0 Answers

Read More

Sunday, August 20, 2017

Set font height as float, instead of integer

Leave a Comment

Is there a way to specify the font size as a float/double instead of integer. I am creating my font as:

Font font = new Font(shell.getDisplay(), "CormorantGaramond",14, SWT.NULL); 

where 14 is the font size in points. But the constructor only accept an integer, which I find odd when the font size is specified in points and not pixel. Is there a way to specify font size as a non integer value. (It is a ttf font, if that matter).

2 Answers

Answers 1

It seems that currently (Eclipse Platform Mars 4.5) there is no platform-independent way to set the font height using the value of the float type.

Reference: org.eclipse.swt.graphics.FontData (Eclipse Platform API Specification) - Help - Eclipse Platform.

Answers 2

There is a way you can set the font size in float. Java provide a built in function "public Font deriveFont(float size)" in ava.awt.Font class. This deriveFont creates a new instance of the Font based on the values you provide, so you will need to maintain a reference to it

And then you have to use the SWTUtils class to convert awt to swt Here it is SWTUtils.java

Download this file and add it to your project. Thats it.

Read More

Friday, March 18, 2016

How to read cookie from org.eclipse.swt.browser.Browser?

Leave a Comment

I want to read JSESSIONID from cookie org.eclipse.swt.browser.Browser. I try to open browser from Eclipse plug-in. I am using below snippet

public static void main(String[] args) {     Display display = new Display();      Shell shell = new Shell(display);     shell.setText("StackOverflow");     shell.setLayout(new FillLayout());      final Browser browser = new Browser(shell, SWT.NONE);      final String url = "https://....";     browser.setUrl(url);     browser.addProgressListener(new ProgressAdapter() {         @Override         public void completed(ProgressEvent event) {             String cookieText = "cookie=" + Browser.getCookie("JSESSIONID", url);             System.out.println(cookieText);         }     });     shell.setSize(400, 300);     shell.open();      while (!shell.isDisposed())     {         if (!display.readAndDispatch())         {             display.sleep();         }     }      display.dispose(); } 

But I am not getting cookie value.

Something like this : c# Get httponly cookie

2 Answers

Answers 1

Try getting the cookie from JavaScript instead of the Browser#getCookie() method. It worked for me during my test, but as I don't know your website, I can't test it against it:

public static void main(String[] args) {     Display display = new Display();      Shell shell = new Shell(display);     shell.setText("StackOverflow");     shell.setLayout(new GridLayout());      final Browser browser = new Browser(shell, SWT.NONE);     browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));      final String url = "https://...";     browser.setUrl(url);      /* Define the function to call from JavaScript */     new BrowserFunction(browser, "cookieCallback") {         @Override         public Object function(Object[] objects) {              Object[] keyValuePairs = (Object[]) objects[0];              for(Object keyValue : keyValuePairs)             {                 Object[] pair = (Object[]) keyValue;                  if(Objects.equals("JSESSIONID", pair[0]))                     System.out.println(pair[1]);             }              return null;         }     };      Button button = new Button(shell, SWT.PUSH);     button.setText("Get cookie");     button.addListener(SWT.Selection, new Listener() {         @Override         public void handleEvent(Event event) {             /* Get the cookie from JavaScript and then call the function */             browser.execute("cookieCallback(document.cookie.split( ';' ).map( function( x ) { return x.trim().split( '=' ); } ));");         }     });      shell.setSize(400, 300);     shell.open();      while (!shell.isDisposed())     {         if (!display.readAndDispatch())         {             display.sleep();         }     }      display.dispose(); } 

Answers 2

If the cookie you wish to get is marked as httpOnly then you won't be able to get it in current SWT versions. See this bug for a discussion.

Read More