Advertisement
Guest User

homepanel.java

a guest
Nov 30th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. package org.limewire.ui.swing.home;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.event.ComponentAdapter;
  7. import java.awt.event.ComponentEvent;
  8. import java.io.IOException;
  9. import java.net.URL;
  10.  
  11. import javax.swing.BorderFactory;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.ScrollPaneConstants;
  14. import javax.swing.event.HyperlinkEvent;
  15. import javax.swing.event.HyperlinkListener;
  16.  
  17. import org.jdesktop.swingx.JXPanel;
  18. import org.limewire.concurrent.FutureEvent;
  19. import org.limewire.core.api.connection.GnutellaConnectionManager;
  20. import org.limewire.inject.LazySingleton;
  21. import org.limewire.listener.EventListener;
  22. import org.limewire.listener.SwingEDTEvent;
  23. import org.limewire.ui.swing.browser.Browser;
  24. import org.limewire.ui.swing.browser.BrowserUtils;
  25. import org.limewire.ui.swing.browser.UriAction;
  26. import org.limewire.ui.swing.components.HTMLPane;
  27. import org.limewire.ui.swing.components.HTMLPane.LoadResult;
  28. import org.limewire.ui.swing.nav.NavCategory;
  29. import org.limewire.ui.swing.nav.Navigator;
  30. import org.limewire.ui.swing.util.NativeLaunchUtils;
  31. import org.limewire.ui.swing.util.SwingUtils;
  32.  
  33. import com.google.inject.Inject;
  34.  
  35. /** Journey **/
  36. import com.codebrig.journey.JourneyBrowserView;
  37.  
  38. import javax.swing.*;
  39. import java.awt.*;
  40. import java.awt.event.WindowAdapter;
  41. import java.awt.event.WindowEvent;
  42.  
  43. /** The main home page.*/
  44. @LazySingleton
  45. public class HomePanel extends JXPanel {
  46.    
  47.     private static final String DEFAULT_URL = "file:///" + System.getProperty("user.dir") + "/lib/WSHome.xhtml";
  48.    
  49.     private final Browser browser;
  50.     private final HTMLPane fallbackBrowser;
  51.     private final GnutellaConnectionManager gnutellaConnectionManager;
  52.    
  53.     private boolean loadedOnce = false;
  54.     private int retryCount = 0;
  55.     private boolean firstRequest = true;
  56.     private long initialLoadTime = -1;
  57.  
  58.     @Inject
  59.     public HomePanel(final Navigator navigator,
  60.             GnutellaConnectionManager gnutellaConnectionManager) {
  61.         this.gnutellaConnectionManager = gnutellaConnectionManager;
  62.         setPreferredSize(new Dimension(500, 500));
  63.        
  64.         setLayout(new GridBagLayout());
  65.         GridBagConstraints gbc = new GridBagConstraints();
  66.         gbc.fill = GridBagConstraints.BOTH;
  67.         gbc.weightx = 1;
  68.         gbc.weighty = 1;
  69.  
  70.         public class JourneyBrowser {
  71.  
  72.             public static void main(String[] args) {
  73.                 JourneyBrowserView browser = new JourneyBrowserView("https://google.com");
  74.                 JFrame frame = new JFrame();
  75.                 frame.getContentPane().add(browser, BorderLayout.CENTER);
  76.  
  77.                 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  78.                 frame.addWindowListener(new WindowAdapter() {
  79.                         @Override
  80.                         public void windowClosing(WindowEvent e) {
  81.                             browser.getCefApp().dispose();
  82.                             frame.dispose();
  83.                         }
  84.                     });
  85.  
  86.                 frame.setTitle("Journey");
  87.                 frame.setSize(1000, 600);
  88.                 frame.setVisible(true);
  89.             }
  90.         }
  91.  
  92.  
  93.             JScrollPane scroller = new JScrollPane(fallbackBrowser,
  94.                     ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
  95.                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  96.             scroller.setBorder(BorderFactory.createEmptyBorder());
  97.             add(scroller, gbc);
  98.         }
  99. }
  100.  
  101.    
  102.    
  103.     /** Notification that a page finished loading. */
  104.     private void pageLoadFinished(boolean success) {
  105.         if(!success) {
  106.             reloadDefaultUrlIfPossibleAndNeeded();
  107.         }
  108.     }
  109.    
  110.     /**
  111.      * Reloads the default URL, with some extra parameters, if we need to.
  112.      */
  113.     private void reloadDefaultUrlIfPossibleAndNeeded() {
  114.         // based on:
  115.         // * if the browser is showing
  116.         // * if the first request was already sent succesfully
  117.         // * if there's no active request already
  118.         // * if the retryCount is below 5 (we don't want to hammer)
  119.         // * if we tried to load atleast once
  120.         // * if the last request was successful
  121.         // * if our current strength indicates we're online
  122.         if (isShowing() && firstRequest && !isRequestInProgress()
  123.                 && retryCount < 5 && loadedOnce && !isLastRequestSuccessful()
  124.                 && gnutellaConnectionManager.getConnectionStrength().isOnline()) {
  125.             retryCount++;
  126.             long delay = System.currentTimeMillis() - initialLoadTime;
  127.             load(DEFAULT_URL + "?rd=" + delay + "&rc=" + retryCount);
  128.         }
  129.     }
  130.    
  131.     /** Returns true if the last request was succesful. */
  132.     private boolean isLastRequestSuccessful() {
  133.         if(MozillaInitialization.isInitialized()) {
  134.             return browser.isLastRequestSuccessful();
  135.         } else {
  136.             return browser.isLastRequestSuccessful();
  137.         }
  138.     }
  139.    
  140.     public void loadDefaultUrl() {
  141.         load(DEFAULT_URL);
  142.     }
  143.  
  144.     public void load(String url) {
  145.         loadedOnce = true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement