ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / utils / ComboBoxUtil.java
blob30fbf01ce3aada38bdfda543d56aab52c58a39e7
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.openapi.util.Pair;
19 import com.intellij.util.Function;
20 import org.jetbrains.annotations.Nullable;
22 import javax.swing.*;
23 import java.util.List;
25 public class ComboBoxUtil {
27 private static class Item {
28 private final Object value;
29 private final String label;
31 private Item(Object value, String label) {
32 this.value = value;
33 this.label = label;
36 public Object getValue() {
37 return value;
40 public String toString() {
41 return label;
45 public static void addToModel(DefaultComboBoxModel model, Object value, String label) {
46 model.addElement(new Item(value, label));
49 public static <T> void setModel(JComboBox comboBox, DefaultComboBoxModel model, List<T> values, Function<T, Pair<String, ?>> func) {
50 model.removeAllElements();
51 for (T each : values) {
52 Pair<String, ?> pair = func.fun(each);
53 addToModel(model, pair.second, pair.first);
55 comboBox.setModel(model);
58 public static void select(DefaultComboBoxModel model, Object value) {
59 for (int i = 0; i < model.getSize(); i++) {
60 Item comboBoxUtil = (Item)model.getElementAt(i);
61 if (comboBoxUtil.getValue().equals(value)) {
62 model.setSelectedItem(comboBoxUtil);
63 return;
66 if (model.getSize() != 0) {
67 model.setSelectedItem(model.getElementAt(0));
71 @Nullable
72 public static String getSelectedString(DefaultComboBoxModel model) {
73 return String.valueOf(getSelectedValue(model));
76 @Nullable
77 public static Object getSelectedValue(DefaultComboBoxModel model) {
78 final Object item = model.getSelectedItem();
79 return item != null ? ((Item)item).getValue() : null;