Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java_lab1;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.tree.DefaultMutableTreeNode;
- import javax.swing.tree.DefaultTreeModel;
- import javax.swing.tree.TreePath;
- import java.awt.GridBagLayout;
- import javax.swing.JSplitPane;
- import java.awt.GridBagConstraints;
- import javax.swing.JList;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JScrollPane;
- import java.awt.Insets;
- import javax.swing.JTree;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.util.Enumeration;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import java.awt.event.ActionEvent;
- import javax.swing.event.TreeSelectionListener;
- import javax.swing.event.TreeSelectionEvent;
- public class Gui extends JFrame {
- private JPanel contentPane;
- private URLClassLoader classLoader;
- private JTree tree;
- private JList<String> list;
- private JarEntry entry;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Gui frame = new Gui();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Gui() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- GridBagLayout gbl_contentPane = new GridBagLayout();
- gbl_contentPane.columnWidths = new int[] { 0, 0 };
- gbl_contentPane.rowHeights = new int[] { 0, 0 };
- gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
- gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
- contentPane.setLayout(gbl_contentPane);
- JSplitPane splitPane = new JSplitPane();
- GridBagConstraints gbc_splitPane = new GridBagConstraints();
- gbc_splitPane.fill = GridBagConstraints.BOTH;
- gbc_splitPane.gridx = 0;
- gbc_splitPane.gridy = 0;
- contentPane.add(splitPane, gbc_splitPane);
- JPanel panel = new JPanel();
- splitPane.setLeftComponent(panel);
- GridBagLayout gbl_panel = new GridBagLayout();
- gbl_panel.columnWidths = new int[] { 0, 0 };
- gbl_panel.rowHeights = new int[] { 0, 0, 0 };
- gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
- gbl_panel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
- panel.setLayout(gbl_panel);
- JButton btnNewButton = new JButton("New button");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- JFileChooser chooser = new JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter("Jar files", "jar");
- chooser.setFileFilter(filter);
- int returnVal = chooser.showOpenDialog(null);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
- }
- File file = chooser.getSelectedFile();
- try {
- JarFile jarFile = new JarFile(file.getAbsolutePath());
- Enumeration<JarEntry>entries=jarFile.entries();
- while(entries.hasMoreElements()){
- JarEntry jarEntry=entries.nextElement();
- String entryName=jarEntry.getName();
- if(entryName.equals()){
- }
- }
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- if (file != null) {
- try {
- classLoader = new URLClassLoader(new URL[] { new URL("file:///" + file.getAbsolutePath()) });
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- DefaultMutableTreeNode root = new DefaultMutableTreeNode(file);
- Thread thread = new Thread(new JarFileReader(file, root));
- thread.start();
- try {
- thread.join();
- } catch (Exception e) {
- e.printStackTrace();
- }
- tree.setModel(new DefaultTreeModel(root));
- tree.repaint();
- }
- }
- });
- GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
- gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
- gbc_btnNewButton.gridx = 0;
- gbc_btnNewButton.gridy = 0;
- panel.add(btnNewButton, gbc_btnNewButton);
- JScrollPane scrollPane = new JScrollPane();
- GridBagConstraints gbc_scrollPane = new GridBagConstraints();
- gbc_scrollPane.fill = GridBagConstraints.BOTH;
- gbc_scrollPane.gridx = 0;
- gbc_scrollPane.gridy = 1;
- panel.add(scrollPane, gbc_scrollPane);
- tree = new JTree();
- tree.addTreeSelectionListener(new TreeSelectionListener() {
- public void valueChanged(TreeSelectionEvent arg0) {
- TreePath path = tree.getSelectionPath();
- StringBuilder pathFile = new StringBuilder("/");
- if (path != null) {
- Object[] pathElement = path.getPath();
- for (int i = 1; i < pathElement.length - 1; i++) {
- pathFile.append(pathElement[i]).append("/");
- }
- pathFile.append(pathElement[pathElement.length - 1]);
- }
- try {
- String classpath = pathFile.toString().replaceAll("/", ".");
- if (classpath.endsWith(".class")) {
- Class<?> c = Class.forName(classpath.substring(1, pathFile.length() - 6), false, classLoader);
- list.setModel(ClassDetail.getInfo(c));
- }
- } catch (ClassNotFoundException e1) {
- e1.printStackTrace();
- }
- }
- });
- scrollPane.setViewportView(tree);
- list = new JList();
- splitPane.setRightComponent(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement