View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
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 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   * @author Jacek Jaroczynski
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  	 * Sets groupBy field used to group the tree and triggers tree to rebuild
57  	 * Only tree should use that method.
58  	 * @param aGroupBy group by option
59  	 */
60  	public void groupBy(BuildGroupBy aGroupBy) {
61  		setGroupBy(aGroupBy);
62  
63  		// clear entire tree
64  		getRoot().removeAllChildren();
65  
66  		// redraw tree
67  		nodeStructureChanged(getRoot());
68  	}
69  
70  	/**
71  	 * Simple setter (does not trigger tree to rebuild)
72  	 * Used when initializig tree (before first load of builds status)
73  	 * @param groupBy group by option
74  	 */
75  	public void setGroupBy(BuildGroupBy groupBy) {
76  		if (groupBy != null) {
77  			this.groupBy = groupBy;
78  		}
79  	}
80  
81  	/*
82  	Override TreeModel methods
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 	// todo add group by handling if necessary
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 //	public void update(final Collection<BambooBuildAdapterIdea> buildStatuses) {
158 //		buildListModel.setBuilds(buildStatuses);
159 //		update();
160 //	}
161 
162 	public void update() {
163 		getRoot().removeAllChildren();
164 		nodeStructureChanged(getRoot());
165 	}
166 
167 	public BuildListModel getBuildListModel() {
168 		return buildListModel;
169 	}
170 }