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.util;
019:
020: import java.awt.BorderLayout;
021: import java.awt.event.ItemEvent;
022: import java.awt.event.ItemListener;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.swing.JDialog;
029: import javax.swing.JOptionPane;
030: import javax.swing.JPanel;
031: import javax.swing.JScrollPane;
032: import javax.swing.JTextField;
033: import javax.swing.border.TitledBorder;
034:
035: import de.finix.contelligent.client.ContelligentClient;
036: import de.finix.contelligent.client.base.PreviewManager;
037: import de.finix.contelligent.client.base.category.ContelligentCategory;
038: import de.finix.contelligent.client.base.category.ContelligentCategoryManager;
039: import de.finix.contelligent.client.gui.configuration.CategoryTableModel;
040: import de.finix.contelligent.client.i18n.Resources;
041: import de.finix.contelligent.client.util.list.NameValueComboBox;
042: import de.finix.contelligent.client.util.list.NameValueListModel;
043:
044: public class PreviewOptionPane extends JOptionPane {
045: private final static String CANCEL = Resources
046: .getLocalString("cancel");
047:
048: private final static String OK = Resources.getLocalString("ok");
049:
050: private final static String TITLE = Resources
051: .getLocalString("select_preview_title");
052:
053: private List previews;
054:
055: private JDialog dialog;
056:
057: private DateChooser datePanel;
058:
059: private JTextField roleField;
060:
061: private CategoryTableModel categoryTableModel;
062:
063: private String path;
064:
065: private NameValueComboBox selection;
066:
067: public PreviewOptionPane(List previews) {
068: this .previews = previews;
069: }
070:
071: public String getDate() {
072: return String.valueOf(datePanel.getDate().getTime());
073: }
074:
075: public String getPath() {
076: return path;
077: }
078:
079: public int getSelectedPreviewIndex() {
080: if (selection == null)
081: return 0;
082: return selection.getSelectedIndex();
083: }
084:
085: public String getSelectedPreviewName() {
086: if (selection == null)
087: return "";
088: return (String) selection.getSelectedItemName();
089: }
090:
091: public int showDialog(String selectedName) {
092: JPanel messagePanel = new JPanel(new TableLayout(
093: new double[][] {
094: { 250 },
095: { TableLayout.PREFERRED, 5,
096: TableLayout.MINIMUM, 3,
097: TableLayout.PREFERRED, 3, 90 } }));
098: NameValueListModel model = new NameValueListModel();
099: for (Iterator i = previews.iterator(); i.hasNext();) {
100: PreviewManager.Preview preview = (PreviewManager.Preview) i
101: .next();
102: model.addElement(new NameValueListModel.NameValuePair(
103: preview.getDisplay(), preview));
104: }
105: selection = new NameValueComboBox(model);
106: selection.addItemListener(new ItemListener() {
107: public void itemStateChanged(ItemEvent event) {
108: Object item = event.getItem();
109: PreviewManager.Preview selectedPreview = (PreviewManager.Preview) ((NameValueListModel.NameValuePair) item).value;
110: composeInfoPanel(selectedPreview);
111: }
112: });
113: messagePanel.add(selection, "0,0");
114: datePanel = new DateChooser();
115: datePanel.setBorder(new TitledBorder(Resources
116: .getLocalString("preview_date")));
117: messagePanel.add(datePanel, "0,2");
118: JPanel rolePanel = new JPanel(new BorderLayout());
119: roleField = new JTextField();
120: roleField.setEditable(false);
121: roleField.setBorder(null);
122: rolePanel.add(roleField, BorderLayout.CENTER);
123: rolePanel.setBorder(new TitledBorder(Resources
124: .getLocalString("preview_role")));
125: messagePanel.add(rolePanel, "0,4");
126: categoryTableModel = new CategoryTableModel();
127: SortableTable categoryTable = new SortableTable(
128: new TableModelSorter(categoryTableModel));
129: messagePanel.add(new JScrollPane(categoryTable), "0,6");
130:
131: setOptions(new String[] { OK, CANCEL });
132: setMessage(messagePanel);
133: setMessageType(QUESTION_MESSAGE);
134:
135: dialog = createDialog(ContelligentClient.getFrame(), TITLE);
136: selection.setSelectedItemByName(selectedName);
137: dialog.setVisible(true);
138: if (getValue() == OK) {
139: return OK_OPTION;
140: }
141: return CANCEL_OPTION;
142: }
143:
144: private void composeInfoPanel(PreviewManager.Preview preview) {
145: roleField.setText(preview.getRole());
146: // Always preselect current date
147: datePanel.select(new Date());
148: Map categoryMap = preview.getCategoryMap();
149: categoryTableModel.setDataVector(new Object[0][0],
150: new String[] { Resources.getLocalString("category"),
151: Resources.getLocalString("value") });
152: for (Iterator i = categoryMap.entrySet().iterator(); i
153: .hasNext();) {
154: Map.Entry entry = (Map.Entry) i.next();
155: String categoryName = (String) entry.getKey();
156: String categoryDesc = ContelligentCategoryManager
157: .getInstance().getCategoryTitleByName(categoryName);
158: ContelligentCategory cat = ContelligentCategoryManager
159: .getInstance().getCategoryByName(categoryName);
160: if (cat == null) {
161: categoryDesc = "(" + categoryName + ")";
162: }
163: categoryTableModel.addRow(new String[] { categoryDesc,
164: (String) entry.getValue() });
165: }
166: categoryTableModel.fireTableDataChanged();
167: path = preview.getPath();
168: dialog.pack();
169: }
170: }
|