/** 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); } }); } }
@Test public void testReadData_ScrollBarsSelectionEvent() { sc.setContent(new Composite(sc, SWT.NONE)); SelectionListener selectionListener = mock(SelectionListener.class); hScroll.addSelectionListener(selectionListener); vScroll.addSelectionListener(selectionListener); Fixture.fakeNotifyOperation(getId(hScroll), "Selection", null); Fixture.fakeNotifyOperation(getId(vScroll), "Selection", null); Fixture.readDataAndProcessAction(sc); verify(selectionListener, times(2)).widgetSelected(any(SelectionEvent.class)); }
/* Initalize the scrollbar and register listeners. */ private void initScrollBars() { ScrollBar horizontal = getHorizontalBar(); horizontal.setEnabled(false); horizontal.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollHorizontally((ScrollBar) event.widget); } }); ScrollBar vertical = getVerticalBar(); vertical.setEnabled(false); vertical.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollVertically((ScrollBar) event.widget); } }); }
private void initScrollBarListener() { ScrollBar scroll = getTable().getVerticalBar(); fScrollSelectionListener = new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { handleScrollBarSelection(); } }; scroll.addSelectionListener(fScrollSelectionListener); }
/** * 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(); }
/** This only works on linux */ private void syncTreeTableScrollBars() { final ScrollBar treeBar = treeComposite.getTree().getHorizontalBar(); final ScrollBar totalBar = totalComposite.getTable().getHorizontalBar(); treeBar.setVisible(false); treeBar.addSelectionListener( new SelectionListener() { @Override public void widgetDefaultSelected(SelectionEvent e) { scrollTable(); } @Override public void widgetSelected(SelectionEvent e) { scrollTable(); } private void scrollTable() { int selection = treeBar.getSelection(); totalBar.setSelection(selection); } }); totalBar.addSelectionListener( new SelectionListener() { @Override public void widgetDefaultSelected(SelectionEvent e) { scrollTree(); } @Override public void widgetSelected(SelectionEvent e) { scrollTree(); } private void scrollTree() { int selection = totalBar.getSelection(); treeBar.setSelection(selection); } }); }
@Test public void testRenderAddScrollBarsSelectionListener_Vertical() throws Exception { Fixture.markInitialized(display); Fixture.markInitialized(sc); Fixture.markInitialized(vScroll); Fixture.preserveWidgets(); vScroll.addSelectionListener(new SelectionAdapter() {}); lca.renderChanges(sc); TestMessage message = Fixture.getProtocolMessage(); assertEquals(JsonValue.TRUE, message.findListenProperty(vScroll, "Selection")); }
@Test public void testRenderScrollBarsSelectionListenerUnchanged_Vertical() throws Exception { Fixture.markInitialized(display); Fixture.markInitialized(sc); Fixture.markInitialized(vScroll); Fixture.preserveWidgets(); vScroll.addSelectionListener(new SelectionAdapter() {}); Fixture.preserveWidgets(); lca.renderChanges(sc); TestMessage message = Fixture.getProtocolMessage(); assertNull(message.findListenOperation(vScroll, "Selection")); }
/** * Initialize the scroll bars This include: a) setting the initial enabled state b) adding the * necessary listeners */ private void initScrollBars() { ScrollBar horizontal = getHorizontalBar(); horizontal.setEnabled(false); horizontal.addSelectionListener( new SelectionAdapter() { /** @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent) */ @Override public void widgetSelected(SelectionEvent event) { // Updates the translation displayRectangle.x = ((ScrollBar) event.widget).getSelection(); // Update the UI layout(); redraw(); } }); ScrollBar vertical = getVerticalBar(); vertical.setEnabled(false); vertical.addSelectionListener( new SelectionAdapter() { /** @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent) */ @Override public void widgetSelected(SelectionEvent event) { // Updates the translation displayRectangle.y = ((ScrollBar) event.widget).getSelection(); // Update the UI layout(); redraw(); } }); debug("Initialized scroll bars"); }
@Test public void testRenderRemoveScrollBarsSelectionListener_Horizontal() throws Exception { SelectionListener listener = new SelectionAdapter() {}; hScroll.addSelectionListener(listener); Fixture.markInitialized(display); Fixture.markInitialized(sc); Fixture.markInitialized(hScroll); Fixture.preserveWidgets(); hScroll.removeSelectionListener(listener); lca.renderChanges(sc); TestMessage message = Fixture.getProtocolMessage(); assertEquals(JsonValue.FALSE, message.findListenProperty(hScroll, "Selection")); }
/** * view command * * @param shell shell * @param repositoryTab repository tab * @param fileData file to view * @param revision revision to view */ CommandView( final Shell shell, final RepositoryTab repositoryTab, final FileData fileData, final String revision) { Composite composite, subComposite; Label label; Button button; Listener listener; // initialize variables this.repositoryTab = repositoryTab; this.fileData = fileData; // get display display = shell.getDisplay(); clipboard = new Clipboard(display); // add files dialog dialog = Dialogs.open(shell, "View: " + fileData.getFileName(), new double[] {1.0, 0.0}, 1.0); composite = Widgets.newComposite(dialog); composite.setLayout(new TableLayout(new double[] {0.0, 1.0, 0.0}, 1.0, 4)); Widgets.layout(composite, 0, 0, TableLayoutData.NSWE, 0, 0, 4); { subComposite = Widgets.newComposite(composite); subComposite.setLayout(new TableLayout(1.0, new double[] {0.0, 1.0})); Widgets.layout(subComposite, 0, 0, TableLayoutData.WE); { label = Widgets.newLabel(subComposite, "Revision:"); Widgets.layout(label, 0, 0, TableLayoutData.W); widgetRevision = Widgets.newSelect(subComposite); widgetRevision.setEnabled(false); Widgets.layout(widgetRevision, 0, 1, TableLayoutData.WE); Widgets.addModifyListener( new WidgetListener(widgetRevision, data) { public void modified(Control control) { Widgets.setEnabled(control, (data.revisionNames != null)); } }); widgetRevision.setToolTipText("Revision to view."); widgetRevisionPrev = Widgets.newButton(subComposite, Onzen.IMAGE_ARROW_LEFT); widgetRevisionPrev.setEnabled(false); Widgets.layout(widgetRevisionPrev, 0, 2, TableLayoutData.NSW); Widgets.addModifyListener( new WidgetListener(widgetRevisionPrev, data) { public void modified(Control control) { Widgets.setEnabled( control, (data.revisionNames != null) && (widgetRevision.getSelectionIndex() > 0)); } }); widgetRevisionPrev.setToolTipText("Show previous revision."); widgetRevisionNext = Widgets.newButton(subComposite, Onzen.IMAGE_ARROW_RIGHT); widgetRevisionNext.setEnabled(false); Widgets.layout(widgetRevisionNext, 0, 3, TableLayoutData.NSW); Widgets.addModifyListener( new WidgetListener(widgetRevisionNext, data) { public void modified(Control control) { Widgets.setEnabled( control, (data.revisionNames != null) && (widgetRevision.getSelectionIndex() < data.revisionNames.length - 1)); } }); widgetRevisionNext.setToolTipText("Show next revision."); } subComposite = Widgets.newComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL); subComposite.setLayout(new TableLayout(1.0, new double[] {0.0, 1.0})); Widgets.layout(subComposite, 1, 0, TableLayoutData.NSWE); { widgetLineNumbers = Widgets.newTextView(subComposite, SWT.RIGHT | SWT.BORDER | SWT.MULTI); widgetLineNumbers.setForeground(Onzen.COLOR_GRAY); Widgets.layout(widgetLineNumbers, 0, 0, TableLayoutData.NS, 0, 0, 0, 0, 60, SWT.DEFAULT); Widgets.addModifyListener( new WidgetListener(widgetLineNumbers, data) { public void modified(Control control) { control.setForeground((data.lines != null) ? null : Onzen.COLOR_GRAY); } }); widgetText = Widgets.newTextView(subComposite, SWT.LEFT | SWT.BORDER | SWT.MULTI); widgetText.setForeground(Onzen.COLOR_GRAY); Widgets.layout(widgetText, 0, 1, TableLayoutData.NSWE); Widgets.addModifyListener( new WidgetListener(widgetText, data) { public void modified(Control control) { control.setForeground((data.lines != null) ? null : Onzen.COLOR_GRAY); } }); } widgetHorizontalScrollBar = subComposite.getHorizontalBar(); widgetVerticalScrollBar = subComposite.getVerticalBar(); subComposite = Widgets.newComposite(composite); subComposite.setLayout(new TableLayout(1.0, new double[] {0.0, 1.0})); Widgets.layout(subComposite, 2, 0, TableLayoutData.NSWE); { label = Widgets.newLabel(subComposite, "Find:", SWT.NONE, Settings.keyFind); Widgets.layout(label, 0, 0, TableLayoutData.W); widgetFind = Widgets.newText(subComposite, SWT.SEARCH | SWT.ICON_CANCEL); widgetFind.setMessage("Enter text to find"); Widgets.layout(widgetFind, 0, 1, TableLayoutData.WE); widgetFindPrev = Widgets.newButton(subComposite, Onzen.IMAGE_ARROW_UP); widgetFindPrev.setEnabled(false); Widgets.layout(widgetFindPrev, 0, 2, TableLayoutData.NSW); Widgets.addModifyListener( new WidgetListener(widgetFindPrev, data) { public void modified(Control control) { Widgets.setEnabled(control, (data.lines != null)); } }); widgetFindPrev.setToolTipText( "Find previous occurrence of text [" + Widgets.acceleratorToText(Settings.keyFindPrev) + "]."); widgetFindNext = Widgets.newButton(subComposite, Onzen.IMAGE_ARROW_DOWN); widgetFindNext.setEnabled(false); Widgets.layout(widgetFindNext, 0, 3, TableLayoutData.NSW); Widgets.addModifyListener( new WidgetListener(widgetFindNext, data) { public void modified(Control control) { Widgets.setEnabled(control, (data.lines != null)); } }); widgetFindNext.setToolTipText( "Find next occurrence of text [" + Widgets.acceleratorToText(Settings.keyFindNext) + "]."); } } // buttons composite = Widgets.newComposite(dialog); composite.setLayout(new TableLayout(0.0, 1.0)); Widgets.layout(composite, 1, 0, TableLayoutData.WE, 0, 0, 4); { button = Widgets.newButton(composite, "Save as..."); Widgets.layout( button, 0, 0, TableLayoutData.W, 0, 0, 0, 0, SWT.DEFAULT, SWT.DEFAULT, 70, SWT.DEFAULT); button.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { // get file name String fileName = Dialogs.fileSave(dialog, "Save file", "", new String[] {"*"}); if (fileName == null) { return; } // check if file exists: overwrite or append File file = new File(fileName); if (file.exists()) { switch (Dialogs.select( dialog, "Confirmation", String.format("File '%s' already exists.", fileName), new String[] {"Overwrite", "Append", "Cancel"}, 2)) { case 0: if (!file.delete()) { Dialogs.error(dialog, "Cannot delete file!"); return; } case 1: break; case 2: return; } } PrintWriter output = null; try { // open file output = new PrintWriter(new FileWriter(file, true)); // write/append file for (String line : data.lines) { output.println(line); } // close file output.close(); } catch (IOException exception) { Dialogs.error( dialog, "Cannot write file '" + file.getName() + "' (error: " + exception.getMessage()); return; } finally { if (output != null) output.close(); } } }); widgetClose = Widgets.newButton(composite, "Close"); Widgets.layout( widgetClose, 0, 1, TableLayoutData.E, 0, 0, 0, 0, SWT.DEFAULT, SWT.DEFAULT, 70, SWT.DEFAULT); widgetClose.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { Settings.geometryView = dialog.getSize(); Dialogs.close(dialog, false); } }); } // listeners widgetRevision.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { Combo widget = (Combo) selectionEvent.widget; int index = widget.getSelectionIndex(); if ((data.revisionNames != null) && (index >= 0) && (index < data.revisionNames.length)) { show(data.revisionNames[index]); } } }); widgetRevisionPrev.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { int index = widgetRevision.getSelectionIndex(); if (index > 0) { show(data.revisionNames[index - 1]); } } }); widgetRevisionNext.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { int index = widgetRevision.getSelectionIndex(); if ((data.revisionNames != null) && (index < data.revisionNames.length - 1)) { show(data.revisionNames[index + 1]); } } }); listener = new Listener() { public void handleEvent(Event event) { StyledText widget = (StyledText) event.widget; int topIndex = widget.getTopIndex(); // Dprintf.dprintf("%d %d",widget.getTopPixel(),widgetText.getTopPixel()); widgetText.setTopIndex(topIndex); widgetText.setCaretOffset(widgetText.getOffsetAtLine(topIndex)); } }; widgetLineNumbers.addListener(SWT.KeyDown, listener); widgetLineNumbers.addListener(SWT.KeyUp, listener); widgetLineNumbers.addListener(SWT.MouseDown, listener); widgetLineNumbers.addListener(SWT.MouseUp, listener); widgetLineNumbers.addListener(SWT.MouseMove, listener); widgetLineNumbers.addListener(SWT.Resize, listener); listener = new Listener() { public void handleEvent(Event event) { StyledText widget = (StyledText) event.widget; int topIndex = widget.getTopIndex(); // Dprintf.dprintf("widget=%s: %d",widget,widget.getTopIndex()); widgetLineNumbers.setTopIndex(topIndex); widgetVerticalScrollBar.setSelection(topIndex); } }; widgetText.addListener(SWT.KeyDown, listener); widgetText.addListener(SWT.KeyUp, listener); widgetText.addListener(SWT.MouseDown, listener); widgetText.addListener(SWT.MouseUp, listener); widgetText.addListener(SWT.MouseMove, listener); widgetText.addListener(SWT.Resize, listener); widgetText.addLineStyleListener( new LineStyleListener() { public void lineGetStyle(LineStyleEvent lineStyleEvent) { // Dprintf.dprintf("x %d %s",lineStyleEvent.lineOffset,lineStyleEvent.lineText); String findText = widgetFind.getText().toLowerCase(); int findTextLength = findText.length(); if (findTextLength > 0) { ArrayList<StyleRange> styleRangeList = new ArrayList<StyleRange>(); int index = 0; while ((index = lineStyleEvent.lineText.toLowerCase().indexOf(findText, index)) >= 0) { styleRangeList.add( new StyleRange( lineStyleEvent.lineOffset + index, findTextLength, COLOR_VIEW_SEARCH_TEXT, COLOR_VIEW_SEARCH_BACKGROUND)); index += findTextLength; } lineStyleEvent.styles = styleRangeList.toArray(new StyleRange[styleRangeList.size()]); // Dprintf.dprintf("lineStyleEvent.styles=%d",lineStyleEvent.styles.length); } else { lineStyleEvent.styles = null; } } }); widgetText.addKeyListener( new KeyListener() { public void keyPressed(KeyEvent keyEvent) { if (Widgets.isAccelerator(keyEvent, SWT.CTRL + 'c')) { Widgets.setClipboard(clipboard, widgetText.getSelectionText()); } } public void keyReleased(KeyEvent keyEvent) {} }); widgetHorizontalScrollBar.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { ScrollBar widget = (ScrollBar) selectionEvent.widget; int index = widget.getSelection(); // sync text widget widgetText.setHorizontalIndex(index); } }); widgetVerticalScrollBar.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { ScrollBar widget = (ScrollBar) selectionEvent.widget; int index = widget.getSelection(); // Dprintf.dprintf("widget=%s: %d %d // %d",widget,widget.getSelection(),widget.getMinimum(),widget.getMaximum()); // sync number text widget, text widget widgetLineNumbers.setTopIndex(index); widgetText.setTopIndex(index); } }); widgetFind.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) { findNext(widgetText, widgetFind); } public void widgetSelected(SelectionEvent selectionEvent) {} }); widgetFind.addKeyListener( new KeyListener() { public void keyPressed(KeyEvent keyEvent) {} public void keyReleased(KeyEvent keyEvent) { updateViewFindText(widgetText, widgetFind); } }); widgetFindPrev.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { findPrev(widgetText, widgetFind); } }); widgetFindNext.addSelectionListener( new SelectionListener() { public void widgetDefaultSelected(SelectionEvent selectionEvent) {} public void widgetSelected(SelectionEvent selectionEvent) { findNext(widgetText, widgetFind); } }); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { if (Widgets.isAccelerator(keyEvent, Settings.keyFind)) { widgetFind.forceFocus(); } else if (Widgets.isAccelerator(keyEvent, Settings.keyFindPrev)) { Widgets.invoke(widgetFindPrev); } else if (Widgets.isAccelerator(keyEvent, Settings.keyFindNext)) { Widgets.invoke(widgetFindNext); } } public void keyReleased(KeyEvent keyEvent) {} }; widgetText.addKeyListener(keyListener); widgetFind.addKeyListener(keyListener); widgetFindPrev.addKeyListener(keyListener); widgetFindNext.addKeyListener(keyListener); dialog.addListener( USER_EVENT_NEW_REVISION, new Listener() { public void handleEvent(Event event) {} }); // show dialog Dialogs.show(dialog, Settings.geometryView, Settings.setWindowLocation); // start show file show(revision); // start add revisions (only if single file is seleccted) Background.run( new BackgroundRunnable(fileData, revision) { public void run(FileData fileData, final String revision) { // get revisions repositoryTab.setStatusText("Get revisions for '%s'...", fileData.getFileName()); try { data.revisionNames = repositoryTab.repository.getRevisionNames(fileData); } catch (RepositoryException exception) { final String exceptionMessage = exception.getMessage(); display.syncExec( new Runnable() { public void run() { Dialogs.error( dialog, String.format("Getting revisions fail: %s", exceptionMessage)); } }); return; } finally { repositoryTab.clearStatusText(); } if (data.revisionNames.length > 0) { // add revisions if (!dialog.isDisposed()) { display.syncExec( new Runnable() { public void run() { if (!widgetRevision.isDisposed()) { int selectIndex = -1; for (int z = 0; z < data.revisionNames.length; z++) { widgetRevision.add(data.revisionNames[z]); if ((revision != null) && revision.equals(data.revisionNames[z])) { selectIndex = z; } } widgetRevision.add(repositoryTab.repository.getLastRevision()); if ((revision != null) && revision.equals(repositoryTab.repository.getLastRevision())) selectIndex = data.revisionNames.length; if (selectIndex == -1) selectIndex = data.revisionNames.length; widgetRevision.select(selectIndex); } // notify modification Widgets.modified(data); } }); } } } }); }