001    package sharpster.client.gui;
002    
003    /**
004     * <p>Title: </p>
005     * <p>Description: </p>
006     * <p>Copyright: Copyright (c) 2003</p>
007     * <p>Company: Kafka</p>
008     * @author Tommy Eriksson
009     * @version 1.0
010     */
011    
012    import java.awt.*;
013    import java.awt.event.*;
014    import javax.swing.*;
015    import javax.swing.tree.*;
016    import javax.swing.event.*;
017    import java.io.File;
018    
019    import java.io.FileInputStream;
020    import java.util.Enumeration;
021    
022    import net.jxta.document.*;
023    
024    public class ShareFileFrame extends JDialog {
025      JPanel contentPane;
026      JButton jButtonShare = new JButton();
027      JButton jButtonCancel = new JButton();
028      JScrollPane jScrollPane1 = new JScrollPane();
029    
030      TreeFile treefile = new TreeFile(new File(readConfigFile("config/sharpster.cfg")));
031    
032      String str = null;
033      String cvsRoot = null;
034    
035      String file = null;
036      String user = null;
037      String rights = "";
038    
039      DefaultMutableTreeNode top = new DefaultMutableTreeNode(treefile);
040    
041    
042      JTree jTree1 = new JTree(top);
043      JTextField jTextFieldShare = new JTextField();
044      JLabel jLabelShare = new JLabel();
045      JCheckBox jCheckRead = new JCheckBox();
046      JCheckBox jCheckWrite = new JCheckBox();
047      JCheckBox jCheckDelete = new JCheckBox();
048      JLabel jLabelRights = new JLabel();
049    
050    
051      //dgfhdfjtgj
052    
053      private void createNodes(DefaultMutableTreeNode top) {
054        addNode(top, treefile);
055      }
056    
057      private void addNode(DefaultMutableTreeNode node, TreeFile file) {
058        DefaultMutableTreeNode map = null;
059    
060        for (int i = 0; file.getFile().list().length > i; i++) {
061    
062          TreeFile tf = new TreeFile(file.getFile().listFiles()[i]);
063    
064          map = new DefaultMutableTreeNode(tf);
065          node.add(map);
066    
067          if (tf.getFile().isDirectory()) {
068            addNode(map, tf);
069          }
070    
071        }
072    
073      }
074    
075    
076      // Construct the frame
077      public ShareFileFrame() {
078        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
079        try {
080          jbInit();
081        }
082        catch (Exception e) {
083          e.printStackTrace();
084        }
085      }
086    
087      // Component initialization
088      private void jbInit() throws Exception {
089        contentPane = (JPanel)this.getContentPane();
090    
091        jButtonShare.setBounds(new Rectangle(58, 280, 71, 25));
092        jButtonShare.setText("Share");
093        jButtonShare.addActionListener(new ShareFileFrame_jButtonShare_actionAdapter(this));
094    
095        jButtonCancel.setBounds(new Rectangle(143, 280, 73, 25));
096        jButtonCancel.setText("Cancel");
097        jButtonCancel.addActionListener(new ShareFileFrame_jButtonCancel_actionAdapter(this));
098    
099        contentPane.setLayout(null);
100        this.setTitle("Sharpster");
101        this.setSize(new Dimension(247, 349));
102    
103        jScrollPane1.setBounds(new Rectangle(9, 8, 220, 185));
104    
105        jTextFieldShare.setText("");
106        jTextFieldShare.setBounds(new Rectangle(84, 204, 144, 24));
107    
108        jLabelShare.setText("Share file to:");
109        jLabelShare.setBounds(new Rectangle(8, 204, 79, 24));
110    
111        jCheckRead.setText("Read");
112        jCheckRead.setBounds(new Rectangle(51, 238, 59, 25));
113    
114        jCheckWrite.setText("Write");
115        jCheckWrite.setBounds(new Rectangle(109, 238, 59, 25));
116    
117        jCheckDelete.setText("Delete");
118        jCheckDelete.setBounds(new Rectangle(167, 238, 63, 25));
119    
120        jLabelRights.setText("Rights:");
121        jLabelRights.setBounds(new Rectangle(8, 242, 41, 15));
122    
123        contentPane.add(jScrollPane1, null);
124        contentPane.add(jTextFieldShare, null);
125        contentPane.add(jLabelShare, null);
126        contentPane.add(jLabelRights, null);
127        contentPane.add(jCheckRead, null);
128        contentPane.add(jCheckWrite, null);
129        contentPane.add(jCheckDelete, null);
130        contentPane.add(jButtonShare, null);
131        contentPane.add(jButtonCancel, null);
132    
133        jScrollPane1.getViewport().add(jTree1, null);
134    
135        /**   ListSelectionModel selectionModel = new SingleSelectionModel() {
136             public void updateSingleSelection(int oldIndex, int newIndex) {
137               str = (String)jList1.getModel().getElementAt(newIndex);
138               System.out.println(str);
139               File f2 = new File("C:\\cvsroot"+"\\"+str);
140               jList1.setListData(f2.list());
141               System.out.println(f2.isFile());
142             }
143           };
144         */
145        jTree1.addTreeSelectionListener(new TreeSelectionListener() {
146          public void valueChanged(TreeSelectionEvent e) {
147            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
148                jTree1.getLastSelectedPathComponent();
149    
150            if (node == null)
151              return;
152    
153            TreeFile nodeInfo = (TreeFile)node.getUserObject();
154    
155            /* React to the node selection. */
156    
157            file = nodeInfo.getFile().getPath().replaceFirst(",v","");
158    
159            System.out.println(file);
160    
161          }
162        });
163    
164        jTree1.getSelectionModel().setSelectionMode
165            (TreeSelectionModel.SINGLE_TREE_SELECTION);
166    
167    
168        createNodes(top);
169    
170        jTree1.expandRow(0);
171    
172      }
173    
174      private String readConfigFile(String file) {
175        try {
176          FileInputStream input = new FileInputStream(file);
177          StructuredDocument document = (StructuredTextDocument)
178              StructuredDocumentFactory.newStructuredDocument(new MimeMediaType(
179              "text/xml"), input);
180    
181          Enumeration parameter = document.getChildren();
182    
183          while (parameter.hasMoreElements()) {
184            Element element = (Element) parameter.nextElement();
185            String key = (String) element.getKey();
186            if (key.equals("cvs-repository")) {
187              cvsRoot = (String) element.getValue();
188            }
189          }
190          input.close();
191          return cvsRoot;
192        }
193        catch (Exception e) {
194          System.out.println("Error: could'nt read the Sharpster config.");
195          System.exit(1);
196          return null;
197        }
198      }
199    
200      void jButtonShare_actionPerformed(ActionEvent e) {
201        user = jTextFieldShare.getText();
202    
203        if (file != null) {
204          if (!user.equals("")) {
205            if (jCheckRead.isSelected()) {
206              rights = "r";
207            }
208            if (jCheckWrite.isSelected()) {
209              rights = rights + "w";
210            }
211            if (jCheckDelete.isSelected()) {
212              rights = rights + "d";
213            }
214            System.out.println(user);
215            System.out.println(file);
216            System.out.println(rights);
217          }
218          else {
219            System.out.println("Error: no user name");
220            new ErrorDialog("Error: no user name", this.getLocation());
221          }
222        }
223        else {
224          System.out.println("Error: no file");
225          new ErrorDialog("Error: no file", this.getLocation());
226        }
227        rights = "";
228      }
229    
230      void jButtonCancel_actionPerformed(ActionEvent e) {
231        this.dispose();
232      }
233    
234    
235    }
236    
237    class ShareFileFrame_jButtonShare_actionAdapter
238        implements java.awt.event.ActionListener {
239      ShareFileFrame adaptee;
240    
241      ShareFileFrame_jButtonShare_actionAdapter(ShareFileFrame adaptee) {
242        this.adaptee = adaptee;
243      }
244    
245      public void actionPerformed(ActionEvent e) {
246        adaptee.jButtonShare_actionPerformed(e);
247      }
248    }
249    
250    class ShareFileFrame_jButtonCancel_actionAdapter
251        implements java.awt.event.ActionListener {
252      ShareFileFrame adaptee;
253    
254      ShareFileFrame_jButtonCancel_actionAdapter(ShareFileFrame adaptee) {
255        this.adaptee = adaptee;
256      }
257    
258      public void actionPerformed(ActionEvent e) {
259        adaptee.jButtonCancel_actionPerformed(e);
260      }
261    }
262