Advertisement
BlueBear

xsosnak.java

Apr 11th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.86 KB | None | 0 0
  1. package forum;
  2.  
  3. import java.awt.BufferCapabilities;
  4. import java.awt.DisplayMode;
  5. import java.awt.EventQueue;
  6. import java.awt.Graphics;
  7. import java.awt.GraphicsConfiguration;
  8. import java.awt.GraphicsDevice;
  9. import java.awt.GraphicsDevice.WindowTranslucency;
  10. import java.awt.GraphicsEnvironment;
  11. import java.awt.ImageCapabilities;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.io.File;
  15.  
  16. import javax.swing.JButton;
  17. import javax.swing.JComponent;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JSeparator;
  23. import javax.swing.JTextField;
  24. import javax.swing.RepaintManager;
  25. import javax.swing.SwingConstants;
  26. import javax.swing.JPasswordField;
  27. import javax.swing.UIManager;
  28.  
  29. import users.User;
  30. import users.UserType;
  31.  
  32. import javax.swing.JComboBox;
  33.  
  34. public class ForumView {
  35.  
  36.     private JFrame frame;
  37.     private JTextField loginName;
  38.     private JSeparator separator;
  39.     private JTextField registerName;
  40.     private JButton registerButton;
  41.     private JSeparator separator_1;
  42.     private JButton loginButton;
  43.     private JButton btnNewButton_2;
  44.     private JButton passwordChange;
  45.     private JButton btnAddPicture;
  46.     private JButton logoutButton;
  47.     private JButton btnBack;
  48.     private JButton btnSortAlphabetically;
  49.     private JButton btnNewButton_5;
  50.     private JLabel userPhoto;
  51.     private JPasswordField loginPasswd;
  52.     private JPasswordField registerPasswd;
  53.     private UserList users = new UserList();
  54.     private User activeUser = null;
  55.     private JLabel userNameLabel;
  56.     private JLabel userTypeLabel;
  57.     private JComboBox userTypeComboBox;
  58.     private JTextField textField;
  59.  
  60.     /**
  61.      * Launch the application.
  62.      */
  63.     public static void main(String[] args) {
  64.         EventQueue.invokeLater(new Runnable() {
  65.             public void run() {
  66.                 try {
  67. //                  GraphicsDevice[] graphicsDevices = getScreenGraphicsDevices();
  68. //                  printInfoAboutEachGraphicsDevice(graphicsDevices);
  69.                     final ForumView window = new ForumView();
  70.                    
  71.                     window.frame.setVisible(true);
  72.                     window.frame.createBufferStrategy(2);
  73.                    
  74.                     // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  75.                     /*UIManager.setLookAndFeel(UIManager
  76.                             .getSystemLookAndFeelClassName());*/
  77.                     new Thread(new Runnable() {
  78.                         @Override
  79.                         public void run() {
  80.                             while (true) {
  81.                                 window.frame.repaint();
  82.                                 try {
  83.                                     Thread.sleep(1);
  84.                                 } catch (InterruptedException e) {
  85.                                 }
  86.                             }
  87.                         }
  88.                     }).start();
  89.                 } catch (Exception e) {
  90.                     e.printStackTrace();
  91.                 }
  92.             }
  93.         });
  94.     }
  95.  
  96. //   /**
  97. //     * Ziska pole se vsemi dostupnymi grafickymi zarizenimi.
  98. //     *
  99. //     * @return pole se vsemi dostupnymi grafickymi zarizenimi
  100. //     */
  101. //    private static GraphicsDevice[] getScreenGraphicsDevices() {
  102. //        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  103. //        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
  104. //        return graphicsDevices;
  105. //    }
  106. //
  107. //    /**
  108. //     * Vypise informaci o vsech grafickych zarizenich predanych v parametru
  109. //     * graphicsDevices.
  110. //     *
  111. //     * @param graphicsDevices
  112. //     *            pole se vsemi dostupnymi grafickymi zarizenimi
  113. //     */
  114. //    private static void printInfoAboutEachGraphicsDevice(GraphicsDevice[] graphicsDevices) {
  115. //        for (int j = 0; j < graphicsDevices.length; j++) {
  116. //           GraphicsDevice graphicsDevice = graphicsDevices[j];
  117. //           printGraphicsDeviceInfo(graphicsDevice);
  118. //        }
  119. //    }
  120. //
  121. //    /**
  122. //     * Vypise informaci o grafickem zarizeni predanem v parametru
  123. //     * graphicsDevice.
  124. //     *
  125. //     * @param graphicsDevice
  126. //     *            objekt reprezentujici dostupne graficke zarizeni.
  127. //     */
  128. //    private static void printGraphicsDeviceInfo(GraphicsDevice graphicsDevice) {
  129. //        System.out.println("Device ID: " + graphicsDevice.getIDstring());
  130. //        System.out.println("Type:      " + getDeviceType(graphicsDevice));
  131. //        System.out.println("Accelerated memory size:  " + graphicsDevice.getAvailableAcceleratedMemory());
  132. //        System.out.println("Display change supported: " + graphicsDevice.isDisplayChangeSupported());
  133. //        System.out.println("Full screen supported:    " + graphicsDevice.isFullScreenSupported());
  134. //        printTransparencyInfo(graphicsDevice);
  135. //        printBuffersConfiguration(graphicsDevice);
  136. //        System.out.println("Graphics modes:");
  137. //        DisplayMode[] modes = graphicsDevice.getDisplayModes();
  138. //        printAvailableDisplayModes(modes);
  139. //        System.out.println();
  140. //    }
  141. //
  142. //    /**
  143. //     * Vypis zda zarizeni podporuje pruhlednost a/nebo prusvitnost.
  144. //     *
  145. //     * @param graphicsDevice
  146. //     *            objekt reprezentujici dostupne graficke zarizeni.
  147. //     */
  148. //    private static void printTransparencyInfo(GraphicsDevice graphicsDevice) {
  149. //        System.out.println("Transparency/translucency support:");
  150. //        System.out.println("    Per pixel transparency:  " + graphicsDevice.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT));
  151. //        System.out.println("    Per pixel translucency:  " + graphicsDevice.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT));
  152. //        System.out.println("    Per window translucency: " + graphicsDevice.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT));
  153. //    }
  154. //
  155. //    /**
  156. //     * Vypis informace o bufferech (prednim a zadnim bufferu atd.).
  157. //     *
  158. //     * @param graphicsDevice
  159. //     *            objekt reprezentujici dostupne graficke zarizeni.
  160. //     */
  161. //    private static void printBuffersConfiguration(GraphicsDevice graphicsDevice) {
  162. //        // ziskat vsechny dostupne graficke konfigurace
  163. //        GraphicsConfiguration[] graphicsConfigurations = graphicsDevice.getConfigurations();
  164. //        for (GraphicsConfiguration graphicsConfiguration : graphicsConfigurations) {
  165. //            System.out.println("Graphics configuration: " + graphicsConfiguration.toString());
  166. //            BufferCapabilities bufferCapabilities = graphicsConfiguration.getBufferCapabilities();
  167. //            System.out.println("    Buffers configuration:");
  168. //            System.out.println("        Multi buffer available:   " + bufferCapabilities.isMultiBufferAvailable());
  169. //            System.out.println("        Page flipping supported:  " + bufferCapabilities.isPageFlipping());
  170. //            System.out.println("        Full screen required:     " + bufferCapabilities.isFullScreenRequired());
  171. //            // moznosti predniho bufferu
  172. //            ImageCapabilities frontBuffer = bufferCapabilities.getFrontBufferCapabilities();
  173. //            // moznosti zadniho bufferu
  174. //            ImageCapabilities backBuffer = bufferCapabilities.getBackBufferCapabilities();
  175. //            System.out.println("        Front buffer accelerated: " + frontBuffer.isAccelerated());
  176. //            System.out.println("        Front buffer volatile:    " + frontBuffer.isTrueVolatile());
  177. //            System.out.println("        Back buffer accelerated:  " + backBuffer.isAccelerated());
  178. //            System.out.println("        Back buffer volatile:     " + backBuffer.isTrueVolatile());
  179. //        }
  180. //    }
  181. //
  182. //    /**
  183. //     * Vrati typ grafickeho zarizeni.
  184. //     *
  185. //     * @param graphicsDevice
  186. //     *            objekt reprezentujici dostupne graficke zarizeni.
  187. //     * @return typ zarizeni (textova reprezentace)
  188. //     */
  189. //    private static String getDeviceType(GraphicsDevice graphicsDevice) {
  190. //        switch (graphicsDevice.getType()) {
  191. //        case GraphicsDevice.TYPE_RASTER_SCREEN:
  192. //            return "raster screen";
  193. //        case GraphicsDevice.TYPE_PRINTER:
  194. //            return "printer";
  195. //        case GraphicsDevice.TYPE_IMAGE_BUFFER:
  196. //            return "image buffer";
  197. //        default: // nemelo by nastat
  198. //            return "???";
  199. //        }
  200. //    }
  201. //
  202. //    /**
  203. //     * Vypise dostupne graficke rezimy a jejich zakladni vlastnosti.
  204. //     *
  205. //     * @param modes pole s grafickymi rezimy pro dane zarizeni.
  206. //     */
  207. //    private static void printAvailableDisplayModes(DisplayMode[] modes) {
  208. //        for (DisplayMode mode : modes) {
  209. //            final int width = mode.getWidth();
  210. //            final int height = mode. getHeight();
  211. //            final int bpp = mode.getBitDepth();
  212. //            final int refreshRate = mode.getRefreshRate();
  213. //            System.out.println("    " +
  214. //                    width + "x" + height +
  215. //                    "@" + bpp + " bpp\t(" + refreshRate + " Hz)");
  216. //        }
  217. //    }
  218.    
  219.     /**
  220.      * Create the application.
  221.      */
  222.     public ForumView() {
  223.         initialize();
  224.     }
  225.  
  226.     /**
  227.      * Initialize the contents of the frame.
  228.      */
  229.     private void initialize() {
  230.         frame = new JFrame();
  231.         frame.setBounds(100, 100, 1318, 814);
  232.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  233.         frame.getContentPane().setLayout(null);
  234.  
  235.         loginName = new JTextField();
  236.         loginName.setText("usr");
  237.  
  238.         loginName.setBounds(10, 11, 86, 28);
  239.         frame.getContentPane().add(loginName);
  240.         loginName.setColumns(10);
  241.  
  242.         loginButton = new JButton("Login");
  243.         loginButton.addActionListener(new ActionListener() {
  244.             public void actionPerformed(ActionEvent e) {
  245.                 if (activeUser == null) {
  246.                     activeUser = users.getUser(loginName.getText(), new String(
  247.                             loginPasswd.getPassword()));
  248.                     if (activeUser != null) {
  249.                         userNameLabel.setText(activeUser.getName());
  250.                         userTypeLabel.setText(activeUser.getClass()
  251.                                 .getSimpleName());
  252.                         if (activeUser.getIcon() != null) {
  253.                             userPhoto.setIcon(activeUser.getIcon());
  254.                         } else {
  255.                             userPhoto.setText("No Photo");
  256.                         }
  257.                     } else {
  258.                         JOptionPane.showMessageDialog(frame,
  259.                                 "Please provide valid login.",
  260.                                 "Username or password mismatch",
  261.                                 JOptionPane.WARNING_MESSAGE);
  262.                         return;
  263.                     }
  264.                 } else {
  265.                     JOptionPane.showMessageDialog(frame, "Click on logout.",
  266.                             "You must log out first",
  267.                             JOptionPane.WARNING_MESSAGE);
  268.                 }
  269.             }
  270.         });
  271.  
  272.         loginButton.setBounds(106, 10, 89, 28);
  273.         frame.getContentPane().add(loginButton);
  274.  
  275.         separator = new JSeparator();
  276.         separator.setOrientation(SwingConstants.VERTICAL);
  277.         separator.setBounds(205, 11, 2, 51);
  278.         frame.getContentPane().add(separator);
  279.  
  280.         registerName = new JTextField();
  281.         registerName.setText("usr");
  282.         registerName.setBounds(217, 11, 86, 28);
  283.         frame.getContentPane().add(registerName);
  284.         registerName.setColumns(10);
  285.  
  286.         registerButton = new JButton("register");
  287.         registerButton.addActionListener(new ActionListener() {
  288.             public void actionPerformed(ActionEvent arg0) {
  289.                 if ((registerName.getText().equals("") || registerName
  290.                         .getText().equals("usr"))
  291.                         || new String(registerPasswd.getPassword()).equals("")
  292.                         || new String(registerPasswd.getPassword())
  293.                                 .equals("passwd")) {
  294.                     JOptionPane.showMessageDialog(frame,
  295.                             "Please provide all login data.",
  296.                             "Incomplete login data",
  297.                             JOptionPane.WARNING_MESSAGE);
  298.                 } else {
  299.                     boolean isOkay = false;
  300.                     final boolean isNotOkay = false;
  301.                     if (userTypeComboBox.getSelectedItem() == "Admin") {
  302.                         isOkay = users.addUser(registerName.getText(),
  303.                                 new String(registerPasswd.getPassword()),
  304.                                 UserType.ADMIN);
  305.                     } else if (userTypeComboBox.getSelectedItem() == "Veteran") {
  306.                         isOkay = users.addUser(registerName.getText(),
  307.                                 new String(registerPasswd.getPassword()),
  308.                                 UserType.VETERAN);
  309.                     } else if (userTypeComboBox.getSelectedItem() == "Newbie") {
  310.                         isOkay = users.addUser(registerName.getText(),
  311.                                 new String(registerPasswd.getPassword()),
  312.                                 UserType.NEWBIE);
  313.                     }
  314.  
  315.                     if (isOkay == isNotOkay) {
  316.                         JOptionPane.showMessageDialog(frame,
  317.                                 "Please change your name",
  318.                                 "Existing user data",
  319.                                 JOptionPane.WARNING_MESSAGE);
  320.                     }
  321.                 }
  322.             }
  323.         });
  324.         registerButton.setBounds(313, 10, 89, 29);
  325.         frame.getContentPane().add(registerButton);
  326.  
  327.         separator_1 = new JSeparator();
  328.         separator_1.setBounds(10, 73, 1282, 2);
  329.         frame.getContentPane().add(separator_1);
  330.  
  331.         btnNewButton_2 = new JButton("Add topic");
  332.         btnNewButton_2.setEnabled(false);
  333.  
  334.         btnNewButton_2.setBounds(10, 120, 100, 23);
  335.         frame.getContentPane().add(btnNewButton_2);
  336.  
  337.         passwordChange = new JButton("Passwd change");
  338.         passwordChange.addActionListener(new ActionListener() {
  339.             public void actionPerformed(ActionEvent arg0) {
  340.                 activeUser.changePassword("");
  341.             }
  342.         });
  343.         passwordChange.setBounds(10, 182, 100, 28);
  344.         frame.getContentPane().add(passwordChange);
  345.  
  346.         btnAddPicture = new JButton("Add picture");
  347.         btnAddPicture.addActionListener(new ActionListener() {
  348.             public void actionPerformed(ActionEvent arg0) {
  349.  
  350.                 final JFileChooser fc = new JFileChooser();
  351.                 RepaintManager.currentManager(fc).setDoubleBufferingEnabled(true);
  352.                 System.out.println(RepaintManager.currentManager(fc).isDoubleBufferingEnabled());
  353.                 //fc.repaint();
  354.                 // in response to a button click:
  355.                 int returnVal = fc.showOpenDialog(frame);
  356.  
  357.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  358.                     File file = fc.getSelectedFile();
  359.                    
  360.                 } else {
  361.                     System.out.println("Open command cancelled by user.");
  362.                 }
  363.             }
  364.         });
  365.         btnAddPicture.setBounds(10, 214, 98, 26);
  366.         frame.getContentPane().add(btnAddPicture);
  367.  
  368.         logoutButton = new JButton("Logout");
  369.         logoutButton.addActionListener(new ActionListener() {
  370.             public void actionPerformed(ActionEvent arg0) {
  371.                 activeUser = null;
  372.                 userNameLabel.setText("Username");
  373.                 userTypeLabel.setText("User Type");
  374.                 userPhoto.setText("No Photo");
  375.             }
  376.         });
  377.         logoutButton.setBounds(106, 43, 89, 26);
  378.         frame.getContentPane().add(logoutButton);
  379.  
  380.         btnBack = new JButton("Back");
  381.         btnBack.setEnabled(false);
  382.         btnBack.setBounds(10, 251, 98, 26);
  383.         frame.getContentPane().add(btnBack);
  384.  
  385.         btnSortAlphabetically = new JButton("Sort Alphabetically");
  386.         btnSortAlphabetically.setEnabled(false);
  387.         btnSortAlphabetically.setBounds(10, 285, 98, 26);
  388.         frame.getContentPane().add(btnSortAlphabetically);
  389.  
  390.         btnNewButton_5 = new JButton("Add Comment");
  391.         btnNewButton_5.setEnabled(false);
  392.         btnNewButton_5.setBounds(10, 322, 98, 26);
  393.         frame.getContentPane().add(btnNewButton_5);
  394.  
  395.         userPhoto = new JLabel("No Photo");
  396.         userPhoto.setBounds(420, 11, 55, 51);
  397.         frame.getContentPane().add(userPhoto);
  398.  
  399.         loginPasswd = new JPasswordField();
  400.         loginPasswd.setBounds(10, 42, 86, 28);
  401.         frame.getContentPane().add(loginPasswd);
  402.  
  403.         registerPasswd = new JPasswordField();
  404.         registerPasswd.setBounds(217, 42, 86, 28);
  405.         frame.getContentPane().add(registerPasswd);
  406.  
  407.         userNameLabel = new JLabel("Username");
  408.         userNameLabel.setBounds(481, 11, 86, 28);
  409.         frame.getContentPane().add(userNameLabel);
  410.  
  411.         userTypeLabel = new JLabel("User Type");
  412.         userTypeLabel.setBounds(481, 34, 86, 28);
  413.         frame.getContentPane().add(userTypeLabel);
  414.  
  415.         String[] userStrings = { "Admin", "Veteran", "Newbie" };
  416.         userTypeComboBox = new JComboBox(userStrings);
  417.         userTypeComboBox.setSelectedIndex(2);
  418.         userTypeComboBox.setBounds(313, 43, 89, 28);
  419.         frame.getContentPane().add(userTypeComboBox);
  420.        
  421.         textField = new JTextField();
  422.         textField.setBounds(10, 148, 100, 28);
  423.         frame.getContentPane().add(textField);
  424.         textField.setColumns(10);
  425.         // frame.validate();
  426.         // txtUsr.validate();
  427.         // txtPasswd.validate();
  428.         // separator.validate();
  429.         // txtUsr_1.validate();
  430.         // txtPasswd_1.validate();
  431.         // btnNewButton_1.validate();
  432.         // separator_1.validate();
  433.         // btnNewButton.validate();
  434.         // btnNewButton_2.validate();
  435.         // btnNewButton_3.validate();
  436.         // btnAddPicture.validate();
  437.         // btnNewButton_4.validate();
  438.         // btnBack.validate();
  439.         // btnSortAlphabetically.validate();
  440.         // btnNewButton_5.validate();
  441.         // lblNewLabel.validate();
  442.  
  443.     }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement