Advertisement
Nicba1010

ProcessesList

Jan 20th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package infoKupProcess;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Point;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.io.DataOutputStream;
  10. import java.io.IOException;
  11. import java.util.Arrays;
  12.  
  13. import javax.swing.JList;
  14. import javax.swing.JMenuItem;
  15. import javax.swing.JPanel;
  16. import javax.swing.JPopupMenu;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.ListSelectionModel;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.event.ListSelectionEvent;
  21. import javax.swing.event.ListSelectionListener;
  22.  
  23. public class ProcessesList {
  24.     int x, y, width, height;
  25.     JPanel panel;
  26.     JList<String> processesListJList;
  27.     JScrollPane processesScrollPane;
  28.     int currentSelectedIndex = -1;
  29.     String currentSelectedProcess;
  30.     String[] processArray = new String[] { "not connected" };
  31.  
  32.     public ProcessesList(int x, int y, int width, int height, JPanel panel) {
  33.         super();
  34.         this.x = x;
  35.         this.y = y;
  36.         this.width = width;
  37.         this.height = height;
  38.  
  39.         final JPopupMenu popup = new JPopupMenu();
  40.         JMenuItem menuItem = new JMenuItem("Ugasi proces");
  41.         menuItem.addActionListener(new ActionListener() {
  42.  
  43.             @Override
  44.             public void actionPerformed(ActionEvent e) {
  45.                 killProc(processesListJList.getSelectedValue());
  46.             }
  47.         });
  48.         popup.add(menuItem);
  49.  
  50.         processesListJList = new JList<String>(processArray);
  51.         processesListJList.setListData(processArray);
  52.         processesListJList
  53.                 .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  54.         processesListJList
  55.                 .addListSelectionListener(new ListSelectionListener() {
  56.  
  57.                     @Override
  58.                     public void valueChanged(ListSelectionEvent e) {
  59.                         if (processesListJList.getSelectedIndex() == -1) {
  60.  
  61.                         } else {
  62.                             if (currentSelectedIndex != processesListJList
  63.                                     .getSelectedIndex()) {
  64.                                 System.out.println(processesListJList
  65.                                         .getSelectedIndex());
  66.                                 System.out.println(processesListJList
  67.                                         .getSelectedValue());
  68.                                 currentSelectedIndex = processesListJList
  69.                                         .getSelectedIndex();
  70.                                 currentSelectedProcess = processesListJList
  71.                                         .getSelectedValue();
  72.                             }
  73.                         }
  74.                     }
  75.                 });
  76.         processesListJList.addMouseListener(new MouseAdapter() {
  77.             public void mousePressed(MouseEvent e) {
  78.                 if (SwingUtilities.isRightMouseButton(e)) {
  79.                     System.out.println("Row: " + getRow(e.getPoint()));
  80.                     processesListJList.setSelectedIndex(getRow(e.getPoint()));
  81.                     showMenu(e);
  82.                 }
  83.             }
  84.  
  85.             public void mouseReleased(MouseEvent e) {
  86.                 if (SwingUtilities.isRightMouseButton(e)) {
  87.                     showMenu(e);
  88.                 }
  89.             }
  90.  
  91.             private void showMenu(MouseEvent e) {
  92.                 popup.show(e.getComponent(), e.getX(), e.getY());
  93.             }
  94.         });
  95.  
  96.         processesScrollPane = new JScrollPane(processesListJList);
  97.         processesScrollPane.setPreferredSize(new Dimension(width, height));
  98.  
  99.         JPanelProcList procPanel = new JPanelProcList();
  100.         procPanel.add(processesScrollPane);
  101.         // procPanel.setBorder(new EmptyBorder(0,-223,150,400));
  102.         procPanel.setBounds(x, y, width, height);
  103.         panel.add(procPanel);
  104.     }
  105.  
  106.     public void setData(String[] processArray) {
  107.         Arrays.sort(processArray);
  108.         processesListJList.setListData(processArray);
  109.         processesListJList.setSelectedIndex(currentSelectedIndex);
  110.     }
  111.  
  112.     private int getRow(Point point) {
  113.         return processesListJList.locationToIndex(point);
  114.     }
  115.  
  116.  
  117.     public void killProc(String proc) {
  118.         try {
  119.             DataOutputStream outToClient = new DataOutputStream(
  120.                     TCPServer.connectionSocket.getOutputStream());
  121.             outToClient.writeBytes("killproc " + proc);
  122.             System.out.println(proc);
  123.         } catch (IOException e) {
  124.             e.printStackTrace();
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement