001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.module;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Color;
022: import java.awt.Dimension;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.Comparator;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import javax.swing.JComponent;
032: import javax.swing.JLabel;
033: import javax.swing.JList;
034: import javax.swing.JPanel;
035: import javax.swing.ListCellRenderer;
036:
037: import com.jgoodies.forms.extras.SimpleFormBuilder;
038:
039: import de.finix.contelligent.client.base.Type;
040: import de.finix.contelligent.client.base.TypeFactory;
041: import de.finix.contelligent.client.gui.GUI;
042: import de.finix.contelligent.client.gui.GUIConfigurationEditor;
043: import de.finix.contelligent.client.gui.GUIFactory;
044: import de.finix.contelligent.client.gui.GUIModification;
045: import de.finix.contelligent.client.gui.GUIRegistry;
046: import de.finix.contelligent.client.i18n.Resources;
047: import de.finix.contelligent.client.util.list.NameValueComboBox;
048: import de.finix.contelligent.client.util.list.NameValueListModel;
049:
050: public class MappingPane extends JPanel {
051: private NameValueComboBox typeBox, commandBox, guiBox;
052:
053: private GUIConfigurationEditor configurationEditor;
054:
055: private JPanel editorPanel = new JPanel(new BorderLayout());
056:
057: private String configuration;
058:
059: public MappingPane(String typeString, String commandString,
060: String guiString, String config) {
061: setOpaque(false);
062: editorPanel.setOpaque(false);
063: configuration = config;
064: List typeList = new ArrayList(TypeFactory.getInstance()
065: .getTypes().values());
066: Collections.sort(typeList, new Comparator() {
067: public int compare(Object o1, Object o2) {
068: String s1 = ((Type) o1).getName();
069: String s2 = ((Type) o2).getName();
070: return (s1.compareTo(s2));
071: }
072:
073: public boolean equals(Object obj) {
074: if (obj == this )
075: return true;
076: return false;
077: }
078: });
079: typeList.add(0, "All types (*)");
080: List typeNames = new ArrayList();
081: for (Iterator i = typeList.iterator(); i.hasNext();) {
082: Object item = i.next();
083: if (item instanceof Type) {
084: typeNames.add(((Type) item).getName());
085: } else if (item instanceof String) {
086: typeNames.add("*");
087: }
088: }
089: typeBox = new NameValueComboBox(NameValueListModel
090: .buildNameValuePairs(typeList.toArray(),
091: (String[]) typeNames.toArray(new String[0])));
092: if (typeString != null) {
093: typeBox.setSelectedItemByName(typeString);
094: } else {
095: typeBox.setSelectedIndex(0);
096: }
097: typeBox.setBackground(Color.white);
098: typeBox.setRenderer(new ComboBoxRenderer());
099:
100: guiBox = new NameValueComboBox(NameValueListModel
101: .buildNameValuePairs(GUIRegistry.getInstance()
102: .getComponentGuiImpls().toArray(),
103: (String[]) GUIRegistry.getInstance()
104: .getComponentGuiSymbolicNames()
105: .toArray(new String[0])));
106: guiBox.addItemListener(new ItemListener() {
107: public void itemStateChanged(ItemEvent e) {
108: setGUIConfigurationEditor(configuration);
109: }
110: });
111: commandBox = new NameValueComboBox(NameValueListModel
112: .buildLocaleNameValuePairs(GUIModification
113: .getCommands()));
114: commandBox.addItemListener(new ItemListener() {
115: public void itemStateChanged(ItemEvent ie) {
116: if (ie.getStateChange() == ItemEvent.SELECTED) {
117: if (commandBox.getSelectedIndex() == GUIModification.TEMPLATE_REPLACE) {
118: guiBox
119: .setModel(NameValueListModel
120: .buildNameValuePairs(
121: GUIRegistry
122: .getInstance()
123: .getTemplateGuiImpls()
124: .toArray(),
125: (String[]) GUIRegistry
126: .getInstance()
127: .getTemplateGuiSymbolicNames()
128: .toArray(
129: new String[0])));
130: } else {
131: guiBox
132: .setModel(NameValueListModel
133: .buildNameValuePairs(
134: GUIRegistry
135: .getInstance()
136: .getComponentGuiImpls()
137: .toArray(),
138: (String[]) GUIRegistry
139: .getInstance()
140: .getComponentGuiSymbolicNames()
141: .toArray(
142: new String[0])));
143: }
144: }
145: }
146: });
147: if (commandString != null) {
148: commandBox.setSelectedItemByValue(commandString);
149: } else {
150: commandBox.setSelectedIndex(0);
151: }
152: if (guiString != null) {
153: guiBox.setSelectedItemByValue(guiString);
154: } else {
155: guiBox.setSelectedIndex(0);
156: }
157: SimpleFormBuilder builder = new SimpleFormBuilder(this ,
158: "labels=p:g(0.6),r,boxes=p:g(0.4)",
159: "p,r,f:p,r,f:p,r,f:p,r,p,r,f:d:g");
160: builder.setDefaultDialogBorder();
161: builder.getLayout().setRowGroups(new int[][] { { 3, 5, 7 } });
162: JComponent sep = builder.addSeparator(Resources
163: .getLocalString("map_gui"), "1, 1, 3, 1");
164: sep.setOpaque(false);
165: builder.addLabel(Resources
166: .getLocalString("select_type_request"), "labels, 3");
167: builder.add(typeBox, "boxes, 3");
168: builder.addLabel(Resources
169: .getLocalString("select_command_request"), "labels, 5");
170: builder.add(commandBox, "boxes,5");
171: builder.addLabel(
172: Resources.getLocalString("select_gui_request"),
173: "labels, 7");
174: builder.add(guiBox, "boxes, 7");
175: sep = builder
176: .addSeparator(Resources
177: .getLocalString("gui_configuration_request"),
178: "1,9,3,1");
179: sep.setOpaque(false);
180: builder.add(editorPanel, "1,11,3,1");
181: }
182:
183: public void setEditable(boolean editable) {
184: guiBox.setEnabled(editable);
185: typeBox.setEnabled(editable);
186: commandBox.setEnabled(editable);
187: }
188:
189: public void setGUI(String gui) {
190: if (!gui.startsWith(":")) {
191: if (!guiBox.setSelectedItemByName(gui)) {
192: gui = GUIRegistry.getInstance().resolveAlias(gui);
193: guiBox.setSelectedItemByName(gui);
194: }
195: } else {
196: guiBox.setSelectedItemByValue(gui.substring(1));
197: }
198: }
199:
200: public String getGUI() {
201: return (String) guiBox.getSelectedItemName();
202: }
203:
204: public String getGUIImpl() {
205: return (String) guiBox.getSelectedItemValue();
206: }
207:
208: public void setCommand(String command) {
209: commandBox.setSelectedItemByValue(command);
210: }
211:
212: public String getCommand() {
213: return (String) commandBox.getSelectedItemValue();
214: }
215:
216: public void setConfiguration(String configuration) {
217: this .configuration = configuration;
218: }
219:
220: public String getConfiguration() {
221: return configurationEditor.getConfiguration();
222: }
223:
224: public void setType(String type) {
225: typeBox.setSelectedItemByName(type);
226: }
227:
228: public String getType() {
229: return (String) typeBox.getSelectedItemName();
230: }
231:
232: private void setGUIConfigurationEditor(String configuration) {
233: GUI gui = GUIFactory.getInstance().createGUIInstance(
234: (String) guiBox.getSelectedItemValue());
235: configurationEditor = gui.getGUIConfigurationEditor();
236: configurationEditor.setConfiguration(configuration);
237: configurationEditor.init();
238: editorPanel.removeAll();
239: editorPanel.add(configurationEditor.getEditorComponent(),
240: BorderLayout.CENTER);
241: editorPanel.revalidate();
242: editorPanel.repaint();
243: }
244:
245: class ComboBoxRenderer extends JLabel implements ListCellRenderer {
246:
247: public ComboBoxRenderer() {
248: setOpaque(true);
249: setHorizontalAlignment(LEFT);
250: setVerticalAlignment(CENTER);
251: setMinimumSize(new Dimension(0, 26));
252: }
253:
254: public java.awt.Component getListCellRendererComponent(
255: JList list, Object value, int index,
256: boolean isSelected, boolean cellHasFocus) {
257:
258: if (isSelected) {
259: setBackground(list.getSelectionBackground());
260: setForeground(list.getSelectionForeground());
261: } else {
262: setBackground(Color.white);
263: setForeground(list.getForeground());
264: }
265: if (value == null) {
266: return new JLabel();
267: }
268: value = ((NameValueListModel.NameValuePair) value).value;
269: if (value instanceof Type) {
270: Type type = (Type) value;
271: setIcon(type.getIcon());
272: setText(type.getName());
273: } else if (value instanceof String) {
274: setIcon(Resources.genericGUI);
275: setText((String) value);
276: }
277: return this;
278: }
279: }
280: }
|