001: package org.enhydra.jawe.base.panel.panels;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Dimension;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ItemEvent;
009: import java.awt.event.ItemListener;
010: import java.awt.event.KeyAdapter;
011: import java.awt.event.KeyEvent;
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.Comparator;
015: import java.util.List;
016: import java.util.Vector;
017:
018: import javax.swing.Box;
019: import javax.swing.JComboBox;
020: import javax.swing.JLabel;
021: import javax.swing.SwingConstants;
022:
023: import org.enhydra.jawe.ResourceManager;
024: import org.enhydra.jawe.Settings;
025: import org.enhydra.jawe.Utils;
026: import org.enhydra.jawe.base.panel.PanelContainer;
027: import org.enhydra.jawe.base.panel.PanelSettings;
028: import org.enhydra.shark.xpdl.XMLAttribute;
029: import org.enhydra.shark.xpdl.XMLComplexChoice;
030: import org.enhydra.shark.xpdl.XMLComplexElement;
031: import org.enhydra.shark.xpdl.XMLElement;
032: import org.enhydra.shark.xpdl.XMLEmptyChoiceElement;
033: import org.enhydra.shark.xpdl.XMLSimpleElement;
034:
035: /**
036: * Creates panel with JLabel and JComboBox.
037: * @author Sasa Bojanic
038: * @author Zoran Milakovic
039: */
040: public class XMLComboPanel extends XMLBasicPanel {
041:
042: Dimension textDim = new Dimension(250, 20);
043:
044: protected JComboBox jcb;
045: protected JLabel jl;
046:
047: public XMLComboPanel(PanelContainer pc, XMLElement myOwner,
048: List choices, boolean ldStrChoices, boolean hasEmptyBorder,
049: boolean isVertical, boolean isEditable, boolean isEnabled) {
050: this (pc, myOwner, null, choices, ldStrChoices, hasEmptyBorder,
051: isVertical, isEditable, isEnabled, true, true);
052: }
053:
054: public XMLComboPanel(PanelContainer pc, XMLElement myOwner,
055: String title, List choices, boolean ldStrChoices,
056: boolean hasEmptyBorder, boolean isVertical,
057: boolean isEditable, boolean isEnabled,
058: boolean performSorting, boolean adjustDimension) {
059:
060: super (pc, myOwner, "", isVertical, false, hasEmptyBorder);
061:
062: boolean rightAllignment = false;
063: boolean comboEnabled = true;
064:
065: Color bkgCol = new Color(245, 245, 245);
066: if (pc != null) {
067:
068: Settings settings = pc.getSettings();
069: rightAllignment = settings
070: .getSettingBoolean("XMLBasicPanel.RightAllignment");
071:
072: // check if there is a property to disable combo
073: if (!(myOwner instanceof XMLAttribute)) {
074: String discbo = settings
075: .getSettingString("XMLComboPanel.DisableCombo");
076: String[] hstra = Utils.tokenize(discbo, " ");
077: if (hstra != null) {
078: for (int i = 0; i < hstra.length; i++) {
079: if (hstra[i].equals(myOwner.toName())) {
080: comboEnabled = false;
081: break;
082: }
083: }
084: }
085: }
086:
087: textDim = new Dimension(settings
088: .getSettingInt("SimplePanelTextWidth"), settings
089: .getSettingInt("SimplePanelTextHeight"));
090: if (title == null) {
091: jl = new JLabel(pc.getLabelGenerator()
092: .getLabel(myOwner)
093: + ": ");
094: } else {
095: jl = new JLabel(title + ": ");
096: }
097:
098: if (settings instanceof PanelSettings) {
099: bkgCol = ((PanelSettings) settings)
100: .getBackgroundColor();
101: }
102:
103: } else {
104: if (title == null) {
105: jl = new JLabel(ResourceManager
106: .getLanguageDependentString(myOwner.toName()
107: + "Key")
108: + ": ");
109: } else {
110: jl = new JLabel(title + ": ");
111: }
112: }
113:
114: jl.setAlignmentX(Component.LEFT_ALIGNMENT);
115: jl.setAlignmentY(Component.BOTTOM_ALIGNMENT);
116: if (rightAllignment) {
117: jl.setHorizontalAlignment(SwingConstants.RIGHT);
118: } else {
119: jl.setHorizontalAlignment(SwingConstants.LEFT);
120: }
121:
122: //jl.setMaximumSize(new Dimension(Short.MAX_VALUE,50));
123: Dimension toSet = new Dimension(textDim);
124: List chs = null;
125: Object chsn = null;
126: boolean ldStr = true;
127: if (choices != null) {
128: ldStr = ldStrChoices;
129: } else {
130: if (myOwner instanceof XMLAttribute) {
131: choices = ((XMLAttribute) myOwner).getChoices();
132: } else if (myOwner instanceof XMLComplexChoice) {
133: choices = ((XMLComplexChoice) myOwner).getChoices();
134: } else {
135: choices = new ArrayList();
136: }
137: }
138: if (!(myOwner instanceof XMLComplexChoice)) {
139: // chs=new ArrayList(((XMLAttribute)myOwner).getChoices());
140: chs = PanelUtilities.toXMLElementViewList(pc, choices,
141: ldStr);
142: chsn = new XMLElementView(pc, myOwner.toValue(), ldStr);
143: } else {
144: XMLElement choosen = ((XMLComplexChoice) myOwner)
145: .getChoosen();
146: chs = PanelUtilities.toXMLElementViewList(pc, choices,
147: ldStr);
148: if (choosen instanceof XMLComplexElement) {
149: chsn = new XMLElementView(pc, choosen,
150: XMLElementView.TONAME);
151: } else {
152: chsn = new XMLElementView(pc, choosen,
153: XMLElementView.TOVALUE);
154: }
155: }
156: Vector cv = null;
157: if (performSorting) {
158: cv = XMLComboPanel.sortComboEntries(chs);
159: } else {
160: cv = new Vector(chs);
161: }
162: jcb = new JComboBox(cv);
163:
164: //jcb.setMinimumSize(new Dimension(400,50));
165: if (chsn != null) {
166: jcb.setSelectedItem(chsn);
167: }
168: if (adjustDimension) {
169: toSet = getComboDimension(chs);
170: }
171: jcb.setEditable(isEditable);
172:
173: jcb.setAlignmentX(Component.LEFT_ALIGNMENT);
174: jcb.setAlignmentY(Component.BOTTOM_ALIGNMENT);
175: jcb.setMinimumSize(new Dimension(toSet));
176: jcb.setMaximumSize(new Dimension(toSet));
177: jcb.setPreferredSize(new Dimension(toSet));
178:
179: jcb.setEnabled(isEnabled && comboEnabled);
180: jcb.setBackground(bkgCol);
181: if (isEditable) {
182: jcb.getEditor().getEditorComponent().setBackground(bkgCol);
183: }
184:
185: final XMLPanel p = this ;
186: jcb.addItemListener(new ItemListener() {
187: public void itemStateChanged(ItemEvent e) {
188: if (getPanelContainer() == null)
189: return;
190: getPanelContainer().panelChanged(p, e);
191: }
192:
193: });
194:
195: if (isEditable) {
196: jcb.addActionListener(new ActionListener() {
197: public void actionPerformed(ActionEvent ae) {
198: if (getPanelContainer() == null)
199: return;
200: getPanelContainer().panelChanged(p, ae);
201: }
202: });
203:
204: jcb.getEditor().getEditorComponent().addKeyListener(
205: new KeyAdapter() {
206: public void keyPressed(KeyEvent e) {
207: if (getPanelContainer() == null)
208: return;
209: if (PanelUtilities.isModifyingEvent(e)) {
210: getPanelContainer().panelChanged(p, e);
211: }
212: }
213: });
214: }
215:
216: if (rightAllignment) {
217: add(Box.createHorizontalGlue());
218: }
219: add(jl);
220: if (!rightAllignment) {
221: add(Box.createHorizontalGlue());
222:
223: }
224: add(jcb);
225:
226: }
227:
228: public boolean validateEntry() {
229: String siv = "";
230: Object selItem = getSelectedItem();
231: if (selItem != null) {
232: // System.err.println("SI="+selItem+", class="+selItem.getClass().getName());
233: if (selItem instanceof XMLElement) {
234: if (selItem instanceof XMLSimpleElement
235: || selItem instanceof XMLAttribute
236: || selItem instanceof XMLEmptyChoiceElement) {
237: siv = ((XMLElement) selItem).toValue();
238: } else {
239: siv = ((XMLElement) selItem).toName();
240: }
241: } else {
242: siv = selItem.toString();
243: }
244: }
245: // System.err.println("SIV="+siv);
246: // System.err.println("IREQ="+getOwner().isRequired()+", iro="+getOwner().isReadOnly());
247: if ((selItem == null || siv.trim().equals(""))
248: && getOwner().isRequired() && !getOwner().isReadOnly()) {
249:
250: XMLBasicPanel.defaultErrorMessage(this .getWindow(), jl
251: .getText());
252: jcb.requestFocus();
253: return false;
254: }
255: return true;
256: }
257:
258: public void setElements() {
259: if (!getOwner().isReadOnly()) {
260: Object sel = getSelectedItem();
261: if (myOwner instanceof XMLComplexChoice
262: && sel instanceof XMLElement) {
263: ((XMLComplexChoice) getOwner())
264: .setChoosen((XMLElement) sel);
265: } else {
266: myOwner.setValue(((String) sel).trim());
267: }
268: }
269: }
270:
271: public JComboBox getComboBox() {
272: return jcb;
273: }
274:
275: public Object getSelectedItem() {
276: try {
277: Object el = null;
278: if (jcb.isEditable()) {
279: el = jcb.getEditor().getItem();
280: } else {
281: el = jcb.getSelectedItem();
282: }
283: if (el instanceof XMLElementView) {
284: XMLElementView ev = (XMLElementView) getComboBox()
285: .getSelectedItem();
286: if (ev != null) {
287: if (ev.getElement() != null) {
288: return ev.getElement();
289: }
290: return ev.getElementString();
291: }
292:
293: } else {
294: if (el != null) {
295: return el.toString();
296: }
297: }
298: return "";
299: } catch (Exception ex) {
300: ex.printStackTrace();
301: return "";
302: }
303: }
304:
305: public Object getValue() {
306: return getSelectedItem();
307: }
308:
309: public void cleanup() {
310: }
311:
312: public Dimension getComboDimension(List choices) {
313: double w = 0;
314: if (choices != null) {
315: double longest = 0;
316: for (int i = 0; i < choices.size(); i++) {
317: try {
318: w = getFontMetrics(getFont()).stringWidth(
319: choices.get(i).toString());
320: if (w > longest)
321: longest = w;
322: } catch (Exception ex) {
323: }
324: }
325:
326: w = longest + 25;
327: }
328: if (w < textDim.width)
329: w = textDim.width;
330: return new Dimension((int) w, textDim.height);
331:
332: }
333:
334: public static Vector sortComboEntries(List ces) {
335: Collections.sort(ces, new ComboEntryComparator());
336: return new Vector(ces);
337: }
338:
339: private static class ComboEntryComparator implements Comparator {
340: public int compare(Object o1, Object o2) {
341: String p1 = o1.toString();
342: String p2 = o2.toString();
343:
344: return p1.compareTo(p2);
345: }
346: }
347:
348: public void requestFocus() {
349: jcb.requestFocus();
350: }
351:
352: }
|