Archive

Archive for February, 2015

NetBeans Days in Athens 17-18 April 2015

February 17, 2015 Leave a comment

The NetBeans Community invites you to a 2-days free event regarding the NetBeans IDE and Rich Client Platform. The event will be hosted in Athens from Friday, 17 to Saturday, 18 April 2015.

You will have the chance to learn about the latest tips and tricks of the NetBeans IDE and the NetBeans platform by experts on the subject.

The free event lasts from 9:00 to 16:00 on Friday and from 9:00 to 12:30 on Saturday. This is an amazing opportunity to combine your passion with holidays and taste the amazing Hellenic cuisine.

Coffee will be provided and we look forward to a fun-filled and active day, at the end of which you will have gained many new insights, while extending your network.

The event will take place at a great venue, the Aquis Mare Nostrum Hotel Thalasso outside Athens just 20′ from Athens airport and by the Aegean sea.

Prices (VAT inclusive) if you choose to stay at the hotel:

– Rich breakfast buffet
– Rich lunch buffet and the following drinks in limitless consumption during dinner: white / pink / red wine, draft beer, juices, soft drinks

– Single main building room with mountain views: 75 EUR per room per night in Half pension
– Double main building room with mountain views: 85 EUR per room per night in Half pension
– Double main building room or bungalow with sea view: 105 euros per room per night half pension
– Triple room in bungalow with sea view: 135 euros per room per night half pension

Lunch buffet fee: 15 euros extra per person per meal, suppl. unlimited consumption of white / pink / red wine, draft beer, juices, soft drinks.

The event is sponsored by the Anderson Software Group, which provides training in Java and JavaFX, C++, Python and Perl, OOD and UML, Linux, and C#. The organizers would like to thank the Anderson Software Group for their support of this event!

Please register here.

Aquis Mare Nostrum Hotel Thalasso
Leoforos Vravronos 30
Artemis 190 16, Greece
lat, lon: 37.930312, 24.006113 (37° 55′ 49”, 24° 0′ 22′)

Categories: Conference

Recipe 15: How to add a toggle action

February 2, 2015 Leave a comment

Problem: How can I add a toggle button as an action? I tried to add a JToggleButton but it doesn’t work.

Solution: JToggleButton doesn’t really work with the NetBeans toolbar, instead use BooleanStateAction. Here are the steps to follow:

  1. Create a new Action, e.g.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Options",
id = "todo.controller.options.QuickFilterAction")
@ActionRegistration(iconBase = "todo/controller/options/find16.png",
displayName = "#CTL_QuickFilterAction")
@ActionReferences({
    @ActionReference(path = "Menu/Options", position = 50),
    @ActionReference(path = "Toolbars/Options", position = 50),
    @ActionReference(path = "Shortcuts", name = "F3")
})
@Messages("CTL_QuickFilterAction=Quick Filter")
public final class QuickFilterAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        // TODO implement action body
    }
}

2.  Extend BooleanStateAction and since it implements ActionListener this can be deleted from our action:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Options",
id = "todo.controller.options.QuickFilterAction")
@ActionRegistration(iconBase = "todo/controller/options/find16.png",
displayName = "#CTL_QuickFilterAction")
@ActionReferences({
    @ActionReference(path = "Menu/Options", position = 50),
    @ActionReference(path = "Toolbars/Options", position = 50),
    @ActionReference(path = "Shortcuts", name = "F3")
})
@Messages("CTL_QuickFilterAction=Quick Filter")
public final class QuickFilterAction extends BooleanStateAction {

 @Override
 protected void initialize() {
   super.initialize();
   setBooleanState(Settings.isSearchVisible());
 }

 @Override
 public void actionPerformed(ActionEvent e) {
   super.actionPerformed(e);
   TasksTopComponent tasksTopComponent = (TasksTopComponent) WindowManager.getDefault().findTopComponent("TasksTopComponent");
   tasksTopComponent.setSearchVisible(getBooleanState());
 }

