Back to index

wims  3.65+svn20090927
SharpOptionPane.java
Go to the documentation of this file.
00001 
00011 package SharpTools;
00012 import javax.swing.*;
00013 import javax.swing.table.*;
00014 import javax.swing.border.*;
00015 import javax.swing.event.*;
00016 import java.util.*;
00017 import java.io.*;
00018 import java.awt.*;
00019 import java.awt.event.*;
00020 
00021 public class SharpOptionPane {
00022 
00033     public static Object showInputDialog(Component parentComponent,
00034                                     Object message,
00035                                     String title,
00036                                     int messageType,
00037                                     Icon icon,
00038                                     Object[] selectionValues,
00039                                     Object initialSelectionValue) {
00040        return showInputDialog(parentComponent, message, title, messageType,
00041                             icon, selectionValues, initialSelectionValue,
00042                             false);
00043     }
00044 
00054     public static Integer showIntegerInputDialog(Component parentComponent,
00055                                             Object message,
00056                                             String title,
00057                                             int messageType,
00058                                             Icon icon,
00059                                             Object initialSelectionValue) {
00060        Object input = showInputDialog(parentComponent, message, title,messageType, icon, null,initialSelectionValue, true);
00061        try {
00062            if (input != null)
00063               return Integer.getInteger(input.toString());
00064        }
00065        catch (Exception e) {
00066            System.out.println("Error\n"+e);
00067        }
00068        return null;
00069     }
00070     
00084     private static Object showInputDialog(Component parentComponent,
00085                                      Object message,
00086                                      String title,
00087                                      int messageType,
00088                                      Icon icon,
00089                                      Object[] selectionValues,
00090                                      Object initialSelectionValue,
00091                                      boolean isInteger) {
00092        JPanel panel = new JPanel(new BorderLayout());
00093        JComponent com;
00094        int choice;
00095 
00096        if (selectionValues == null) {
00097            // we use a JTextField
00098            if (isInteger) {
00099               com = new NumberField(true, true);
00100               ((JTextField)com).setText(initialSelectionValue.toString());
00101            }
00102            else
00103               com = new JTextField(initialSelectionValue.toString());
00104 
00105            ((JTextField)com).selectAll();
00106        }
00107        else {
00108            JComboBox box = new JComboBox(selectionValues);
00109            if (initialSelectionValue != null)
00110               box.setSelectedItem(initialSelectionValue);
00111            else
00112               box.setSelectedIndex(0);
00113 
00114            com = box;
00115        
00116        }
00117 
00118        if (message instanceof Component)
00119            panel.add((Component)message, BorderLayout.NORTH);
00120        else
00121            panel.add(new Label(message.toString()), BorderLayout.NORTH);
00122        
00123        panel.add(com, BorderLayout.CENTER);
00124        
00125        choice = showOptionDialog(parentComponent, panel, title,
00126                               JOptionPane.OK_CANCEL_OPTION,
00127                               messageType, icon);
00128 
00129        if (choice == JOptionPane.OK_OPTION) {
00130            if (com instanceof JTextField)
00131               return ((JTextField)com).getText();
00132            else
00133               return ((JComboBox)com).getSelectedItem();
00134        }
00135        else
00136            return null;
00137     }
00138           
00143     public static void showMessageDialog(Component parentComponent,
00144                                     Object message) {
00145        showMessageDialog(parentComponent, message, null, 0, null);
00146     }
00147     
00155     public static void showMessageDialog(Component parentComponent,
00156                                     Object message,
00157                                     String title,
00158                                     int messageType) {
00159        showMessageDialog(parentComponent, message, title, messageType, null);
00160     }
00161     
00169     public static void showMessageDialog(Component parentComponent,
00170                                     Object message,
00171                                     String title,
00172                                     int messageType,
00173                                     Icon icon) { 
00174        showOptionDialog(parentComponent, message, title,
00175                       JOptionPane.DEFAULT_OPTION,
00176                       messageType, icon, 0);
00177     }      
00178 
00188     public static int showOptionDialog(Component parentComponent,
00189                                    Object message,
00190                                    String title,
00191                                    int optionType,
00192                                    int messageType,
00193                                    Icon icon) {
00194        return showOptionDialog(parentComponent, message, title, optionType,
00195                             messageType, icon, 0);
00196     }
00197     
00210     public static int showOptionDialog(Component parentComponent,
00211                                    Object message,
00212                                    String title,
00213                                    int optionType,
00214                                    int messageType,
00215                                    Icon icon,                         
00216                                    int defaultIndex) {
00217 
00218        SharpDialog dialog;
00219 
00220        if (parentComponent instanceof Frame)
00221            dialog = new SharpDialog((Frame)parentComponent, title, true);
00222        else
00223            dialog = new SharpDialog((Dialog)parentComponent, title, true);
00224            
00225        dialog.setOptionPane(message, messageType,
00226                           optionType, icon,
00227                           defaultIndex);
00228        
00229        dialog.show();
00230        return dialog.getChoice();
00231     }
00232     
00233 }    
00234