Guest User

Untitled

a guest
Jan 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.75 KB | None | 0 0
  1. import java.awt.*;
  2.  
  3. import java.awt.event.*;
  4.  
  5. import java.io.*;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. public class project3 extends Frame implements ActionListener, WindowListener
  12.  
  13. {   // start of class project 3
  14.  
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     File curDir;
  18.  
  19.  
  20.  
  21.     Button OKButton = new Button("OK");
  22.  
  23.     Button TargetButton = new Button("Target");
  24.  
  25.     Label TargetLabel = new Label("Select a Target Directory");
  26.  
  27.     Label sourceSelected = new Label("MAN THIS PROJECT MIGHT BE HARD");
  28.  
  29.     Label sourceLabel = new Label("Source: ");
  30.  
  31.     Label FilenameLabel = new Label("File Name: ");
  32.  
  33.     TextField Filename = new TextField(70);
  34.  
  35.     List contentsList = new List(12);
  36.  
  37.  
  38.  
  39.     boolean targ_file = false;
  40.  
  41.     boolean source_file = false;
  42.  
  43.     boolean output = false;
  44.  
  45.    
  46.  
  47.      project3(String dir)
  48.  
  49.     {
  50.  
  51.  
  52.  
  53.     /***********************************************************
  54.  
  55.     * THIS IS ALL GUI DESIGN
  56.  
  57.     ************************************************************/
  58.  
  59.     int[] column_width = { 1, 1, 1,
  60.  
  61.                    1, 1, 1,
  62.  
  63.                    1, 1 };
  64.  
  65.     int[] row_height = { 1, 1, 1,
  66.  
  67.                  1, 1, 1,
  68.  
  69.                  1, 1, 1,
  70.  
  71.                  1, 1, 1,
  72.  
  73.                  1, 1, 1,
  74.  
  75.                  1, 1, 1 };
  76.  
  77.     double[] column_weight = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
  78.  
  79.     double[] row_weight = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
  80.  
  81.     GridBagLayout gbLayout = new GridBagLayout();
  82.  
  83.     GridBagConstraints gbc = new GridBagConstraints();
  84.  
  85.    
  86.  
  87.     gbLayout.rowWeights = row_weight;
  88.  
  89.     gbLayout.rowHeights = row_height;
  90.  
  91.     gbLayout.columnWidths = column_width;
  92.  
  93.     gbLayout.columnWeights = column_weight;
  94.  
  95.  
  96.  
  97.     this.setLayout(gbLayout);
  98.  
  99.     this.setSize(640, 320);
  100.  
  101.        
  102.  
  103.     gbc.weightx = 1.0D;
  104.  
  105.     gbc.weighty = 1.0D;
  106.  
  107.     gbc.gridx = 0;
  108.  
  109.     gbc.gridy = 0;
  110.  
  111.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  112.  
  113.     gbc.fill = GridBagConstraints.BOTH;
  114.  
  115.     gbLayout.setConstraints(this.contentsList, gbc);
  116.  
  117.     add(contentsList);
  118.  
  119.  
  120.  
  121.     gbc = new GridBagConstraints();
  122.  
  123.     gbc.anchor = GridBagConstraints.EAST;
  124.  
  125.     gbLayout.setConstraints(sourceLabel, gbc);
  126.  
  127.     add(sourceLabel);
  128.  
  129.  
  130.  
  131.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  132.  
  133.     gbc.anchor = GridBagConstraints.WEST;
  134.  
  135.     gbLayout.setConstraints(sourceSelected, gbc);
  136.  
  137.     add(sourceSelected);
  138.  
  139.  
  140.  
  141.            
  142.  
  143.     gbc = new GridBagConstraints();
  144.  
  145.     gbc.anchor = GridBagConstraints.EAST;
  146.  
  147.     gbLayout.setConstraints(TargetButton, gbc);
  148.  
  149.     add(TargetButton);
  150.  
  151.  
  152.  
  153.     gbc.fill = GridBagConstraints.BOTH;
  154.  
  155.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  156.  
  157.     gbLayout.setConstraints(TargetLabel, gbc);
  158.  
  159.     add(TargetLabel);
  160.  
  161.  
  162.  
  163.     gbc = new GridBagConstraints();
  164.  
  165.     gbLayout.setConstraints(Filename, gbc);
  166.  
  167.     gbc.anchor = GridBagConstraints.EAST;
  168.  
  169.     gbLayout.setConstraints(FilenameLabel, gbc);
  170.  
  171.     gbc.fill = GridBagConstraints.BOTH;
  172.  
  173.     gbLayout.setConstraints(OKButton, gbc);
  174.  
  175.    
  176.  
  177.     add(FilenameLabel);
  178.  
  179.     add(Filename);
  180.  
  181.     add(OKButton);
  182.  
  183.  
  184.  
  185.     pack();
  186.  
  187.     setVisible(true);
  188.  
  189.  
  190.  
  191.     /****************************************************
  192.  
  193.     * END GUI DESIGN!!
  194.  
  195.     *****************************************************/
  196.  
  197.  
  198.  
  199.     addWindowListener(this);
  200.  
  201.     this.Filename.addActionListener(this);
  202.  
  203.     this.TargetButton.addActionListener(this);
  204.  
  205.     this.OKButton.addActionListener(this);
  206.  
  207.     this.contentsList.addActionListener(this);
  208.  
  209.     setFont(new Font("Monospaced", 0, 12));
  210.  
  211.     display(dir);
  212.  
  213.     } // end of Main constructor
  214.  
  215.    
  216.  
  217.  
  218.  
  219.  
  220.  
  221.     public void display(String dir)
  222.  
  223.     {
  224.  
  225.         boolean flag = false;    
  226.  
  227.    
  228.  
  229.         if(dir != null)     // if not initilization call
  230.  
  231.         {
  232.  
  233.            if(dir.equals(".."))                 //  if looking for the parent directory
  234.  
  235.            {    
  236.  
  237.                curDir = new File(curDir.getParent()); // get it
  238.  
  239.            }
  240.  
  241.            else         // otherwise
  242.  
  243.            {
  244.  
  245.                File temp = new File(curDir, dir);  // make temp file for manip of the passed string.
  246.  
  247.                
  248.  
  249.                if (temp.isDirectory())  // if the passed in string is a directory
  250.  
  251.                {
  252.  
  253.                    curDir = new File(curDir, dir);   //
  254.  
  255.                }
  256.  
  257.                
  258.  
  259.                if ((targ_file==true) && (source_file==true))  // if the sequential machiene has got the target and the source file
  260.  
  261.                {
  262.  
  263.                    FilenameLabel.setText(dir);      //  set the file name label to the passed string
  264.  
  265.                    output=true;                     //  set the output flag to true
  266.  
  267.                    flag = true;                     // set the boolean variable flag to true so we do not execute the next if
  268.  
  269.                }
  270.  
  271.                if(flag==false)  // if the above loop didn't execute get the absolute path name and make it the source lable,
  272.  
  273.                {
  274.  
  275.                     sourceLabel.setText(curDir.getAbsolutePath() + "\\" + dir);   // setting the text in the source label
  276.  
  277.                     source_file = true;    // and setting the flag
  278.  
  279.                }    
  280.  
  281.             }
  282.  
  283.         }
  284.  
  285.                
  286.  
  287.         String [] names = null;
  288.  
  289.         try {
  290.  
  291.             names = curDir.list(); // making the string array  
  292.  
  293.         }
  294.  
  295.         catch (NullPointerException e) {
  296.  
  297.         System.out.println("It's likely that it's not a directory.");
  298.  
  299.         }
  300.  
  301.        
  302.  
  303.         System.out.println("curDir list: " + names);
  304.  
  305.  
  306.  
  307.        if(names==null)
  308.  
  309.        {
  310.  
  311.         //curDir.list() says null if there is an I/O error or if it is not a directory.
  312.  
  313.                 //      says empty array if there is nothing in the array, in which case the only thing shown should be ..
  314.  
  315.            //names = new String[0];  
  316.  
  317.        }
  318.  
  319.        else // begin else A
  320.  
  321.        {
  322.  
  323.            setTitle(curDir.getAbsolutePath());  // set the title of the window to the current directory with full absolute path
  324.  
  325.            for (int i=0; i < names.length; i++)
  326.  
  327.            {
  328.  
  329.                
  330.  
  331.                 File temp = new File(names[i]);  // temp file for storage of current string for manipulation
  332.  
  333.                 if(temp.isDirectory()==false)     // if the current file isn't a directory, contune to next iteration of the for loop...
  334.  
  335.                 {
  336.  
  337.                     continue;
  338.  
  339.             //shouldnt this add to the contentsList component?
  340.  
  341.                 }
  342.  
  343.                 else        // otherwise append a + sign to the filename cause it's a directory...
  344.  
  345.                 {
  346.  
  347.                     names[i] = names[i] + " +";
  348.  
  349.             //use the list() method to determine if it is not a directory and if it is, if it has anything in it
  350.  
  351.                 }
  352.  
  353.                
  354.  
  355.             }
  356.  
  357.         }
  358.  
  359.        
  360.  
  361.    
  362.  
  363.        
  364.  
  365.        
  366.  
  367.         contentsList.removeAll(); // clear the display contentlist
  368.  
  369.        
  370.  
  371.         if(curDir.getParent()!=null)        // if current directory has a parent, add the double dots to the list
  372.  
  373.         contentsList.add("..");
  374.  
  375.        
  376.  
  377.         // output the list to the screen
  378.  
  379.        
  380.  
  381.         int count=0;
  382.  
  383.         int length=0;
  384.  
  385.        
  386.  
  387.         length=names.length;
  388.  
  389.        
  390.  
  391.         while (count<=length)
  392.  
  393.         {
  394.  
  395.             contentsList.add(names[count]);
  396.  
  397.         }
  398.  
  399.        
  400.  
  401.     } // end of display
  402.  
  403.  
  404.  
  405.     public static void main(String[] args)
  406.  
  407.     {
  408.  
  409.         if (args.length > 0) {
  410.  
  411.          new project3(args[0]);
  412.  
  413.         } else {
  414.  
  415.          new project3(null);
  416.  
  417.         }
  418.  
  419.     } // end of main
  420.  
  421.    
  422.  
  423.     public void windowClosing(WindowEvent e)
  424.  
  425.     {
  426.  
  427.     }
  428.  
  429.     public void windowClosed(WindowEvent e)
  430.  
  431.     {
  432.  
  433.     }
  434.  
  435.     public void windowOpened(WindowEvent e)
  436.  
  437.     {
  438.  
  439.     }
  440.  
  441.     public void windowActivated(WindowEvent e)
  442.  
  443.     {
  444.  
  445.     }
  446.  
  447.     public void windowDeactivated(WindowEvent e)
  448.  
  449.     {
  450.  
  451.     }
  452.  
  453.     public void windowIconified(WindowEvent e)
  454.  
  455.     {
  456.  
  457.     }
  458.  
  459.     public void windowDeiconified(WindowEvent e)
  460.  
  461.     {
  462.  
  463.     }
  464.  
  465.     public void actionPerformed(ActionEvent e)
  466.  
  467.     {
  468.  
  469.         Object o = e.getSource();
  470.  
  471.         if (o == OKButton) {
  472.  
  473.             handleOKButton();      
  474.  
  475.         }
  476.  
  477.  
  478.  
  479.         System.out.println("o: " + o);
  480.  
  481.     }
  482.  
  483.    
  484.  
  485.     void handleOKButton() {
  486.  
  487.  
  488.  
  489.     }
  490.  
  491.    
  492.  
  493. } // end of class project 3
Add Comment
Please, Sign In to add comment