package org.reldb.dbrowser.ui.monitors;

import org.eclipse.swt.widgets.Composite;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.wb.swt.SWTResourceManager;
import org.reldb.dbrowser.ui.updates.UpdatesCheck;
import org.reldb.dbrowser.ui.updates.UpdatesCheckDialog;
import org.reldb.dbrowser.ui.updates.UpdatesCheck.SendStatus;
import org.reldb.dbrowser.utilities.FontSize;

public class CheckForUpdates extends Composite {

	private UpdatesCheck updateChecker;
	
	private Label lblStatus;
	
	private MouseAdapter mouseHandler = new MouseAdapter() {
		@Override
		public void mouseUp(MouseEvent e) {
			UpdatesCheckDialog.launch(getShell());
		}
	};
	
	protected void completed(SendStatus sendStatus) {
		try {
			if (sendStatus.getResponse() != null && sendStatus.getResponse().startsWith("Success")) {
				String updateURL = UpdatesCheck.getUpdateURL(sendStatus);
				if (updateURL != null) {
					System.out.println("CheckForUpdates: updates available: " + updateURL);
					lblStatus.setText("Updates Available");
					lblStatus.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
				} else {
					System.out.println("CheckForUpdates: no new updates.");
					lblStatus.setText("Up to date");
					lblStatus.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
				}
				getParent().layout();
	        }
		} catch (Exception e) {
			System.out.println("CheckForUpdates: exception: " + e);
		}
	}
		
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public CheckForUpdates(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout());
		
		lblStatus = new Label(this, SWT.WRAP);
		lblStatus.setAlignment(SWT.CENTER);
		lblStatus.setText("Check for updates");
		lblStatus.setFont(FontSize.getThisFontInNewSize(lblStatus.getFont(), 10, SWT.NORMAL));
		lblStatus.addMouseListener(mouseHandler);
		
		updateChecker = new UpdatesCheck(parent.getDisplay()) {
			@Override
		    public void completed(SendStatus sendStatus) {
				CheckForUpdates.this.completed(sendStatus);
			}
		};
		
		TimerTask checkForUpdates = new TimerTask() {
			@Override
			public void run() {
				System.out.println("CheckForUpdates: check for updates.");
				updateChecker.doCancel();
				updateChecker.doSend();
			}
		};
		
		// Check for updates after 10 seconds, then every 12 hours
		Timer checkTimer = new Timer();
		checkTimer.schedule(checkForUpdates, 1000 * 10, 1000 * 60 * 60 * 12);
	}
}

@@ -38,51 +38,8 @@
 s;%0A%0A
-import org.eclipse.swt.widgets.Composite;%0A%0A
 impo
@@ -87,16 +87,95 @@
 rTask;%0A%0A
+import org.eclipse.swt.widgets.Composite;%0Aimport org.eclipse.swt.widgets.Text;%0A
 import o
@@ -284,46 +284,8 @@
 nt;%0A
-import org.eclipse.swt.widgets.Label;%0A
 impo
@@ -322,16 +322,17 @@
 Layout;%0A
+%0A
 import o
@@ -365,24 +365,25 @@
 rceManager;%0A
+%0A
 import org.r
@@ -689,17 +689,16 @@
 ate 
-Label lbl
+Text txt
 Stat
@@ -807,19 +807,48 @@
 eEvent e
-) %7B
+vent) %7B%0A%09%09%09super.mouseUp(event);
 %0A%09%09%09Upda
@@ -1209,35 +1209,35 @@
 pdateURL);%0A%09%09%09%09%09
-lbl
+txt
 Status.setText(%22
@@ -1246,11 +1246,10 @@
 date
-s A
+ a
 vail
@@ -1253,35 +1253,35 @@
 ailable%22);%0A%09%09%09%09%09
-lbl
+txt
 Status.setForegr
@@ -1406,35 +1406,35 @@
 pdates.%22);%0A%09%09%09%09%09
-lbl
+txt
 Status.setText(%22
@@ -1452,19 +1452,19 @@
 );%0A%09%09%09%09%09
-lbl
+txt
 Status.s
@@ -1852,19 +1852,19 @@
 );%0A%09%09%0A%09%09
-lbl
+txt
 Status =
@@ -1872,13 +1872,12 @@
 new 
-Label
+Text
 (thi
@@ -1891,54 +1891,105 @@
 WRAP
-);%0A%09%09lblStatus.setAlignment(SWT.CENTER
+ %7C SWT.CENTER);%0A%09%09txtStatus.setEditable(false);%0A%09%09txtStatus.setBackground(getBackground()
 );%0A%09%09
-lbl
+txt
 Stat
@@ -2019,27 +2019,27 @@
 pdates%22);%0A%09%09
-lbl
+txt
 Status.setFo
@@ -2071,19 +2071,19 @@
 NewSize(
-lbl
+txt
 Status.g
@@ -2116,11 +2116,11 @@
 ;%0A%09%09
-lbl
+txt
 Stat

Fix ignored mouse clicks.