001: package com.calipso.reportgenerator.userinterface;
002:
003: import com.calipso.reportgenerator.common.*;
004:
005: import javax.swing.*;
006: import java.awt.*;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ActionEvent;
009: import java.util.HashMap;
010: import java.util.Map;
011: import java.util.Iterator;
012:
013: import com.calipso.reportgenerator.common.InfoException;
014:
015: /**
016: * Representa el dialogo de parametros de usuario
017: */
018:
019: public class UPDialog extends JDialog implements ActionListener {
020:
021: private UPsPanel uPsPanel;
022: private HashMap params;
023: private JButton btAccept;
024: private JButton btCancel;
025: private boolean hasBeenCanceled = true;
026:
027: public UPDialog(UPCollection upCollection) throws HeadlessException {
028: initialize(upCollection);
029: //hasBeenCanceled = false;
030: }
031:
032: public UPDialog(Frame owner, UPCollection upCollection,
033: ReportGeneratorConfiguration configuration)
034: throws HeadlessException {
035: super (owner);
036: initialize(upCollection);
037: //hasBeenCanceled = false;
038: setDialogIcon(owner, configuration);
039: }
040:
041: public UPDialog(Frame owner, boolean modal,
042: UPCollection upCollection,
043: ReportGeneratorConfiguration configuration)
044: throws HeadlessException {
045: super (owner, modal);
046: initialize(upCollection);
047: //hasBeenCanceled = false;
048: setDialogIcon(owner, configuration);
049: }
050:
051: public UPDialog(Frame owner, String title,
052: UPCollection upCollection,
053: ReportGeneratorConfiguration configuration)
054: throws HeadlessException {
055: super (owner, title);
056: initialize(upCollection);
057: //hasBeenCanceled = false;
058: setDialogIcon(owner, configuration);
059: }
060:
061: public UPDialog(Frame owner, String title, boolean modal,
062: UPCollection upCollection,
063: ReportGeneratorConfiguration configuration)
064: throws HeadlessException {
065: super (owner, title, modal);
066: initialize(upCollection);
067: //hasBeenCanceled = false;
068: setDialogIcon(owner, configuration);
069: }
070:
071: public UPDialog(Frame owner, String title, boolean modal,
072: GraphicsConfiguration gc, UPCollection upCollection,
073: ReportGeneratorConfiguration configuration) {
074: super (owner, title, modal, gc);
075: initialize(upCollection);
076: //hasBeenCanceled = false;
077: setDialogIcon(owner, configuration);
078: }
079:
080: private void setDialogIcon(Frame owner,
081: ReportGeneratorConfiguration configuration) {
082: Image icon = configuration.getImage("ICON");
083: if (icon != null) {
084: owner.setIconImage(icon);
085: }
086: }
087:
088: private void initialize(UPCollection upCollection) {
089: uPsPanel = new UPsPanel(upCollection);
090: getContentPane().add(uPsPanel, BorderLayout.CENTER);
091: getContentPane().add(getSouthPanel(), BorderLayout.SOUTH);
092: setTitle(LanguageTraslator.traslate("186"));
093: pack();
094: setLocation(getDefaultLocation());
095: }
096:
097: private Point getDefaultLocation() {
098: Point ownerLocation = getOwner().getLocation();
099: Dimension ownerSize = getOwner().getSize();
100: Dimension size = getSize();
101:
102: int x = ownerLocation.x + ownerSize.width / 2 - size.width / 2;
103: int y = ownerLocation.y + ownerSize.height / 2 - size.height
104: / 2;
105: return new Point(x, y);
106: }
107:
108: private JPanel getSouthPanel() {
109: FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
110: JPanel pnlSouth = new JPanel(flowLayout);
111: btAccept = new JButton(LanguageTraslator.traslate("112"));
112: btAccept.addActionListener(this );
113: btCancel = new JButton(LanguageTraslator.traslate("113"));
114: btCancel.addActionListener(this );
115: pnlSouth.add(btAccept);
116: pnlSouth.add(btCancel);
117: return pnlSouth;
118: }
119:
120: public void actionPerformed(ActionEvent e) {
121: if (e.getSource() == btAccept) {
122: boolean succeded = false;
123: try {
124: succeded = uPsPanel.fillParamsMap(getParams());
125: } catch (Exception ex) {
126: succeded = false;
127: }
128: if (succeded) {
129: hasBeenCanceled = false;
130: this .dispose();
131: } else {
132: JOptionPane.showMessageDialog(this , LanguageTraslator
133: .traslate("355"), LanguageTraslator
134: .traslate("231"), JOptionPane.ERROR_MESSAGE);
135: params = new HashMap();
136: }
137: } else if (e.getSource() == btCancel) {
138: hasBeenCanceled = true;
139: this .dispose();
140: }
141: }
142:
143: public HashMap getParams() {
144: if (params == null) {
145: params = new HashMap();
146: }
147: return params;
148: }
149:
150: public boolean isHasBeenCanceled() {
151: return hasBeenCanceled;
152: }
153:
154: public static HashMap getParams(JFrame owner,
155: ReportSpec reportSpec,
156: ReportGeneratorConfiguration configuration,
157: Map existingParams) throws InfoException {
158: UPCollection upCollection = new UPCollection(reportSpec,
159: existingParams);
160: if (upCollection.getUpCollection().size() != 0) {
161: UPDialog dialog = new UPDialog(owner, true, upCollection,
162: configuration);
163: dialog.setVisible(true);
164: if (dialog.hasBeenCanceled) {
165: return null;
166: } else {
167: HashMap params = dialog.getParams();
168: translateParamsValues(params, reportSpec);
169: return params;
170: }
171: } else {
172: return new HashMap();
173: }
174: }
175:
176: private static void translateParamsValues(HashMap params,
177: ReportSpec reportSpec) throws InfoException {
178: Iterator iterator = params.entrySet().iterator();
179: while (iterator.hasNext()) {
180: Map.Entry current = (Map.Entry) iterator.next();
181: ReportFilterSpec filterSpec = reportSpec
182: .getFilterSpecFromParamName(current.getKey()
183: .toString());
184: String dim = filterSpec.getDimensionName();
185: if (dim != null && !dim.equalsIgnoreCase("")) {
186: current.setValue(reportSpec.getDimensionFromName(dim)
187: .getValueFor(current.getValue()));
188: } else {
189: current.setValue(ReportDimensionSpec.getValueFor(
190: current.getValue(), filterSpec.getDataType()
191: .getType()));
192: }
193: }
194: }
195:
196: }
|