wtorek, 22 marca 2011

Breadcrumbs composite with SWT (Eclipse Forms)

Breadcrumbs panel is very popular navigation widget. Especially useful when comes to navigation in big hierarchical models where one need a way to quickly move to the parent elements.


At the picture you can see breadcrumbs in action.

Implementation details


Two providers needs to be delivered to BreadcrumbsPanel instance: IBreadcrumbsItemProvider and IBreadcrumbsLinkActionProvider.

public interface IBreadcrumbsItemProvider{
  public List<IBreadcrumbsItem> getBreadcrumbs();
 }

 public interface IBreadcrumbsLinkActionProvider{
  public void run(IBreadcrumbsItem item);
 }

 public interface IBreadcrumbsItem {
  public abstract Object getObject();
  public abstract String getLabel();
 }

Instances of both providers needs to be delivered to BreadcrumbsPanel constructor.

public BreadcrumbsPanel(
             FormToolkit toolkit, // only when composite is used with Eclipse Forms
             Composite parent, 
             IBreadcrumbsItemProvider itemProvider, 
             final IBreadcrumbsLinkActionProvider actionProvider) {
  super(parent, SWT.NONE);
  this.toolkit = toolkit;
  this.itemProvider = itemProvider;
  this.actionProvider = actionProvider;
  
  this.setLayout(new RowLayout());
  updateLinks();
 }

UpdateLinks method is particularly important piece of code here. It is responsible for generating actual list of hyperlinks. This method make use of both providers. For each element returned by item provider creates a hyperlink control which on click will performed an action delivered with action provider.

private void updateLinks() {
  
  for (Control c :this.getChildren()){
   if (c instanceof Hyperlink || c instanceof Label){
    c.dispose();
   }
  }
  
  links = new ArrayList<Hyperlink>();
  
  final List<IBreadcrumbsItem> items = itemProvider.getBreadcrumbs();
  
  for (int i = 0;i < items.size(); i++) { 
   String label = items.get(i).getLabel();
   if (1+i < items.size()){
    Hyperlink link = toolkit.createHyperlink(this, label, SWT.None);
    link.setHref(items.get(i));
    
    toolkit.createLabel(this, "/");
    
    link.addHyperlinkListener(new HyperlinkAdapter(){
     @Override
     public void linkActivated(HyperlinkEvent e) {
      if (e.getHref() instanceof IBreadcrumbsItem){
       actionProvider.run((IBreadcrumbsItem)e.getHref());
      }
     }
    });
    links.add(link);
   } else {
    toolkit.createLabel(this, label);
   }
  }
 }

Note that in case of pure SWT you may use raw Link and Label controls.

piątek, 18 lutego 2011

Ubuntu: apt-get - how to install already installed package

Let's imagine a case where one has been removed by chance some files comes with one of deb packages installed with apt-get utility.

For sake of this example lets remove /usr/lib/php5/20060613/pdo_mysql.so file.

Now after Apache restart the following entry will appear in /var/log/apache2/error.log
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20060613/pdo_mysql.so' 
- /usr/lib/php5/20060613/pdo_mysql.so: cannot open shared object file: 
No such file or directory in Unknown on line 0

In this particular case is quite easy to guess which package pdo_mysql.so file comes with. But in general dpkg --search can help.

$ dpkg --search /usr/lib/php5/20060613/pdo_mysql.so
php5-mysql: /usr/lib/php5/20060613/pdo_mysql.so

Now we know for sure which packed needs to be installed again. To ask apt-get to install package which already is installed --reinstall option needs to be used.

sudo apt-get --reinstall install php5-mysql

Now pdo_mysql.so should be again on its place.

czwartek, 10 lutego 2011

Linux - "find" command by examples

  • Remove all subversion metadata (all directories with .svn name)
    find . -type d -name ".svn" | xargs rm -rf
    
  • Remove all file begining with two digits number followed by underscore character
    find . -name "[0-9][0-9]_*" | xargs rm -rf
    
  • Find all file which where modified within last 24 hours
    find . -mtime 0
    

piątek, 14 stycznia 2011

Eclipse PDT most useful shortcuts

Today I would like to share and keep for the future couple of tricks I found out today after years of working with Eclipse (shame - I know ;).

Incremental search (Ctrl+J)


First of all if you miss "/" command of your favourite vi editor the Ctrl+J is what you need. Once you type pharse you looking for (look at status bar - red color surprisingly means "sorry no match"). But if you are lucky "Up/Down" key buttons allow you navigate back and forth among all found matches (b/n for vi masters ;>).

Open Type (Ctrl+Shift+T)


Immortal Ctrl+Shift+T works as well allows you open any Type available in workspace.

Quick Outline (Ctrl+O)


Ctrl+O invoke nice yellow frame with outline view. Local search ability and "Up/Down" keys allow you handy navigation.

piątek, 7 stycznia 2011

Bash - how to do string split

STR=A.B.C && STR2=${STR%.*} && echo ${STR2%.*} && echo ${STR2#*.} && STR4=${STR#*.} && echo ${STR4#*.}

DELIMITER='.' && STR=A.B.C && echo ${STR%${DELIMITER}*}

for i in $(echo A.B.C | sed -e 's/\./\n/g'); do echo $i; done