Plugin development: composite of button in a view plugin - eclipse

I am using zest to create plugin that show graph. I am trying to create button that refresh the plugin. The button is working, and I see the graph in the view also. But, the button is taking a lot of space in the view, and limit the place for the graph.I want that the button want limit the graph place.. How can I fix it? Thanks]
public void createPartControl(Composite parent) {
//create refresh button
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(1, false));
Button btnMybutton = new Button(container, SWT.PUSH);
btnMybutton.setBounds(0, 10, 75, 25);
btnMybutton.setText("Refresh Graph");
btnMybutton.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
init();
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
// Graph will hold all other objects
graph = new Graph(parent, SWT.NONE);
}

If you want to show the button on top of the graph, you should use a FormLayout instead of a GridLayout:
public void createPartControl(Composite parent) {
//create refresh button
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new FormLayout()); // Use FormLayout instead of GridLayout
Button btnMybutton = new Button(container, SWT.PUSH);
// btnMybutton.setBounds(0, 10, 75, 25); This line is unnecessary
// Assign the right FormData to the button
FormData formData = new FormData();
formData.left = new FormAttachment(0, 5);
formData.top = new FormAttachment(0, 5);
btnMybutton.setLayoutData(formData);
btnMybutton.setText("Refresh Graph");
// Use SelectionAdapter instead of SelectionListener
// (it's not necessary but saves a few lines of code)
btnMybutton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
init();
}
});
// Graph will hold all other objects
graph = new Graph(container, SWT.NONE); // Note parent changed to container
// Assignt the right FormData to the graph
FormData formData2 = new FormData();
formData2.left = new FormAttachment(0, 0);
formData2.top = new FormAttachment(0, 0);
formData2.right = new FormAttachment(100, 0);
formData2.bottom = new FormAttachment(100, 0);
graph.setLayoutData(formData2);
}

Related

how to get scrollbar in case of formlayout in parent composite In swt?

I am currently working on a layout where I would like to have scrollbar .I have a parent which is having form layout(Which I can not change). Below sample code reproduce same scenario .
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates ScrolledComposite
*/
public class ScrolledCompositeTest {
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void createContents(Composite parent) {
parent.setLayout(new FormLayout());
// Create the ScrolledComposite to scroll horizontally and vertically
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
// Create a child composite to hold the controls
Composite child = new Composite(sc, SWT.NONE);
child.setLayout(new FillLayout());
sc.setBackground(new Color(parent.getDisplay(), 0,0,0));
// Create the buttons
new Button(child, SWT.PUSH).setText("One");
new Button(child, SWT.PUSH).setText("Two");
/*
* // Set the absolute size of the child child.setSize(400, 400);
*/
// Set the child as the scrolled content of the ScrolledComposite
sc.setContent(child);
// Set the minimum size
sc.setMinSize(500, 500);
// Expand both horizontally and vertically
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
}
public static void main(String[] args) {
new ScrolledCompositeTest().run();
}
}
If i change the parent layout to fill or grid the scrollbar works as expected.Any clue on this will be helpfull.
Please add FormData to ScrolledComposite
FormData data = new FormData();
data.top = new FormAttachment(0, 5);
data.left = new FormAttachment(0, 5);
data.bottom = new FormAttachment(100, -5);
data.right = new FormAttachment(100, -5);
sc.setLayoutData(data);
public class ScrolledCompositeTest {
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void createContents(Composite parent) {
parent.setLayout(new FormLayout());
// Create the ScrolledComposite to scroll horizontally and vertically
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
sc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_CYAN));
FormData data = new FormData();
data.top = new FormAttachment(0, 5);
data.left = new FormAttachment(0, 5);
data.bottom = new FormAttachment(100, -5);
data.right = new FormAttachment(100, -5);
sc.setLayoutData(data);
// Create a child composite to hold the controls
Composite child = new Composite(sc, SWT.NONE);
child.setLayout(new FillLayout());
// Create the buttons
new Button(child, SWT.PUSH).setText("One");
new Button(child, SWT.PUSH).setText("Two");
/*
* // Set the absolute size of the child child.setSize(400, 400);
*/
// Set the child as the scrolled content of the ScrolledComposite
sc.setContent(child);
// Set the minimum size
sc.setMinSize(500, 500);
// Expand both horizontally and vertically
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
}
public static void main(String[] args) {
new ScrolledCompositeTest().run();
}
}
Output :

