The Explorer API in Java Encoding QR Code JIS X 0510 in Java The Explorer API

The Explorer API generate, create none none in none projects Code 2 of 5 private Lookup.Resu none none lt selectedCards; public void addNotify() { super.addNotify(); Lookup.

Provider provider = (Lookup.Provider) SwingUtilities.getAncestorOfClass(Lookup.

Provider.class, this); if (provider != null) { selectedCards = provider.getLookup().

lookup(new Lookup.Template(Card.class)); selectedCards.

addLookupListener(this); Collection < extends Card> cards = selectedCards.allInstances(); setCard (cards.isEmpty() null : cards.

iterator().next()); } } public void removeNotify() { if (selectedCards != null) { selectedCards.removeLookupListener(this); selectedCards = null; } super.

removeNotify(); } public void paintComponent(Graphics g) { if (card == null) { super.paintComponent(g); } else { Graphics2D g2d = (Graphics2D) g; BufferedImage img = card.getImage (getSize()); g2d.

drawRenderedImage( img, AffineTransform.getTranslateInstance(0D, 0D)); } } public Dimension getPreferredSize () { return new Dimension (150, 215); } public Card getCard() { return card; } public void resultChanged(LookupEvent lookupEvent) { Collection < extends Card> cards = selectedCards.allInstances(); setCard (cards.

isEmpty() null : cards.iterator().next()); } }.

9 . Nodes, Explorer Views, Actions, and Presenters The result is a component that does something very similar to what BeanTreeView does. Just as any explorer view locates its ExplorerManager by searching the co mponent hierarchy for a parent container that implements ExplorerManager.Provider, CardComponent s addNotify() method searches for a Lookup.Provider.

As TopComponent implements Lookup.Provider, it will nd the instance of CardTopComponent that we will place it in. Note that if we wanted to make this component a standalone view that could be put in any TopComponent, all we would need to do is change the code from nding the Lookup of a parent TopComponent to getting its Lookup.

Result from Utilities.actionsGlobalContext().lookup().

To use this component, we will modify the second half of the constructor of CardTopComponent slightly, to create a JSplitPane with the tree on one side and the detail view on the other:. setLayout (new Bord none for none erLayout()); BeanTreeView view = new BeanTreeView(); view.setRootVisible(false); manager.setRootContext(new AbstractNode (new GameChildren())); CardComponent comp = new CardComponent(); JSplitPane split = new JSplitPane(JSplitPane.

HORIZONTAL_SPLIT, view, comp); add (split, BorderLayout.CENTER);. The end result will look as shown in Figure 9.6. Adding Another Detail View Using the Explorer API The detail view we just created which shows an image of the selected card simply used the Lookup API to track the selection. We can also do that using the Explorer API, to create explorer views that show a subtree of the Nodes in a master view. In this case we will add yet another component to our game window.

This one will show the children of whatever Node is selected in the tree. And to introduce FilterNode, it will show those cards with a different icon, as Figure 9.7 demonstrates.

. 9 . Nodes, Explorer Views, Actions, and Presenters Create a new Java c none none lass called CardsDetailView. Its goal is very simple. It is a JPanel subclass, which, as with CardTopComponent, implements ExplorerManager.

Provider. We will wire up this component s ExplorerManager to the one in CardTopComponent so that the selected node in the tree view will be the root node for our new view. Our new view will be using org.


ListView, which shows Nodes in a JList instead of a JTree as in BeanTreeView. Implement it as shown in Example 9.7.

. Example 9.7: Implem enting CardsDetailView public class CardsDetailView extends JPanel implements ExplorerManager.Provider, PropertyChangeListener { private final ExplorerManager mgr = new ExplorerManager(); public CardsDetailView() { setLayout (new BorderLayout()); ListView view = new ListView(); add (view, BorderLayout.

CENTER); if ((Utilities.getOperatingSystem() & Utilities.OS_WINDOWS_MASK) != 0) { view.

getViewport().getView().setFont (new Font ("SansSerif", Font.

PLAIN, 12)); } } public ExplorerManager getExplorerManager() { return mgr; } private ExplorerManager parentManager = null; public void addNotify() { super.addNotify(); ExplorerManager.Provider provider = (ExplorerManager.

Provider) SwingUtilities.getAncestorOfClass (ExplorerManager.Provider.

class, this); if (provider != null) { parentManager = provider.getExplorerManager(); parentManager.addPropertyChangeListener (this); updateRootNode(); } }.

The Explorer API public void removeN otify() { if (parentManager != null) { parentManager.removePropertyChangeListener (this); } } private void updateRootNode() { Node[] n = parentManager.getSelectedNodes(); if (n.

length == 0) { mgr.setRootContext(new AbstractNode (Children.LEAF)); } else { // don"t worry about multiple selection Node selectionInParent = n[0]; mgr.

setRootContext ( selectionInParent ); } } public void propertyChange(PropertyChangeEvent e) { if (ExplorerManager.PROP_SELECTED_NODES.equals (e.

getPropertyName())) { updateRootNode(); } } }. The code in Example none for none 9.7 is quite straightforward. When our JPanel subclass is added to a container, it will search the hierarchy of parent containers looking for an ExplorerManager.

Provider. This is exactly what BeanTreeView and ListView and the other explorer view components do. When it nds one, it will get the ExplorerManager in question and begin listening for property changes.

Whenever ExplorerManager.PROP_SELECTED_NODES is red, it knows that the parent ExplorerManager s selection has changed and updates its own ExplorerManager. The parent ExplorerManager is the one that belongs to CardTopComponent.

When it changes, it gets the new selection from that ExplorerManager and sets its own ExplorerManager s root context to the selection from the parent. In Example 9.7, if there is nothing selected, we simply create an empty dummy node by calling new AbstractNode (Children.

LEAF) and set that as the root context. Since it will have no children, the list view will be empty if nothing is selected..

Copyright © . All rights reserved.