Changing dynamically view content in Eclipse plugin view - eclipse

I'm developing Eclipse plugin that has a treeview with toolbar and buttons. I'd want to make the plugin work so that it would show the treeview as default but in case of some error there would be some text and button to initialize or update the plugin. The plugin view should change dynamically depending on the state of the plugin between the treeview and the "error view".
For now I'm creating the treeview instance and doing the other needed actions in createPartControl method to show the treeview right. How should I implement the dynamic view showing different kind of content in the plugin view? Is that possible at all?
Code of the createPartControlMethod:
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
drillDownAdapter = new DrillDownAdapter(viewer);
viewContentProvider = new ViewContentProvider();
viewer.setContentProvider(viewContentProvider);
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());
viewer.expandToLevel(2);
// Create the help context id for the viewer's control
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "my.plugin.viewer");
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
setToolBarButtonsEnabled();

Simplest way would be to create a Composite with a StackLayout and two children instead:
private Composite container;
private TreeViewer viewer;
private Composite errorComposite;
private StackLayout layout;
public void createPartControl(Composite parent) {
container = new Composite(parent);
layout = new StackLayout();
viewer = new TreeViewer(container, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
... // setup viewer
errorComposite = new Composite(container, SWT.NONE);
... // setup error view
}

Related

How to capture the value of a cell of a TableViewer where a contextual menu has been activated in eclipse e4?

In one of my eclipse e4 application JMSToolBox, some data is displayed in aTableViewer
A contextual menu is defined in the e4 model file (e4xmi) and linked to theTableViewer like this
menuService.registerContextMenu(tableViwere.getTable(), <name of the e4 part menu>);
Attached to the contextual menu in the e4 model, a "menu item" is linked to a"Dynamic Menu Contribution" class that dynamically add the menu items to the menu:
public class VisualizerShowPayloadAsMenu {
#Inject private EModelService modelService;
#AboutToShow
public void aboutToShow(EModelService modelService, List<MMenuElement> items) {
// Not the real code..., illustrate adding a dynamic menu item to the contextual menu
MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
dynamicItem.setLabel(<name..>);
dynamicItem.setContributorURI(Constants.BASE_CORE_PLUGIN);// "platform:/plugin/org.titou10.jtb.core");
dynamicItem.setContributionURI(Constants.VISUALIZER_MENU_URI);// "bundleclass://org.titou10.jtb.core/org.titou10.jtb.visualizer.ui.VisualizerShowPayloadAsHandler");
items.add(dynamicItem);
}
Now, what I want to do is to capture the data in the underlying cell where the contextual menu has been activated, and get that value back in the method annotated by"#AboutToShow" in order
to addMDirectMenuItementries to the contextual menu with a label containing that value
Q: how to do that with eclipse rcp e4?
In the attached picture, the right click happened in the cell with content="ID:414d5120514d41414544202020202020ee4bb25612666920". I would like to get this value back in the #AboutToShowmethod and add menu items to the"Open Payload as..."menu based on that value
Thanks
I found a way to do it!
I'm not sure it is the best way, but at least it works and it is quite simple
The following code is here to illustrate the idea, it is not valid Java.
In the part that manage theTableViewer:
TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
{...}
new TableViewerFocusCellManager(tableViewer, new JTBFocusCellHighlighter(tableViewer, windowContext));
JTBFocusCellHighlighterclass:
public class JTBFocusCellHighlighter extends FocusCellHighlighter {
private IEclipseContext windowContext;
private Table table;
public JTBFocusCellHighlighter(ColumnViewer viewer, IEclipseContext windowContext) {
super(viewer);
this.windowContext = windowContext;
this.table = ((TableViewer) viewer).getTable();
}
#Override
protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
super.focusCellChanged(newCell, oldCell);
// Capture the content of the cell (or other info..) and store it in Eclipse Context
windowContext.set("key", newCell.getText());
TableColumn tableColumn = table.getColumn(newCell.getColumnIndex());
}
}
Real code implementation: JTBSessionContentViewPart , JTBFocusCellHighlighter and FilterMenu

Change form item editor type dynamically in smartGwt

I have a smartGwt DynamicForm with a FormItem
FormItem item = createTextItem();
form.setFields(item);
After creating the and setting fields, I need to dynamically set an editor type for the item. I have to do it dynamically based on some conditions.
I'm calling item.setEditorType(new PasswordItem());
just after I call form.editRecord(record); so that the new editor type should appear. But it is not working.
Tried calling item.redraw() and is not working.
My goal is to set the editor type dynamically based on the record that is edited.Please help.
Try with Custom Data Binding (see page 23 for more details). What you tried won't work, AFAIK, because the ListGridField has already been created with the initial custom editor, and it can't be changed dynamically with setEditorCustomizer.
Take a look at this sample (based on this showcase demo), which does what you want to do to the password field when it is being edited in the DynamicForm, and after the changes have been saved (please pay attention to the comments, as without some of these settings it won't work as expected):
public void onModuleLoad() {
final DataSource dataSource = ItemSupplyLocalDS.getInstance();
final DynamicForm form = new DynamicForm();
form.setIsGroup(true);
form.setNumCols(4);
form.setDataSource(dataSource);
// very important for not having to set all fields all over again
// when the target field is customized
form.setUseAllDataSourceFields(true);
final ListGrid listGrid = new ListGrid();
listGrid.setWidth100();
listGrid.setHeight(200);
listGrid.setDataSource(dataSource);
listGrid.setAutoFetchData(true);
IButton editButton = new IButton("Edit");
editButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.editRecord(listGrid.getSelectedRecord());
// when the button is clicked, the password field is rendered with
// a plain text item editor, for easy verification of values entered
FormItem passwordField = new FormItem("passwordFieldName");
passwordField.setEditorProperties(new TextItem());
form.setFields(passwordField);
form.markForRedraw();
}
});
IButton saveButton = new IButton("Save");
saveButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.saveData();
// when the button is clicked, the password field is rendered with
// a password editor, for added privacy/security
FormItem passwordField = new FormItem("passwordFieldName");
passwordField.setEditorProperties(new PasswordItem());
form.setFields(passwordField);
form.markForRedraw();
}
});
VLayout layout = new VLayout(15);
layout.setWidth100();
layout.setHeight100();
layout.addMember(listGrid);
layout.addMember(editButton);
layout.addMember(form);
layout.addMember(saveButton);
layout.draw();
}

