Back to index

wims  3.65+svn20090927
HistoDialog.java
Go to the documentation of this file.
00001 /*
00002  * @(#)HistoDialog.java
00003  * 
00004  * $Id: HistoDialog.java,v 1.6 2001/05/27 22:28:57 huaz Exp $
00005  * 
00006  * Created Novenmber 23, 2000, 2:55 PM
00007 */
00008 package SharpTools;
00009 import java.awt.*;
00010 import java.awt.event.*;
00011 import javax.swing.*;
00012 import java.util.*;
00020 public final class HistoDialog extends SharpDialog {
00021     //static Properties translation=SharpTools.translation;
00022     private JFrame owner;
00023     private JTextField title;
00024     private AddressField startCell;
00025     private AddressField endCell;
00026     private NumberField startValue;
00027     private NumberField endValue;
00028     private NumberField bucket;
00029     private NumberField xMin;
00030     private NumberField xMax;
00031     private NumberField xUnit;
00032     private NumberField yMin;
00033     private NumberField yMax;
00034     private NumberField yUnit;
00035     private JRadioButton percentageButton; // radio button for "Percentage"
00036     private JRadioButton countButton;
00037     final ImageIcon histogramIcon32=new ImageIcon(getClass().getResource("/images/chart32.gif"));
00038     //final private ImageIcon histogramIcon32 = SharpTools.getImageIcon ("chart32.gif");
00039 
00045     HistoDialog(JFrame aFrame) {
00046        super(aFrame, "Histogram Options", true);
00047 
00048        owner = aFrame;
00049 
00050        // initialize all the components
00051        title = new JTextField(10);
00052        startCell = new AddressField(5);
00053        endCell = new AddressField(5);
00054        startValue = new NumberField(5);
00055        endValue = new NumberField(5);
00056        bucket = new NumberField(5, true);
00057        xMin = new NumberField(5);
00058        xMax = new NumberField(5);
00059        xUnit = new NumberField(5, true);
00060        yMin = new NumberField(5);
00061        yMax = new NumberField(5);
00062        yUnit = new NumberField(5, true);
00063 
00064        // set tooltip text
00065        startCell.setToolTipText(SharpTools.translation.getProperty("Start_cell"));
00066        endCell.setToolTipText(SharpTools.translation.getProperty("End_cell"));
00067        startValue.setToolTipText(SharpTools.translation.getProperty("Start_value"));
00068        endValue.setToolTipText(SharpTools.translation.getProperty("End_value"));
00069        xMin.setToolTipText(SharpTools.translation.getProperty("Minumum_value_on_X_axis"));
00070        xMax.setToolTipText(SharpTools.translation.getProperty("Maximum_value_on_X_axis"));
00071        xUnit.setToolTipText(SharpTools.translation.getProperty("Unit_on_X_axis"));
00072        yMin.setToolTipText(SharpTools.translation.getProperty("Minumum_value_on_Y_axis"));
00073        yMax.setToolTipText(SharpTools.translation.getProperty("Maximum_value_on_Y_axis"));
00074        yUnit.setToolTipText(SharpTools.translation.getProperty("Unit_on_Y_axis"));
00075        
00076        /*
00077         * All the following crap is laying out the component.
00078         * Nothing interesting.
00079         */
00080        
00081        JPanel east = new JPanel();
00082        east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
00083        east.setBorder(BorderFactory.createEmptyBorder(10,10,20,10));
00084 
00085        // title
00086        JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00087        JLabel label = new JLabel(SharpTools.translation.getProperty("Chart_Title")+" : ");
00088        label.setLabelFor(title);
00089        label.setDisplayedMnemonic(KeyEvent.VK_T);
00090        textPanel.add(label);
00091        textPanel.add(title);
00092        
00093        east.add(textPanel);
00094 
00095        // separator
00096        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00097        textPanel.add(new JSeparator());
00098        //     textPanel.setBorder(BorderFactory.createEtchedBorder());
00099        east.add(textPanel);
00100        
00101        // Cell Range
00102        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00103        
00104        label = new JLabel(SharpTools.translation.getProperty("Cell_Range")+" : ");
00105        label.setLabelFor(startCell);
00106        label.setDisplayedMnemonic(KeyEvent.VK_C);
00107 
00108        textPanel.add(label);
00109        textPanel.add(startCell);
00110 
00111        textPanel.add(new JLabel(":"));
00112        textPanel.add(endCell);
00113 
00114        east.add(textPanel);
00115 
00116        // Value Range
00117        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00118 
00119        label = new JLabel(SharpTools.translation.getProperty("Value_Range")+" : ");
00120        label.setLabelFor(startValue);
00121        label.setDisplayedMnemonic(KeyEvent.VK_V);
00122 
00123        textPanel.add(label);
00124        textPanel.add(startValue);
00125 
00126        label = new JLabel(" "+SharpTools.translation.getProperty("to")+" ");
00127        label.setLabelFor(endValue);
00128        textPanel.add(label);
00129        textPanel.add(endValue);
00130 
00131        east.add(textPanel);
00132 
00133        // Bucket Size
00134        label = new JLabel("   "+SharpTools.translation.getProperty("Bucket")+"  ");
00135        label.setLabelFor(bucket);
00136        label.setDisplayedMnemonic(KeyEvent.VK_B);
00137        textPanel.add(label);
00138        textPanel.add(bucket);
00139        
00140        east.add(textPanel);
00141 
00142        // separator
00143        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00144        textPanel.add(new JSeparator());
00145        //     textPanel.setBorder(BorderFactory.createEtchedBorder());
00146        east.add(textPanel);
00147        
00148        // X Scale
00149        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00150        
00151 
00152        label = new JLabel(SharpTools.translation.getProperty("X_Scale")+" : ");
00153        label.setLabelFor(xMin);
00154        label.setDisplayedMnemonic(KeyEvent.VK_X);
00155 
00156        textPanel.add(label);
00157        textPanel.add(xMin);
00158 
00159        label = new JLabel(" "+SharpTools.translation.getProperty("to")+" ");
00160        label.setLabelFor(xMax);
00161        textPanel.add(label);
00162        textPanel.add(xMax);
00163 
00164        label = new JLabel(" "+SharpTools.translation.getProperty("Unit")+" ");
00165        label.setLabelFor(xUnit);
00166        label.setDisplayedMnemonic(KeyEvent.VK_U);
00167        textPanel.add(label);
00168        textPanel.add(xUnit);
00169 
00170        east.add(textPanel);
00171 
00172        // Y Scale
00173        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00174 
00175        label = new JLabel(SharpTools.translation.getProperty("Y_Scale")+" : ");
00176        label.setLabelFor(yMin);
00177        label.setDisplayedMnemonic(KeyEvent.VK_Y);
00178 
00179        textPanel.add(label);
00180        textPanel.add(yMin);
00181 
00182        label = new JLabel(" "+SharpTools.translation.getProperty("to")+" ");
00183        label.setLabelFor(yMax);
00184        textPanel.add(label);
00185        textPanel.add(yMax);
00186 
00187        label = new JLabel(" "+SharpTools.translation.getProperty("Unit")+" ");
00188        label.setLabelFor(yUnit);
00189        label.setDisplayedMnemonic(KeyEvent.VK_N);
00190        textPanel.add(label);
00191        textPanel.add(yUnit);
00192        
00193        east.add(textPanel);
00194 
00195        // separator
00196        textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00197        textPanel.add(new JSeparator());
00198        east.add(textPanel);
00199 
00200        // Radio buttons     
00201        textPanel = new JPanel();
00202        textPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
00203 
00204        // add radio buttons
00205        ButtonGroup metric = new ButtonGroup();
00206        percentageButton = new JRadioButton(SharpTools.translation.getProperty("percentage"));
00207        percentageButton.setMnemonic(KeyEvent.VK_P);
00208        //     radio.setActionCommand("Count");
00209        metric.add(percentageButton);
00210        
00211        label = new JLabel(SharpTools.translation.getProperty("Display_Y_by")+" ");
00212        textPanel.add(label);
00213        
00214        textPanel.add(percentageButton);
00215        //     percentageButton.setSelected(true);
00216 
00217        countButton = new JRadioButton(SharpTools.translation.getProperty("Count"));
00218        countButton.setMnemonic(KeyEvent.VK_O);
00219        
00220        metric.add(countButton);
00221        textPanel.add(countButton);
00222        
00223        east.add(textPanel);
00224 
00225        
00226        setOptionPane(east,
00227                     JOptionPane.PLAIN_MESSAGE,
00228                     JOptionPane.OK_CANCEL_OPTION,
00229                     histogramIcon32);
00230 
00231        // this is OK; onClose() will not be called
00232        // but choice's default value is CLOSED_OPTION
00233        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
00234 
00235        startCell.requestFocus();   
00236        
00237     }
00238 
00239     // strings defined here
00240     final static private String bucketstring = SharpTools.translation.getProperty("Bucket_Size");
00241     final static private String startvaluestring = SharpTools.translation.getProperty("Start_Value");
00242     final static private String endvaluestring = SharpTools.translation.getProperty("End_Value");
00243     final static private String xunitstring = SharpTools.translation.getProperty("X_Unit");
00244     final static private String yunitstring = SharpTools.translation.getProperty("Y_Unit");
00245     final static private String xminstring = SharpTools.translation.getProperty("X_Start");
00246     final static private String xmaxstring = SharpTools.translation.getProperty("X_End");
00247     final static private String yminstring = SharpTools.translation.getProperty("Y_Start");
00248     final static private String ymaxstring = SharpTools.translation.getProperty("Y_End");
00249 
00255     private boolean validateInput() {
00256        /*
00257         * get values from text fields and check the validity,
00258         * give friendly error report
00259         */
00260        if (!addressOK(startCell, SharpTools.translation.getProperty("Start_Cell")))
00261            return false;
00262 
00263        if (!addressOK(endCell, SharpTools.translation.getProperty("End_Cell")))
00264            return false;
00265        
00266        if (!orderOK(startValue, endValue, startvaluestring, endvaluestring))
00267            return false;
00268        
00269        if (!orderOK(xMin, xMax, xminstring, xmaxstring))
00270            return false;
00271        
00272        if (!orderOK(yMin, yMax, yminstring, ymaxstring))
00273            return false;
00274        
00275        if (isNegative(bucket, bucketstring))
00276            return false;
00277 
00278        if (isNegative(xUnit, xunitstring))
00279            return false;
00280 
00281        if (isNegative(yUnit, yunitstring))
00282            return false;
00283 
00284        return true;
00285     }
00286 
00294     private boolean isNegative(NumberField field, String name) {
00295        Float f = field.getNumber();
00296        if (f == null || f.floatValue() <= 0) {
00297            invalidInput(name);
00298            field.selectAll();
00299            field.requestFocus();
00300            return true;
00301        }
00302        return false;
00303     }
00304     
00314     private boolean orderOK(NumberField start, NumberField end,
00315                          String startname, String endname) {
00316        // calculate
00317        Float f1 = start.getNumber();
00318 
00319        if (f1 == null) {
00320            invalidInput(startname);
00321            start.selectAll();
00322            start.requestFocus();
00323            return false;
00324        }
00325        
00326        Float f2 = end.getNumber();
00327 
00328        if (f2 == null) {
00329            invalidInput(endname);
00330            end.selectAll();
00331            end.requestFocus();
00332            return false;
00333        }
00334        
00335        if (f1.floatValue() > f2.floatValue()) {
00336            // exchange          
00337            start.setNumber(f2);
00338            end.setNumber(f1);
00339        }
00340 
00341        return true;
00342     }
00343 
00351     private boolean addressOK(AddressField field, String name) {
00352        // calculate
00353        CellPoint addr = field.getAddress();
00354 
00355        if (addr == null) {
00356            invalidInput(name);
00357            field.selectAll();
00358            field.requestFocus();
00359            return false;
00360        }
00361        
00362        return true;
00363     }
00364 
00365     /*
00366      * Set the Title textfield
00367      * @param s the title
00368      */
00369     public void setTitleField(String s) {
00370        title.setText(s);
00371        setTitle(SharpTools.translation.getProperty("Options")+"-"+s);
00372     }
00373 
00379     public String getTitleField() {
00380        return title.getText();
00381     }
00382 
00388     public void setCellRange(CellRange range) {
00389        if (range != null) {
00390            startCell.setAddress(range.getminCorner());
00391            endCell.setAddress(range.getmaxCorner());
00392        }
00393     }
00394 
00400     public CellRange getCellRange() {
00401        CellPoint point1 = startCell.getAddress();
00402        CellPoint point2 = endCell.getAddress();
00403        if (point1 == null || point2 == null)
00404            return null;
00405        else {
00406            // correct problems like "A2:B1".  Always convert to "A1:B2"
00407            int minrow = Math.min(point1.getRow(), point2.getRow());
00408            int maxrow = Math.max(point1.getRow(), point2.getRow());
00409            int mincol = Math.min(point1.getCol(), point2.getCol());
00410            int maxcol = Math.max(point1.getCol(), point2.getCol());
00411            
00412            return new CellRange(minrow, maxrow, mincol, maxcol);
00413        }
00414     }
00415 
00416     // many get/set functions
00417     public CellPoint getStartCell() { return startCell.getAddress(); }
00418     public void setStartCell(CellPoint addr) { startCell.setAddress(addr); }
00419     
00420     public CellPoint getEndCell() { return endCell.getAddress(); }    
00421     public void setEndCell(CellPoint addr) { endCell.setAddress(addr); }
00422 
00423     public Float getStartValue() { return startValue.getNumber(); }
00424     public void setStartValue(Float f) { startValue.setNumber(f); }
00425     
00426     public Float getEndValue() { return endValue.getNumber(); }
00427     public void setEndValue(Float f) { endValue.setNumber(f); }
00428 
00429     public Float getBucket() { return bucket.getNumber(); }
00430     public void setBucket(Float f) { bucket.setNumber(f); }
00431 
00432     public Float getXMin() { return xMin.getNumber(); }
00433     public void setXMin(Float f) { xMin.setNumber(f); }
00434 
00435     public Float getXMax() { return xMax.getNumber(); }
00436     public void setXMax(Float f) { xMax.setNumber(f); }
00437 
00438     public Float getXUnit() { return xUnit.getNumber(); }
00439     public void setXUnit(Float f) { xUnit.setNumber(f); }
00440 
00441     public Float getYMin() { return yMin.getNumber(); }
00442     public void setYMin(Float f) { yMin.setNumber(f); }
00443 
00444     public Float getYMax() { return yMax.getNumber(); }
00445     public void setYMax(Float f) { yMax.setNumber(f); }
00446 
00447     public Float getYUnit() { return yUnit.getNumber(); }
00448     public void setYUnit(Float f) { yUnit.setNumber(f); }
00449 
00450     public boolean getByPercentage() { return percentageButton.isSelected(); }
00451     public void setByPercentage(boolean set) {
00452        percentageButton.setSelected(set);
00453        countButton.setSelected(!set);
00454     }
00455 
00461     private void invalidInput(String s){
00462        SharpOptionPane.showMessageDialog(owner,
00463                                      SharpTools.translation.getProperty("Sorry"),
00464                                      SharpTools.translation.getProperty("Histogram"),
00465                                      JOptionPane.ERROR_MESSAGE, null);
00466     }
00467 
00468     protected boolean onOK() {
00469        // validate the input here
00470        return validateInput();
00471     }
00472 }
00473