package forum; import java.awt.BufferCapabilities; import java.awt.DisplayMode; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsDevice.WindowTranslucency; import java.awt.GraphicsEnvironment; import java.awt.ImageCapabilities; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.RepaintManager; import javax.swing.SwingConstants; import javax.swing.JPasswordField; import javax.swing.UIManager; import users.User; import users.UserType; import javax.swing.JComboBox; public class ForumView { private JFrame frame; private JTextField loginName; private JSeparator separator; private JTextField registerName; private JButton registerButton; private JSeparator separator_1; private JButton loginButton; private JButton btnNewButton_2; private JButton passwordChange; private JButton btnAddPicture; private JButton logoutButton; private JButton btnBack; private JButton btnSortAlphabetically; private JButton btnNewButton_5; private JLabel userPhoto; private JPasswordField loginPasswd; private JPasswordField registerPasswd; private UserList users = new UserList(); private User activeUser = null; private JLabel userNameLabel; private JLabel userTypeLabel; private JComboBox userTypeComboBox; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { // GraphicsDevice[] graphicsDevices = getScreenGraphicsDevices(); // printInfoAboutEachGraphicsDevice(graphicsDevices); final ForumView window = new ForumView(); window.frame.setVisible(true); window.frame.createBufferStrategy(2); // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); /*UIManager.setLookAndFeel(UIManager .getSystemLookAndFeelClassName());*/ new Thread(new Runnable() { @Override public void run() { while (true) { window.frame.repaint(); try { Thread.sleep(1); } catch (InterruptedException e) { } } } }).start(); } catch (Exception e) { e.printStackTrace(); } } }); } // /** // * Ziska pole se vsemi dostupnymi grafickymi zarizenimi. // * // * @return pole se vsemi dostupnymi grafickymi zarizenimi // */ // private static GraphicsDevice[] getScreenGraphicsDevices() { // GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); // GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices(); // return graphicsDevices; // } // // /** // * Vypise informaci o vsech grafickych zarizenich predanych v parametru // * graphicsDevices. // * // * @param graphicsDevices // * pole se vsemi dostupnymi grafickymi zarizenimi // */ // private static void printInfoAboutEachGraphicsDevice(GraphicsDevice[] graphicsDevices) { // for (int j = 0; j < graphicsDevices.length; j++) { // GraphicsDevice graphicsDevice = graphicsDevices[j]; // printGraphicsDeviceInfo(graphicsDevice); // } // } // // /** // * Vypise informaci o grafickem zarizeni predanem v parametru // * graphicsDevice. // * // * @param graphicsDevice // * objekt reprezentujici dostupne graficke zarizeni. // */ // private static void printGraphicsDeviceInfo(GraphicsDevice graphicsDevice) { // System.out.println("Device ID: " + graphicsDevice.getIDstring()); // System.out.println("Type: " + getDeviceType(graphicsDevice)); // System.out.println("Accelerated memory size: " + graphicsDevice.getAvailableAcceleratedMemory()); // System.out.println("Display change supported: " + graphicsDevice.isDisplayChangeSupported()); // System.out.println("Full screen supported: " + graphicsDevice.isFullScreenSupported()); // printTransparencyInfo(graphicsDevice); // printBuffersConfiguration(graphicsDevice); // System.out.println("Graphics modes:"); // DisplayMode[] modes = graphicsDevice.getDisplayModes(); // printAvailableDisplayModes(modes); // System.out.println(); // } // // /** // * Vypis zda zarizeni podporuje pruhlednost a/nebo prusvitnost. // * // * @param graphicsDevice // * objekt reprezentujici dostupne graficke zarizeni. // */ // private static void printTransparencyInfo(GraphicsDevice graphicsDevice) { // System.out.println("Transparency/translucency support:"); // System.out.println(" Per pixel transparency: " + graphicsDevice.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)); // System.out.println(" Per pixel translucency: " + graphicsDevice.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)); // System.out.println(" Per window translucency: " + graphicsDevice.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)); // } // // /** // * Vypis informace o bufferech (prednim a zadnim bufferu atd.). // * // * @param graphicsDevice // * objekt reprezentujici dostupne graficke zarizeni. // */ // private static void printBuffersConfiguration(GraphicsDevice graphicsDevice) { // // ziskat vsechny dostupne graficke konfigurace // GraphicsConfiguration[] graphicsConfigurations = graphicsDevice.getConfigurations(); // for (GraphicsConfiguration graphicsConfiguration : graphicsConfigurations) { // System.out.println("Graphics configuration: " + graphicsConfiguration.toString()); // BufferCapabilities bufferCapabilities = graphicsConfiguration.getBufferCapabilities(); // System.out.println(" Buffers configuration:"); // System.out.println(" Multi buffer available: " + bufferCapabilities.isMultiBufferAvailable()); // System.out.println(" Page flipping supported: " + bufferCapabilities.isPageFlipping()); // System.out.println(" Full screen required: " + bufferCapabilities.isFullScreenRequired()); // // moznosti predniho bufferu // ImageCapabilities frontBuffer = bufferCapabilities.getFrontBufferCapabilities(); // // moznosti zadniho bufferu // ImageCapabilities backBuffer = bufferCapabilities.getBackBufferCapabilities(); // System.out.println(" Front buffer accelerated: " + frontBuffer.isAccelerated()); // System.out.println(" Front buffer volatile: " + frontBuffer.isTrueVolatile()); // System.out.println(" Back buffer accelerated: " + backBuffer.isAccelerated()); // System.out.println(" Back buffer volatile: " + backBuffer.isTrueVolatile()); // } // } // // /** // * Vrati typ grafickeho zarizeni. // * // * @param graphicsDevice // * objekt reprezentujici dostupne graficke zarizeni. // * @return typ zarizeni (textova reprezentace) // */ // private static String getDeviceType(GraphicsDevice graphicsDevice) { // switch (graphicsDevice.getType()) { // case GraphicsDevice.TYPE_RASTER_SCREEN: // return "raster screen"; // case GraphicsDevice.TYPE_PRINTER: // return "printer"; // case GraphicsDevice.TYPE_IMAGE_BUFFER: // return "image buffer"; // default: // nemelo by nastat // return "???"; // } // } // // /** // * Vypise dostupne graficke rezimy a jejich zakladni vlastnosti. // * // * @param modes pole s grafickymi rezimy pro dane zarizeni. // */ // private static void printAvailableDisplayModes(DisplayMode[] modes) { // for (DisplayMode mode : modes) { // final int width = mode.getWidth(); // final int height = mode. getHeight(); // final int bpp = mode.getBitDepth(); // final int refreshRate = mode.getRefreshRate(); // System.out.println(" " + // width + "x" + height + // "@" + bpp + " bpp\t(" + refreshRate + " Hz)"); // } // } /** * Create the application. */ public ForumView() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 1318, 814); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); loginName = new JTextField(); loginName.setText("usr"); loginName.setBounds(10, 11, 86, 28); frame.getContentPane().add(loginName); loginName.setColumns(10); loginButton = new JButton("Login"); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (activeUser == null) { activeUser = users.getUser(loginName.getText(), new String( loginPasswd.getPassword())); if (activeUser != null) { userNameLabel.setText(activeUser.getName()); userTypeLabel.setText(activeUser.getClass() .getSimpleName()); if (activeUser.getIcon() != null) { userPhoto.setIcon(activeUser.getIcon()); } else { userPhoto.setText("No Photo"); } } else { JOptionPane.showMessageDialog(frame, "Please provide valid login.", "Username or password mismatch", JOptionPane.WARNING_MESSAGE); return; } } else { JOptionPane.showMessageDialog(frame, "Click on logout.", "You must log out first", JOptionPane.WARNING_MESSAGE); } } }); loginButton.setBounds(106, 10, 89, 28); frame.getContentPane().add(loginButton); separator = new JSeparator(); separator.setOrientation(SwingConstants.VERTICAL); separator.setBounds(205, 11, 2, 51); frame.getContentPane().add(separator); registerName = new JTextField(); registerName.setText("usr"); registerName.setBounds(217, 11, 86, 28); frame.getContentPane().add(registerName); registerName.setColumns(10); registerButton = new JButton("register"); registerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if ((registerName.getText().equals("") || registerName .getText().equals("usr")) || new String(registerPasswd.getPassword()).equals("") || new String(registerPasswd.getPassword()) .equals("passwd")) { JOptionPane.showMessageDialog(frame, "Please provide all login data.", "Incomplete login data", JOptionPane.WARNING_MESSAGE); } else { boolean isOkay = false; final boolean isNotOkay = false; if (userTypeComboBox.getSelectedItem() == "Admin") { isOkay = users.addUser(registerName.getText(), new String(registerPasswd.getPassword()), UserType.ADMIN); } else if (userTypeComboBox.getSelectedItem() == "Veteran") { isOkay = users.addUser(registerName.getText(), new String(registerPasswd.getPassword()), UserType.VETERAN); } else if (userTypeComboBox.getSelectedItem() == "Newbie") { isOkay = users.addUser(registerName.getText(), new String(registerPasswd.getPassword()), UserType.NEWBIE); } if (isOkay == isNotOkay) { JOptionPane.showMessageDialog(frame, "Please change your name", "Existing user data", JOptionPane.WARNING_MESSAGE); } } } }); registerButton.setBounds(313, 10, 89, 29); frame.getContentPane().add(registerButton); separator_1 = new JSeparator(); separator_1.setBounds(10, 73, 1282, 2); frame.getContentPane().add(separator_1); btnNewButton_2 = new JButton("Add topic"); btnNewButton_2.setEnabled(false); btnNewButton_2.setBounds(10, 120, 100, 23); frame.getContentPane().add(btnNewButton_2); passwordChange = new JButton("Passwd change"); passwordChange.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { activeUser.changePassword(""); } }); passwordChange.setBounds(10, 182, 100, 28); frame.getContentPane().add(passwordChange); btnAddPicture = new JButton("Add picture"); btnAddPicture.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { final JFileChooser fc = new JFileChooser(); RepaintManager.currentManager(fc).setDoubleBufferingEnabled(true); System.out.println(RepaintManager.currentManager(fc).isDoubleBufferingEnabled()); //fc.repaint(); // in response to a button click: int returnVal = fc.showOpenDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); } else { System.out.println("Open command cancelled by user."); } } }); btnAddPicture.setBounds(10, 214, 98, 26); frame.getContentPane().add(btnAddPicture); logoutButton = new JButton("Logout"); logoutButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { activeUser = null; userNameLabel.setText("Username"); userTypeLabel.setText("User Type"); userPhoto.setText("No Photo"); } }); logoutButton.setBounds(106, 43, 89, 26); frame.getContentPane().add(logoutButton); btnBack = new JButton("Back"); btnBack.setEnabled(false); btnBack.setBounds(10, 251, 98, 26); frame.getContentPane().add(btnBack); btnSortAlphabetically = new JButton("Sort Alphabetically"); btnSortAlphabetically.setEnabled(false); btnSortAlphabetically.setBounds(10, 285, 98, 26); frame.getContentPane().add(btnSortAlphabetically); btnNewButton_5 = new JButton("Add Comment"); btnNewButton_5.setEnabled(false); btnNewButton_5.setBounds(10, 322, 98, 26); frame.getContentPane().add(btnNewButton_5); userPhoto = new JLabel("No Photo"); userPhoto.setBounds(420, 11, 55, 51); frame.getContentPane().add(userPhoto); loginPasswd = new JPasswordField(); loginPasswd.setBounds(10, 42, 86, 28); frame.getContentPane().add(loginPasswd); registerPasswd = new JPasswordField(); registerPasswd.setBounds(217, 42, 86, 28); frame.getContentPane().add(registerPasswd); userNameLabel = new JLabel("Username"); userNameLabel.setBounds(481, 11, 86, 28); frame.getContentPane().add(userNameLabel); userTypeLabel = new JLabel("User Type"); userTypeLabel.setBounds(481, 34, 86, 28); frame.getContentPane().add(userTypeLabel); String[] userStrings = { "Admin", "Veteran", "Newbie" }; userTypeComboBox = new JComboBox(userStrings); userTypeComboBox.setSelectedIndex(2); userTypeComboBox.setBounds(313, 43, 89, 28); frame.getContentPane().add(userTypeComboBox); textField = new JTextField(); textField.setBounds(10, 148, 100, 28); frame.getContentPane().add(textField); textField.setColumns(10); // frame.validate(); // txtUsr.validate(); // txtPasswd.validate(); // separator.validate(); // txtUsr_1.validate(); // txtPasswd_1.validate(); // btnNewButton_1.validate(); // separator_1.validate(); // btnNewButton.validate(); // btnNewButton_2.validate(); // btnNewButton_3.validate(); // btnAddPicture.validate(); // btnNewButton_4.validate(); // btnBack.validate(); // btnSortAlphabetically.validate(); // btnNewButton_5.validate(); // lblNewLabel.validate(); } }