How to set ImageHyperlink to have title and description like in the welcome page?

I need to draw an ImageHyperlink that looks exactly like the ones in
Eclipse's welcome page where the text consists of bold title and
description but I can't find this feature in the ImageHyperlink widget
itself, do I have to override the paintHyperlink() myself ? if so, could
someone provide a snippet?
If I read the source correctly (and it is very complex) the welcome page is actually done using HTML and not ImageHyperlink.
You an override the paintHyperlink method of ImageHyperlink:
protected void paintHyperlink(GC gc, Rectangle bounds)
so you should be able to do something similar.
Or, as an option, you can try to mimic appearance, by using standard approach of creating composites (just style it as you wish;)):
private void demoHyperlinks() {
form.getBody().setLayout(new GridLayout());
Composite container = new Composite(form.getBody(), SWT.NONE);
container.setLayout(new GridLayout());
ImageHyperlink imageHyperlink = toolkit.createImageHyperlink(container, SWT.NULL);
imageHyperlink.setText("This is an image hyperlink.");
imageHyperlink.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_BLUE));
imageHyperlink.setFont(new Font(form.getDisplay(), new FontData("Verdana", 20, SWT.BOLD)));
imageHyperlink.setImage(new Image(getShell().getDisplay(), "images/help.gif"));
imageHyperlink.addHyperlinkListener(new HyperlinkAdapter() {
#Override
public void linkActivated(HyperlinkEvent e) {
System.out.println("Image hyperlink activated.");
}
});
Composite descriptionContainer = new Composite(container, SWT.NONE);
GridLayout gl = new GridLayout();
gl.marginLeft = 25;
descriptionContainer.setLayout(gl);
Hyperlink hyperlink = toolkit.createHyperlink(descriptionContainer, "This is a hyperlink link", SWT.NULL);
hyperlink.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_BLUE));
HyperlinkGroup group = new HyperlinkGroup(getShell().getDisplay());
group.add(imageHyperlink);
group.add(hyperlink);
group.setHyperlinkUnderlineMode(HyperlinkSettings.UNDERLINE_NEVER);
}

Disable the window resize on JFace WizardPage

