View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.support.contentdirectory.ui;
17  
18  import org.fourthline.cling.controlpoint.ActionCallback;
19  import org.fourthline.cling.controlpoint.ControlPoint;
20  import org.fourthline.cling.model.meta.Service;
21  import org.fourthline.cling.support.model.container.Container;
22  
23  import javax.swing.JTree;
24  import javax.swing.event.TreeWillExpandListener;
25  import javax.swing.tree.DefaultMutableTreeNode;
26  import javax.swing.tree.DefaultTreeCellRenderer;
27  import javax.swing.tree.DefaultTreeModel;
28  import javax.swing.tree.TreeSelectionModel;
29  
30  /**
31   * Ready-to-use JTree with interactive browsing of a backend <em>ContentDirectory</em> service.
32   * <p>
33   * Shows the loading status as icon + text informational node directly in the tree.
34   * </p>
35   *
36   * @author Christian Bauer
37   */
38  public abstract class ContentTree extends JTree implements ContentBrowseActionCallbackCreator {
39  
40      protected Container rootContainer;
41      protected DefaultMutableTreeNode rootNode;
42  
43      protected ContentTree() {
44      }
45  
46      public ContentTree(ControlPoint controlPoint, Service service) {
47          init(controlPoint, service);
48      }
49  
50      public void init(ControlPoint controlPoint, Service service) {
51          rootContainer = createRootContainer(service);
52          rootNode = new DefaultMutableTreeNode(rootContainer) {
53              @Override
54              public boolean isLeaf() {
55                  return false;
56              }
57          };
58  
59          DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
60          setModel(treeModel);
61  
62          getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
63          addTreeWillExpandListener(createContainerTreeExpandListener(controlPoint, service, treeModel));
64          setCellRenderer(createContainerTreeCellRenderer());
65  
66          controlPoint.execute(createContentBrowseActionCallback(service, treeModel, getRootNode()));
67      }
68  
69      public Container getRootContainer() {
70          return rootContainer;
71      }
72  
73      public DefaultMutableTreeNode getRootNode() {
74          return rootNode;
75      }
76  
77      public DefaultMutableTreeNode getSelectedNode() {
78          return (DefaultMutableTreeNode) getLastSelectedPathComponent();
79      }
80  
81      protected Container createRootContainer(Service service) {
82          Container rootContainer = new Container();
83          rootContainer.setId("0");
84          rootContainer.setTitle("Content Directory on " + service.getDevice().getDisplayString());
85          return rootContainer;
86      }
87  
88      protected TreeWillExpandListener createContainerTreeExpandListener(ControlPoint controlPoint,
89                                                                         Service service,
90                                                                         DefaultTreeModel treeModel) {
91          return new ContentTreeExpandListener(controlPoint, service, treeModel, this);
92      }
93  
94      protected DefaultTreeCellRenderer createContainerTreeCellRenderer() {
95          return new ContentTreeCellRenderer();
96      }
97  
98      public ActionCallback createContentBrowseActionCallback(Service service,
99                                                                DefaultTreeModel treeModel,
100                                                               DefaultMutableTreeNode treeNode) {
101 
102         return new ContentBrowseActionCallback(service, treeModel, treeNode) {
103             public void updateStatusUI(Status status, DefaultMutableTreeNode treeNode, DefaultTreeModel treeModel) {
104                 ContentTree.this.updateStatus(status, treeNode, treeModel);
105             }
106             public void failureUI(String failureMessage) {
107                 ContentTree.this.failure(failureMessage);
108             }
109         };
110     }
111 
112     // Show some of the status messages _inside_ the tree as a special node
113     public void updateStatus(ContentBrowseActionCallback.Status status, DefaultMutableTreeNode treeNode, DefaultTreeModel treeModel) {
114         switch(status) {
115             case LOADING:
116             case NO_CONTENT:
117                 treeNode.removeAllChildren();
118                 int index = treeNode.getChildCount() <= 0 ? 0 : treeNode.getChildCount();
119                 treeModel.insertNodeInto(new DefaultMutableTreeNode(status.getDefaultMessage()), treeNode, index);
120                 treeModel.nodeStructureChanged(treeNode);
121                 break;
122         }
123     }
124 
125     public abstract void failure(String message);
126 
127 }