BrightSide Workbench Full Report + Source Code
elephant-workingdir/src/main/java/org/turro/wd/tree/DefaultTask.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.wd.tree;
19 
20 import java.text.ParseException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.pivot.dialog.ProgressSheet;
24 import org.apache.pivot.collections.List;
25 import org.apache.pivot.collections.Sequence;
26 import org.apache.pivot.util.concurrent.Task;
27 import org.apache.pivot.util.concurrent.TaskExecutionException;
28 import org.apache.pivot.wtk.TreeView;
29 import org.apache.pivot.wtk.content.TreeNode;
30 
35 public abstract class DefaultTask extends Task<String> {
36 
37  protected TreeView treeView;
38  private double inc;
39 
40  public DefaultTask(TreeView treeView, String caption) {
41  this.treeView = treeView;
42  ProgressSheet.startMeasuredProgress(caption, true);
43  }
44 
45  protected abstract String done();
46  protected abstract String failed();
47  protected abstract void process(TreeDirectory dir) throws ParseException;
48  protected abstract void process(TreeWorkFile file) throws ParseException;
49 
50  @Override
51  public String execute() throws TaskExecutionException {
52  try {
53  inc = 1 / (countNodes((List<TreeNode>) treeView.getTreeData(), false, 0.0) * 1.0);
54  processNodes((List<TreeNode>) treeView.getTreeData(), false);
56  return done();
57  } catch (ParseException ex) {
58  Logger.getLogger(DefaultTask.class.getName()).log(Level.SEVERE, null, ex);
59  }
61  return failed();
62  }
63 
64  private void processNodes(List<TreeNode> nodes, boolean selected) throws ParseException {
65  for(TreeNode tn : nodes) {
66  boolean sel = selected || treeView.isNodeChecked(
67  Sequence.Tree.pathOf((List<TreeNode>) treeView.getTreeData(), tn));
69  return;
70  }
71  if(tn instanceof TreeDirectory) {
72  ProgressSheet.nextProgress(null, tn.getText(), -1);
73  processNodes((TreeDirectory) tn, sel);
74  if(sel) {
75  process((TreeDirectory) tn);
76  }
77  } else if(tn instanceof TreeWorkFile) {
78  if(sel) {
79  process((TreeWorkFile) tn);
80  ProgressSheet.nextProgress(null, tn.getText(), inc);
81  } else {
82  ProgressSheet.nextProgress(null, tn.getText(), -1);
83  }
84  }
85  }
86  }
87 
88  private double countNodes(List<TreeNode> nodes, boolean selected, double count) throws ParseException {
89  for(TreeNode tn : nodes) {
90  boolean sel = selected || treeView.isNodeChecked(
91  Sequence.Tree.pathOf((List<TreeNode>) treeView.getTreeData(), tn));
92  if(tn instanceof TreeDirectory) {
93  count = countNodes((TreeDirectory) tn, sel, count);
94  } else if(tn instanceof TreeWorkFile) {
95  if(sel) {
96  count++;
97  }
98  }
99  }
100  return count;
101  }
102 
103 }
static void startMeasuredProgress(String caption, boolean canCancel)
static void endProgress(String caption)
abstract void process(TreeWorkFile file)
abstract void process(TreeDirectory dir)
abstract String failed()