Search in sources :

Example 1 with Main

use of org.pepsoft.worldpainter.Main in project WorldPainter by Captain-Chaos.

the class MapExplorer method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    // Load or initialise configuration
    File configDir = Configuration.getConfigDir();
    if (!configDir.isDirectory()) {
        configDir.mkdirs();
    }
    // This will migrate the configuration directory if necessary
    Configuration config = Configuration.load();
    if (config == null) {
        if (!logger.isDebugEnabled()) {
            // If debug logging is on, the Configuration constructor will
            // already log this
            logger.info("Creating new configuration");
        }
        config = new Configuration();
    }
    Configuration.setInstance(config);
    logger.info("Installation ID: " + config.getUuid());
    // Load and install trusted WorldPainter root certificate
    X509Certificate trustedCert = null;
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        trustedCert = (X509Certificate) certificateFactory.generateCertificate(Main.class.getResourceAsStream("/wproot.pem"));
        WPTrustManager trustManager = new WPTrustManager(trustedCert);
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (CertificateException e) {
        logger.error("Certificate exception while loading trusted root certificate", e);
    } catch (NoSuchAlgorithmException e) {
        logger.error("No such algorithm exception while loading trusted root certificate", e);
    } catch (KeyManagementException e) {
        logger.error("Key management exception while loading trusted root certificate", e);
    }
    // Load the plugins
    if (trustedCert != null) {
        PluginManager.loadPlugins(new File(configDir, "plugins"), trustedCert.getPublicKey());
    } else {
        logger.error("Trusted root certificate not available; not loading plugins");
    }
    WPPluginManager.initialise(config.getUuid());
    SwingUtilities.invokeLater(() -> {
        JFrame frame = new JFrame("Minecraft Map Explorer");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        MapTreeModel treeModel = new MapTreeModel();
        // File minecraftDir = MinecraftUtil.findMinecraftDir();
        // File defaultDir;
        // if (minecraftDir != null) {
        // defaultDir = new File(minecraftDir, "saves");
        // } else {
        // defaultDir = new File(System.getProperty("user.home"));
        // }
        JTree tree = new JTree(treeModel);
        tree.setRootVisible(false);
        tree.setShowsRootHandles(true);
        tree.setCellRenderer(new MapTreeCellRenderer());
        JScrollPane scrollPane = new JScrollPane(tree);
        // tree.expandPath(treeModel.getPath(defaultDir));
        // tree.scrollPathToVisible(treeModel.getPath(defaultDir));
        // Automatically expand any nodes if they only have one child
        tree.addTreeExpansionListener(new TreeExpansionListener() {

            @Override
            public void treeExpanded(TreeExpansionEvent event) {
                Object node = event.getPath().getLastPathComponent();
                if ((!treeModel.isLeaf(node)) && (treeModel.getChildCount(node) == 1)) {
                    tree.expandPath(event.getPath().pathByAddingChild(treeModel.getChild(node, 0)));
                }
            }

            @Override
            public void treeCollapsed(TreeExpansionEvent event) {
            }
        });
        tree.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    TreePath path = tree.getPathForLocation(e.getX(), e.getY());
                    if (path != null) {
                        ((Node) path.getLastPathComponent()).doubleClicked();
                    }
                }
            }
        });
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setSize(1024, 768);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) MouseEvent(java.awt.event.MouseEvent) Configuration(org.pepsoft.worldpainter.Configuration) WPTrustManager(org.pepsoft.worldpainter.browser.WPTrustManager) MouseAdapter(org.pepsoft.worldpainter.MouseAdapter) SecureRandom(java.security.SecureRandom) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) TreePath(javax.swing.tree.TreePath) File(java.io.File) Main(org.pepsoft.worldpainter.Main) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Aggregations

MouseEvent (java.awt.event.MouseEvent)1 File (java.io.File)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1 CertificateException (java.security.cert.CertificateException)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1 SSLContext (javax.net.ssl.SSLContext)1 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)1 TreeExpansionListener (javax.swing.event.TreeExpansionListener)1 TreePath (javax.swing.tree.TreePath)1 Configuration (org.pepsoft.worldpainter.Configuration)1 Main (org.pepsoft.worldpainter.Main)1 MouseAdapter (org.pepsoft.worldpainter.MouseAdapter)1 WPTrustManager (org.pepsoft.worldpainter.browser.WPTrustManager)1