 @Override
 public String getName() {
   return "Quick Filter";
 }

 @Override
 public HelpCtx getHelpCtx() {
   return HelpCtx.DEFAULT_HELP;
 }

 @Override
 protected String iconResource() {
   return "todo/controller/options/find16.png";
 }
}

Since, due to lazy initialization, it doesn’t recognize the values passed in the annotations, we need to override the iconResource() method. Because, by default, it is selected, we need to set it to unselected in the initialize() method.

Categories: Action, Filter, Quick Search

Recipe 14: How to add a Quick Filter

February 1, 2015 Leave a comment

Problem: How can I display a Quick Filter button with a Quick Filter text field to filter my OutlineView?

Solution:  In Recipe 9 we showed how to create a QuickFilter in order to filter an OutlineView. NetBeans RCP allows you to create a graphical filter text field in order to filter your OutlineView dynamically based on what you are typing (display the Notifications window by clicking on menu Window | IDE Tools | Notifications for an example — you need to have NetBeans version 7.3 or later).

notifications

In the class that contains your TopComponent or your panel you need to define the following:

/** Quick Search filter */
private final QuickSearch quickSearch;
/** Callback */
private final QuickSearch.Callback quickFilterCallback = new QuickFilterCallback();

and in the constructor (you need to create an empty JPanel — here called pnlQuickSearchFilter — that will host the quick filter text box):

GridBagConstraints searchConstrains = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
quickSearch = QuickSearch.attach(pnlQuickSearchFilter, searchConstrains, quickFilterCallback, true);
FilterRepository.getInstance().addPropertyChangeListener(pcl);
setSearchVisible(Settings.isSearchVisible());

QuickFilterCallback is called when the user types something in the quick filter text field:

private class QuickFilterCallback implements QuickSearch.Callback {

    @Override
    public void quickSearchUpdate(String searchText) {
        FilterRepository.getInstance().setQuickSearchFilter(searchText);
        if (quickSearch != null && !quickSearch.isAlwaysShown()) {
            setSearchVisible(true);
        }
    }

    @Override
    public void showNextSelection(boolean forward) {
    }

    @Override
    public String findMaxPrefix(String prefix) {
        return prefix;
    }

    @Override
    public void quickSearchConfirmed() {
    }

    @Override
    public void quickSearchCanceled() {
        FilterRepository.getInstance().setQuickSearchFilter(null);
    }
}

FilterRepository handles the quick filter:

package org.netbeans.filters.filter.checklist;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public final class FilterRepository {

    /** Property when a new quick filter is applied. */
    public static final String PROP_QUICK_FILTER = "QUICK-FILTER";
    /** Observable. */
    private PropertyChangeSupport pcs = null;
    /** A quick search filter. */
    private String quickSearchFilter;

    /** Constructor, default. */
    private FilterRepository() {
    }

    /**
     * Singleton.
     *
     * @return the single instance of this class.
     */
    public static FilterRepository getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /** SingletonHolder. */
    private static final class SingletonHolder {

        /** The only INSTANCE. */
        private static final FilterRepository INSTANCE = new FilterRepository();

        /** Private. */
        private SingletonHolder() {
        }
    }

    /**
     * @return a thread-safe PropertyChangeSupport
     */
    private PropertyChangeSupport getPropertyChangeSupport() {
        if (pcs == null) {
            pcs = new PropertyChangeSupport(this);
        }
        return pcs;
    }

    public void addPropertyChangeListener(PropertyChangeListener pcl) {
        getPropertyChangeSupport().addPropertyChangeListener(pcl);
    }

    public void removePropertyChangeListener(PropertyChangeListener pcl) {
        getPropertyChangeSupport().removePropertyChangeListener(pcl);
    }

    public void addPropertyChangeListener(String name, PropertyChangeListener pcl) {
        getPropertyChangeSupport().addPropertyChangeListener(name, pcl);
    }

    public void removePropertyChangeListener(String name, PropertyChangeListener pcl) {
        getPropertyChangeSupport().removePropertyChangeListener(name, pcl);
    }

    /**
     * Quick search filter is used by bottom panel.
     *
     * @param searchText
     * text to filter
     */
    public void setQuickSearchFilter(String searchText) {
        String oldFilter = quickSearchFilter;
        quickSearchFilter = (searchText == null || searchText.isEmpty()) ? null : searchText;
        getPropertyChangeSupport().firePropertyChange(PROP_QUICK_FILTER, oldFilter, quickSearchFilter);
    }

    public boolean isQuickSearchFilter() {
        return quickSearchFilter != null;
    }

    public String getQuickSearchFilter() {
        return quickSearchFilter;
    }
}

