ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / utils / MavenUIUtil.java
blob466421c7d4d6504a3ffdfe9737359253488cb379
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.jetbrains.idea.maven.utils;
18 import com.intellij.ide.DataManager;
19 import com.intellij.ide.util.ElementsChooser;
20 import com.intellij.openapi.actionSystem.ActionManager;
21 import com.intellij.openapi.actionSystem.AnAction;
22 import com.intellij.openapi.actionSystem.AnActionEvent;
23 import com.intellij.openapi.actionSystem.Presentation;
24 import com.intellij.ui.treeStructure.SimpleTree;
25 import com.intellij.util.ui.UIUtil;
26 import org.jetbrains.idea.maven.project.MavenProfileState;
28 import javax.swing.*;
29 import javax.swing.tree.DefaultMutableTreeNode;
30 import javax.swing.tree.TreeCellRenderer;
31 import javax.swing.tree.TreePath;
32 import java.awt.*;
33 import java.awt.event.*;
34 import java.util.*;
36 public class MavenUIUtil {
37 public static void executeAction(final String actionId, final InputEvent e) {
38 final ActionManager actionManager = ActionManager.getInstance();
39 final AnAction action = actionManager.getAction(actionId);
40 if (action != null) {
41 final Presentation presentation = new Presentation();
42 final AnActionEvent event = new AnActionEvent(e, DataManager.getInstance().getDataContext(e.getComponent()), "", presentation, actionManager, 0);
43 action.update(event);
44 if (presentation.isEnabled()) {
45 action.actionPerformed(event);
50 public static <E> void setElements(ElementsChooser<E> chooser, Collection<E> all, Collection<E> selected, Comparator<E> comparator){
51 java.util.List<E> selection = chooser.getSelectedElements();
52 chooser.clear();
53 Collection<E> sorted = new TreeSet<E>(comparator);
54 sorted.addAll(all);
55 for (E element : sorted) {
56 chooser.addElement( element, selected.contains(element));
58 chooser.selectElements(selection);
61 public static void installCheckboxRenderer(final SimpleTree tree, final CheckboxHandler handler) {
62 final JCheckBox checkbox = new JCheckBox();
64 final JPanel panel = new JPanel(new BorderLayout());
65 panel.add(checkbox, BorderLayout.WEST);
67 final TreeCellRenderer baseRenderer = tree.getCellRenderer();
68 tree.setCellRenderer(new TreeCellRenderer() {
69 public Component getTreeCellRendererComponent(final JTree tree,
70 final Object value,
71 final boolean selected,
72 final boolean expanded,
73 final boolean leaf,
74 final int row,
75 final boolean hasFocus) {
76 final Component baseComponent = baseRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
78 final Object userObject = ((DefaultMutableTreeNode)value).getUserObject();
79 if (!handler.isVisible(userObject)) {
80 return baseComponent;
83 final Color foreground = selected ? UIUtil.getTreeSelectionForeground() : UIUtil.getTreeTextForeground();
85 panel.add(baseComponent, BorderLayout.CENTER);
86 panel.setBackground(selected ? UIUtil.getTreeSelectionBackground() : UIUtil.getTreeTextBackground());
87 panel.setForeground(foreground);
89 CheckBoxState state = handler.getState(userObject);
90 checkbox.setSelected(state != CheckBoxState.UNCHECKED);
91 checkbox.setEnabled(state != CheckBoxState.PARTIAL);
92 checkbox.setBackground(UIUtil.getTreeTextBackground());
93 checkbox.setForeground(foreground);
95 return panel;
97 });
99 tree.addMouseListener(new MouseAdapter() {
100 public void mousePressed(MouseEvent e) {
101 int row = tree.getRowForLocation(e.getX(), e.getY());
102 if (row >= 0) {
103 Rectangle checkBounds = checkbox.getBounds();
104 checkBounds.setLocation(tree.getRowBounds(row).getLocation());
105 if (checkBounds.contains(e.getPoint())) {
106 handler.toggle(tree.getPathForRow(row), e);
107 e.consume();
108 tree.setSelectionRow(row);
114 tree.addKeyListener(new KeyAdapter() {
115 public void keyPressed(KeyEvent e) {
116 if (e.getKeyCode() == KeyEvent.VK_SPACE) {
117 TreePath[] treePaths = tree.getSelectionPaths();
118 if (treePaths != null) {
119 for (TreePath treePath : treePaths) {
120 handler.toggle(treePath, e);
122 e.consume();
129 public interface CheckboxHandler {
130 void toggle(TreePath treePath, final InputEvent e);
132 boolean isVisible(Object userObject);
134 CheckBoxState getState(Object userObject);
137 public enum CheckBoxState {
138 CHECKED, UNCHECKED, PARTIAL