I want to disable the button maximize/minimize, below I post image to explain
this is my code :
public class ProjectWizardPageOne extends WizardPage {
private String platform;
public ProjectWizardPageOne(String title) {
super(title);
this.setTitle(title);
this.setMessage("Configure Project Name and Location");
}
#Override
public void createControl(Composite parent) {
Composite container = new Composite(parent,SWT.NONE);
setPageComplete(false);
setControl(container);
Canvas leftPanel = new Canvas(container, SWT.NONE);
leftPanel.setBackgroundImage(new Image(leftPanel.getDisplay(), this
.getClass().getClassLoader()
.getResourceAsStream("/icons/mypicture.png")));
leftPanel.setBounds(0, 0, 183, 282);
Composite rightContainer = new Composite(container, SWT.NONE);
rightContainer.setBackground(new Color(null, 255, 255, 255));
rightContainer.setBounds(181, 0, 399, 282);
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
I tried to get the Composite's Shell like this "container.getShell();"
but I don't understand How I can set these parameters "SWT.SHELL_TRIM | SWT.TOOL"!
Thanks
Controlling the Window/Shell is not the responsibility of a WizardPage, it can not do that. It's the responsibility of the WizardDialog or the code that creates it. In fact, there is no guarantee that a Wizard and its WizardPages will even be contained in a WizardDialog; anything can implement the IWizardContainer interface to present wizards in a different way.
Is it a File -> New wizard or a custom wizard that is programatically launched. If it is custom, you would have to create WizardDialog and then pass Wizard instance to it. When creating WizardDialog, you would also create Shell, for which you can send the argument without SWT.RESIZE. For File -> New, since the dialog is not created by you, I dont think you can control resize option there. The resize can only be passed in the constructor of Shell.
In case of dialogs, I have observed that I need to explicitly specify that I need min, max buttons at upper-right corner. For that I need to call the below method in a constructor:
setShellStyle(getShellStyle() | SWT.MAX | SWT.MIN | SWT.RESIZE);
Since Wizard is also a dialog, I can call the above method to reset the shellStyle not to include max, min, and other buttons (see above code). The wizard by default might be adding these buttons. But I think you can override this by recalling at the end of wizard creation. Hope this helps.
public class InstallerWizard extends Wizard{
...
main()
{
WizardDialog dialog = new DisableMax(shell, new InstallerWizard());
dialog.open();
}
}
public class DisableMax extends WizardDialog {
public DisableMax(Shell parentShell, IWizard newWizard) {
super(parentShell, newWizard);
setShellStyle(SWT.CLOSE | SWT.MIN | SWT.RESIZE | getDefaultOrientation());
}
}

Editable SWT table

How to edit SWT table Values without Using Mouse Listeners?
Do the TableEditor snippets in the below link help?
SWT Snippets
The first example in the TableEditor section uses a SelectionListener on the table (unlike the second example which uses a MouseDown event you mentioned you don't want)
You could perhaps make use of the TraverseListener or KeyListener too to help you achieve what you want.
final int EDITABLECOLUMN = 1;
tblProvisionInfo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// Clean up any previous editor control
final TableEditor editor = new TableEditor(tblProvisionInfo);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
Control oldEditor = editor.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// Identify the selected row
TableItem item = (TableItem) e.item;
if (item == null)
return;
// The control that will be the editor must be a child of the
// Table
Text newEditor = new Text(tblProvisionInfo, SWT.NONE);
newEditor.setText(item.getText(EDITABLECOLUMN));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent me) {
Text text = (Text) editor.getEditor();
editor.getItem()
.setText(EDITABLECOLUMN, text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, EDITABLECOLUMN);
}
});
Here tblProvision is the name of your table. you can just now edit Your table by clicking on it. I have Declare EDITABLECOLUMN. this is the column that u want to edit.
If you can use JFace as well and not just pain SWT, have a look at the JFace Snippets, especially
Snippet036FocusBorderCellHighlighter - Demonstrates keyboard navigation by highlighting the currently selected cell with a focus border showing once more the flexibility of the new cell navigation support
Snippet034CellEditorPerRowNewAPI - Demonstrates different CellEditor-Types in one COLUMN with 3.3-API of JFace-Viewers
You can get or set the value of a item, for example:
Table table = new Table(parent, SWT.NONE);
TableItem item = new TableItem(table, SWT.NONE);
item.setText("My new Text");
I suggest you to us TableViewer, it is very powerful table which it you can use databinding very easy too.