1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.atlassian.theplugin.idea.bamboo.tree;
17
18 import com.atlassian.theplugin.idea.bamboo.BambooBuildAdapterIdea;
19 import com.atlassian.theplugin.idea.bamboo.BuildGroupBy;
20 import com.atlassian.theplugin.idea.bamboo.BuildListModel;
21
22 import javax.swing.tree.DefaultMutableTreeNode;
23 import javax.swing.tree.DefaultTreeModel;
24 import javax.swing.tree.TreePath;
25 import java.util.ArrayList;
26
27
28
29
30 public class BuildTreeModel extends DefaultTreeModel {
31
32 private BuildListModel buildListModel;
33
34 private BuildGroupBy groupBy = BuildGroupBy.NONE;
35
36 private BuildNodeManipulator generalNodeManipulator;
37 private BuildNodeManipulator stateNodeManipulator;
38 private BuildNodeManipulator serverNodeManipulator;
39 private BuildNodeManipulator dateNodeManipulator;
40 private BuildNodeManipulator projectNodeManipulator;
41
42 public BuildTreeModel(final BuildListModel buildListModel) {
43 super(new DefaultMutableTreeNode());
44
45 this.buildListModel = buildListModel;
46
47 generalNodeManipulator = new GeneralBuildNodeManipulator(buildListModel, getRoot());
48 stateNodeManipulator = new StateBuildNodeManipulator(buildListModel, getRoot());
49 serverNodeManipulator = new ServerBuildNodeManipulator(buildListModel, getRoot());
50 dateNodeManipulator = new DateBuildNodeManipulator(buildListModel, getRoot());
51 projectNodeManipulator = new ProjectBuildNodeManipulator(buildListModel, getRoot());
52
53 }
54
55
56
57
58
59
60 public void groupBy(BuildGroupBy aGroupBy) {
61 setGroupBy(aGroupBy);
62
63
64 getRoot().removeAllChildren();
65
66
67 nodeStructureChanged(getRoot());
68 }
69
70
71
72
73
74
75 public void setGroupBy(BuildGroupBy groupBy) {
76 if (groupBy != null) {
77 this.groupBy = groupBy;
78 }
79 }
80
81
82
83
84
85 @Override
86 public DefaultMutableTreeNode getRoot() {
87 return (DefaultMutableTreeNode) super.getRoot();
88 }
89
90 @Override
91 public Object getChild(Object parent, int index) {
92
93 switch (groupBy) {
94 case DATE:
95 return dateNodeManipulator.getChild(parent, index);
96 case PROJECT:
97 return projectNodeManipulator.getChild(parent, index);
98 case SERVER:
99 return serverNodeManipulator.getChild(parent, index);
100 case STATE:
101 return stateNodeManipulator.getChild(parent, index);
102 case NONE:
103 default:
104 return generalNodeManipulator.getChild(parent, index);
105 }
106 }
107
108 @Override
109 public int getChildCount(Object parent) {
110
111 switch (groupBy) {
112 case DATE:
113 return dateNodeManipulator.getChildCount(parent);
114 case PROJECT:
115 return projectNodeManipulator.getChildCount(parent);
116 case SERVER:
117 return serverNodeManipulator.getChildCount(parent);
118 case STATE:
119 return stateNodeManipulator.getChildCount(parent);
120 case NONE:
121 default:
122 return generalNodeManipulator.getChildCount(parent);
123 }
124 }
125
126 @Override
127 public boolean isLeaf(Object node) {
128 if (node == getRoot()
129 || node instanceof BuildProjectTreeNode
130 || node instanceof BuildStateTreeNode
131 || node instanceof BuildDateTreeNode
132 || node instanceof BuildServerTreeNode) {
133 return false;
134 }
135
136 return true;
137 }
138
139 @Override
140 public void valueForPathChanged(TreePath path, Object newValue) {
141 System.out.println("valueForPathChanged");
142 }
143
144 @Override
145
146 public int getIndexOfChild(Object parent, Object child) {
147 if (parent == getRoot()) {
148 if (child instanceof BuildTreeNode) {
149 BambooBuildAdapterIdea build = ((BuildTreeNode) child).getBuild();
150 return new ArrayList<BambooBuildAdapterIdea>(buildListModel.getBuilds()).indexOf(build);
151 }
152 }
153
154 return -1;
155 }
156
157
158
159
160
161
162 public void update() {
163 getRoot().removeAllChildren();
164 nodeStructureChanged(getRoot());
165 }
166
167 public BuildListModel getBuildListModel() {
168 return buildListModel;
169 }
170 }