Wednesday, October 31, 2012

How to customize eclipse market place dialog


By default, Eclipse Market place command is handled by MarketplaceWizardCommand handler . This handler loads remote catalogs by invoking this API http://marketplace.eclipse.org/catalogs/api/p if you do not specify any of your catalogs.
I can think of following solution
  1. Add a command ( ABC Marketplace command)
  2. Add a handler ( ABC Marketplace handler)
  3. attach handler to the command and add this command to main Help menu.
  4. Invoke Market place client in the handler code
MarketplaceClient.openMarketplaceWizard(List catalogDescriptors)

or follow below doc to set parameter

http://wiki.eclipse.org/Marketplace/REST

How to use SWT Tracker

Here is sample code to use Tracker to drag corners of the composite.
 
public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.open();

    final Composite b = new Composite(shell, SWT.NONE);
    b.setBounds(20, 20, 80, 80);
    b.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

    b.addListener(SWT.MouseDown, new Listener() {

      public void handleEvent(Event e) {

        Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE);
        tracker.setStippled(true);
        Rectangle rect = b.getBounds();
        tracker.setRectangles(new Rectangle[] { rect });
        if (tracker.open()) {
          Rectangle after = tracker.getRectangles()[0];
          b.setBounds(after);
        }
        tracker.dispose();
      }
    });
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }  

How to Use TableEditor

Here is sample code put Button in SWT Table Cell. 
Make sure Button gets disposed when it is not required.
 
public class TableEditorTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        shell.setLayout(new FillLayout());


        TableViewer viewer = new TableViewer(shell);
        viewer.getTable().setHeaderVisible(true);
        viewer.getTable().setLinesVisible(true);
        viewer.setContentProvider(new ArrayContentProvider());

        TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("First Name");
        column.setWidth(100);
        TableViewerColumn firstNameCol = new TableViewerColumn(viewer, column);
        firstNameCol.setLabelProvider(new ColumnLabelProvider(){

            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getFirstName();
            }

        });

        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Last Name");
        column.setWidth(100);
        TableViewerColumn lastNameCol = new TableViewerColumn(viewer, column);
        lastNameCol.setLabelProvider(new ColumnLabelProvider(){

            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }

        });




        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Actions");
        column.setWidth(100);
        TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
        actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
            //make sure you dispose these buttons when viewer input changes
            Map<Object, Button> buttons = new HashMap<Object, Button>();


            @Override
            public void update(ViewerCell cell) {

                TableItem item = (TableItem) cell.getItem();
                Button button;
                if(buttons.containsKey(cell.getElement()))
                {
                    button = buttons.get(cell.getElement());
                }
                else
                {
                     button = new Button((Composite) cell.getViewerRow()
                                .getControl(),SWT.NONE);
                    button.setText("Remove");
                    buttons.put(cell.getElement(), button);
                }
                TableEditor editor = new TableEditor(item.getParent());
                editor.grabHorizontal  = true;
                editor.grabVertical = true;
                editor.setEditor(button , item, cell.getColumnIndex());
                editor.layout();
            }

        });



        Person p1 = new Person();
        p1.setFirstName("George");
        p1.setLastName("Burne");

        Person p2 = new Person();
        p2.setFirstName("Adam");
        p2.setLastName("Silva");

        Person p3 = new Person();
        p3.setFirstName("Nathan");
        p3.setLastName("Cowl");

        List<Person> persons = new ArrayList<Person>();
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        viewer.setInput(persons);

        shell.open();
        while(!shell.isDisposed())
        {

            if(!display.readAndDispatch())
            {
                display.sleep();
            }
        }

        display.dispose();

    }


    private static class Person
    {

        String firstName;
        String lastName;

        Person()
        {

        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

    }


}

Resize Table Columns

