/** Creates the "Example" widgets. */ void createExampleWidgets() { /* Compute the widget style */ int style = getDefaultStyle(); if (horizontalButton.getSelection()) style |= SWT.H_SCROLL; if (verticalButton.getSelection()) style |= SWT.V_SCROLL; if (borderButton.getSelection()) style |= SWT.BORDER; if (noBackgroundButton.getSelection()) style |= SWT.NO_BACKGROUND; if (noFocusButton.getSelection()) style |= SWT.NO_FOCUS; if (noMergePaintsButton.getSelection()) style |= SWT.NO_MERGE_PAINTS; if (noRedrawResizeButton.getSelection()) style |= SWT.NO_REDRAW_RESIZE; /* Create the example widgets */ paintCount = 0; cx = 0; cy = 0; canvas = new Canvas(canvasGroup, style); canvas.addPaintListener( new PaintListener() { public void paintControl(PaintEvent e) { paintCount++; GC gc = e.gc; if (fillDamageButton.getSelection()) { Color color = e.display.getSystemColor(colors[paintCount % colors.length]); gc.setBackground(color); gc.fillRectangle(e.x, e.y, e.width, e.height); } Point size = canvas.getSize(); gc.drawArc(cx + 1, cy + 1, size.x - 2, size.y - 2, 0, 360); gc.drawRectangle(cx + (size.x - 10) / 2, cy + (size.y - 10) / 2, 10, 10); } }); canvas.addControlListener( new ControlAdapter() { public void controlResized(ControlEvent event) { Point size = canvas.getSize(); maxX = size.x * 3 / 2; maxY = size.y * 3 / 2; resizeScrollBars(); } }); ScrollBar bar = canvas.getHorizontalBar(); if (bar != null) { bar.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollHorizontal((ScrollBar) event.widget); } }); } bar = canvas.getVerticalBar(); if (bar != null) { bar.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollVertical((ScrollBar) event.widget); } }); } }
/** * Installs this highlighter into the specified StyledText object. Client can manually call * detach() method, then wants to destroy this object. */ public void attach(StyledText parent) { detach(); text = parent; text.addDisposeListener(ml); text.addLineStyleListener(ml); text.addLineBackgroundListener(ml); text.addPaintListener(ml); text.addVerifyListener(ml); text.addExtendedModifyListener(ml); text.addControlListener(ml); text.addKeyListener(ml); text.addTraverseListener(ml); text.addMouseListener(ml); text.addSelectionListener(ml); text.getContent().addTextChangeListener(ml); ScrollBar sb = text.getVerticalBar(); if (sb != null) sb.addSelectionListener(ml); updateViewport(); new Thread() { public void run() { // setPriority(Thread.NORM_PRIORITY-1); while (true) { try { sleep(300); } catch (InterruptedException e) { } if (baseEditor == null || text == null) break; if (backParserDelay) { backParserDelay = false; try { sleep(1500); } catch (InterruptedException e) { } continue; } ; Display.getDefault() .syncExec( new Runnable() { public void run() { if (baseEditor == null || text == null) return; if (text.isDisposed()) return; // System.out.println(System.currentTimeMillis()); baseEditor.idleJob(80); // redrawFrom(text.getLineAtOffset(text.getCaretOffset())); } }); } ; }; }.start(); }
/** * Removes this object from the corresponding StyledText widget. Object can't be used after this * call, until another attach. This method is called automatically, when StyledText widget is * disposed */ public void detach() { if (text == null) return; text.removeDisposeListener(ml); text.removeLineStyleListener(ml); text.removeLineBackgroundListener(ml); text.removePaintListener(ml); text.removeVerifyListener(ml); text.removeExtendedModifyListener(ml); text.removeControlListener(ml); text.removeKeyListener(ml); text.removeTraverseListener(ml); text.removeMouseListener(ml); text.removeSelectionListener(ml); ScrollBar sb = text.getVerticalBar(); if (sb != null) sb.removeSelectionListener(ml); baseEditor = null; }
void onDispose(Event event) { removeListener(SWT.Dispose, listener); notifyListeners(SWT.Dispose, event); event.type = SWT.None; table.removeListener(SWT.FocusIn, tableListener); table.removeListener(SWT.MouseDown, tableListener); unhookRowColumnListeners(); ScrollBar hBar = table.getHorizontalBar(); if (hBar != null) { hBar.removeListener(SWT.Selection, resizeListener); } ScrollBar vBar = table.getVerticalBar(); if (vBar != null) { vBar.removeListener(SWT.Selection, resizeListener); } }
/** * Scrolls the canvas vertically. * * @param scrollBar */ void scrollVertical(ScrollBar scrollBar) { Rectangle bounds = canvas.getClientArea(); int y = -scrollBar.getSelection(); if (y + maxY < bounds.height) { y = bounds.height - maxY; } canvas.scroll(cx, y, cx, cy, maxX, maxY, false); cy = y; }
/** * Scrolls the canvas horizontally. * * @param scrollBar */ void scrollHorizontal(ScrollBar scrollBar) { Rectangle bounds = canvas.getClientArea(); int x = -scrollBar.getSelection(); if (x + maxX < bounds.width) { x = bounds.width - maxX; } canvas.scroll(x, cy, cx, cy, maxX, maxY, false); cx = x; }
/** Resizes the maximum and thumb of both scrollbars. */ void resizeScrollBars() { Rectangle clientArea = canvas.getClientArea(); ScrollBar bar = canvas.getHorizontalBar(); if (bar != null) { bar.setMaximum(maxX); bar.setThumb(clientArea.width); bar.setPageIncrement(clientArea.width); } bar = canvas.getVerticalBar(); if (bar != null) { bar.setMaximum(maxY); bar.setThumb(clientArea.height); bar.setPageIncrement(clientArea.height); } }
/** * Constructs a new instance of this class given its parent table and a style value describing its * behavior and appearance. * * <p>The style value is either one of the style constants defined in class <code>SWT</code> which * is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together * (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style * constants. The class description lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * * @param parent a Table control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null * </ul> * * @exception SWTException * <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass * </ul> * * @see SWT#BORDER * @see Widget#checkSubclass() * @see Widget#getStyle() */ public TableCursor(Table parent, int style) { super(parent, style); table = parent; setBackground(null); setForeground(null); listener = event -> { switch (event.type) { case SWT.Dispose: onDispose(event); break; case SWT.FocusIn: case SWT.FocusOut: redraw(); break; case SWT.KeyDown: keyDown(event); break; case SWT.Paint: paint(event); break; case SWT.Traverse: { event.doit = true; switch (event.detail) { case SWT.TRAVERSE_ARROW_NEXT: case SWT.TRAVERSE_ARROW_PREVIOUS: case SWT.TRAVERSE_RETURN: event.doit = false; break; } break; } } }; int[] events = new int[] {SWT.Dispose, SWT.FocusIn, SWT.FocusOut, SWT.KeyDown, SWT.Paint, SWT.Traverse}; for (int i = 0; i < events.length; i++) { addListener(events[i], listener); } tableListener = event -> { switch (event.type) { case SWT.MouseDown: tableMouseDown(event); break; case SWT.FocusIn: tableFocusIn(event); break; } }; table.addListener(SWT.FocusIn, tableListener); table.addListener(SWT.MouseDown, tableListener); disposeItemListener = event -> { unhookRowColumnListeners(); row = null; column = null; _resize(); }; disposeColumnListener = event -> { unhookRowColumnListeners(); row = null; column = null; _resize(); }; resizeListener = event -> _resize(); ScrollBar hBar = table.getHorizontalBar(); if (hBar != null) { hBar.addListener(SWT.Selection, resizeListener); } ScrollBar vBar = table.getVerticalBar(); if (vBar != null) { vBar.addListener(SWT.Selection, resizeListener); } getAccessible() .addAccessibleControlListener( new AccessibleControlAdapter() { @Override public void getRole(AccessibleControlEvent e) { e.detail = ACC.ROLE_TABLECELL; } }); getAccessible() .addAccessibleListener( new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { if (row == null) return; int columnIndex = column == null ? 0 : table.indexOf(column); e.result = row.getText(columnIndex); } }); }