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.ItemEvent;
024 import java.awt.event.ItemListener;
025
026 import javax.swing.JComboBox;
027
028 import org.mactor.ui.gui.project.ProjectNodeType;
029 import org.mactor.ui.gui.project.ProjectTreeNode;
030 import org.mactor.ui.gui.project.ProjectTreeUtil;
031
032 public class FileNodeComboBox extends JComboBox {
033 private ProjectNodeType type;
034 public FileNodeComboBox(ProjectNodeType type) {
035 this.type = type;
036 addItem(null);
037 setAlignmentX(Component.LEFT_ALIGNMENT);
038 super.addItemListener(new ItemListener() {
039 public void itemStateChanged(ItemEvent e) {
040 String newSel = (String) getSelectedItem();
041 if (!isEqual(selectedName, newSel)) {
042 selectedName = newSel;
043 if (l != null)
044 l.fileNodeChanged(newSel);
045 }
046 }
047 });
048 }
049 public void setConfig(ProjectTreeNode anyNode) {
050 removeAllItems();
051 addItem(null);
052 ProjectTreeNode mbcNode = ProjectTreeUtil.navigateToFirstFileNodeOfType(type, anyNode);
053 if (mbcNode != null) {
054 mbcNode = mbcNode.getParentNode();
055 for (ProjectTreeNode node : mbcNode.getChildNodes()) {
056 addItem(node.getCaption());
057 }
058 }
059 }
060 public String getSelectedName() {
061 Object item = getSelectedItem();
062 if (item == null)
063 return "";
064 return (String) item;
065 }
066 String selectedName;
067 public void setSelectedName(String name) {
068 this.selectedName = name;
069 int count = getItemCount();
070 if (name == null || name.trim().length() == 0) {
071 setSelectedIndex(0);
072 return;
073 }
074 for (int i = 1; i < count; i++) {
075 String item = (String) getItemAt(i);
076 if (item.equals(name)) {
077 setSelectedIndex(i);
078 return;
079 }
080 }
081 setSelectedIndex(0);
082 }
083 FileNodeChangedListener l;
084 public void setFileNodeChangedListener(FileNodeChangedListener l) {
085 this.l = l;
086 }
087 public interface FileNodeChangedListener {
088 void fileNodeChanged(String newName);
089 }
090 private static boolean isEqual(String s1, String s2) {
091 if (s1 == s2)
092 return true;
093 if (s1 == null)
094 return false;
095 return s1.equals(s2);
096 }
097 }