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  
17  package com.atlassian.theplugin.idea.crucible.tree;
18  
19  import com.atlassian.theplugin.idea.ui.tree.AtlassianTree;
20  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeModel;
21  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
22  import com.intellij.openapi.util.IconLoader;
23  import com.intellij.util.Icons;
24  import org.jetbrains.annotations.NotNull;
25  
26  import javax.swing.*;
27  import javax.swing.tree.TreePath;
28  import java.awt.*;
29  
30  /**
31   * @author Lukasz Guminski
32   */
33  public class AtlassianTreeWithToolbar extends ComponentWithToolbar {
34  	private AtlassianTree tree;
35  	private ModelProvider modelProvider = ModelProvider.EMPTY_MODEL_PROVIDER;
36  	private STATE state = STATE.DIRED;
37  
38  
39  	private VIEW_STATE viewState = VIEW_STATE.EXPANDED;
40  
41  	public AtlassianTreeWithToolbar(String toolbar, final ModelProvider modelProvider) {
42  		this(toolbar);
43  		setModelProvider(modelProvider);
44  	}
45  
46  	public AtlassianTreeWithToolbar(final String toolbarName) {
47  		super(toolbarName);
48  	}
49  
50  	@Override
51  	public Component getTreeComponent() {
52  		if (tree == null) {
53  			tree = new AtlassianTree();
54  		}
55  		return tree;
56  	}
57  
58  
59  	public void setRootVisible(final boolean isVisible) {
60  		tree.setRootVisible(isVisible);
61  	}
62  
63  	public void setModel(final AtlassianTreeModel model) {
64  		tree.setModel(model);
65  		setViewState(getViewState());
66  	}
67  
68  	public void expandAll() {
69  		tree.expandAll();
70  	}
71  
72  	public void setModelProvider(final ModelProvider modelProvider) {
73  		this.modelProvider = modelProvider;
74  		triggerModelUpdated();
75  	}
76  
77  	public ModelProvider getModelProvider() {
78  		return modelProvider;
79  	}
80  
81  	public STATE getState() {
82  		return state;
83  	}
84  
85  	public void setState(final STATE state) {
86  		this.state = state;
87  		triggerModelUpdated();
88  	}
89  
90  	public void triggerModelUpdated() {
91  		setModel(modelProvider.getModel(state));
92  	}
93  
94  	public void changeState() {
95  		setState(getState().getNextState());
96  	}
97  
98  	public VIEW_STATE getViewState() {
99  		return tree.isExpanded(0) ? VIEW_STATE.EXPANDED : VIEW_STATE.COLLAPSED;
100 	}
101 
102 	public void setViewState(final VIEW_STATE viewState) {
103 		this.viewState = viewState;
104 		switch (viewState) {
105 			case COLLAPSED:
106 				collapseAll();
107 				break;
108 			case EXPANDED:
109 				expandAll();
110 				break;
111 			default:
112 				throw new IllegalStateException("Unknown state of tree: " + viewState.toString());
113 		}
114 	}
115 
116 	public void collapseAll() {
117 		tree.collapseAll();
118 	}
119 
120 	public AtlassianTreeNode getSelectedTreeNode() {
121 		if (tree != null) {
122 			TreePath path = tree.getSelectionPath();
123 			if (path != null) {
124 				return (AtlassianTreeNode) path.getLastPathComponent();
125 			}
126 		}
127 		return null;
128 	}
129 
130 	@NotNull
131 	public AtlassianTreeModel getModel() {
132 		return (AtlassianTreeModel) tree.getModel();
133 	}
134 
135 	public void focusOnNode(final AtlassianTreeNode node) {
136 		tree.focusOnNode(node);
137 	}
138 
139 	public boolean isEmpty() {
140 		return !tree.isRootVisible() && (tree.getModel().getChildCount(tree.getModel().getRoot()) == 0);
141 	}
142 
143 	public enum STATE {
144 		FLAT(Icons.DIRECTORY_CLOSED_ICON, "Show flat") {
145 			@Override
146 			public STATE getNextState() {
147 				return DIRED;
148 			}},
149 		DIRED(Icons.DIRECTORY_OPEN_ICON, "Show dired") {
150 			public STATE getNextState() {
151 				return FLAT;
152 			}};
153 
154 		private Icon icon;
155 		private String string;
156 
157 		STATE(final Icon icon, final String string) {
158 			this.icon = icon;
159 			this.string = string;
160 		}
161 
162 		public abstract STATE getNextState();
163 
164 		public Icon getIcon() {
165 			return icon;
166 		}
167 
168 		@Override
169 		public String toString() {
170 			return string;
171 		}
172 	}
173 
174 	public enum VIEW_STATE {
175 		COLLAPSED(IconLoader.getIcon("/actions/collapseall.png"), "Collapse all") {
176 
177 			public VIEW_STATE getNextState() {
178 				return EXPANDED;
179 			}
180 		},
181 		EXPANDED(IconLoader.getIcon("/actions/expandall.png"), "Expand all") {
182 
183 			public VIEW_STATE getNextState() {
184 				return COLLAPSED;
185 			}
186 		};
187 
188 		private Icon icon;
189 		private String string;
190 
191 		VIEW_STATE(final Icon icon, final String string) {
192 			this.icon = icon;
193 			this.string = string;
194 		}
195 
196 		public abstract VIEW_STATE getNextState();
197 
198 		public Icon getIcon() {
199 			return icon;
200 		}
201 
202 		@Override
203 		public String toString() {
204 			return string;
205 		}
206 	}
207 }
208 
209