Pages

2009-11-04

Indeterminate progress bar as popup window

A small indeterminate progress bar. Use like this:

pleaseWait wait = new pleaseWait();
wait.start();

/* some long operation */

wait.stop();

--- cut here –--

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class pleaseWait extends Thread {

        public void run() {
                Display display = new Display();
                Shell shell = new Shell(display, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
                shell.setText("Please wait...");

                ProgressBar bar = new ProgressBar(shell, SWT.INDETERMINATE);
                bar.setBounds(1, 0, 200, 20);
                bar.setMaximum(5);
                bar.setMinimum(0);

                shell.pack();

                Monitor primary = display.getPrimaryMonitor();
                Rectangle bounds = primary.getBounds();
                Rectangle rect = shell.getBounds();

                int x = bounds.x + (bounds.width - rect.width) / 2;
                int y = bounds.y + (bounds.height - rect.height) / 2;

                shell.setLocation(x, y);
                shell.open();

                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch())
                                display.sleep();
                }
        }
}
--- cut here –--



2009-11-03

Read XML file, set a node value by XPATH and save the XML file w/ Perl


use XML::XPath;
use XML::XPath::XMLParser;


# Load XML Structure
my $xml_file_name = 'somewhere/somefile.xml';
my $xp = XML::XPath->new(filename => $xml_file_name);


# Set Value of an XPATH node
my $new_value = “My New Value”;
set_node($xp,'/SomeRoot/SomeChild/SomeTag[@symbol="SomeAttribute"]', $new_value);


# Save modified XML file
my $nodeSet = $xp->find("/");
my $xml_file_content = $nodeSet->get_node(1)->toString;


# Preserve xml entities
$ xml_file_content =~s/&#/&#/g; 


# Save file
open(XML,"> $xml_file_name") or die "ERROR: Can't open $xml_file_name \n$!";
print XML $xml_file_name;
close XML;


sub set_node {
        my $xp = shift @_;
        my $xpath=shift @_;
        my $xvalue=shift @_;


my $nodeset = $xp->find($xpath);
         foreach my $node ($nodeset->get_nodelist) {
                $xp->setNodeText($xpath,$xvalue);
         }
}


Convert plain text to HTML&XML escaped entities w/ Perl


sub xmlize {
       # unpack byte-by-byte (set to "U0U*" in case of UTF-8)

        my $format="C*"; 
           my @output;
        while($line=shift @_) {
                my @array=map {
                        if($_>127) {
                                sprintf("&#%04ld;",$_);
                        }
                        else {
                                sprintf("%s",chr($_));
                        }
                }unpack($format,$line);
                push(@output,join "", @array);
        }
        return join "",@output;
}






Sort SWT Table by a column reagardless the column count


Create a table, add columns and the following listener to each column:


private Listener TableSortListener(final Table table) {
            return new Listener() {
                public void handleEvent(Event e) {
                    TableItem[] items = table.getItems();
                    Collator collator = Collator.getInstance(Locale.getDefault());
                    TableColumn column = (TableColumn)e.widget;
                    int index = 0;
                   
                    // Get selected column
                    for(int i=0;i<table.getColumnCount();i++)
                         if(column == table.getColumn(i)) index=i;


                    // Set sort direction
                    int cmp =-1;         
                    if(table.getSortDirection() == SWT.UP) cmp = 1;
                   
                    for (int i = 1; i < items.length; i++) {
                        String value1 = items[i].getText(index);
                        for (int j = 0; j < i; j++){
                            String value2 = items[j].getText(index);
                            if (collator.compare(value1, value2) * cmp < 0) {
                                
                                 // Cache Strings
                                 String[] values = new String[table.getColumnCount()] ;
                                 for(int c=0;c<table.getColumnCount();c++) values[c] = items[i].getText(c);
        
                                // Cache Data & Images
                                String data = (String) items[i].getData();
                                org.eclipse.swt.graphics.Image image = items[i].getImage();
                               
                                // Redraw
                                items[i].dispose();
                                TableItem item = new TableItem(table, SWT.NONE, j);
                                item.setText(values);
                                item.setData(data);
                                item.setImage(image);
                               
                                items = table.getItems();
                                break;
                            }
                        }
                    }

                    table.setSortColumn(column);
                    table.setSortDirection((table.getSortDirection() == SWT.UP)? SWT.DOWN : SWT.UP);
                }
            };
        }