Posts

Showing posts with the label SWT

Find out a monitor’s size / resolution in Eclipse SWT

For Single Monitor : @Inject @Optional public void receiveActiveShell(@Named(IServiceConstants.ACTIVE_SHELL) final Shell shell) { if (shell != null) { final Display display = shell.getDisplay(); final Rectangle screenSize = display.getBounds(); final Point size = shell.getSize(); shell.setLocation((int) (screenSize.width - size.x) / 2, (int) (screenSize.height - size.y) / 2); } } For Multi Monitor : Monitor[] monitors = getShell().getDisplay().getMonitors(); Monitor activeMonitor = null; Rectangle rect = shell.getBounds(); for (int i = 0; i < monitors.length; i++) { if (monitors[i].getBounds().intersects(rect)) { activeMonitor = monitors[i]; } } System.out.println("The resolution of the active monitor is " + activeMonitor.getBounds().width + " x " + activeMonitor.getBounds().height);

How to remove QuickAccess field from your Eclipse application

Image
Sometimes we need to remove the QuickAccess widget from Eclipse RCP application. Even after following below steps Ctrl + 3 will work. Add these dependency in your eclipse rcp plugin org.eclipse.e4.ui.model.workbench org.eclipse.e4.ui.workbench org.eclipse.e4.core.contexts In ApplicationWorkbenchWindowAdvisor, in postWindowOpen add these statements at end IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window instanceof WorkbenchWindow) { MWindow mWindow = ((WorkbenchWindow) window).getModel(); EModelService modelService = mWindow.getContext().get(EModelService.class); MToolControl searchField = (MToolControl)modelService.find("SearchField", mWindow); if (searchField != null) { searchField.setToBeRendered(false); } }

How to add Xtext DSL editor in Multi-Page Editor

To add your Xtext Dsl Editor to Multi-Page Editor Getting the instance of XText editor MyDslActivator activator = MyDslActivator.getInstance(); final Injector injector = activator.getInjector("org.shashwat.xtext.mydsl.MyDsl"); XtextEditor xtextEditor = injector.getInstance(XtextEditor.class); Remember to have editor input instance of IFileEditorInput. XtextEditor uses XtextDocumentProvider which extends FileDocumentProvider FileEditorInput fileEditorInput = new FileEditorInput(file); // Here file is instance of IFile int index = addPage(xtextEditor, fileEditorInput);

Getting Started with NatTable

Image
Sample code to create Nat table with dummy data private NatTable createNatTable(Composite parent) { DummyGridLayerStack dummyLayer = new DummyGridLayerStack(4, 10); DataLayer rowHeader = (DataLayer) dummyLayer .getRowHeaderDataLayer(); rowHeader.setColumnWidthByPosition(0, 20); return new NatTable(parent, dummyLayer ); } Output :

Implement Auto-Hide Scrollbar in SWT Text Component

Text textControl = new Text(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); Listener listener = new Listener (){ @Override public void handleEvent(Event event) { Text t = (Text)event.widget; Rectangle r1 = t.getClientArea(); Rectangle r2 = t.computeTrim(r1.x, r1.y, r1.width, r1.height); Point p = t.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); t.getHorizontalBar().setVisible(r2.width <= p.x); t.getVerticalBar().setVisible(r2.height <= p.y); if (event.type == SWT.Modify){ t.getParent().layout(true); t.showSelection(); } }}; textControl.addListener(SWT.Resize, listener); textControl.addListener(SWT.Modify, listener);

Hiding SWT Controls

There are many ways to hide SWT widgets. First one comes to mind is setVisible method of control. But the disadvantage of it is, hidden widget will not released and can't be used by other widgets. Another approach is. You can use layout data. In case of GridLayout, you can use exclude specific widget from being drown on canvas. Eg, Composite comp = new Composite(shell, SWT.NONE); comp.setLayout(new GridLayout(4, false)); Label hidenLabel = new Label (comp, SWT.NONE); GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); hidenLabel.setGridData(gridData ); //hide the button gridData .exclude = true; comp.pack();