001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package examples.withtemplate;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024: import javax.swing.event.*;
025: import javax.swing.plaf.*;
026: import javax.swing.tree.*;
027:
028: import net.ar.webonswing.*;
029: import net.ar.webonswing.helpers.*;
030: import net.ar.webonswing.remote.*;
031: import net.ar.webonswing.render.templates.*;
032: import net.ar.webonswing.swing.components.*;
033: import net.ar.webonswing.swing.layouts.*;
034:
035: public class TreeDemoWindow extends JFrame {
036: public TreeDemoWindow() {
037: super ("CheckNode TreeExample");
038: String[] strs = { "swing", "platf", "basic", "metal", "JTree" };
039:
040: CheckNode[] nodes = new CheckNode[strs.length];
041: for (int i = 0; i < strs.length; i++)
042: nodes[i] = new CheckNode(strs[i]);
043:
044: nodes[0].add(nodes[1]);
045: nodes[1].add(nodes[2]);
046: nodes[1].add(new CheckNode("leaf1"));
047: nodes[1].add(new CheckNode("leaf2"));
048: nodes[1].add(new CheckNode("leaf3"));
049: nodes[1].add(nodes[3]);
050: nodes[2].add(new CheckNode("leaf4"));
051: nodes[2].add(new CheckNode("leaf5"));
052: nodes[3].add(new CheckNode("leaf6"));
053: nodes[0].add(nodes[4]);
054: nodes[0].add(new CheckNode("leaf7"));
055: nodes[0].add(new CheckNode("leaf8"));
056: nodes[3].setSelected(true);
057:
058: final JTree tree = new JTree(nodes[0]);
059: tree.setCellRenderer(new CheckRenderer());
060: tree.getSelectionModel().setSelectionMode(
061: TreeSelectionModel.SINGLE_TREE_SELECTION);
062: tree.putClientProperty("JTree.lineStyle", "Angled");
063:
064: tree.addTreeSelectionListener(new CheckTreeSelectionListener(
065: tree));
066:
067: //tree.addMouseListener(new NodeSelectionListener(tree));
068: tree.setLayout(new TemplateLayout(WosFramework
069: .getKeyPositionTemplateForName("TreeDemo.JTree")));
070:
071: JPanel thePanel = (JPanel) getContentPane();
072: thePanel.setLayout(new TemplateLayout(WosFramework
073: .getKeyPositionTemplateForName("TreeDemo.theDemo")));
074: getContentPane().add(tree, "theTree");
075:
076: DefaultListModel theListModel = new DefaultListModel();
077: theListModel.addElement("list element 1");
078: theListModel.addElement("list element 2");
079: theListModel.addElement("list element 3");
080: theListModel.addElement("list element 4");
081:
082: JList theList = new JList(theListModel);
083: //theList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
084: theList.addListSelectionListener(new ListSelectionListener() {
085: public void valueChanged(ListSelectionEvent e) {
086: if (!e.getValueIsAdjusting())
087: System.out.println(((JList) e.getSource())
088: .getSelectedValue());
089: }
090: });
091:
092: getContentPane().add(theList, "theTextField");
093:
094: getContentPane().add(
095: new TemplateRadioButtonDemo.RadioButtonDemoPanel(),
096: "theRadioDemo");
097: }
098:
099: public static class CheckTreeSelectionListener implements
100: TreeSelectionListener, RemoteListener {
101: private final JTree tree;
102:
103: public CheckTreeSelectionListener(JTree aTree) {
104: super ();
105: this .tree = aTree;
106: }
107:
108: public void valueChanged(TreeSelectionEvent e) {
109: TreePath path = e.getNewLeadSelectionPath();
110:
111: if (e.getNewLeadSelectionPath().getLastPathComponent() instanceof CheckNode) {
112: CheckNode node = (CheckNode) e
113: .getNewLeadSelectionPath()
114: .getLastPathComponent();
115: boolean isSelected = !(node.isSelected());
116:
117: node.setSelected(isSelected);
118:
119: if (node.getSelectionMode() == CheckNode.DIG_IN_SELECTION) {
120: if (isSelected)
121: tree.expandPath(path);
122: else
123: tree.collapsePath(path);
124: }
125: ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
126: }
127: }
128:
129: public String getRemoteName() {
130: return "CheckTreeSelectionListener";
131: }
132:
133: public Object[] getRemoteParameters() {
134: return new Object[0];
135: }
136: }
137:
138: static public class CheckRenderer implements TreeCellRenderer {
139: public Component getTreeCellRendererComponent(JTree theTree,
140: Object value, boolean isSelected, boolean expanded,
141: boolean leaf, int row, boolean hasFocus) {
142: MyCell theCell = null;
143:
144: if (value instanceof CheckNode) {
145: TreeNode theRootNode = (TreeNode) theTree.getModel()
146: .getRoot();
147: CheckNode theCheckNode = (CheckNode) value;
148:
149: Template theTemplate = WosSwingHelper
150: .getTemplateForComponent("JTree", theTree);
151:
152: JCheckBox theCheck = new JCheckBox();
153: JLabel theLabel = new JLabel();
154:
155: String theNodeType = theRootNode == theCheckNode ? "theRootNode"
156: : leaf ? "theLeafNode"
157: : expanded ? "theExpandedNode"
158: : "theCollapsedNode";
159:
160: theCheck
161: .setLayout(new TemplateLayout(
162: WosFramework
163: .getKeyPositionTemplateForSubTemplate(
164: theTemplate,
165: theNodeType
166: + ".theTreeCell.theContent.theCheck")));
167: theLabel
168: .setLayout(new TemplateLayout(
169: WosFramework
170: .getKeyPositionTemplateForSubTemplate(
171: theTemplate,
172: theNodeType
173: + ".theTreeCell.theContent.theLabel")));
174:
175: theCell = new MyCell(new TemplateLayout(WosFramework
176: .getKeyPositionTemplateForSubTemplate(
177: theTemplate, theNodeType
178: + ".theTreeCell.theContent")),
179: theCheck, "theCheck", theLabel, "theLabel");
180:
181: String stringValue = theTree.convertValueToText(value,
182: isSelected, expanded, leaf, row, hasFocus);
183:
184: theCell.setEnabled(theTree.isEnabled());
185: theCell.theCheck.setSelected(theCheckNode.isSelected());
186: theCell.theLabel.setText(stringValue);
187:
188: if (leaf) {
189: theCell.theLabel.setIcon(new ImageIcon(
190: "resources/images/tree/page.gif"));
191: } else if (expanded) {
192: theCell.theLabel.setIcon(new ImageIcon(
193: "resources/images/tree/folderopen.gif"));
194: } else {
195: theCell.theLabel.setIcon(new ImageIcon(
196: "resources/images/tree/folder.gif"));
197: }
198: }
199:
200: return theCell;
201: }
202:
203: class MyCell extends JPanel {
204: JCheckBox theCheck;
205: JLabel theLabel;
206:
207: public MyCell(LayoutManager aLayout, JCheckBox aCheck,
208: String aCheckPosition, JLabel aLabel,
209: String aLabelPosition) {
210: super (aLayout);
211:
212: add(theCheck = aCheck, aCheckPosition);
213: add(theLabel = aLabel, aLabelPosition);
214: }
215:
216: public Dimension getPreferredSize() {
217: Dimension d_check = theCheck.getPreferredSize();
218: Dimension d_label = theLabel.getPreferredSize();
219: return new Dimension(
220: d_check.width + d_label.width,
221: (d_check.height < d_label.height ? d_label.height
222: : d_check.height));
223: }
224:
225: public void setBackground(Color color) {
226: if (color instanceof ColorUIResource)
227: color = null;
228:
229: super .setBackground(color);
230: }
231:
232: public JCheckBox getCheck() {
233: return theCheck;
234: }
235:
236: public void setCheck(JCheckBox aCheck) {
237: theCheck = aCheck;
238: }
239:
240: public JLabel getLabel() {
241: return theLabel;
242: }
243:
244: public void setLabel(JLabel aLabel) {
245: theLabel = aLabel;
246: }
247: }
248: }
249:
250: public static void main(String args[]) {
251: TreeDemoWindow frame = new TreeDemoWindow();
252: frame.addWindowListener(new WindowAdapter() {
253: public void windowClosing(WindowEvent e) {
254: System.exit(0);
255: }
256: });
257: frame.setSize(300, 200);
258: frame.setVisible(true);
259: }
260: }
|