Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
JRuby/SWTSnippets/Snippet2.rb
< JRuby | SWTSnippets
################################################################################ # Copyright (c) 2009 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at # http://www.eclipse.org/legal/epl-v10.html # # Contributors: # IBM Corporation - initial API and implementation ################################################################################ require 'java' # # Table example snippet: sort a table by column # # For a list of SWT example snippets ported to JRuby, see # http://wiki.eclipse.org/JRuby/SWTSnippets # # @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
Compare with the Java version.