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.BorderLayout;
021: import java.awt.LayoutManager;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: import javax.swing.JCheckBox;
030: import javax.swing.JComponent;
031: import javax.swing.JDialog;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036:
037: import de.finix.contelligent.client.ContelligentClient;
038: import de.finix.contelligent.client.base.ContelligentComponent;
039: import de.finix.contelligent.client.base.Description;
040: import de.finix.contelligent.client.base.PackageManager;
041: import de.finix.contelligent.client.base.PackageNotFoundException;
042: import de.finix.contelligent.client.base.Type;
043: import de.finix.contelligent.client.base.TypeCreationException;
044: import de.finix.contelligent.client.base.TypeFactory;
045: import de.finix.contelligent.client.base.TypeNotFoundException;
046: import de.finix.contelligent.client.gui.GUIRegistry;
047: import de.finix.contelligent.client.i18n.Resources;
048: import de.finix.contelligent.client.util.JTextUtil;
049: import de.finix.contelligent.client.util.RestrictedTextField;
050: import de.finix.contelligent.client.util.TableLayout;
051: import de.finix.contelligent.client.util.list.NameValueComboBox;
052: import de.finix.contelligent.client.util.list.NameValueListModel;
053:
054: public class TypeWizard extends AbstractWizard {
055:
056: private static Logger logger = Logger.getLogger(TypeWizard.class
057: .getName());
058:
059: private ContelligentComponent component;
060:
061: private String typeName, packageName, typeGui, super TypeName,
062: iconIdentifier;
063:
064: private List contentCategories, templateCategories;
065:
066: private NameValueComboBox typeGuiList;
067:
068: private JCheckBox guiAvailable = new JCheckBox();
069:
070: private Map descriptions = new HashMap();
071:
072: private String typeDialogType = null;
073:
074: private String environment;
075:
076: public String getTypeDialogType() {
077: return typeDialogType;
078: }
079:
080: public void setTypeDialogType(String typeDialogType) {
081: this .typeDialogType = typeDialogType;
082: }
083:
084: public TypeWizard(ContelligentComponent component,
085: String typeDialogType, String environment)
086: throws PackageNotFoundException {
087: super ();
088: this .typeDialogType = typeDialogType;
089: this .environment = environment;
090: packageName = PackageManager.getInstance()
091: .getPackage(component).getIdentifier();
092: iconIdentifier = component.getType().getIconPath();
093: descriptions = component.getType().getDescriptions();
094: if (component.isBlueprint()) {
095: typeName = component.getDefinedBlueprintType().substring(
096: packageName.length() + 1);
097: try {
098: Type definedType = TypeFactory.getInstance().getType(
099: component.getDefinedBlueprintType());
100: descriptions = definedType.getDescriptions();
101: iconIdentifier = definedType.getIconPath();
102: } catch (TypeNotFoundException tne) {
103: logger.log(Level.SEVERE,
104: "Could not load defined type.", tne);
105: }
106: } else {
107: typeName = component.getName();
108: }
109: typeGui = component.getType().getGUI();
110: super TypeName = component.getType().getName();
111: this .component = component;
112: contentCategories = component.getContentCategories();
113: templateCategories = component.getTemplateCategories();
114: }
115:
116: public Type startWizard() {
117: int currentStep = 0;
118: int totalSteps = 3;
119: int state = NEXT_OPTION;
120: do {
121: if (currentStep == 0) {
122: int step = FIRST_STEP;
123: state = showTypeDialog(step);
124: } else if (currentStep == 1) {
125: int step = MIDDLE_STEP;
126: IconWizard iconWizard = new IconWizard(iconIdentifier,
127: environment);
128: state = iconWizard.startWizard(step);
129: iconIdentifier = iconWizard.getIconIdentifier();
130: } else if (currentStep == 2) {
131: int step = LAST_STEP;
132: DescriptionWizard descriptionWizard = new DescriptionWizard(
133: descriptions);
134: state = descriptionWizard.startWizard(step);
135: descriptions = descriptionWizard.getDescriptions();
136: }
137: if (state == NEXT_OPTION) {
138: currentStep++;
139: } else if (state == BACK_OPTION) {
140: currentStep--;
141: } else {
142: currentStep = -1;
143: }
144: } while (currentStep != -1 && currentStep < totalSteps);
145: if (currentStep != -1) {
146: // register type
147: try {
148: Type definedType = new Type(packageName + "."
149: + typeName, super TypeName, component.getPath());
150: if (guiAvailable.isSelected()
151: && !typeGui
152: .equals(component.getType().getGUI())) {
153: definedType.setGUI(typeGui);
154: } else {
155: definedType.setGUI(null);
156: }
157: definedType.setIconPath(iconIdentifier);
158: Iterator i = descriptions.entrySet().iterator();
159: while (i.hasNext()) {
160: Map.Entry entry = (Map.Entry) i.next();
161: Description description = (Description) entry
162: .getValue();
163: definedType.addDescription((String) entry.getKey(),
164: description);
165: }
166: return definedType;
167: } catch (TypeCreationException tce) {
168: logger.log(Level.SEVERE, "Could not create type", tce);
169: }
170: }
171: return null;
172: }
173:
174: private int showTypeDialog(int step) {
175: LayoutManager tableLayout = new TableLayout(new double[][] {
176: { 20, 250 },
177: { TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 10,
178: TableLayout.PREFERRED, 15,
179: TableLayout.PREFERRED, 5,
180: TableLayout.PREFERRED, 10,
181: TableLayout.PREFERRED } });
182: JPanel panel = new JPanel(tableLayout);
183:
184: JLabel title = JTextUtil.createTitle(Resources
185: .getLocalString("name"));
186: panel.add(title, "0,0,1,0");
187: JComponent info = JTextUtil.createMessageArea(Resources
188: .getLocalString("blueprint_wizard_description"));
189: info.setOpaque(false);
190: panel.add(info, "0,2,1,2");
191: JPanel typeNamePanel = new JPanel(new BorderLayout());
192: JLabel typePrefix = new JLabel(packageName + ".");
193: RestrictedTextField nameField = new RestrictedTextField(
194: typeName);
195: nameField
196: .setAllowString(ContelligentComponent.getAllowString());
197: typePrefix.setFont(nameField.getFont());
198: if (component.isBlueprint()) {
199: nameField.setEnabled(false);
200: }
201: typeNamePanel.add(typePrefix, BorderLayout.WEST);
202: typeNamePanel.add(nameField, BorderLayout.CENTER);
203: panel.add(typeNamePanel, "0,4,1,4");
204: nameField.requestFocus();
205: JLabel gui = JTextUtil.createTitle("GUI");
206: panel.add(gui, "0,6,1,6");
207: JComponent guiInfo = JTextUtil.createMessageArea(Resources
208: .getLocalString("blueprint_wizard_gui"));
209: guiInfo.setOpaque(false);
210: panel.add(guiInfo, "0,8,1,8");
211: typeGuiList = new NameValueComboBox(NameValueListModel
212: .buildNameValuePairs(
213: GUIRegistry.getInstance()
214: .getComponentGuiSymbolicNames()
215: .toArray(),
216: (String[]) GUIRegistry.getInstance()
217: .getComponentGuiSymbolicNames()
218: .toArray(new String[0])).sort(
219: NameValueListModel.SORT_DESCENDING));
220: guiAvailable.addChangeListener(new ChangeListener() {
221: public void stateChanged(ChangeEvent e) {
222: typeGuiList.setEnabled(((JCheckBox) e.getSource())
223: .isSelected());
224: }
225: });
226: panel.add(guiAvailable, "0,10");
227: panel.add(typeGuiList, "1,10");
228: if (typeGui != null
229: && GUIRegistry.getInstance().getSymbolicName(typeGui) != null) {
230: String symbolicName = GUIRegistry.getInstance()
231: .getSymbolicName(typeGui);
232: typeGuiList.setSelectedItemByValue(symbolicName);
233: typeGuiList.setEnabled(true);
234: guiAvailable.setSelected(true);
235: } else {
236: typeGuiList.setEnabled(false);
237: guiAvailable.setSelected(false);
238: }
239: panel.doLayout();
240: setMessage(panel);
241: if (step == FIRST_STEP) {
242: setOptions(new String[] { CANCEL, NEXT });
243: } else if (step == MIDDLE_STEP) {
244: setOptions(new String[] { CANCEL, BACK, NEXT });
245: } else if (step == LAST_STEP) {
246: setOptions(new String[] { CANCEL, FINISH });
247: }
248:
249: JDialog dialog = null;
250:
251: if (getTypeDialogType().equals("update")) {
252: dialog = createDialog(ContelligentClient.getActiveFrame(),
253: Resources.getLocalString("update_blueprint"));
254: } else if (getTypeDialogType().equals("create")) {
255: dialog = createDialog(ContelligentClient.getActiveFrame(),
256: Resources.getLocalString("create_blueprint"));
257:
258: }
259: dialog.setVisible(true);
260: if (getValue() == NEXT) {
261: typeName = nameField.getText();
262: typeGui = GUIRegistry.getInstance().getImpl(
263: (String) typeGuiList.getSelectedItemValue());
264: return NEXT_OPTION;
265: }
266: return CANCEL_OPTION;
267: }
268:
269: public String getPackageName() {
270: return packageName;
271: }
272:
273: public List getContentCategories() {
274: return contentCategories;
275: }
276:
277: public List getTemplateCategories() {
278: return templateCategories;
279: }
280: }
|