package org.lantern;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Browser dashboard for controlling lantern.
 */
public class Dashboard {
    
    private final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * Opens the browser.
     */
    public void openBrowser() {
        LanternHub.display().syncExec(new Runnable() {
            @Override
            public void run() {
                buildBrowser();
            }
        });
    }
    
    protected void buildBrowser() {
        log.info("Creating shell...");
        final Shell shell = new Shell(LanternHub.display());
        final Image small = newImage("16on.png");
        final Image medium = newImage("32on.png");
        final Image large = newImage("64on.png");
        final Image[] icons = new Image[]{small, medium, large};
        shell.setImages(icons);
        // this.shell = createShell(this.display);
        shell.setText("Lantern Dashboard");
        //this.shell.setSize(720, 540);
        // shell.setFullScreen(true);

        log.debug("Centering on screen...");
        final Monitor primary = LanternHub.display().getPrimaryMonitor();
        final Rectangle bounds = primary.getBounds();
        final Rectangle rect = shell.getBounds();

        final int x = bounds.x + (bounds.width - rect.width) / 2;
        final int y = bounds.y + (bounds.height - rect.height) / 2;

        shell.setLocation(x, y);
        
        log.info("Creating new browser...");
        final Browser browser = new Browser(shell, SWT.NONE);
        browser.setSize(800, 600);
        //browser.setBounds(0, 0, 800, 600);
        browser.setUrl("http://localhost:"+
            LanternHub.settings().getApiPort()+"/dashboard.html");
        shell.open();
        shell.forceActive();
        while (!shell.isDisposed()) {
            if (!LanternHub.display().readAndDispatch())
                LanternHub.display().sleep();
        }
    }

    private Image newImage(final String path) {
        final String toUse;
        final File path1 = new File(path);
        if (path1.isFile()) {
            toUse = path1.getAbsolutePath();
        } else {
            final File path2 = new File("install/common", path);
            toUse = path2.getAbsolutePath();
        }
        return new Image(LanternHub.display(), toUse);
    }
}

@@ -187,16 +187,58 @@
 tangle;%0A
+import org.eclipse.swt.layout.FillLayout;%0A
 import o
@@ -1324,24 +1324,94 @@
 een(true);%0A%0A
+        final int minWidth = 970;%0A        final int minHeight = 630;%0A%0A
         log.
@@ -1930,24 +1930,35 @@
 setSize(
-800, 600
+minWidth, minHeight
 );%0A     
@@ -2041,16 +2041,16 @@
 host:%22+%0A
-
         
@@ -2104,24 +2104,225 @@
 ard.html%22);%0A
+        shell.setLayout(new FillLayout());%0A        Rectangle minSize = shell.computeTrim(0, 0, minWidth, minHeight); %0A        shell.setMinimumSize(minSize.width, minSize.height);%0A        shell.pack();%0A
         shel

Allow dashboard to resize to fill window, set minimum size to 970x630 #83