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.wizard;
019:
020: import java.awt.Dimension;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.swing.JCheckBox;
029: import javax.swing.JComponent;
030: import javax.swing.JDialog;
031: import javax.swing.JLabel;
032: import javax.swing.JList;
033: import javax.swing.JPanel;
034: import javax.swing.JScrollPane;
035:
036: import de.finix.contelligent.client.ContelligentClient;
037: import de.finix.contelligent.client.base.Description;
038: import de.finix.contelligent.client.base.category.ContelligentCategory;
039: import de.finix.contelligent.client.base.category.ContelligentCategoryManager;
040: import de.finix.contelligent.client.base.category.DefaultContelligentCategory;
041: import de.finix.contelligent.client.gui.GUI;
042: import de.finix.contelligent.client.i18n.Resources;
043: import de.finix.contelligent.client.util.JTextUtil;
044: import de.finix.contelligent.client.util.TableLayout;
045:
046: public class CategorySensitiveWizard extends AbstractWizard {
047: private ContelligentCategory[] definedCategories;
048:
049: private ContelligentCategory[] sensitiveCategories;
050:
051: private Map categoryMap = new HashMap();
052:
053: private int contentOrTemplate;
054:
055: public CategorySensitiveWizard(int contentOrTemplate) {
056: definedCategories = ContelligentCategoryManager.getInstance()
057: .getDefinedCategories();
058: this .contentOrTemplate = contentOrTemplate;
059: }
060:
061: public CategorySensitiveWizard(List categories,
062: int contentOrTemplate) {
063: this (contentOrTemplate);
064: Iterator iterator = categories.iterator();
065: while (iterator.hasNext()) {
066: DefaultContelligentCategory category = (DefaultContelligentCategory) iterator
067: .next();
068: categoryMap.put(category.getName(), category);
069: }
070: }
071:
072: public List getCategories() {
073: List categories = Collections.synchronizedList(new ArrayList());
074: Iterator iterator = categoryMap.values().iterator();
075: while (iterator.hasNext()) {
076: DefaultContelligentCategory category = (DefaultContelligentCategory) iterator
077: .next();
078: categories.add(category);
079: }
080: return categories;
081: }
082:
083: public int startWizard(int step) {
084: int currentStep = 0;
085: int option;
086: do {
087: option = NEXT_OPTION;
088: if (currentStep == 0) {
089: boolean isFirstStep = (step == FIRST_STEP);
090: option = showSelectCategoriesDialog(isFirstStep);
091: }
092: if (currentStep > 0) {
093: boolean isLastStep = (currentStep == sensitiveCategories.length)
094: && step == LAST_STEP;
095: option = showCategoryDialog(
096: sensitiveCategories[currentStep - 1],
097: isLastStep);
098: }
099: if (option == NEXT_OPTION) {
100: currentStep++;
101: } else if (option == BACK_OPTION) {
102: currentStep--;
103: } else {
104: currentStep = -1;
105: }
106: } while (currentStep != -1
107: && currentStep <= sensitiveCategories.length);
108: return option;
109: }
110:
111: private int showCategoryDialog(ContelligentCategory category,
112: boolean isLastStep) {
113: JPanel panel = new JPanel(new TableLayout(
114: new double[][] {
115: { TableLayout.FILL },
116: { TableLayout.PREFERRED, TableLayout.PREFERRED,
117: TableLayout.PREFERRED,
118: TableLayout.PREFERRED } }));
119: String categoryName = getCategoryName(category);
120: JLabel title = JTextUtil.createTitle(Resources
121: .getLocalString("category")
122: + ": " + categoryName);
123: panel.add(title, "0,0");
124: String defaultDescription;
125: if (contentOrTemplate == GUI.CONTENT) {
126: defaultDescription = Resources.getLocalString(
127: "category_wizard_supported_content_values",
128: new String[] { getCategoryName(category) });
129: } else {
130: defaultDescription = Resources.getLocalString(
131: "category_wizard_supported_template_values",
132: new String[] { getCategoryName(category) });
133: }
134: JComponent description = JTextUtil
135: .createMessageArea(getCategoryDescription(category,
136: defaultDescription));
137: description.setOpaque(false);
138: panel.add(description, "0,1");
139: panel.setPreferredSize(new Dimension(400, 260));
140:
141: JLabel label = JTextUtil.createTitle(Resources
142: .getLocalString("category_wizard_select_values"));
143: panel.add(label, "0,2");
144:
145: JList values = new JList(category.getSupportedValues());
146:
147: DefaultContelligentCategory contelligentCategory = (DefaultContelligentCategory) categoryMap
148: .get(category.getName());
149: String[] selectedCategoryValues = contelligentCategory
150: .getSupportedValues();
151: int[] selectedIndices = new int[selectedCategoryValues.length];
152: for (int i = 0; i < selectedCategoryValues.length; i++) {
153: String[] definedCategoryValues = category
154: .getSupportedValues();
155: for (int j = 0; j < definedCategoryValues.length; j++) {
156: if (definedCategoryValues[j]
157: .equals(selectedCategoryValues[i])) {
158: selectedIndices[i] = j;
159: }
160: }
161: }
162: values.setSelectedIndices(selectedIndices);
163:
164: JScrollPane scroller = new JScrollPane(values);
165: panel.add(scroller, "0,3");
166: if (isLastStep) {
167: setOptions(new String[] { CANCEL, BACK, FINISH });
168: } else {
169: setOptions(new String[] { CANCEL, BACK, NEXT });
170: }
171: setMessage(panel);
172:
173: JDialog dialog = createDialog(ContelligentClient
174: .getActiveFrame(), Resources
175: .getLocalString("category_wizard_category_title"));
176: dialog.setVisible(true);
177:
178: Object[] selectedValues = values.getSelectedValues();
179: contelligentCategory = new DefaultContelligentCategory(category
180: .getName());
181: for (int i = 0; i < selectedValues.length; i++) {
182: contelligentCategory.addValue((String) selectedValues[i]);
183: }
184: if (getValue() == NEXT || getValue() == FINISH) {
185: categoryMap.put(category.getName(), contelligentCategory);
186: return NEXT_OPTION;
187: } else if (getValue() == BACK) {
188: categoryMap.put(category.getName(), contelligentCategory);
189: return BACK_OPTION;
190: }
191: return CANCEL_OPTION;
192: }
193:
194: private int showSelectCategoriesDialog(boolean isFirstStep) {
195: JCheckBox[] checkBoxes = new JCheckBox[definedCategories.length];
196:
197: double[] rows = new double[checkBoxes.length + 1];
198: rows[0] = 100;
199: for (int i = 0; i < definedCategories.length; i++) {
200: rows[i + 1] = 25;
201: }
202: JPanel panel = new JPanel(new TableLayout(new double[][] {
203: { TableLayout.FILL }, rows }));
204:
205: JComponent info;
206: if (contentOrTemplate == GUI.CONTENT) {
207: info = JTextUtil
208: .createMessageArea(Resources
209: .getLocalString("category_wizard_content_description"));
210: } else {
211: info = JTextUtil
212: .createMessageArea(Resources
213: .getLocalString("category_wizard_template_description"));
214: }
215: info.setOpaque(false);
216: panel.add(info, "0,0");
217: panel
218: .setPreferredSize(new Dimension(400,
219: 75 + rows.length * 25));
220:
221: for (int i = 0; i < definedCategories.length; i++) {
222: ContelligentCategory category = definedCategories[i];
223: String name = getCategoryName(category);
224: checkBoxes[i] = new JCheckBox(name);
225: if (categoryMap.containsKey(definedCategories[i].getName())) {
226: checkBoxes[i].setSelected(true);
227: }
228: String location = "0, " + (i + 1);
229: panel.add(checkBoxes[i], location);
230: }
231:
232: if (isFirstStep) {
233: setOptions(new String[] { CANCEL, NEXT });
234: } else {
235: setOptions(new String[] { CANCEL, BACK, NEXT });
236: }
237: setMessage(panel);
238:
239: JDialog dialog;
240: if (contentOrTemplate == GUI.CONTENT) {
241: dialog = createDialog(
242: ContelligentClient.getActiveFrame(),
243: Resources
244: .getLocalString("category_wizard_title_content"));
245: } else {
246: dialog = createDialog(
247: ContelligentClient.getActiveFrame(),
248: Resources
249: .getLocalString("category_wizard_title_template"));
250: }
251: dialog.setVisible(true);
252: if (getValue() == NEXT || getValue() == FINISH) {
253: // count selected categories...
254: int counter = 0;
255: for (int i = 0; i < checkBoxes.length; i++) {
256: if (checkBoxes[i].isSelected()) {
257: counter++;
258: }
259: }
260: sensitiveCategories = new ContelligentCategory[counter];
261: counter = 0;
262: for (int i = 0; i < checkBoxes.length; i++) {
263: String name = definedCategories[i].getName();
264: if (checkBoxes[i].isSelected()) {
265: if (!categoryMap.containsKey(definedCategories[i]
266: .getName())) {
267: categoryMap.put(name,
268: new DefaultContelligentCategory(name));
269: }
270: sensitiveCategories[counter] = definedCategories[i];
271: counter++;
272: } else {
273: categoryMap.remove(name);
274: }
275: }
276: return NEXT_OPTION;
277: } else if (getValue() == BACK) {
278: return BACK_OPTION;
279: }
280: return CANCEL_OPTION;
281: }
282:
283: private String getCategoryName(ContelligentCategory category) {
284: String name = category.getName();
285: Description description = category.getDescription(Resources
286: .getLocale());
287: if (description != null) {
288: if (!name.equals(description.getTitle())) {
289: name = name + " (" + description.getTitle() + ")";
290: }
291: }
292: return name;
293: }
294:
295: private String getCategoryDescription(
296: ContelligentCategory category, String defaultDescription) {
297: String categoryDescription = defaultDescription;
298: Description description = category.getDescription(Resources
299: .getLocale());
300: if (description != null) {
301: categoryDescription = description.getText();
302: }
303: return categoryDescription;
304: }
305: }
|