001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.ui.gui.project.editors; 021 022 import java.awt.Component; 023 import java.awt.event.ActionEvent; 024 import java.awt.event.ActionListener; 025 import java.util.Collection; 026 027 import javax.swing.JComboBox; 028 029 public class CommandTypeComboBox extends JComboBox { 030 TipListener tipListener; 031 public void setTipListener(TipListener tipListener) { 032 this.tipListener = tipListener; 033 } 034 ActionListener tl = new ActionListener() { 035 public void actionPerformed(ActionEvent e) { 036 NodeEditorConfig.CommandType type = (NodeEditorConfig.CommandType) getSelectedItem(); 037 if (type != null && tipListener != null) 038 tipListener.onTip(2, type.getTip()); 039 else if (tipListener != null) 040 tipListener.onTip(2, null); 041 } 042 }; 043 public CommandTypeComboBox() { 044 addItem(null); 045 setAlignmentX(Component.LEFT_ALIGNMENT); 046 addActionListener(tl); 047 } 048 public void setConfig(Collection<NodeEditorConfig.CommandType> types) { 049 removeAllItems(); 050 addItem(null); 051 for (NodeEditorConfig.CommandType type : types) { 052 addItem(type); 053 } 054 } 055 public String getSelectedType() { 056 NodeEditorConfig.CommandType type = (NodeEditorConfig.CommandType) getSelectedItem(); 057 if (type == null) 058 return ""; 059 return type.getName(); 060 } 061 public void setSelectedType(String type) { 062 int count = getItemCount(); 063 if (type == null || type.trim().length() == 0) { 064 setSelectedIndex(0); 065 return; 066 } 067 for (int i = 1; i < count; i++) { 068 NodeEditorConfig.CommandType item = (NodeEditorConfig.CommandType) getItemAt(i); 069 if (type.equalsIgnoreCase(item.getName())) { 070 setSelectedIndex(i); 071 return; 072 } 073 } 074 setSelectedIndex(0); 075 } 076 }