/Users/lyon/j4p/src/ip/gui/dialog/ArrayDialog.java

1    //-------------------------------- 
2    //File:         ArrayDialog.java 
3    //Date:         03/29/99 
4    //Student id:   0424811 
5    //Student name: Jitendra Shiralkar 
6    //Compiled on:  JDK 1.2 
7    //-------------------------------- 
8    // 
9    //Class implemented: ArrayDialog 
10   //Derived from:      Dialog 
11   // 
12   //Description: 
13   //  The ArrayDialog class accepts string values for the array elements. 
14   //The user is prompted to enter the no. of rows and columns at the system console. 
15   // 
16   package ip.gui.dialog; 
17    
18   import ip.gui.frames.ClosableFrame; 
19    
20   import java.awt.*; 
21   import java.awt.event.ActionEvent; 
22   import java.awt.event.ActionListener; 
23    
24    
25   public class ArrayDialog extends 
26           ClosableFrame implements ActionListener { 
27       private TextField fields[][]; 
28       public Button cancelButton = new Button("Cancel"); 
29       public Button setButton = new Button("Set"); 
30       private int fieldSize; 
31    
32       private static int colSpace = 5; 
33       private static int rowSpace = 10; 
34       private Panel inputPanel = new Panel(); 
35    
36       int noOfRows = 0, noOfCols = 0; 
37    
38    
39       public ArrayDialog(Frame frame, String title, 
40                          String defaults[][], int row, int col, int _fieldSize) { 
41    
42           super(title); 
43    
44           if (row == 0) { 
45               RowColLog rcl = new RowColLog(); 
46           } else { 
47               noOfRows = row; 
48               noOfCols = col; 
49           } 
50    
51           initialize(defaults, noOfRows, noOfCols, _fieldSize); 
52           pack(); 
53           show(); 
54       } 
55    
56       private void initialize(String defaults[][], int row, int col, int _fieldSize) { 
57    
58           fieldSize = _fieldSize; 
59           fields = new TextField[row][col]; 
60    
61           inputPanel.setLayout(new GridLayout(row + 1, col + 1, colSpace, rowSpace)); 
62    
63           inputPanel.add(new Label("R\\C")); 
64           for (int i = 0; i < col; i++) 
65               inputPanel.add(new Label("col " + (i + 1))); 
66    
67           for (int i = 0; i < row; i++) { 
68               inputPanel.add(new Label("row " + (i + 1))); 
69    
70    
71               for (int j = 0; j < col; j++) { 
72                   if (defaults == null) 
73                       fields[i][j] = new TextField(fieldSize); 
74                   else 
75                       fields[i][j] = new TextField(defaults[i][j], fieldSize); 
76    
77                   inputPanel.add(fields[i][j]); 
78               } 
79           } 
80           add("Center", inputPanel); 
81           buttonPanel(); 
82       } 
83    
84    
85       private void buttonPanel() { 
86           Panel p2 = new Panel(); 
87           p2.setLayout(new FlowLayout(FlowLayout.CENTER)); //RIGHT)); 
88    
89           p2.add(cancelButton); 
90           p2.add(setButton); 
91           cancelButton.addActionListener(this); 
92           setButton.addActionListener(this); 
93           add("South", p2); 
94           pack(); 
95           show(); 
96       } 
97    
98       public void printUserInput() { 
99           String userInput[][] = getUserInput(); 
100          for (int i = 0; i < fields.length; i++) { 
101              for (int j = 0; j < fields[0].length; j++) { 
102                  userInput[i][j] = fields[i][j].getText(); 
103                  System.out.print(userInput[i][j] + " "); 
104              } 
105              System.out.println(); 
106          } 
107      } 
108   
109      public String[][] getUserInput() { 
110          String userInput[][] = new String[fields.length][fields[0].length]; 
111          for (int i = 0; i < fields.length; i++) 
112              for (int j = 0; j < fields[0].length; j++) 
113                  userInput[i][j] = fields[i][j].getText(); 
114          return userInput; 
115      } 
116   
117      public static void main(String args[]) { 
118          String title = "Rotation Dialog"; 
119          int fieldSize = 6; 
120          String defaults[][] = null; 
121   
122          ArrayDialog xpol = new ArrayDialog(new Frame(), title, 
123                  defaults, 3, 3, fieldSize); 
124      } 
125   
126      public void actionPerformed(ActionEvent e) { 
127          Button b = (Button) e.getSource(); 
128          if (b == cancelButton) 
129              setVisible(false); 
130          else 
131              printUserInput(); 
132      } 
133  //} 
134   
135      class RowColLog extends Dialog implements ActionListener { 
136          Button okButton = new Button("OK"); 
137          Button cancelButton = new Button("CANCEL"); 
138   
139          Label rowLabel = new Label("Enter no. of rows:"); 
140          Label colLabel = new Label("Enter no. of columns:"); 
141          TextField rowField = new TextField(); 
142          TextField colField = new TextField(); 
143   
144   
145          public RowColLog() { 
146              super(new Frame(), "Rows and columns input", true); //sets the RowColLog modal 
147   
148              okButton.addActionListener(this); 
149              cancelButton.addActionListener(this); 
150   
151              setLayout(new GridLayout(0, 2)); 
152              add(rowLabel); 
153              add(rowField); 
154              add(colLabel); 
155              add(colField); 
156              add(okButton); 
157              add(cancelButton); 
158   
159              pack(); 
160              show(); 
161          } 
162   
163          public void actionPerformed(ActionEvent e) { 
164              if (e.getSource() == okButton) { 
165                  try { 
166                      noOfRows = Integer.parseInt(rowField.getText()); 
167                      noOfCols = noOfRows = Integer.parseInt(colField.getText()); 
168                      setVisible(false); 
169                  } catch (NumberFormatException ne) { 
170                      StringBuffer message = new StringBuffer(); 
171                      message.append("Please enter a valid integer at: "); 
172                      message.append(ne.getMessage()); 
173   
174                      MessageLog m1 = new MessageLog("Input error", message.toString()); 
175                  } 
176  //          setVisible(false); 
177              } else if (e.getSource() == cancelButton) { 
178                  System.exit(1); 
179              } 
180          } 
181      } 
182   
183      class MessageLog extends Dialog implements ActionListener { 
184          Button okButton = new Button("OK"); 
185          Panel buttonPanel = new Panel(); 
186          Label messageLabel = new Label(); 
187   
188          public MessageLog(String title, String message) { 
189              super(new Frame(), title, true);    //sets the MessageLog modal 
190              okButton.addActionListener(this); 
191              messageLabel.setText(message); 
192   
193              setLayout(new GridLayout(0, 1)); 
194              add(messageLabel); 
195              add(okButton); 
196              pack(); 
197              show(); 
198          } 
199   
200          public void actionPerformed(ActionEvent e) { 
201              if (e.getSource() == okButton) 
202                  setVisible(false); 
203          } 
204      } 
205  } 
206