Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. package java_lab1;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.filechooser.FileNameExtensionFilter;
  10. import javax.swing.tree.DefaultMutableTreeNode;
  11. import javax.swing.tree.DefaultTreeModel;
  12. import javax.swing.tree.TreePath;
  13.  
  14. import java.awt.GridBagLayout;
  15. import javax.swing.JSplitPane;
  16. import java.awt.GridBagConstraints;
  17. import javax.swing.JList;
  18. import javax.swing.JButton;
  19. import javax.swing.JFileChooser;
  20. import javax.swing.JScrollPane;
  21. import java.awt.Insets;
  22. import javax.swing.JTree;
  23. import java.awt.event.ActionListener;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28. import java.net.URLClassLoader;
  29. import java.util.Enumeration;
  30. import java.util.jar.JarEntry;
  31. import java.util.jar.JarFile;
  32. import java.awt.event.ActionEvent;
  33. import javax.swing.event.TreeSelectionListener;
  34. import javax.swing.event.TreeSelectionEvent;
  35.  
  36. public class Gui extends JFrame {
  37.  
  38. private JPanel contentPane;
  39. private URLClassLoader classLoader;
  40. private JTree tree;
  41. private JList<String> list;
  42. private JarEntry entry;
  43.  
  44. /**
  45. * Launch the application.
  46. */
  47. public static void main(String[] args) {
  48. EventQueue.invokeLater(new Runnable() {
  49. public void run() {
  50. try {
  51. Gui frame = new Gui();
  52. frame.setVisible(true);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. });
  58. }
  59.  
  60. /**
  61. * Create the frame.
  62. */
  63. public Gui() {
  64. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65. setBounds(100, 100, 450, 300);
  66. contentPane = new JPanel();
  67. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  68. setContentPane(contentPane);
  69. GridBagLayout gbl_contentPane = new GridBagLayout();
  70. gbl_contentPane.columnWidths = new int[] { 0, 0 };
  71. gbl_contentPane.rowHeights = new int[] { 0, 0 };
  72. gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
  73. gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
  74. contentPane.setLayout(gbl_contentPane);
  75.  
  76. JSplitPane splitPane = new JSplitPane();
  77. GridBagConstraints gbc_splitPane = new GridBagConstraints();
  78. gbc_splitPane.fill = GridBagConstraints.BOTH;
  79. gbc_splitPane.gridx = 0;
  80. gbc_splitPane.gridy = 0;
  81. contentPane.add(splitPane, gbc_splitPane);
  82.  
  83. JPanel panel = new JPanel();
  84. splitPane.setLeftComponent(panel);
  85. GridBagLayout gbl_panel = new GridBagLayout();
  86. gbl_panel.columnWidths = new int[] { 0, 0 };
  87. gbl_panel.rowHeights = new int[] { 0, 0, 0 };
  88. gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
  89. gbl_panel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
  90. panel.setLayout(gbl_panel);
  91.  
  92. JButton btnNewButton = new JButton("New button");
  93. btnNewButton.addActionListener(new ActionListener() {
  94. public void actionPerformed(ActionEvent arg0) {
  95. JFileChooser chooser = new JFileChooser();
  96. FileNameExtensionFilter filter = new FileNameExtensionFilter("Jar files", "jar");
  97. chooser.setFileFilter(filter);
  98. int returnVal = chooser.showOpenDialog(null);
  99. if (returnVal == JFileChooser.APPROVE_OPTION) {
  100. System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
  101. }
  102. File file = chooser.getSelectedFile();
  103. try {
  104. JarFile jarFile = new JarFile(file.getAbsolutePath());
  105. Enumeration<JarEntry>entries=jarFile.entries();
  106. while(entries.hasMoreElements()){
  107. JarEntry jarEntry=entries.nextElement();
  108. String entryName=jarEntry.getName();
  109. if(entryName.equals()){
  110.  
  111. }
  112.  
  113. }
  114. } catch (IOException e1) {
  115. // TODO Auto-generated catch block
  116. e1.printStackTrace();
  117. }
  118.  
  119.  
  120. if (file != null) {
  121. try {
  122. classLoader = new URLClassLoader(new URL[] { new URL("file:///" + file.getAbsolutePath()) });
  123. } catch (MalformedURLException e) {
  124. e.printStackTrace();
  125. }
  126. DefaultMutableTreeNode root = new DefaultMutableTreeNode(file);
  127. Thread thread = new Thread(new JarFileReader(file, root));
  128. thread.start();
  129. try {
  130. thread.join();
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. tree.setModel(new DefaultTreeModel(root));
  135. tree.repaint();
  136. }
  137. }
  138. });
  139. GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
  140. gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
  141. gbc_btnNewButton.gridx = 0;
  142. gbc_btnNewButton.gridy = 0;
  143. panel.add(btnNewButton, gbc_btnNewButton);
  144.  
  145. JScrollPane scrollPane = new JScrollPane();
  146. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  147. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  148. gbc_scrollPane.gridx = 0;
  149. gbc_scrollPane.gridy = 1;
  150. panel.add(scrollPane, gbc_scrollPane);
  151.  
  152. tree = new JTree();
  153. tree.addTreeSelectionListener(new TreeSelectionListener() {
  154. public void valueChanged(TreeSelectionEvent arg0) {
  155. TreePath path = tree.getSelectionPath();
  156. StringBuilder pathFile = new StringBuilder("/");
  157. if (path != null) {
  158. Object[] pathElement = path.getPath();
  159. for (int i = 1; i < pathElement.length - 1; i++) {
  160. pathFile.append(pathElement[i]).append("/");
  161. }
  162. pathFile.append(pathElement[pathElement.length - 1]);
  163. }
  164. try {
  165. String classpath = pathFile.toString().replaceAll("/", ".");
  166. if (classpath.endsWith(".class")) {
  167. Class<?> c = Class.forName(classpath.substring(1, pathFile.length() - 6), false, classLoader);
  168. list.setModel(ClassDetail.getInfo(c));
  169. }
  170. } catch (ClassNotFoundException e1) {
  171. e1.printStackTrace();
  172. }
  173. }
  174. });
  175. scrollPane.setViewportView(tree);
  176.  
  177. list = new JList();
  178. splitPane.setRightComponent(list);
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement