/Users/lyon/j4p/src/net/scp/ScpDialog.java

1    package net.scp; 
2     
3     
4    import futils.Futil; 
5    import gui.In; 
6    import gui.dialogs.LabelledItemPanel; 
7    import gui.dialogs.ModalJDialog; 
8    import gui.run.RunButton; 
9    import gui.run.RunPasswordField; 
10   import gui.run.RunTextField; 
11    
12   import javax.swing.*; 
13   import java.beans.PropertyChangeSupport; 
14   import java.io.File; 
15    
16   /** 
17    * This class demonstrates the usage of the StandardDialog class. 
18    */ 
19   public class ScpDialog extends ModalJDialog { 
20    
21       PropertyChangeSupport pcs = new PropertyChangeSupport(this); 
22       ScpModel scpModel ; 
23    
24     
25    
26       private LabelledItemPanel c = new LabelledItemPanel(); 
27    
28       public ScpDialog() { 
29           init(); 
30       } 
31    
32       private void init() { 
33           setTitle("Scp Dialog"); 
34           scpModel = ScpModel.restore(); 
35           c.addItem("host", new RunTextField(scpModel.getHost(),20) { 
36               public void run() { 
37                   scpModel.setHost(getText()); 
38               } 
39           }); 
40           final RunTextField localFileTextField = getLocalFileTextField(); 
41           c.addItem("localFile", localFileTextField); 
42           c.addItem("setLocalFile", new RunButton("browse") { 
43               public void run() { 
44                   File f = Futil.getReadFile("select local file"); 
45                   localFileTextField.setText(f.getAbsolutePath()); 
46                   scpModel.setLocalFile(f.getAbsolutePath()); 
47    
48               } 
49           }); 
50           c.addItem("remoteFile", new RunTextField(scpModel.getRemoteFile(),20) { 
51               public void run() { 
52                   scpModel.setRemoteFile(getText()); 
53               } 
54           }); 
55           c.addItem("user", new RunTextField(scpModel.getUser(),20) { 
56               public void run() { 
57                   scpModel.setUser(getText()); 
58               } 
59           }); 
60           c.addItem("password", new RunPasswordField(scpModel.getPassword(),20) { 
61               public void run() { 
62                   scpModel.setPassword(getPasswordString()); 
63               } 
64           }); 
65           c.setBorder(BorderFactory.createEtchedBorder()); 
66    
67           setContentPane(c); 
68       } 
69    
70       private RunTextField getLocalFileTextField() { 
71           return new RunTextField( 
72                   scpModel.getLocalFile(),20) { 
73               public void run() { 
74                   scpModel.setLocalFile(getText()); 
75               } 
76           }; 
77       } 
78    
79       /** 
80        * This method gets the values of the panel entry fields. 
81        * 
82        * @return an object containing the Customer data 
83        */ 
84       public ScpModel getData() { 
85           return scpModel; 
86       } 
87       public void addScpChangeEventListener(ScpModelChangeEventListener l){ 
88           pcs.addPropertyChangeListener(l); 
89       } 
90       public void okPressed(){ 
91           if (isValidData()){ 
92               processOk(); 
93           } 
94           else 
95               debugData(); 
96       } 
97    
98       private void processOk() { 
99           System.out.println("OK process!"); 
100          scpModel.save(); 
101          final ScpModelChangeEvent evt = new ScpModelChangeEvent(this,scpModel); 
102          pcs.firePropertyChange(evt); 
103          System.out.println("uploading file"); 
104          scpModel.upload(); 
105          System.out.println("upload complete"); 
106          //super.okPressed(); 
107   
108      } 
109   
110      public void debugData() { 
111          In.message("data not valid! Model="+scpModel); 
112   
113      } 
114   
115   
116      /** 
117       * This method checks that the data entered is valid. 
118       * 
119       * @return <code>true</code> if the data is valid, otherwise 
120       *         <code>false</code> 
121       */ 
122      protected boolean isValidData() { 
123   
124          return scpModel.isValidData(); 
125      } 
126   
127      public static void main(String[] args) { 
128          ScpDialog dialog = new ScpDialog(); 
129          dialog.pack(); 
130          dialog.show(); 
131   
132          System.exit(0); 
133      } 
134  }