001: //==============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.gui;
025:
026: import java.awt.Component;
027: import java.awt.Dimension;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.util.HashMap;
031: import javax.swing.Icon;
032: import javax.swing.JPanel;
033: import javax.swing.JTree;
034: import javax.swing.tree.DefaultTreeCellRenderer;
035: import org.dlib.gui.FlexLayout;
036: import org.dlib.gui.treeview.TreeView;
037: import org.dlib.gui.treeview.TreeViewNode;
038: import org.dlib.gui.treeview.TreeViewSelEvent;
039: import org.dlib.gui.treeview.TreeViewSelListener;
040:
041: //==============================================================================
042:
043: public class ViewPanel extends JPanel {
044: //---------------------------------------------------------------------------
045: //---
046: //--- Constructor
047: //---
048: //---------------------------------------------------------------------------
049:
050: public ViewPanel() {
051: FlexLayout flexL = new FlexLayout(1, 1);
052: flexL.setColProp(0, FlexLayout.EXPAND);
053: flexL.setRowProp(0, FlexLayout.EXPAND);
054: setLayout(flexL);
055: setMinimumSize(new Dimension(100, 50));
056:
057: tree.addSelectionListener(selList);
058: tree.setCellRenderer(renderer);
059: tree.setRootVisible(false);
060: tree.setShowRootHandles(false);
061:
062: add("0,0,x,x", tree);
063: }
064:
065: //---------------------------------------------------------------------------
066: //---
067: //--- API methods
068: //---
069: //---------------------------------------------------------------------------
070:
071: public Object addContainer(String label, Icon icon) {
072: label = "<html><b>" + label + "</b>";
073:
074: TreeViewNode node = new TreeViewNode(label);
075:
076: tree.getRootNode().addChild(node);
077: hmNodeImages.put(node, icon);
078:
079: return node;
080: }
081:
082: //---------------------------------------------------------------------------
083:
084: public void addForm(Object container, String id, String label,
085: Icon icon) {
086: TreeViewNode node = new TreeViewNode(label);
087: node.setUserData(id);
088:
089: ((TreeViewNode) container).addChild(node);
090:
091: hmNodeImages.put(node, icon);
092:
093: tree.setRootNode(tree.getRootNode());
094: tree.getRootNode().expand(true, 3);
095: }
096:
097: //---------------------------------------------------------------------------
098:
099: public void setActionListener(ActionListener al) {
100: this .al = al;
101: }
102:
103: //---------------------------------------------------------------------------
104: //---
105: //--- Variables
106: //---
107: //---------------------------------------------------------------------------
108:
109: private TreeView tree = new TreeView(false);
110:
111: private ActionListener al;
112:
113: private HashMap<TreeViewNode, Icon> hmNodeImages = new HashMap<TreeViewNode, Icon>();
114:
115: //---------------------------------------------------------------------------
116:
117: private TreeViewSelListener selList = new TreeViewSelListener() {
118: public void nodeSelected(TreeViewSelEvent e) {
119: TreeViewNode node = e.getSelectedNode();
120:
121: String id = (node == null) ? null : (String) node
122: .getUserData();
123:
124: al.actionPerformed(new ActionEvent(tree, 0, id));
125: }
126: };
127:
128: //---------------------------------------------------------------------------
129:
130: private DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
131: public Component getTreeCellRendererComponent(JTree tree,
132: Object value, boolean sel, boolean exp, boolean leaf,
133: int row, boolean hasFocus) {
134: super .getTreeCellRendererComponent(tree, value, sel, exp,
135: leaf, row, hasFocus);
136: setIcon(hmNodeImages.get(value));
137:
138: return this ;
139: }
140: };
141: }
142:
143: //==============================================================================
|