Guest User

Untitled

a guest
Jul 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Properties;
  7.  
  8. import java.util.List;
  9. import java.util.ArrayList;
  10. import java.util.ListIterator;
  11.  
  12. import javax.swing.*;
  13.  
  14. import com.serena.dmclient.api.LoginFailedException;
  15. import com.serena.dmclient.api.Request;
  16. import java.util.Properties;
  17.  
  18. public class MainFrame {
  19.    
  20.     /*
  21.      * Nytt
  22.      */
  23.    
  24.     String username, password,  product, project;
  25.     static boolean loggedIn = false;
  26.     Dimensions di = new Dimensions();
  27.     Properties properties = new Properties();
  28.     protected MainFrame null;
  29.    
  30.     MainFrame() {
  31.         try {
  32.             properties.load(new FileInputStream("userdata.properties"));
  33.         } catch(IOException ex) {}
  34.     }
  35.    
  36.    
  37.     private static void createAndShowGUI(final MainFrame mf) {
  38.        
  39.         final Session session = new Session();
  40.        
  41.         JFrame frame = new JFrame("Dimensions helper");
  42.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.         frame.setLocationRelativeTo(null);
  44.  
  45.         final JPanel contentPane = new JPanel();
  46.         contentPane.setLayout(new CardLayout(5, 5));
  47.              
  48.        
  49.         Win1 win1 = new Win1();
  50.         contentPane.add(win1);
  51.         Win2 win2 = new Win2();
  52.         contentPane.add(win2);
  53.         Win3 win3 = new Win3();
  54.         contentPane.add(win3);
  55.        
  56.         /*
  57.          * Laddar tidigare data från properties-filen
  58.          */
  59.        
  60.         win1.setUsername(session.properties.getProperty("username", ""));
  61.         win2.setProduct(session.properties.getProperty("product", ""));
  62.         win2.setDesignpart(session.properties.getProperty("designpart", ""));
  63.  
  64.         JPanel buttonPanel = new JPanel();
  65.         final JButton previousButton = new JButton("< PREVIOUS");
  66.         previousButton.setEnabled(false);
  67.         final JButton nextButton = new JButton("NEXT >");
  68.         final JButton cancelButton = new JButton("CANCEL");
  69.         buttonPanel.add(cancelButton);
  70.         buttonPanel.add(previousButton);
  71.         buttonPanel.add(nextButton);        
  72.                
  73.         nextButton.addActionListener(new ActionListener() {
  74.             public void actionPerformed(ActionEvent ae) {
  75.                 nextButton.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  76.                 Component[] contents = contentPane.getComponents();
  77.                 for(Component component : contents) {
  78.                     if(component.isVisible() && component instanceof Interface1) {
  79.                         Interface1 window = (Interface1) component;
  80.                         String username = window.getUsername();
  81.                         String password = window.getPassword();
  82.                         //boolean login = session.login(username, password);
  83.                         //loggedIn = mf.login(username, password);
  84.                         System.out.print(mf.login(username, password));
  85.                        
  86.                         if(loggedIn) {
  87.                             nextButton.setCursor(Cursor.getDefaultCursor());
  88.                             CardLayout cardLayout = (CardLayout) contentPane.getLayout();
  89.                             cardLayout.next(contentPane);
  90.                             previousButton.setEnabled(true);
  91.                            
  92.                         } else {
  93.                             JOptionPane.showMessageDialog(component, "Wrong username and/or password",
  94.                                     "Error", JOptionPane.ERROR_MESSAGE);
  95.                         }
  96.                         break;
  97.                        
  98.                     } else if(component.isVisible() && component instanceof Interface2) {
  99.                         Interface2 window = (Interface2) component;
  100.                         String items = window.getItems();
  101.                         String product = window.getProduct();
  102.                         String designpart = window.getDesignpart();
  103.                                                
  104.                         //List<Item> items2 = session.test(items, product, designpart);
  105.                         List<Item> itemList = session.test(testitem, testproduct, testproj);
  106.                         List<Item> finalItemList = new ArrayList<Item>();
  107.                         List<Request> relatedRequests = new ArrayList<Request>();
  108.                        
  109.                         finalItemList.addAll(itemList);
  110.                         for(int i=0; i<itemList.size(); i++) {
  111.                             finalItemList.addAll(itemList.get(i).getPreviousRevisions());
  112.                         }
  113.                        
  114.                         for(int j=0; j<finalItemList.size(); j++) {
  115.                             relatedRequests.addAll(finalItemList.get(j).getRelatedRequests());
  116.                             System.out.print(finalItemList.get(j).status + "\n");
  117.                         }
  118.                        
  119.                        
  120.                     }
  121.                     nextButton.setCursor(Cursor.getDefaultCursor());
  122.                     //break;
  123.                 }
  124.             }
  125.         });
  126.        
  127.  
  128.         cancelButton.addActionListener(new ActionListener() {
  129.             public void actionPerformed(ActionEvent ae) {
  130.                 session.terminateConnection();
  131.                 System.exit(0);
  132.             }
  133.            
  134.         });
  135.  
  136.         frame.add(contentPane);
  137.         frame.add(buttonPanel, BorderLayout.PAGE_END);
  138.         frame.setSize(400, 400);
  139.         frame.setVisible(true);
  140.     }
  141.    
  142.    
  143.     public boolean login(String username, String password) {
  144.         this.username = username;
  145.         this.password = password;
  146.                
  147.         try {
  148.             di.getDimensionsConnection(username2, password2");
  149.             loggedIn = true;
  150.             try {
  151.                 properties.setProperty("username", this.username);
  152.                 properties.store(new FileOutputStream("userdata.properties"), null);
  153.             } catch(Exception e) {}
  154.         } catch(LoginFailedException e) {
  155.             System.out.print(e);
  156.             loggedIn = false;
  157.         }
  158.         return loggedIn;
  159.    }
  160.    
  161.    public boolean isLoggedOn() {
  162.         return loggedIn;
  163.    }
  164.    
  165.    public List<Item> test(String input, String product, String project) {
  166.         String[] itemsArr = input.split(",");
  167.         String newItemsArr[] = new String[itemsArr.length];
  168.        
  169.         for(int i=0; i<itemsArr.length; i++) {
  170.             newItemsArr[i] = itemsArr[i].replaceAll("\"", "");
  171.         }
  172.         return di.getItems2(newItemsArr, product, project);
  173.     }
  174.  
  175.     public static void main(String[] args) {
  176.         SwingUtilities.invokeLater(new Runnable() {
  177.             public void run() {
  178.                 createAndShowGUI();
  179.             }
  180.         });
  181.     }
  182. }
Add Comment
Please, Sign In to add comment