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  
22  import javax.swing.event.TreeExpansionEvent;
23  import javax.swing.event.TreeWillExpandListener;
24  import javax.swing.tree.DefaultMutableTreeNode;
25  import javax.swing.tree.DefaultTreeModel;
26  import javax.swing.tree.ExpandVetoException;
27  
28  /**
29   * @author Christian Bauer
30   */
31  public class ContentTreeExpandListener implements TreeWillExpandListener {
32  
33      final protected ControlPoint controlPoint;
34      final protected Service service;
35      final protected DefaultTreeModel treeModel;
36      final protected ContentBrowseActionCallbackCreator actionCreator;
37  
38      public ContentTreeExpandListener(ControlPoint controlPoint,
39                                       Service service,
40                                       DefaultTreeModel treeModel,
41                                       ContentBrowseActionCallbackCreator actionCreator) {
42          this.controlPoint = controlPoint;
43          this.service = service;
44          this.treeModel = treeModel;
45          this.actionCreator = actionCreator;
46      }
47  
48      public void treeWillExpand(final TreeExpansionEvent e) throws ExpandVetoException {
49          final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
50  
51          // Remove all "old" children such as the loading/progress messages
52          treeNode.removeAllChildren();
53          treeModel.nodeStructureChanged(treeNode);
54  
55          // Perform the loading in a background thread
56          ActionCallback callback =
57                  actionCreator.createContentBrowseActionCallback(
58                          service, treeModel, treeNode
59                  );
60          controlPoint.execute(callback);
61      }
62  
63      public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
64  
65      }
66  
67  }