want to create drop down list (Combo box viewer) in TreeColumn in SWT

I am using Tree and in this tree I have a five treecolumn. Also create two treeItem one is parent and other child, put their values in treecolumn by programatically. Now I need a dropdown List(Combobox) in each tree column(except first one) to view the list data. Currently getting only single value. Please see the below code to get tree item values editable in treecolumn.
private void editTreeTable(final Tree table){
final TreeEditor editor = new TreeEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseUp(final MouseEvent e) {
final Control oldEditor = editor.getEditor();
if (oldEditor != null) {
oldEditor.dispose();
}
final Point p = new Point(e.x, e.y);
final TreeItem item = table.getItem(p);
if (item == null) {
return;
}
for (int i = 1; i < table.getColumnCount(); ++i) {
if (item.getBounds(i).contains(p)) {
final int columnIndex = i;
// The control that will be the editor must be a
final Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText(columnIndex ));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(final ModifyEvent e) {
final Text text = (Text) editor.getEditor();
editor.getItem().setText(columnIndex , text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, columnIndex );
}
}
}
});
}
Now find the below code to get the tree item value from API
private void createTestSuiteTable( final Tree table)
{
//Dispose all elements
TreeItem items[] = table.getItems();
for(int i=0;i<items.length;i++)
{
items[i].dispose();
}
TSGson tsGsons[] = TestSuiteAPIHandler.getInstance().getAllTestSuites();
boolean checked=false;
for (TSGson tsGson : tsGsons)
{
parentTestSuite = new TreeItem(table, SWT.NONE|SWT.MULTI);
parentTestSuite.setText(new String[] { "" +tsGson.tsName, "", "","","","" });
parentTestSuite.setData("EltType","TESTSUITE");
if(tsGson.tsTCLink==null)
continue;
for(TSTCGson tsTCGson : tsGson.tsTCLink)
{
TreeItem trtmTestcases = new TreeItem(parentTestSuite, SWT.NONE|SWT.MULTI);
trtmTestcases.setText(new String[] {tsTCGson.tcName,
tsTCGson.tcParams.get(0)!=null ?tsTCGson.tcParams.get(0).tcparamValue:"",
tsTCGson.tcParams.get(1)!=null ?tsTCGson.tcParams.get(1).tcparamValue:"",
tsTCGson.tcParams.get(2)!=null ?tsTCGson.tcParams.get(2).tcparamValue:"",
"local",
tsTCGson.tcParams.get(4)!=null ?tsTCGson.tcParams.get(4).tcparamValue:"" });
trtmTestcases.setData("EltType","TESTCASE");
table.setSelection(parentTestSuite);
if(checked)
{
trtmTestcases.setChecked(checked);
}
}
}
}
Find the below code for tree column creation in SWT
localHostTable = new Tree(composite_2,SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
localHostTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
localHostTable.setLinesVisible(true);
localHostTable.setHeaderVisible(true);
TreeColumn trclmnNewColumn_1 = new TreeColumn(localHostTable, SWT.NONE);
trclmnNewColumn_1.setWidth(113);
trclmnNewColumn_1.setText("TestSuite/TestCase");
TreeColumn trclmnColumn_5 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_5.setWidth(73);
trclmnColumn_5.setText("Exe_Platform");
TreeColumn trclmnColumn_6 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_6.setWidth(77);
trclmnColumn_6.setText("Exe_Type");
TreeColumn trclmnColumn_7 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_7.setWidth(85);
trclmnColumn_7.setText("Run_On");
TreeColumn trclmnColumn_8 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_8.setWidth(81);
trclmnColumn_8.setText("Thread-Count");
final TreeColumn trclmnColumn_9 = new TreeColumn(localHostTable, SWT.NONE);
trclmnColumn_9.setWidth(97);
trclmnColumn_9.setText("Column5");
please suggest
Since there's nothing in your question about Combo or CCombo controls, I can't help you troubleshoot an issue. I also am not going to write your code for you, but I can try to point you in the right direction with a short example.
Yes, i want the combo to always be visible.
You can still use a TreeEditor to accomplish this, and it will actually be simpler than the code snippet you posted with the MouseListener.
Create the CCombo (or Combo) as you would in any other situation, and use TreeEditor.setEditor(...) methods to specify that the CCombo control should be displayed in that cell:
// ...
final CCombo combo = new CCombo(tree, SWT.NONE);
final TreeEditor editor = new TreeEditor(tree);
editor.setEditor(combo, item, 1);
// ...
Full MCVE:
public class TreeComboBoxTest {
private final Display display;
private final Shell shell;
public TreeComboBoxTest() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
final Tree tree = new Tree(shell, SWT.BORDER | SWT.VIRTUAL | SWT.FULL_SELECTION);
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
tree.setLinesVisible(true);
tree.setHeaderVisible(true);
final TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
column1.setWidth(75);
column1.setText("Column 1");
final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
column2.setWidth(75);
column2.setText("Column 2");
final TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText(0, "Hello");
final CCombo combo = new CCombo(tree, SWT.NONE);
combo.setItems(new String[] { "Item 1", "Item 2", "Item 3" });
final TreeEditor editor = new TreeEditor(tree);
editor.setEditor(combo, item, 1);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
// Optional, but allows you to get the current value by calling
// item.getText() instead of going through the TreeEditor and
// calling ((CCombo) editor.getEditor()).getText()
combo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
item.setText(1, combo.getText());
}
});
}
public void run() {
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String... args) {
new TreeComboBoxTest().run();
}
}
Note the SelectionListener added to the CCombo. Even though you've used the TreeEditor, if you call item.getText(index), it will return an empty String because setText(...) has not been called. By calling setText(...) in the listener, you won't have to go through the TreeEditor to get the value.
So you can call item.getText(index) instead of ((CCombo) editor.getEditor()).getText().

