1 package com.atlassian.theplugin.idea;
2
3 import com.atlassian.theplugin.idea.jira.StatusBarIssuesPane;
4 import com.intellij.openapi.actionSystem.ActionGroup;
5 import com.intellij.openapi.actionSystem.ActionManager;
6 import com.intellij.openapi.actionSystem.ActionToolbar;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.ui.Splitter;
9 import com.intellij.ui.SearchTextField;
10 import org.jetbrains.annotations.NotNull;
11
12 import javax.swing.*;
13 import java.awt.*;
14 import java.awt.event.ComponentAdapter;
15 import java.awt.event.ComponentEvent;
16 import java.util.Collection;
17
18
19
20
21 public abstract class PluginToolWindowPanel extends JPanel {
22 public static final float PANEL_SPLIT_RATIO = 0.3f;
23 protected static final float MANUAL_FILTER_PROPORTION_VISIBLE = 0.5f;
24 protected static final float MANUAL_FILTER_PROPORTION_HIDDEN = 0.9f;
25
26 protected Project project;
27 private StatusBarIssuesPane statusBarPane;
28 private final Splitter splitPane = new Splitter(true, PANEL_SPLIT_RATIO);
29 private Splitter splitLeftPane;
30 private JPanel rightPanel;
31 private JScrollPane rightScrollPane;
32 private SearchTextField searchField = new SearchTextField();
33 private JTree rightTree;
34 private JTree leftTree;
35 private JScrollPane leftUpperScrollPane;
36 private String rightToolbarName;
37 private String leftToolbarName;
38
39 public PluginToolWindowPanel(@NotNull final Project project, String leftToolbarName, String rightToolbarName) {
40
41 this.project = project;
42 setLayout(new BorderLayout());
43 this.leftToolbarName = leftToolbarName;
44 this.rightToolbarName = rightToolbarName;
45
46 this.statusBarPane = new StatusBarIssuesPane("");
47 add(statusBarPane, BorderLayout.SOUTH);
48 splitPane.setShowDividerControls(false);
49 splitPane.setSecondComponent(createRightContent());
50 splitPane.setHonorComponentsMinimumSize(false);
51
52 addComponentListener(new ComponentAdapter() {
53 @Override
54 public void componentResized(ComponentEvent e) {
55 final Dimension dimension = e.getComponent().getSize();
56 final boolean doVertical = dimension.getWidth() < dimension.getHeight();
57 if (doVertical != splitPane.getOrientation()) {
58 splitPane.setOrientation(doVertical);
59 }
60
61 }
62 });
63
64 add(splitPane, BorderLayout.CENTER);
65 }
66
67 public void init(int margin) {
68 splitPane.setFirstComponent(createLeftContent());
69 leftUpperScrollPane.setViewportView(getLeftTree());
70 leftUpperScrollPane.setViewportBorder(BorderFactory.createEmptyBorder(0, margin, 0, 0));
71 rightScrollPane.setViewportView(getRightTree());
72 addSearchBoxListener();
73 }
74
75 public void enableGetMoreIssues(boolean enable) {
76 statusBarPane.enableGetMoreIssues(enable);
77 }
78
79 @NotNull
80 public Project getProject() {
81 return project;
82 }
83
84 public Splitter getSplitLeftPane() {
85 return splitLeftPane;
86 }
87
88 public StatusBarIssuesPane getStatusBarPane() {
89 return statusBarPane;
90 }
91
92 public void setStatusBarPane(StatusBarIssuesPane statusBarPane) {
93 this.statusBarPane = statusBarPane;
94 }
95
96 public JComponent createLeftContent() {
97 JPanel leftPanel = new JPanel(new BorderLayout());
98
99 leftUpperScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
100 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
101
102 leftUpperScrollPane.setWheelScrollingEnabled(true);
103 splitLeftPane = new Splitter(false, MANUAL_FILTER_PROPORTION_HIDDEN);
104 splitLeftPane.setOrientation(true);
105 splitLeftPane.setShowDividerControls(true);
106 splitLeftPane.setHonorComponentsMinimumSize(true);
107
108
109
110
111 leftPanel.add(splitLeftPane, BorderLayout.CENTER);
112
113 final JComponent toolBar = createToolBar(leftToolbarName, getActionPlaceName());
114
115 if (toolBar != null) {
116 leftPanel.add(toolBar, BorderLayout.NORTH);
117 }
118
119 splitLeftPane.setFirstComponent(leftUpperScrollPane);
120
121 return leftPanel;
122
123 }
124
125 public JScrollPane getRightScrollPane() {
126 return rightScrollPane;
127 }
128
129 public void setRightScrollPane(JScrollPane rightScrollPane) {
130 this.rightScrollPane = rightScrollPane;
131 }
132
133 public SearchTextField getSearchField() {
134 return searchField;
135 }
136
137 private JComponent createToolBar(String toolbarName, String toolbarPalce) {
138 JComponent component = null;
139 ActionManager actionManager = ActionManager.getInstance();
140 ActionGroup toolbar = (ActionGroup) actionManager.getAction(toolbarName);
141 if (toolbar != null) {
142 final ActionToolbar actionToolbar = actionManager.createActionToolbar(toolbarPalce, toolbar, true);
143 actionToolbar.setTargetComponent(this);
144 component = actionToolbar.getComponent();
145 }
146 return component;
147 }
148
149 public void setStatusErrorMessage(final String error, final Throwable exception) {
150 EventQueue.invokeLater(new Runnable() {
151 public void run() {
152 statusBarPane.setErrorMessage(error, exception);
153 }
154 });
155 }
156
157
158 public void setStatusErrorMessages(final String error, final Collection<Throwable> exceptions) {
159 EventQueue.invokeLater(new Runnable() {
160 public void run() {
161 statusBarPane.setErrorMessages(error, exceptions);
162 }
163 });
164 }
165
166 public void setStatusErrorMessage(final String error) {
167 EventQueue.invokeLater(new Runnable() {
168 public void run() {
169 statusBarPane.setErrorMessage(error);
170 }
171 });
172 }
173
174 public void setStatusInfoMessage(final String message) {
175 setStatusInfoMessage(message, false);
176 }
177
178 public void setStatusInfoMessage(final String msg, final boolean rightAlign) {
179 EventQueue.invokeLater(new Runnable() {
180 public void run() {
181 statusBarPane.setInfoMessage(msg, rightAlign);
182 }
183 });
184 }
185
186 private JComponent createRightContent() {
187 rightPanel = new JPanel(new BorderLayout());
188
189 rightScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
190 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
191 rightScrollPane.setWheelScrollingEnabled(true);
192
193 rightPanel.add(rightScrollPane, BorderLayout.CENTER);
194
195
196 JComponent rightToolbar = createToolBar(rightToolbarName, getActionPlaceName());
197
198
199
200
201
202
203
204
205
206
207 final JPanel toolBarPanel = new JPanel(new GridBagLayout());
208 GridBagConstraints gbc = new GridBagConstraints();
209 gbc.gridx = 0;
210 gbc.gridy = 0;
211
212 gbc.weighty = 0.0;
213
214
215
216
217
218
219 gbc.fill = GridBagConstraints.HORIZONTAL;
220 gbc.weightx = 1.0;
221 if (rightToolbar != null) {
222 toolBarPanel.add(rightToolbar, gbc);
223 }
224 gbc.gridx++;
225 gbc.fill = GridBagConstraints.NONE;
226 gbc.weightx = 0.0;
227 gbc.anchor = GridBagConstraints.FIRST_LINE_END;
228
229 searchField.setMinimumSize(searchField.getPreferredSize());
230 searchField.setMaximumSize(searchField.getPreferredSize());
231 toolBarPanel.add(searchField, gbc);
232
233 rightPanel.add(toolBarPanel, BorderLayout.NORTH);
234
235 return rightPanel;
236 }
237
238 public JPanel getRightPanel() {
239 return rightPanel;
240 }
241
242 public JTree getRightTree() {
243 if (rightTree == null) {
244 rightTree = createRightTree();
245 }
246 return rightTree;
247 }
248
249 public JTree getLeftTree() {
250 if (leftTree == null) {
251 leftTree = createLeftTree();
252 }
253 return leftTree;
254 }
255
256 public void expandAllRightTreeNodes() {
257 for (int i = 0; i < getRightTree().getRowCount(); i++) {
258 getRightTree().expandRow(i);
259 }
260 }
261
262 public void collapseAllRightTreeNodes() {
263 for (int i = 0; i < getRightTree().getRowCount(); i++) {
264 getRightTree().collapseRow(i);
265 }
266 }
267
268 public void expandAllLeftTreeNodes() {
269 for (int i = 0; i < getLeftTree().getRowCount(); i++) {
270 getLeftTree().expandRow(i);
271 }
272 }
273
274 public void collapseAllLeftTreeNodes() {
275 for (int i = 0; i < getLeftTree().getRowCount(); i++) {
276 getLeftTree().collapseRow(i);
277 }
278 }
279
280 protected abstract void addSearchBoxListener();
281
282 public abstract JTree createRightTree();
283
284 public abstract JTree createLeftTree();
285
286 public abstract String getActionPlaceName();
287
288
289 }