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;
    }