Trying to set the size/location of a button in a tab for SWT

This should be pretty simple but this is the first time I've worked with SWT. This is what I have so far.
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display){
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open ();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder){
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Button okButton = new Button(folder, SWT.PUSH);
okButton.setText("OK");
okButton.setSize(10,10);
NFL.setControl(okButton);
CTabItem NBA = new CTabItem(folder,SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder,SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800,500);
}
public static void main (String [] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}
What this currently gives me is this....
How would I make this a small button in the bottom right corner? Or just in general make it smaller and move it somewhere.
Since you are using a FillLayout the control takes up the entire space available. What you need is a different kind of a layout. I will suggest you to read this article, it will be a good start.
I generally prefer GridLayout as it is quite easy to use and it fulfills most needs.
Edited: Modifying your code to use GridLayout
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display) {
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new GridLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
folder.setLayoutData(new GridData(GridData.FILL_BOTH));
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder) {
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Composite nflParent = new Composite(folder, SWT.NONE);
nflParent.setBackground(folder.getDisplay().getSystemColor(SWT.COLOR_BLUE));
nflParent.setLayout(new GridLayout());
Button okButton = new Button(nflParent, SWT.PUSH);
okButton.setText("OK");
GridData gd = new GridData();
gd.verticalAlignment = GridData.END;
gd.horizontalAlignment = GridData.END;
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
okButton.setLayoutData(gd);
NFL.setControl(nflParent);
CTabItem NBA = new CTabItem(folder, SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder, SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800, 500);
}
public static void main(String[] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}

TableViewer Centered Layout

