RCP E4 Statusbar is to small - swt

I create a statusbar for my e4 rcp application but the statusbar is to small.
In my Appliication.e4xmi is the following configured:
Trimmed Window -> TrimBars -> Window Trim (Bottom) -> Toolbar -> Tool Control (link to my StatusBar class)
Linked StatusBar class:
public class StatusBar {
private Label label;
#Inject
private IEventBroker eventBroker;
public static final String STATUSBAR = "statusbar";
#Inject
#Optional
public void getEvent(#UIEventTopic(STATUSBAR) String message) {
updateInterface(message);
}
#PostConstruct
public void createControls(Composite parent) {
label = new Label(parent, SWT.LEFT);
label.setBackground(parent.getBackground());
}
public void updateInterface(String message) {
try {
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
try {
label.setText(message);
} catch (Exception e) {
logger.error(e.fillInStackTrace());
}
}
});
} catch (Exception exception) {
logger.error(exception.fillInStackTrace());
}
}
View of the Statusbar

You don't need a Toolbar for a ToolControl. Put the ToolControl as the immediate child of the Trim Bar.
Put the label in a composite to get it layed out properly:
#PostConstruct
public void createGui(final Composite parent)
{
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new GridLayout());
Label label = new Label(body, SWT.LEFT);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
....
}
You may also want to specify a tag value of stretch in ToolControl definiton in the e4xmi to make the control use all available horizontal space in the trim bar.

Related

Toolbar Handled Item Icon vanishes if ICommandService is injected in Part

I am adding a toolbar handled item in a Part's toolbar. The part stack has a part with bundle class as DataBaseConnectPart. When I inject commandService and IHandlerService. The Button that is in Parts Toolbar vanishes. I am totally confused how to fire the command on selection of a tree item.
public class DatabaseExplorerPart {
#Inject
private IEventBroker eventBroker;
#Inject
private EPartService partService;
#Inject private ICommandService commandService;
#Inject private IHandlerService service;
#Inject private IEvaluationService evaluationService;
#PostConstruct
public void createControls(Composite parent, EMenuService menuService, EclipseContext context ) {
createImages();
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(SelectionChangedEvent arg0) {
TreeSelection ts = (TreeSelection)arg0.getSelection();
Object o = ts.getFirstElement();
if (o instanceof Query){
context.set(Constants.KEY_SELECTED_OBJECT_TYPE, "query");
context.set(Constants.KEY_SELECTED_OBJECT, o);
String name = ((Query)o).getName();
if(name.equalsIgnoreCase("New Query")){
executeCommand();
}
}
}
}
});
viewer.setLabelProvider(new ViewLabelProvider());
}
private void executeCommand(){
try {
Command theCommand = commandService.getCommand("com.db.connect.command");
theCommand.executeWithChecks(new ExecutionEvent(theCommand, new HashMap(), null, evaluationService.getCurrentState()));
} catch (Exception exception) {
logger.error(exception.getMessage(), exception);
}
}

Updating ListView data onItemClick

My code does update the ListView when onItemClick is fired but only if I replace the line "inside the onItemClick method"
categoryAdapter.notifyDataSetChanged();
with the line
lv.setAdapter(new CategoryAdapter(categories));
Should I replace the line as indicated above or the notifyDataSetChanged is what I should use? since the docs say that it should update the view as well.
besides, when I do that, the app runs but hitting the back button shuts the app down instead of go back to the last screen which what I like to see.
public class MainActivity extends ActionBarActivity {
ListView lv;
String[] categories = null;
private static String[] failCategories = {"a","b","c};
private static String[] category_a= { "a1","a2"};
CategoryAdapter categoryAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
lv = (ListView) findViewById(R.id.lvCategories);
categories = failCategories; //initial set
categoryAdapter = new CategoryAdapter(categories);
lv.setAdapter(categoryAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
categories = category_a;
categoryAdapter.notifyDataSetChanged();
}
}
});
}
private class CategoryAdapter extends ArrayAdapter<String>{
CategoryAdapter(String[] objects) {
super(getBaseContext(), R.layout.activity_categories,R.id.tvCategory, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
return convertView;
}
}
}
please try this part of code inside your code if you want to update respected item only, make sure category_a has equal items
Version 1
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
categoryAdapter.insert(position,category_a[position]);
categoryAdapter.notifyDataSetChanged();
}
Version 2
if you want to update whole listview with all items in category_a then
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
categoryAdapter.clear();
categoryAdapter.addAll(category_a);
categoryAdapter.notifyDataSetChanged();
}

No scrollbars in ScrolledComposite in RCP/RAP

