Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

JRuby/SWTSnippets/Snippet2.rb

< JRuby‎ | SWTSnippets
Revision as of 08:01, 28 July 2009 by Unnamed Poltroon (Talk) (New page: ################################################################################ # Copyright (c) 2009 IBM Corporation and others. # All rights reserved. This program and the accompanying m...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  1. Copyright (c) 2009 IBM Corporation and others.
  2. All rights reserved. This program and the accompanying materials
  3. are made available under the terms of the Eclipse Public License v1.0
  4. which accompanies this distribution, and is available at
  5. http://www.eclipse.org/legal/epl-v10.html
  6. Contributors:
  7. IBM Corporation - initial API and implementation

require 'java'

  1. Table example snippet: sort a table by column
  2. For a list of SWT example snippets ported to JRuby, see
  3. http://wiki.eclipse.org/JRuby/SWTSnippets
  4. @since 3.2

class Snippet2 include_package 'org.eclipse.swt' include_package 'org.eclipse.swt.widgets' include_package 'org.eclipse.swt.layout' include_package 'java.text' include_package 'java.util'


def Snippet2.main display = Display.new shell = Shell.new(display) shell.setLayout(FillLayout.new) table = Table.new(shell, SWT::BORDER) table.setHeaderVisible(true) column1 = TableColumn.new(table, SWT::NONE) column1.setText("Column 1") column2 = TableColumn.new(table, SWT::NONE) column2.setText("Column2 ") item = TableItem.new(table, SWT::NONE) item.setText(["a", "3"].to_java(:string)) item = TableItem.new(table, SWT::NONE) item.setText(["b", "2"].to_java(:string)) item = TableItem.new(table, SWT::NONE) item.setText(["c", "1"].to_java(:string)) column1.setWidth(100) column2.setWidth(100) doSort = Proc.new do |e| items = table.getItems collator = Collator.getInstance(Locale.getDefault) column = e.widget index = column == column1 ? 0 : 1 1.upto(items.length-1) do |i| value1 = items[i].getText(index) 0.upto(i-1) do |j| value2 = items[j].getText(index) if collator.compare(value1, value2) < 0 values = [items[i].getText(0), items[i].getText(1)] items[i].dispose item = TableItem.new(table, SWT::NONE, j) item.setText(values.to_java(:string)) items = table.getItems break end end end end column1.addListener(SWT::Selection) {|e| doSort.call(e) } column2.addListener(SWT::Selection) {|e| doSort.call(e) } table.setSortColumn(column1) table.setSortDirection(SWT::UP) shell.setSize(shell.computeSize(SWT::DEFAULT, SWT::DEFAULT).x, 300) shell.open display.sleep unless display.readAndDispatch until shell.isDisposed display.dispose end end

Snippet2.main

Back to the top