1 package com.calipso.reportgenerator.userinterface; 2 3 import com.calipso.reportgenerator.common.*; 4 5 import javax.swing.*; 6 import java.awt.*; 7 import java.awt.event.ActionListener ; 8 import java.awt.event.ActionEvent ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 import java.util.Iterator ; 12 13 import com.calipso.reportgenerator.common.InfoException; 14 15 18 19 public class UPDialog extends JDialog implements ActionListener { 20 21 private UPsPanel uPsPanel; 22 private HashMap params; 23 private JButton btAccept; 24 private JButton btCancel; 25 private boolean hasBeenCanceled = true; 26 27 public UPDialog(UPCollection upCollection) throws HeadlessException { 28 initialize(upCollection); 29 } 31 public UPDialog(Frame owner, UPCollection upCollection, ReportGeneratorConfiguration configuration) throws HeadlessException { 32 super(owner); 33 initialize(upCollection); 34 setDialogIcon(owner, configuration); 36 } 37 public UPDialog(Frame owner, boolean modal, UPCollection upCollection, ReportGeneratorConfiguration configuration) throws HeadlessException { 38 super(owner, modal); 39 initialize(upCollection); 40 setDialogIcon(owner, configuration); 42 } 43 44 public UPDialog(Frame owner, String title, UPCollection upCollection, ReportGeneratorConfiguration configuration) throws HeadlessException { 45 super(owner, title); 46 initialize(upCollection); 47 setDialogIcon(owner, configuration); 49 } 50 51 public UPDialog(Frame owner, String title, boolean modal, UPCollection upCollection, ReportGeneratorConfiguration configuration) throws HeadlessException { 52 super(owner, title, modal); 53 initialize(upCollection); 54 setDialogIcon(owner, configuration); 56 } 57 58 public UPDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc, UPCollection upCollection, ReportGeneratorConfiguration configuration) { 59 super(owner, title, modal, gc); 60 initialize(upCollection); 61 setDialogIcon(owner, configuration); 63 } 64 65 private void setDialogIcon(Frame owner, ReportGeneratorConfiguration configuration) { 66 Image icon = configuration.getImage("ICON"); 67 if(icon != null) { 68 owner.setIconImage(icon); 69 } 70 } 71 72 private void initialize(UPCollection upCollection) { 73 uPsPanel = new UPsPanel(upCollection); 74 getContentPane().add(uPsPanel, BorderLayout.CENTER); 75 getContentPane().add(getSouthPanel(), BorderLayout.SOUTH); 76 setTitle(LanguageTraslator.traslate("186")); 77 pack(); 78 setLocation(getDefaultLocation()); 79 } 80 81 private Point getDefaultLocation() { 82 Point ownerLocation = getOwner().getLocation(); 83 Dimension ownerSize = getOwner().getSize(); 84 Dimension size = getSize(); 85 86 int x = ownerLocation.x + ownerSize.width / 2 - size.width / 2; 87 int y = ownerLocation.y + ownerSize.height / 2 - size.height / 2; 88 return new Point(x, y); 89 } 90 91 private JPanel getSouthPanel() { 92 FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT); 93 JPanel pnlSouth = new JPanel(flowLayout); 94 btAccept = new JButton(LanguageTraslator.traslate("112")); 95 btAccept.addActionListener(this); 96 btCancel = new JButton(LanguageTraslator.traslate("113")); 97 btCancel.addActionListener(this); 98 pnlSouth.add(btAccept); 99 pnlSouth.add(btCancel); 100 return pnlSouth; 101 } 102 103 public void actionPerformed(ActionEvent e) { 104 if(e.getSource() == btAccept) { 105 boolean succeded = false; 106 try{ 107 succeded = uPsPanel.fillParamsMap(getParams()); 108 }catch (Exception ex){ 109 succeded = false; 110 } 111 if(succeded) { 112 hasBeenCanceled = false; 113 this.dispose(); 114 } else { 115 JOptionPane.showMessageDialog(this, LanguageTraslator.traslate("355"), LanguageTraslator.traslate("231"), JOptionPane.ERROR_MESSAGE); 116 params = new HashMap (); 117 } 118 } else if(e.getSource() == btCancel) { 119 hasBeenCanceled = true; 120 this.dispose(); 121 } 122 } 123 124 public HashMap getParams() { 125 if(params == null) { 126 params = new HashMap (); 127 } 128 return params; 129 } 130 131 public boolean isHasBeenCanceled() { 132 return hasBeenCanceled; 133 } 134 135 public static HashMap getParams(JFrame owner, ReportSpec reportSpec, ReportGeneratorConfiguration configuration, Map existingParams) throws InfoException{ 136 UPCollection upCollection = new UPCollection(reportSpec, existingParams); 137 if(upCollection.getUpCollection().size() != 0) { 138 UPDialog dialog = new UPDialog(owner, true, upCollection, configuration); 139 dialog.setVisible(true); 140 if(dialog.hasBeenCanceled) { 141 return null; 142 } else { 143 HashMap params = dialog.getParams(); 144 translateParamsValues(params, reportSpec); 145 return params; 146 } 147 } else { 148 return new HashMap (); 149 } 150 } 151 152 private static void translateParamsValues(HashMap params, ReportSpec reportSpec) throws InfoException{ 153 Iterator iterator = params.entrySet().iterator(); 154 while(iterator.hasNext()){ 155 Map.Entry current = (Map.Entry )iterator.next(); 156 ReportFilterSpec filterSpec = reportSpec.getFilterSpecFromParamName(current.getKey().toString()); 157 String dim = filterSpec.getDimensionName(); 158 if(dim!=null && !dim.equalsIgnoreCase("")){ 159 current.setValue(reportSpec.getDimensionFromName(dim).getValueFor(current.getValue())); 160 }else{ 161 current.setValue(ReportDimensionSpec.getValueFor(current.getValue(), filterSpec.getDataType().getType())); 162 } 163 } 164 } 165 166 } 167 | Popular Tags |