View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.examples.tutorial.mainwnd;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import net.sf.jguiraffe.examples.tutorial.model.DirectoryData;
24  import net.sf.jguiraffe.examples.tutorial.model.FileData;
25  import net.sf.jguiraffe.examples.tutorial.viewset.ViewSettings;
26  import net.sf.jguiraffe.gui.builder.components.model.TreeNodePath;
27  import net.sf.jguiraffe.gui.cmd.CommandBase;
28  
29  import org.apache.commons.configuration.HierarchicalConfiguration;
30  import org.apache.commons.configuration.tree.ConfigurationNode;
31  import org.apache.commons.configuration.tree.DefaultConfigurationKey;
32  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
33  import org.apache.commons.configuration.tree.DefaultExpressionEngine;
34  
35  /**
36   * <p>
37   * A command class for reading the content of a directory.
38   * </p>
39   * <p>
40   * This class scans a directory and creates a corresponding structure of {@code
41   * ConfigurationNode} objects in a background thread. Then it updates the tree
42   * view.
43   * </p>
44   *
45   * @author Oliver Heger
46   * @version $Id: ReadDirectoryCommand.java 205 2012-01-29 18:29:57Z oheger $
47   */
48  class ReadDirectoryCommand extends CommandBase
49  {
50      /** The data object for the directory to be read. */
51      private final DirectoryData directoryData;
52  
53      /** The main controller. */
54      private final MainWndController controller;
55  
56      /** The current path within the tree model. */
57      private final TreeNodePath currentPath;
58  
59      /** The nodes created by the scan operation. */
60      private List<ConfigurationNode> nodes;
61  
62      /** The view settings for the current directory. */
63      private ViewSettings directorySettings;
64  
65      /**
66       * Creates a new instance of {@code ReadDirectoryCommand} and initializes
67       * it.
68       *
69       * @param ctrl the controller
70       * @param p the currently selected path of the tree
71       */
72      public ReadDirectoryCommand(MainWndController ctrl, TreeNodePath p)
73      {
74          super(true);
75          directoryData = (DirectoryData) p.getTargetNode().getValue();
76          controller = ctrl;
77          currentPath = p;
78      }
79  
80      /**
81       * Executes this command. Reads the content of the current directory;
82       * creates {@code ConfigurationNode} objects for the sub directories and
83       * {@code FileData} objects for the content.
84       */
85      @Override
86      public void execute() throws Exception
87      {
88          directorySettings = ViewSettings.forDirectory(directoryData
89                  .getDirectory());
90  
91          File[] content = directoryData.getDirectory().listFiles(
92                  directorySettings.createFileFilter());
93          if (content != null)
94          {
95              nodes = new ArrayList<ConfigurationNode>(content.length);
96              List<FileData> data = new ArrayList<FileData>(content.length);
97  
98              for (File f : content)
99              {
100                 Object icon;
101                 if (f.isDirectory())
102                 {
103                     DefaultConfigurationNode nd = new DefaultConfigurationNode(
104                             f.getName(), new DirectoryData(f));
105                     nodes.add(nd);
106                     icon = controller.getIconDirectory();
107                 }
108                 else
109                 {
110                     icon = controller.getIconFile();
111                 }
112                 data.add(new FileData(f, icon));
113             }
114 
115             Collections.sort(data, directorySettings.createComparator());
116             directoryData.setContent(data);
117         }
118 
119         else
120         {
121             directoryData.setContent(new ArrayList<FileData>(0));
122         }
123     }
124 
125     /**
126      * Updates the UI after the execution of the command. Here the newly created
127      * nodes are added to the tree model, and the selection is updated.
128      */
129     @Override
130     protected void performGUIUpdate()
131     {
132         if (nodes != null)
133         {
134             HierarchicalConfiguration model = controller.getTreeModel();
135 
136             // obtain the path to the node in the model
137             DefaultConfigurationKey key = new DefaultConfigurationKey(
138                     (DefaultExpressionEngine) model.getExpressionEngine());
139             currentPath.pathToKey(key);
140 
141             // add the nodes for the sub directories of the directory
142             model.addNodes(key.toString(), nodes);
143         }
144 
145         // handle selection and update table
146         controller.getTree().setSelectedPath(currentPath);
147         controller.fillTable(directoryData, directorySettings);
148     }
149 }