/Users/lyon/j4p/src/ip/hak/InputDialog.java

1    package ip.hak; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6     
7    public class InputDialog extends ClosableDialog implements ActionListener { 
8        Label lb1 = new Label("Source Dir"); 
9        Label lb2 = new Label("Destination Dir"); 
10       Label lb3 = new Label("Input Name"); 
11       Label lb4 = new Label("Output Name"); 
12       Label lb5 = new Label("Search String"); 
13       Label lb6 = new Label("Replace String"); 
14       TextField tf1 = new TextField(); 
15       TextField tf2 = new TextField(); 
16       Choice ch = new Choice(); 
17       TextField tf4 = new TextField(); 
18       TextField tf5 = new TextField(); 
19       TextField tf6 = new TextField(); 
20       Button okButton = new Button("OK"); 
21       Button canButton = new Button("Cancel"); 
22       String[] s; 
23       String sd, dd; 
24       String result[]; 
25    
26       public InputDialog(Frame fr, String title, boolean md, String[] st, String sdi, String ddi) { 
27           super(fr, title, md); 
28           s = st; 
29           sd = sdi; 
30           dd = ddi; 
31           init(); 
32           setVisible(true); 
33       } 
34    
35       public void init() { 
36           int x = 20; 
37           int y = 30; 
38    
39           setSize(320, 250); 
40           setLayout(null); 
41           lb1.setSize(100, 20); 
42           lb1.setLocation(x, y); 
43           add(lb1); 
44    
45           lb2.setSize(100, 20); 
46           lb2.setLocation(x, y += 30); 
47           add(lb2); 
48    
49           lb3.setSize(100, 20); 
50           lb3.setLocation(x, y += 30); 
51           add(lb3); 
52    
53           lb4.setSize(100, 20); 
54           lb4.setLocation(x, y += 30); 
55           add(lb4); 
56    
57           lb5.setSize(100, 20); 
58           lb5.setLocation(x, y += 30); 
59           add(lb5); 
60    
61           lb6.setSize(100, 20); 
62           lb6.setLocation(x, y += 30); 
63           add(lb6); 
64    
65           tf1.setText(sd); 
66           tf1.setEditable(false); 
67           tf1.setSize(160, 20); 
68           tf1.setLocation(x += 120, y = 30); 
69           add(tf1); 
70    
71           tf2.setText(dd); 
72           tf2.setEditable(false); 
73           tf2.setSize(160, 20); 
74           tf2.setLocation(x, y += 30); 
75           add(tf2); 
76    
77           ch.setSize(160, 20); 
78           ch.setLocation(x, y += 30); 
79           ch.add("Just copy, No replace"); // if you select this item, all files are copyed to destination directory 
80           for (int i = 0; i < s.length; i++) 
81               ch.add(s[i]); 
82           add(ch); 
83    
84    
85           tf4.setSize(160, 20); 
86           tf4.setLocation(x, y += 30); 
87           add(tf4); 
88    
89           tf5.setSize(160, 20); 
90           tf5.setLocation(x, y += 30); 
91           add(tf5); 
92    
93           tf6.setSize(160, 20); 
94           tf6.setLocation(x, y += 30); 
95           add(tf6); 
96    
97           okButton.setSize(60, 20); 
98           okButton.setLocation(66, y += 30); 
99           add(okButton); 
100          okButton.addActionListener(this); 
101   
102          canButton.setSize(60, 20); 
103          canButton.setLocation(192, y); 
104          add(canButton); 
105          canButton.addActionListener(this); 
106      } 
107   
108      public boolean checkError() { 
109          int ind = ch.getSelectedIndex(); 
110          if (ind == 0) { 
111              result = null; 
112              return true; 
113          } 
114   
115          result = new String[4]; 
116          result[0] = ch.getSelectedItem(); 
117          result[1] = tf4.getText(); 
118          result[2] = tf5.getText(); 
119          result[3] = tf6.getText(); 
120   
121          // i<result.length-1 : because replaceString can be null... 
122          for (int i = 0; i < result.length - 1; i++) 
123              if (result[i] == null) 
124                  return false; 
125   
126          return true; 
127      } 
128   
129      public String[] getInput() { 
130          return result; 
131      } 
132   
133      public void actionPerformed(ActionEvent e) { 
134          if (e.getSource() == okButton) { 
135              if (!checkError()) { 
136                  ErrorDialog ed = new ErrorDialog(new Frame(), "Error!", "String should be not null"); 
137                  return; 
138              } 
139              dispose(); 
140          } 
141   
142          if (e.getSource() == canButton) { 
143              dispose(); 
144          } 
145      } 
146  } 
147