ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / committed / LabeledComboBoxAction.java
blob572f101e195327c018accc3a2a07173bc4c6245a
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 com.intellij.openapi.vcs.changes.committed;
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.Presentation;
21 import com.intellij.openapi.actionSystem.ex.CustomComponentAction;
23 import javax.swing.*;
24 import java.awt.*;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
28 /**
29 * @author yole
31 public abstract class LabeledComboBoxAction extends AnAction implements CustomComponentAction {
32 private final JLabel myLabel;
33 private JPanel myPanel;
34 private final JComboBox myComboBox;
36 protected LabeledComboBoxAction(String label) {
37 final String labelString = label;
38 myComboBox = new JComboBox();
39 myLabel = new JLabel(labelString);
42 public void actionPerformed(AnActionEvent e) {
45 public JComponent createCustomComponent(Presentation presentation) {
46 if (myPanel == null) {
47 myPanel = new JPanel(new BorderLayout());
48 myPanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
49 myPanel.add(myLabel, BorderLayout.WEST);
50 myComboBox.addActionListener(new ActionListener() {
51 public void actionPerformed(final ActionEvent e) {
52 selectionChanged(myComboBox.getSelectedItem());
54 });
55 myComboBox.setModel(createModel());
56 myPanel.add(myComboBox, BorderLayout.CENTER);
58 return myPanel;
61 protected void setModel(final ComboBoxModel model) {
62 myComboBox.setModel(model);
65 protected void enableSelf(final boolean enable) {
66 myComboBox.setEnabled(enable);
67 myLabel.setEnabled(enable);
70 protected boolean isEnabled() {
71 return myComboBox.isEnabled();
74 protected Object getSelected() {
75 return myComboBox.getSelectedItem();
78 protected ComboBoxModel getModel() {
79 return myComboBox.getModel();
82 protected void setRenderer(final ListCellRenderer renderer) {
83 myComboBox.setRenderer(renderer);
86 protected abstract void selectionChanged(Object selection);
88 protected abstract ComboBoxModel createModel();
90 public void setSelected(final int idx) {
91 final ComboBoxModel boxModel = getModel();
92 if (boxModel.getSize() > 0) {
93 boxModel.setSelectedItem(boxModel.getElementAt(idx));