001: package org.enhydra.jawe;
002:
003: import java.awt.Component;
004: import java.awt.Dimension;
005: import java.awt.Insets;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.util.ArrayList;
009: import java.util.Collections;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import javax.swing.ImageIcon;
014: import javax.swing.JComboBox;
015: import javax.swing.JList;
016: import javax.swing.plaf.basic.BasicComboBoxRenderer;
017: import javax.swing.plaf.basic.BasicComboPopup;
018:
019: import org.enhydra.shark.utilities.SequencedHashMap;
020: import org.enhydra.shark.xpdl.XMLComplexElement;
021: import org.enhydra.shark.xpdl.XMLElement;
022:
023: /**
024: * Creates button witch displays popup with available choices for showing xpdl objects.
025: *
026: * @author Sasa Bojanic
027: * @author Miroslav Popov
028: */
029: public class XMLElementChoiceButton2 extends ChoiceButton implements
030: ActionListener {
031:
032: protected ChoiceButtonListener parent;
033:
034: protected SequencedHashMap choiceMap = new SequencedHashMap();
035:
036: BasicComboPopup popup;
037:
038: JComboBox aList;
039: protected Class choiceType;
040:
041: public XMLElementChoiceButton2(Class choiceType,
042: ChoiceButtonListener p, ImageIcon icon) {
043: this .parent = p;
044: this .choiceType = choiceType;
045: aList = new JComboBox();
046: aList.setRenderer(new BasicComboBoxRenderer() {
047: public Component getListCellRendererComponent(JList list,
048: Object value, int index, boolean isSelected,
049: boolean cellHasFocus) {
050: if (isSelected) {
051: setBackground(list.getSelectionBackground());
052: setForeground(list.getSelectionForeground());
053: if (-1 < index) {
054: Object selItem = aList.getModel().getElementAt(
055: index);
056: XMLElement el = null;
057: if (selItem != null) {
058: el = (XMLElement) choiceMap.get(selItem);
059: }
060: String ttt = "";
061: if (el != null) {
062: ttt = JaWEManager.getInstance()
063: .getTooltipGenerator().getTooltip(
064: el);
065: } else {
066: ttt = super .getToolTipText();
067: }
068: list.setToolTipText(ttt);
069:
070: }
071: } else {
072: setBackground(list.getBackground());
073: setForeground(list.getForeground());
074: }
075: setFont(list.getFont());
076: setText((value == null) ? "" : value.toString());
077: return this ;
078: }
079:
080: });
081: popup = new BasicComboPopup(aList);
082: aList.addActionListener(this );
083:
084: setIcon(icon);
085: addActionListener(this );
086: setMargin(new Insets(1, 2, 1, 2));
087: setSize(new Dimension(10, 8));
088: setAlignmentY(0.5f);
089: }
090:
091: public void actionPerformed(ActionEvent ae) {
092: if (ae.getSource() == this ) {
093: aList.removeActionListener(this );
094: refresh();
095: aList.addActionListener(this );
096: if (choiceMap.size() > 0) {
097: popup.show(this .getParent(), this .getX(), this .getY()
098: + this .getHeight());
099: }
100: } else {
101: Object selItem = aList.getSelectedItem();
102: if (selItem != null) {
103: Object obj = choiceMap.get(selItem);
104: parent.selectionChanged(this , obj);
105: }
106: choiceMap.clear();
107: popup.hide();
108:
109: }
110:
111: }
112:
113: protected void refresh() {
114: choiceMap.clear();
115: aList.removeAllItems();
116:
117: List choices = parent.getChoices(this );
118: if (choices != null) {
119: Iterator it = choices.iterator();
120: while (it.hasNext()) {
121: XMLElement choice = (XMLElement) it.next();
122: String dispName = " "
123: + JaWEManager.getInstance()
124: .getDisplayNameGenerator()
125: .getDisplayName(choice) + " ";
126: if (choiceMap.containsKey(dispName)) {
127: if (choice instanceof XMLComplexElement) {
128: XMLElement idEl = ((XMLComplexElement) choice)
129: .get("Id");
130: dispName += "["
131: + ResourceManager
132: .getLanguageDependentString("IdKey")
133: + "=" + idEl.toValue() + "] ";
134: }
135: }
136: choiceMap.put(dispName, choice);
137: }
138: }
139:
140: if (choiceMap.size() > 0) {
141: List sorted = new ArrayList(choiceMap.sequence());
142: Collections.sort(sorted);
143: String[] names = new String[sorted.size()];
144: sorted.toArray(names);
145: for (int i = 0; i < choiceMap.size(); i++) {
146: aList.addItem(names[i]);
147: }
148: }
149: }
150:
151: public Class getChoiceType() {
152: return choiceType;
153: }
154:
155: }
|