So now I have successfully added a TableViewer in my TitleAreaDialog.
I am trying to figure out some of layout issues I am having.
Can I control the layout and location of my tableViewer in my Dialog window.
Right now the table is showing up on the right side.
I want it to be centered in my parent Composite.
Can I add the TableViewer to a Parent Layout in the createDialogArea method?
I will be adding more composites to the Dialog and would like to be able to control where they go and how they look.
Also my table shows a half empty column at the end of the table, is there a way to remove that?
Something like:
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.CENTER;
TableViewer d = createTableViewer(area);
d.setLayoutData(gridData);
This is my createDialogArea code.
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NULL);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
TableViewer d = createTableViewer(area);
return area;
}
Here is my tableviewer code
private TableViewer createTableViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent, viewer);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setInput(AplotSelectedDataTable.getInstance().getArrayData());
// Layout the viewer
GridData gridData = new GridData(SWT.CENTER);
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
return viewer;
}
Here is a small example that should help you with your layout issues:
public class TestClass extends Dialog {
private TableViewer viewer;
protected TestClass(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout(2, true);
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createButtons(area);
createTableViewer(area);
return area;
}
private void createButtons(Composite parent)
{
Button button1 = new Button(parent, SWT.PUSH);
button1.setText("Button1");
button1.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
Button button2 = new Button(parent, SWT.PUSH);
button2.setText("Button2");
button2.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
}
private void createTableViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
// Layout the viewer
GridData gridData = new GridData(SWT.CENTER, SWT.FILL, true, true);
gridData.horizontalSpan = 2;
table.setLayoutData(gridData);
}
private void createColumns(Composite parent)
{
TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText("Title");
column.setWidth(100);
column.setResizable(true);
column.setMoveable(false);
}
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell(display);
TestClass test = new TestClass(shell);
test.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
It basically creates a small Dialog with 2 buttons at the top and a centered TableViewer below. This should give you an idea on how to solve your problem.

a SWT control refuses to "grab excess vertical space"

The case is that the TopTaskGroup(left one) can "grab excess vertical space" while resizing window, but the NewTaskGroup(the right one), after adding a TooBar on it(see the createAddBtnOnGroup method), it doesn't grow as you resize the window. Why is that?
(I have a shell instance with 2-column GridLayout)
Code is here:
private void createTaskWidgets() {
createTopTaskGroup();
createNewTaskGroup();
}
private void createTopTaskGroup() {
Group topTasksGroup = new Group(shell, SWT.SHADOW_NONE);
topTasksGroup.setText(TaskConsts.TOP_TASK_LIST);
topTasksTable = new TaskTable(topTasksGroup, TaskTable.SORT_BY_VOTES, iteration, this);
topTasksTable.setLayoutData(getTableGridData() );
topTasksGroup.setLayout(new GridLayout() );
topTasksGroup.setLayoutData(getTableGridData() );
topTasksGroup.pack();
}
private void createNewTaskGroup() {
Group newTasksGroup = new Group(shell, SWT.SHADOW_NONE);
newTasksGroup.setText(TaskConsts.NEW_TASK_LIST);
newTasksTable = new TaskTable(newTasksGroup, TaskTable.SORT_BY_CREATION_TIME, iteration, this);
topTasksTable.setLayoutData(getTableGridData() );
ToolBar actionToolBar = createAddBtnOnGroup(newTasksGroup);
newTasksGroup.setLayout(new GridLayout() );
newTasksGroup.setLayoutData(getTableGridData() );
newTasksGroup.layout();
newTasksGroup.pack();
// set actionToolBar's location to newTasksGroup's right-top position
actionToolBar.setLocation(
newTasksGroup.getLocation().x + newTasksGroup.getSize().x
- actionToolBar.getSize().x - 5,
newTasksGroup.getLocation().y - 2);
}
private GridData getTableGridData() {
GridData gridData = new GridData(0, SWT.FILL, false, true);
return gridData;
}
private ToolBar createAddBtnOnGroup(Group newTasksGroup) {
ToolBar actionToolBar = new ToolBar(newTasksGroup, SWT.HORIZONTAL | SWT.RIGHT);
addTaskToolItem = new ToolItem(actionToolBar, SWT.PUSH | SWT.RIGHT);
addTaskToolItem.setImage(new Image(display, TaskConsts.ICON_PLUS));
final MainWindow mainWindow = this;
addTaskToolItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new CreateTask(getShell(), mainWindow);
}
});
GridData gridData = new GridData();
gridData.exclude = true;
actionToolBar.setLayoutData(gridData);
actionToolBar.pack();
return actionToolBar;
}
private void organize() {
GridLayout gridLayout = new GridLayout(2, false);
shell.setLayout(gridLayout);
shell.pack();
}
Thanks in advance~
Thanks for your excellent problem description!
It seems to me that this is a simple copy-paste bug.
The fourth line in your createNewTaskGroup method should not be
topTasksTable.setLayoutData(getTableGridData() );
but
newTasksTable.setLayoutData(getTableGridData() );