Monday, October 1, 2012

How to open a Java file in read-only mode?

In one of my eclipse plugin based application I wanted to open Java files in read only mode. One option was to use org.eclipse.jface.text.source.SourceViewer , where I could have used JavaSourceViewerConfiguration
for providing Java highlighting and all. But there were two problems:
  1. Source viewer won't inherit the Java editor settings (and if you have modified eclipse Java editor theme), which means your background and foreground in the SourceViewer gets completely messed up.
  2. Secondly you would lose all the markup and bookmarking facilities available with editors
So, I decided to extend the existing Java editor. The steps are extremely simple,
  1. Create an editor extension
  2. Extend the extension class with org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor
  3. Override some of the methods
  4. Open your file with this editor

Code

Editor Code

import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;

@SuppressWarnings("restriction")
public class ReadonlyEd extends CompilationUnitEditor 
{
 public static final String ID = "read.only";
 public ReadonlyEd() {
 }
 @Override
 public boolean isEditable() {
     return false;
 }
 @Override
 public boolean isEditorInputModifiable() {
     return false;
 }
 @Override
 public boolean isEditorInputReadOnly() {
     return true;
 }
 @Override
 public boolean isDirty() {
     return false;
 }
}

Code for Invoking the Editor

IFile file = ...; // Get the Java file instance
IWorkbenchPage page = ...; // Get the workbench page
  
FileEditorInput input = new FileEditorInput(file);
try {
 IDE.openEditor(window.getActivePage(), input, ReadonlyEd.ID);
} catch (PartInitException e) {
 e.printStackTrace();
}

Monday, September 24, 2012

How to hide a window from Win32 Taskbar?


Argh !!! I don't know why but I keep forgetting the ex-style bit for this... 
To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.
The Shell will remove a window's button from the taskbar only if the window's style supports visible taskbar buttons. If you want to dynamically change a window's style to one that doesn't support visible taskbar buttons, you must hide the window first (by calling ShowWindow with SW_HIDE), change the window style, and then show the window.
Taken from here: The Taskbar

Tuesday, September 18, 2012

3 easy steps to self-sign a Applet Jar file

Found this link that explains how to self-sign an applet in 3 easy steps:
  1. keytool -genkey -keystore myKeyStore -alias me
  2. keytool -selfcert -keystore myKeyStore -alias me
  3. jarsigner -keystore myKeyStore jarfile.jar me

Thursday, August 23, 2012

Firebug Lite for SWT Browser on Windows

Recently I came across this question on stackoverflow 'Eclipse swt browser and firebug lite'. The solution seemed very obvious. I answered the question there also, you can always upvote it ;).

The output:


The code for the same is as follows:

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FirebugLite 
{
 public static void main(String[] args) {
  new FirebugLite().start();
 }
 
 public void start()
 {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setLayout(new GridLayout(1, false));
  GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
  gridData.widthHint = SWT.DEFAULT;
  gridData.heightHint = SWT.DEFAULT;
  shell.setLayoutData(gridData);
  shell.setText("Firebug Lite for SWT ;)");
  
  final Browser browser = new Browser(shell, SWT.NONE);
  GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, true);
  gridData2.widthHint = SWT.DEFAULT;
  gridData2.heightHint = SWT.DEFAULT;
  browser.setLayoutData(gridData2);
  
  Button button = new Button(shell, SWT.PUSH);
  button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
  button.setText("Install");
  button.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    browser.setUrl("javascript:(function(F,i,r,e,b,u,g,L,I,T,E){if(F.getElementById(b))return;E=F[i+'NS']&&F.documentElement.namespaceURI;E=E?F[i+'NS'](E,'script'):F[i]('script');E[r]('id',b);E[r]('src',I+g+T);E[r](b,u);(F[e]('head')[0]||F[e]('body')[0]).appendChild(E);E=new%20Image;E[r]('src',I+L);})(document,'createElement','setAttribute','getElementsByTagName','FirebugLite','4','firebug-lite.js','releases/lite/latest/skin/xp/sprite.png','https://getfirebug.com/','#startOpened');");
   }
  });
  
  browser.setUrl("http://stackoverflow.com/questions/12003602/eclipse-swt-browser-and-firebug-lite");

  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }
}

Tuesday, July 31, 2012

SWT Browser and Image Capture

Recently I was playing with the SWT-COM bridge and its Win32 APIs. Believe me or not, but the SWT Browser is the best SWT based UI control I have seen (you can argue but without any success). 

One of the experimental feature I tried accomplishing was to print the full HTML page as Image, and the result was superb !!

See the below images for the embedded browser control and the output of the image capturing utility !! Cool right ;)

Modified Browser Control:




Captured Image: