001: package net.xoetrope.builder.editor;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Container;
006: import java.awt.Dimension;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ItemListener;
009: import javax.swing.JButton;
010: import javax.swing.JComboBox;
011: import javax.swing.JComponent;
012: import javax.swing.JLabel;
013: import javax.swing.JPanel;
014: import javax.swing.JTextField;
015: import javax.swing.border.EmptyBorder;
016:
017: /**
018: * <p>For creating rows within the PropertyEditor and the StyleEditor</p>
019: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
020: * $Revision: 1.1 $
021: */
022: public class XPaletteComponentFactory {
023: public XPaletteComponentFactory() {
024: }
025:
026: /**
027: * Adds a edit field and a label to the toolbar for the named property
028: * @param title the property name/label
029: * @return the edit field for the property
030: */
031: public static void addSpacer(String title, Container page) {
032: // This is added as a Panel a the XPanel gives a sizing problem if used as
033: // the first child component
034: JPanel panel = new JPanel();
035: panel.setLayout(null);
036:
037: panel.setSize(260, 20);
038:
039: JLabel label = new JLabel();
040: label.setFont(XuiDefaults.defaultFont);
041: label.setBounds(1, 1, 258, 16);
042: label.setText(title);
043: panel.add(label);
044:
045: page.add(panel);
046: }
047:
048: /**
049: * Adds a combo field and a label to the toolbar for the named property
050: * @param propertyName the property name/label
051: * @param selIdx the index of the selected item in the dropdown
052: * @param values the dropdown list's values
053: * @param page the XPage to get the componentFactory from
054: * @param ilistener the ItemListener which will be added to the combobox
055: * @return the edit field for the property
056: */
057: public static JComboBox addComboProperty(String propertyName,
058: int selIdx, String[] values, Container page,
059: ItemListener ilistener) {
060: JPanel panel = new JPanel();
061: panel.setBounds(0, 0, 260, 16);
062: panel.setLayout(null);
063: panel.setName("XPageBuilder_OptionalProperty");
064:
065: JLabel label = new JLabel();
066: label.setName(propertyName);
067: label.setFont(XuiDefaults.defaultFont);
068: panel.add(label);
069:
070: JComboBox acombo = new JComboBox();
071: acombo.setLightWeightPopupEnabled(true);
072: acombo.setFont(XuiDefaults.defaultFont);
073: panel.add(acombo);
074:
075: label.setHorizontalAlignment(JLabel.RIGHT);
076:
077: loadCombo(acombo, values);
078: acombo.setSelectedIndex(selIdx);
079:
080: acombo.addItemListener(ilistener);
081:
082: return acombo;
083: }
084:
085: /**
086: * Resets the values of a combobox and populates it with the new values
087: * @param combo The target combobox
088: * @param values array of values to be inserted into the combobox
089: */
090: private static void loadCombo(JComboBox combo, String[] values) {
091: combo.removeAllItems();
092: for (int i = 0; i < values.length; i++)
093: combo.addItem(values[i]);
094: }
095:
096: public static JTextField addProperty(String propertyName,
097: String content, boolean optional, JComponent parent,
098: ActionListener alistener, int width) {
099: JPanel pnlCont = new JPanel(new BorderLayout());
100: pnlCont.setBorder(new EmptyBorder(1, 1, 1, 2));
101: pnlCont.setPreferredSize(new Dimension(240, 18));
102:
103: JLabel lblPrompt = new JLabel(propertyName);
104: lblPrompt.setPreferredSize(new Dimension(80, 16));
105: lblPrompt.setFont(XuiDefaults.defaultFont);
106: pnlCont.add(lblPrompt, BorderLayout.WEST);
107:
108: JTextField txtProp = new JTextField();
109: txtProp.setPreferredSize(new Dimension(130, 16));
110: txtProp.setFont(XuiDefaults.defaultFont);
111: pnlCont.add(txtProp, BorderLayout.CENTER);
112:
113: parent.add(pnlCont);
114: pnlCont.doLayout();
115: return txtProp;
116: }
117:
118: /**
119: * @param propertyName the name of the property to add
120: * @param parent The component to which to add the newly created component.
121: * @param action The String to set as the action command
122: * @param aListener The ActionListener to add to the component
123: * @param width The width of the new component
124: * @param btnoffset The horizontal offset at which to position the component
125: * @return the newly created XLabel
126: */
127: public static JLabel addLabelProperty(String propertyName,
128: JComponent parent, String action, ActionListener aListener,
129: boolean addButton) {
130: JPanel pnlCont = new JPanel(new BorderLayout());
131: pnlCont.setBorder(new EmptyBorder(1, 1, 1, 2));
132: pnlCont.setPreferredSize(new Dimension(240, 18));
133:
134: JLabel lblPrompt = new JLabel(propertyName);
135: lblPrompt.setPreferredSize(new Dimension(80, 16));
136: lblPrompt.setFont(XuiDefaults.defaultFont);
137: pnlCont.add(lblPrompt, BorderLayout.WEST);
138:
139: JLabel lblProp = new JLabel();
140: lblProp.setOpaque(true);
141: lblProp.setBackground(Color.white);
142: lblProp.setFont(XuiDefaults.defaultFont);
143: lblProp.setPreferredSize(new Dimension(130, 16));
144: pnlCont.add(lblProp, BorderLayout.CENTER);
145:
146: if (addButton) {
147: JButton btnMore = new JButton("...");
148: btnMore.setPreferredSize(new Dimension(20, 16));
149: btnMore.addActionListener(aListener);
150: btnMore.setActionCommand(action);
151: btnMore.setFont(XuiDefaults.defaultFont);
152: pnlCont.add(btnMore, BorderLayout.EAST);
153: } else {
154: JLabel dummy = new JLabel();
155: dummy.setPreferredSize(new Dimension(20, 16));
156: pnlCont.add(dummy, BorderLayout.EAST);
157: }
158:
159: parent.add(pnlCont);
160: pnlCont.doLayout();
161: return lblProp;
162: }
163: }
|