I have a piece of code with a ScrolledComposite where the vertical scrollbar does not appear even if the content of the widgets is much more then the space in the widget. I was able to reproduce this behaviour in a simple example.
I need this to work as RAP application but the code also does not work if I run it in a view in RCP application.
Here is the code for a simple view (I omit the imports part):
public class View extends ViewPart {
public static final String ID = "View_spike.view";
List<String> inputData = new ArrayList<String>();
final String LONG_TEXT = "Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text.";
private TableViewer viewer;
/**
* The content provider class is responsible for providing objects to the
* view. It can wrap existing objects in adapters or simply return objects
* as-is. These objects may be sensitive to the current input of the view,
* or ignore it and always show the same content (like Task List, for
* example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if (parent instanceof Object[]) {
return (Object[]) parent;
}
return new Object[0];
}
}
class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
populateInputData();
final SashForm container = new SashForm(parent, SWT.HORIZONTAL);
final ScrolledComposite objectViewerContainer = new ScrolledComposite(
container, SWT.H_SCROLL | SWT.V_SCROLL);
objectViewerContainer.setLayout(new GridLayout());
objectViewerContainer.setExpandHorizontal(true);
objectViewerContainer.setExpandVertical(true);
objectViewerContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite someContainer = new Composite(container, SWT.NONE);
someContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setWeights(new int[] { 1, 2 });
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createObjectViewerArea(objectViewerContainer);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
private void populateInputData() {
for(int i = 0; i < 100; i++) {
inputData.add(LONG_TEXT);
}
}
private void createObjectViewerArea(final ScrolledComposite parent) {
final Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(resetMargin(new TableWrapLayout()));
createContent(panel);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
parent.setContent(panel);
}
private void createContent(final Composite parent) {
final Tree tree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setLayoutData(new TableWrapData());
TreeViewer treeViewer = new TreeViewer(tree);
treeViewer.setContentProvider(new ITreeContentProvider() {
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
viewer.refresh();
parent.layout();
}
#Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
return ((List) inputData).toArray();
}
#Override
public Object[] getChildren(Object parentElement) {
// TODO Auto-generated method stub
return null;
}
#Override
public Object getParent(Object element) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean hasChildren(Object element) {
// TODO Auto-generated method stub
return false;
}
});
treeViewer.setInput(this.inputData);
treeViewer.setAutoExpandLevel(1); // don't expand past top level
treeViewer.setLabelProvider(new ILabelProvider() {
#Override
public void removeListener(ILabelProviderListener listener) {
}
#Override
public boolean isLabelProperty(Object element, String property) {
return false;
}
#Override
public void dispose() {
}
#Override
public void addListener(ILabelProviderListener listener) {
}
#Override
public String getText(Object element) {
// TODO Auto-generated method stub
return (String) element;
}
#Override
public Image getImage(Object element) {
// TODO Auto-generated method stub
return null;
}
});
}
public TableWrapLayout resetMargin(final TableWrapLayout layout) {
layout.topMargin = 0;
layout.leftMargin = 0;
layout.bottomMargin = 1;
layout.rightMargin = 0;
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
return layout;
}
}
The result is following:
Of course there are more rows in the tree widget than what is displayed (the code adds 100 objects). I would expect scrollbars to appear (both vertical and horizontal) in the tree widget.
Any ideas what I should improve in my code?
There are two ways to use the ScrolledComposite (and none is used in your snippet). See javadoc for the ScrolledComposite http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fcustom%2FScrolledComposite.html

Prompt Text for javaFX ChoiceBox

I want to add "Please Select" as the promptText for javaFX ChoiceBox component.Can any one please help me.
(Note:- This is not the default selected value. It is the value that display before get select any thing)
Thanks
You might want to use ComboBox instead. It is similar to ChoiceBox but has a promptText property.
public class Main extends Application {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
ComboBox combo = new ComboBox();
combo.getItems().addAll("Item 1", "Item 2");
combo.setPromptText("Please Select");
vbox.getChildren().add(combo);
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

auto refresh eclipse view

i have created a sample view in eclipse using the following code.i want the view to be
automatically refereshed.the part of code in quotes "" gives refresh option but it is done
manually.can anyone help me know how it can be done automatically
public class SampleView extends ViewPart {
public static final String ID = "tab.views.SampleView";
private TableViewer viewer;
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
return new String[] { "Status of your hudson build is: " +hudson.d};
}
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().
getSharedImages().getImage(ISharedImages.IMG_OBJ_ADD);
}
}
public SampleView() {
}
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "Tab.viewer");
hookContextMenu();
}
" private void hookContextMenu() {
MenuManager menuMgr = new MenuManager("#PopupMenu");
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
Action refresh =new Action() {
public void run() {
// initialize();
viewer.refresh();
}
};
refresh.setText("Refresh");
menuMgr.add(refresh);
}"
public void setFocus() {
viewer.getControl().setFocus();
}
}
It is only possible to refresh the tree contents automatically, if you fill it using JFace Data Binding, that would not work with remote build results.
I recommend either using a model with notification support: when the model changes, its listeners are notified. Then your view could listen for these notifications and refresh itself.
If for some reason this is not possible, you have to poll your models manually. For that I recommend creating a Job that is executed in the background automatically (its last step is to reschedule itself some times later), that checks whether the model changed and refreshes the view.