How to make the columns size to be adjusted to fill complete 
display area of SWT Table? 
 
 
 public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, true));

        TableViewer viewer1 = getViewer(shell);

        List<String> rows = new ArrayList<String>();
        rows.add("Row 1");
        rows.add("Row 2");

        viewer1.setInput(rows);

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

    private static TableViewer getViewer(final Shell shell) {
     TableViewer viewer = new TableViewer(shell, SWT.FULL_SELECTION
                | SWT.H_SCROLL | SWT.V_SCROLL | SWT.NONE);

        viewer.getTable().addListener(SWT.Resize, new Listener() {

          @Override
          public void handleEvent(Event event) {


            Table table = (Table)event.widget;
            int columnCount = table.getColumnCount();
            if(columnCount == 0)
              return;
            Rectangle area = table.getClientArea();
            int totalAreaWdith = area.width;
            int lineWidth = table.getGridLineWidth();
            int totalGridLineWidth = (columnCount-1)*lineWidth; 
            int totalColumnWidth = 0;
            for(TableColumn column: table.getColumns())
            {
              totalColumnWidth = totalColumnWidth+column.getWidth();
            }
            int diff = totalAreaWdith-(totalColumnWidth+totalGridLineWidth);

            TableColumn lastCol = table.getColumns()[columnCount-1];

//check diff is valid or not. setting negetive width doesnt make sense.
            lastCol.setWidth(diff+lastCol.getWidth());

          }
        });

        viewer.setContentProvider(ArrayContentProvider.getInstance());

        viewer.getTable().setLayoutData(
                new GridData(SWT.FILL, SWT.FILL, true, true));

        TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
        col.getColumn().setWidth(100);
        col.getColumn().setText("Text Column");
        col.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public void update(ViewerCell cell) {
                cell.setText((String) cell.getElement());
            }
        });

        col = new TableViewerColumn(viewer, SWT.NONE);
        col.getColumn().setWidth(100);
        col.getColumn().setText("Second Text Column");
        col.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public void update(ViewerCell cell) {
                cell.setText((String) cell.getElement());
            }
        });


        viewer.getTable().setHeaderVisible(true);

        return viewer;
    }

Thursday, August 2, 2012

Where to find Menu/Toolbar URI constants?

MenuUtil has defined constants that are used to form URI for adding menuitems/toolitems/controls to  eclipse menu/toolbar structure.
org.eclipse.ui.menus.MenuUtil
It also has trim constants that are used to position controls in trim positions on Status/Toolbar.


public final static String WORKBENCH_MENU = "menu:org.eclipse.ui.workbench.menu"; 


    /** Main Menu */
    public final static String MAIN_MENU = "menu:org.eclipse.ui.main.menu";    


 /** Main ToolBar (CoolBar) */
    public final static String MAIN_TOOLBAR = "toolbar:org.eclipse.ui.main.toolbar"; 


    /** -Any- Popup Menu */
    public final static String ANY_POPUP = "popup:org.eclipse.ui.popup.any";

    /** Top Left Trim Area */
    public final static String TRIM_COMMAND1 = "toolbar:org.eclipse.ui.trim.command1"; 


    /** Top Right Trim Area */
    public final static String TRIM_COMMAND2 = "toolbar:org.eclipse.ui.trim.command2"; 



    /** Left Vertical Trim Area */
    public final static String TRIM_VERTICAL1 = "toolbar:org.eclipse.ui.trim.vertical1";
    /** Right Vertical Trim Area */
    public final static String TRIM_VERTICAL2 = "toolbar:org.eclipse.ui.trim.vertical2"; 


    /** Bottom (Status) Trim Area */
    public final static String TRIM_STATUS = "toolbar:org.eclipse.ui.trim.status";

Wednesday, September 8, 2010

Set SWT combo width

Setting SWT combo width to its widest item?

1. either re-compute its size and call setSize()

2. combo.pack(true) works as well

Tuesday, July 27, 2010

Internationalize your RCP app


I came across this article that talks about globalizing RCP application by i18n as well as setting up localized splash screens.



http://www.ibm.com/developerworks/java/library/os-eclipse-globalrcp/index.html