Settings class (copied from NetBeans source code):

import java.util.prefs.Preferences;
import org.openide.util.NbPreferences;

/**
 * @author jpeska
 */
public class Settings {

   private static final String SEARCH_VISIBLE = "searchVisible"; //NOI18N
   private static final boolean DEFAULT_SEARCH_VISIBLE = true;

   private Settings() {
   }

   public static boolean isSearchVisible() {
      return getPreferences().getBoolean(SEARCH_VISIBLE, DEFAULT_SEARCH_VISIBLE);
   }

   public static void setSearchVisible(boolean searchVisible) {
      getPreferences().putBoolean(SEARCH_VISIBLE, searchVisible);
   }

   private static Preferences getPreferences() {
      return NbPreferences.forModule(Settings.class);
   }
}

setSearchVisible() method does the actual filtering of the OutlineView using the setQuickFilter() and unsetQuickFilter() methods explained in Recipe 9.

    /**
     * @param visible if {@code true} then display the quick search panel
     */
    public void setSearchVisible(boolean visible) {
        quickSearch.setAlwaysShown(visible);
        if (!visible) {
            unsetQuickFilter();
        } else if (FilterRepository.getInstance().isQuickSearchFilter()) {
            setQuickFilter(0, getQuickFilter(FilterRepository.getInstance().getQuickSearchFilter()));
        }
        Settings.setSearchVisible(visible);
    }

    /**
     * Create a QuickFilter from the given String.
     *
     * @param qsFilter filter as String
     * @return a QuickFilter from the given String
     */
    private QuickFilter getQuickFilter(String qsFilter) {
        return (Object aValue) -> {
            if (aValue instanceof TaskNode) {
                Task task = ((TaskNode) aValue).getLookup().lookup(Task.class);
                return task.getDescription().contains(qsFilter)
                        || String.valueOf(task.getPriority()).contains(qsFilter)
                        || task.getDueDate().toString().contains(qsFilter);
            } else {
                return false;
            }
        };
    }

In the above example we show how to filter an OutlineView that contains TaskNodes. The filter is trying to match the Task‘s description, priority or due date.

The last remaining change is the property change listener which listens to changes in FilterRepository.

    /** Listen to changes in the FilterRepository and filter the outline view
     * accordingly. */
    private final PropertyChangeListener pcl = (final PropertyChangeEvent evt) -> {
        SwingUtilities.invokeLater(() -> {
            // filter outline view on QuickSearchFilter input
            if (FilterRepository.PROP_QUICK_FILTER.equals(evt.getPropertyName())) {
                final String newQuickSearchFilter = (String) evt.getNewValue();
                if (newQuickSearchFilter != null && !newQuickSearchFilter.isEmpty()) {
                    unsetQuickFilter();   // should run in EDT
                    if (!newQuickSearchFilter.isEmpty()) { // should run in EDT
                        setQuickFilter(0, getQuickFilter(newQuickSearchFilter));
                    }
                } else {
                    unsetQuickFilter();  // should run in EDT
                }
            }
        });
    };
Categories: Filter, Quick Search