
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import java.awt.Desktop;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;


public class MainUI 
{
	static int cFormWidth;//=1000;
	static int cFormHeight;//=740;	
	static int cTabWidth;//=cFormWidth-40;
	static int cTabHeight;//=cFormHeight-90;		
	
	static  Connection sConnection = null;	
	
	static  PreparedStatement sStmGoalInsert;
	static  PreparedStatement sStmGoalSelectId;
	static  PreparedStatement sStmGoalSelectAll;	
	static  PreparedStatement sStmGoalUpdate;
	static  PreparedStatement sStmGoalDelete;

	
	static  PreparedStatement sStmFeatInsert;
	static  PreparedStatement sStmFeatSelectId;
	static  PreparedStatement sStmFeatSelectAll;	
	static  PreparedStatement sStmFeatUpdate;
	static  PreparedStatement sStmFeatDelete;	
	
	
	static Table GoalTable;
	static Table  FeatTable;


	private static void DBup()
    {
		String     vDBName        = "GoalsAndFeatsDB";
		String     vConnectionURL = "jdbc:derby:" + vDBName + ";create=true";
	    Statement  vStm;
	    String     vStringCreateGoals = "create table tGoals  "
	            +  "(GoalId INT NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT Goal_PK PRIMARY KEY, " 
	            +  " Content VARCHAR(32000) NOT NULL, "
	    		+  " DueDate  DATE, "
	    		+  " Achieved DATE, "
	    		+  " Created  DATE NOT NULL)";
	    String     vStringCreateFeats = "create table tFeats  "
            +  "(FeatId INT NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT Feat_PK PRIMARY KEY, " 
            +  " Content VARCHAR(32000) NOT NULL, "
    		+  " Achieved  DATE NOT NULL)";
	    try 
	    {
			sConnection = DriverManager.getConnection(vConnectionURL);
	        System.out.println("Connected to database: " + vDBName);		
	        vStm = sConnection.createStatement();
	        if (!checkTable())
	        {  
	        	System.out.println (" . . . . creating tables tGoals, tFeats");
	        	vStm.execute(vStringCreateGoals);
	        	vStm.execute(vStringCreateFeats);	        	
	        }
			sStmGoalInsert    = sConnection.prepareStatement("insert into tGoals(Content, DueDate, Achieved, Created) values (?,?,?,?)");
			sStmGoalSelectId  = sConnection.prepareStatement("select Content, DueDate, Achieved, Created from tGoals where GoalId=?");
			sStmGoalUpdate    = sConnection.prepareStatement("update tGoals set Content=?, DueDate=?, Achieved=? where GoalId=?"); 
			sStmGoalDelete    = sConnection.prepareStatement("delete from tGoals where GoalId=?");
			sStmGoalSelectAll = sConnection.prepareStatement("select GoalId, Content, DueDate, Achieved, Created from tGoals order by GoalId");

			sStmFeatInsert    = sConnection.prepareStatement("insert into tFeats(Content, Achieved) values (?,?)");
			sStmFeatSelectId  = sConnection.prepareStatement("select Content, Achieved from tFeats where FeatId=?");
			sStmFeatUpdate    = sConnection.prepareStatement("update tFeats set Content=?, Achieved=? where FeatId=?"); 
			sStmFeatDelete    = sConnection.prepareStatement("delete from tFeats where FeatId=?");
			sStmFeatSelectAll = sConnection.prepareStatement("select FeatId, Content, Achieved from tFeats order by FeatId");
	    } 
	    catch (SQLException vEx) 
		{
	    	errorPrint(vEx);
		}

    }
    
	private static void DBdown()
    {
		try 
		{
			sStmGoalInsert.close();
			sStmGoalSelectId.close(); 
			sStmGoalUpdate.close();   
			sStmGoalDelete.close();   
			sStmGoalSelectAll.close();			

			sStmFeatInsert.close();
			sStmFeatSelectId.close(); 
			sStmFeatUpdate.close();   
			sStmFeatDelete.close();   
			sStmFeatSelectAll.close();			
			
			
			sConnection.close();
			System.out.println("Closed connection");
		}
		catch (SQLException vEx) 
		{
			errorPrint(vEx);
		}
       	boolean vGotSQLExc = false;
        try 
        {
        	DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } 
        catch (SQLException vEx)  
        {	
        	if (vEx.getSQLState().equals("XJ015")) 
            {		
        		vGotSQLExc = true;
            }
        }
        
        if (!vGotSQLExc) 
        {
        	System.out.println("Database did not shut down normally");
        }  
        else  
        {
        	System.out.println("Database shut down normally");	
        }      	
    }
    
	public static void main(String[] args) 
	{
		final Display vDisplay = new Display();
		org.eclipse.swt.graphics.Rectangle vArea=vDisplay.getClientArea();   
		cFormWidth=vArea.width;
		cFormHeight=vArea.height;	
		cTabWidth=cFormWidth-40;
		cTabHeight=cFormHeight-80;		

	    DBup();		
		
		final Shell vShell = new Shell(vDisplay);
		vShell.setBounds(0, 0, cFormWidth, cFormHeight);
		vShell.setText("Цели и достижения");

		Menu vMainMenu = new Menu(vShell, SWT.BAR);
		vShell.setMenuBar(vMainMenu);
		MenuItem vMenuAbout = new MenuItem(vMainMenu, SWT.CASCADE);
		vMenuAbout.setText("О программе");
		vMenuAbout.addListener(SWT.Selection, new Listener() 
		{
			public void handleEvent(Event e) 
			{
				DialogAbout(vDisplay);
            }
        });   
	        
		Content(vShell);
		vShell.open();
		while(!vShell.isDisposed())
		{	
			if(!vDisplay.readAndDispatch())
			{
				vDisplay.sleep();
			}
		}  
		vDisplay.dispose();

		DBdown();
	}
		
	private static void ContentGoals(final Shell pShell, TabFolder pTabFolder, TabItem pTabGoals)
	{ 	
	    Composite vComposite = new Composite(pTabFolder, SWT.NONE);
	    pTabGoals.setControl(vComposite);
	    
		final Text     vGoalContent	     = new Text(vComposite, SWT.BORDER | SWT.H_SCROLL |SWT.V_SCROLL);
		final DateTime vGoalDueDate	     = new DateTime(vComposite,SWT.CALENDAR);
		final Button   vGoalFlagAchieved = new Button(vComposite,SWT.CHECK);
		final DateTime vGoalAchieved	 = new DateTime(vComposite,SWT.CALENDAR);
		
		int vY=10;
		int vX=0;
		final int cXshift=10;
		final int cTableX=0;
		final int cTableHight=cTabHeight-50;
		int vTableWidth=0;

		final int cColumnsLenght=5;
		TableColumn[] vColumns = new TableColumn[cColumnsLenght];
		String[] vColumnTitle =  {"Id","Цель", "Срок", "Достигнута", "Создана"};
		final int cMainColunmWidth=cTabWidth-900;
		int[] vColumnWidth={30,cMainColunmWidth, 90, 90, 90};		
			
		for(int i = 0;i<cColumnsLenght;i++)
		{
			vTableWidth+=vColumnWidth[i];
		}		
		vTableWidth+=4;
		GoalTable = new Table(vComposite, SWT.FULL_SELECTION | SWT.BORDER);
		GoalTable.setLinesVisible(true);
		GoalTable.setHeaderVisible(true);
		GoalTable.setBounds(cTableX+4,vY,vTableWidth,cTableHight);
		
		GoalTable.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{
				int vSelectedItem = GoalTable.getSelectionIndex();
				String   vContent="";
				Calendar vDueDate=null;
				Calendar vAchieved=null;
				Calendar vCreated = Calendar.getInstance();	
				
				if ((vSelectedItem+1)<GoalTable.getItemCount())
				{
					try
					{
						TableItem vItem = GoalTable.getItem(vSelectedItem);
						sStmGoalSelectId.setInt(1, Integer.valueOf(vItem.getText(0)));
						ResultSet vGoal = sStmGoalSelectId.executeQuery();
						vGoal.next();
						vContent  = vGoal.getString(1);
						vDueDate = Calendar.getInstance();
						vDueDate.setTime(vGoal.getDate(2));
						if (vGoal.getDate(3)!=null)
						{
							vAchieved = Calendar.getInstance();	
							vAchieved.setTime(vGoal.getDate(3));
						}	
						vCreated.setTime(vGoal.getDate(4));
						vGoal.close();					
					}
					catch (SQLException vEx) 
					{
						errorPrint(vEx);
					}					
				}
				vGoalContent.setText(vContent);					
				
				if (vDueDate!=null)
				{
					vGoalDueDate.setDate(vDueDate.get(Calendar.YEAR),vDueDate.get(Calendar.MONTH),vDueDate.get(Calendar.DAY_OF_MONTH));
				}
				if (vAchieved!=null)
				{					
					vGoalAchieved.setDate(vAchieved.get(Calendar.YEAR),vAchieved.get(Calendar.MONTH),vAchieved.get(Calendar.DAY_OF_MONTH));
					vGoalAchieved.setVisible(true);
					vGoalFlagAchieved.setSelection(true);
				}	
				else
				{
					vGoalAchieved.setVisible(false);
					vGoalFlagAchieved.setSelection(false);						
				}
			}
		});			
		for(int i = 0;i<cColumnsLenght;i++)
		{
			vColumns[i] = new TableColumn(GoalTable,SWT.LEFT);
			vColumns[i].setText(vColumnTitle[i]);
			vColumns[i].setWidth(vColumnWidth[i]);
		}
		FillGoalTable();

		vX=vTableWidth+10+cXshift;
		Text vLabDueDate = new Text(vComposite,SWT.SINGLE);
		vLabDueDate.setBounds(vX, vY,130,20);  			
		vLabDueDate.setText("Срок достижения цели");
		vGoalDueDate.setBounds(vX, vY+25,170,150); 

		vX+=170+cXshift;
		Text vLabGoal = new Text(vComposite,SWT.SINGLE);
		vLabGoal.setBounds(vX, vY,130,20);  			
		vLabGoal.setText("Цель");	    	
		vGoalContent.setTextLimit(500);
		vGoalContent.setBounds(vX, vY+29,200, 141); 		

		
		vX=vX+200+cXshift;		
		Text vLabGoalAchieved = new Text(vComposite,SWT.SINGLE);
		vLabGoalAchieved.setBounds(vX, vY,130,20);  			
		vLabGoalAchieved.setText("Дата достижения цели");		    	
		
		vGoalFlagAchieved.setBounds(vX, vY+25,120, 20);		
		vGoalFlagAchieved.setText("Цель достигнута");
		vGoalFlagAchieved.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{
				if (vGoalFlagAchieved.getSelection())
				{
					vGoalAchieved.setVisible(true);
				}
				else
				{
					vGoalAchieved.setVisible(false);
				}
			}
		});			
		
		vY+=50;
		vGoalAchieved.setBounds(vX-3, vY,170,150);  	
		vGoalAchieved.setVisible(false);		
		
		vX=vTableWidth+200;
		vY+=150;
		Button vSave = new Button(vComposite,SWT.PUSH);
		vSave.setBounds(vX,vY,70,25);
		vSave.setText("Сохранить");
		vSave.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{
				Calendar vDueDate = Calendar.getInstance();
				vDueDate.set(vGoalDueDate.getYear(), vGoalDueDate.getMonth(), vGoalDueDate.getDay());

				Calendar vAchieved = null;
				if (vGoalFlagAchieved.getSelection())
				{
					vAchieved = Calendar.getInstance();
					vAchieved.set(vGoalAchieved.getYear(), vGoalAchieved.getMonth(), vGoalAchieved.getDay());
				}
					  
				int vSelectedItem = GoalTable.getSelectionIndex();
				try 
				{						
					if ((vSelectedItem+1)<GoalTable.getItemCount() && vSelectedItem!=-1)
					{
						sStmGoalUpdate.setString(1,vGoalContent.getText());
						sStmGoalUpdate.setDate(2,(java.sql.Date)new java.sql.Date(vDueDate.getTime().getTime()));
						if (vAchieved!=null)
						{	                    	
							sStmGoalUpdate.setDate(3,(java.sql.Date)new java.sql.Date(vAchieved.getTime().getTime()));
	                    }
						else
						{
							sStmGoalUpdate.setDate(3,null);
	                    }
						TableItem vItem = GoalTable.getItem(vSelectedItem);
						sStmGoalUpdate.setInt(4, Integer.valueOf(vItem.getText(0)));
						sStmGoalUpdate.executeUpdate();
						FillGoalTable();
						GoalTable.setSelection(vSelectedItem);
					} 
					else
					{
						sStmGoalInsert.setString(1,vGoalContent.getText());
						sStmGoalInsert.setDate(2,(java.sql.Date)new java.sql.Date(vDueDate.getTime().getTime()));

						if (vAchieved!=null)
						{
							sStmGoalInsert.setDate(3,(java.sql.Date)new java.sql.Date(vAchieved.getTime().getTime()));
	                    }
						else
						{
							sStmGoalInsert.setDate(3,null);
	                    }
						Calendar vCreated = Calendar.getInstance();	                    	
						sStmGoalInsert.setDate(4,(java.sql.Date)new java.sql.Date(vCreated.getTime().getTime()));
						
						sStmGoalInsert.executeUpdate();
						FillGoalTable();
						GoalTable.setSelection(GoalTable.getItemCount()-1);
                    }
				} 
				catch (SQLException vEx) 
				{
					errorPrint(vEx);
				}
			}
		});
		
		vX=vX+70+cXshift;	
		Button vDelete = new Button(vComposite,SWT.PUSH);
		vDelete.setBounds(vX,vY,70,25);
		vDelete.setText("Удалить");
		vDelete.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{			
				int vSelectedItem = GoalTable.getSelectionIndex();
				try 
				{						
					if ((vSelectedItem+1)<GoalTable.getItemCount() && vSelectedItem!=-1)
					{
						MessageBox vBox = new MessageBox(pShell, SWT.YES|SWT.NO);
						vBox.setMessage("Вы уверены, что хотите удалить цель?");
						if (vBox.open()==SWT.YES)
						{
							TableItem vItem = GoalTable.getItem(vSelectedItem);
							sStmGoalDelete.setInt(1, Integer.valueOf(vItem.getText(0)));
							sStmGoalDelete.executeUpdate();
							FillGoalTable();
							if (vSelectedItem>1)
							{
								vSelectedItem--;
                    		}
							GoalTable.setSelection(vSelectedItem);
                    	}
					} 
				} 
				catch (SQLException vEx) 
				{
					errorPrint(vEx);
				}					
			}
		});	
	}
	
	
//	
	
	private static void ContentFeats(final Shell pShell, TabFolder pTabFolder, TabItem pTabFeats)
	{ 	
		Composite vComposite = new Composite(pTabFolder, SWT.NONE);
		pTabFeats.setControl(vComposite);	  			
		
		int vY=10;
		int vX=0;
		final int cXshift=10;
		final int cTableX=0;
		final int cTableHight=cTabHeight-50;
		int vTableWidth=0;

		final int cColumnsLenght=3;
		TableColumn[] vColumns = new TableColumn[cColumnsLenght];
		String[] vColumnTitle =  {"Id","Достижение", "Дата"};
		final int cMainColunmWidth=cTabWidth-540;
		int[] vColumnWidth={30,cMainColunmWidth,90};		
			
		for(int i = 0;i<cColumnsLenght;i++)
		{
			vTableWidth+=vColumnWidth[i];
		}		
		vTableWidth+=4;
		
		final Text     vFeatContent = new Text(vComposite, SWT.BORDER | SWT.H_SCROLL |SWT.V_SCROLL);		
		final DateTime vFeatAchieved = new DateTime(vComposite,SWT.CALENDAR);		
		
		FeatTable = new Table(vComposite, SWT.FULL_SELECTION | SWT.BORDER);
		FeatTable.setLinesVisible(true);
		FeatTable.setHeaderVisible(true);
		FeatTable.setBounds(cTableX+4,vY,vTableWidth,cTableHight);
	
		for(int i = 0;i<cColumnsLenght;i++)
		{
			vColumns[i] = new TableColumn(FeatTable,SWT.LEFT);
			vColumns[i].setText(vColumnTitle[i]);
			vColumns[i].setWidth(vColumnWidth[i]);
		}		

		FeatTable.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{
				int vSelectedItem = FeatTable.getSelectionIndex();
				String   vContent="";
				Calendar vAchieved=null;
				
				if ((vSelectedItem+1)<FeatTable.getItemCount())
				{
					try
					{
						TableItem vItem = FeatTable.getItem(vSelectedItem);
						sStmFeatSelectId.setInt(1, Integer.valueOf(vItem.getText(0)));
						ResultSet vFeat = sStmFeatSelectId.executeQuery();
						vFeat.next();
						vContent  = vFeat.getString(1);
   					    vAchieved = Calendar.getInstance();	
						vAchieved.setTime(vFeat.getDate(2));
						vFeat.close();					
					}
					catch (SQLException vEx) 
					{
						errorPrint(vEx);
					}					
				}
				vFeatContent.setText(vContent);					
				
				if (vAchieved!=null)
				{
					vFeatAchieved.setDate(vAchieved.get(Calendar.YEAR),vAchieved.get(Calendar.MONTH),vAchieved.get(Calendar.DAY_OF_MONTH));
				}
			}
		});			
		FillFeatTable();

		vX=vTableWidth+cXshift+10;
		Text vLabGoal = new Text(vComposite,SWT.SINGLE);
		vLabGoal.setBounds(vX, vY,130,20);  			
		vLabGoal.setText("Достижение");	    	
		vFeatContent.setTextLimit(500);
		vFeatContent.setBounds(vX, vY+29,200, 141); 	

		vX+=cXshift+200;		
		Text vLabGoalAchieved = new Text(vComposite,SWT.SINGLE);
		vLabGoalAchieved.setBounds(vX, vY,130,20);  			
		vLabGoalAchieved.setText("Дата достижения");	    	
		vFeatAchieved.setBounds(vX, vY+25,170,150);  	

	    	
		vX=vTableWidth+150;
		vY+=200;
		Button vSave = new Button(vComposite,SWT.PUSH);
		vSave.setBounds(vX,vY,70,25);
		vSave.setText("Сохранить");
		vSave.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{

				Calendar vAchieved = null;
				vAchieved = Calendar.getInstance();
				vAchieved.set(vFeatAchieved.getYear(), vFeatAchieved.getMonth(),vFeatAchieved.getDay());
					  
				int vSelectedItem = FeatTable.getSelectionIndex();
				try 
				{						
					if ((vSelectedItem+1)<FeatTable.getItemCount() && vSelectedItem!=-1)
					{
						sStmFeatUpdate.setString(1,vFeatContent.getText());
						sStmFeatUpdate.setDate(2,(java.sql.Date)new java.sql.Date(vAchieved.getTime().getTime()));
			
						TableItem vItem = FeatTable.getItem(vSelectedItem);
						sStmFeatUpdate.setInt(3, Integer.valueOf(vItem.getText(0)));
						sStmFeatUpdate.executeUpdate();
						FillFeatTable();
						FeatTable.setSelection(vSelectedItem);
					} 
					else
					{
						sStmFeatInsert.setString(1,vFeatContent.getText());
						sStmFeatInsert.setDate(2,(java.sql.Date)new java.sql.Date(vAchieved.getTime().getTime()));

						sStmFeatInsert.executeUpdate();
						FillFeatTable();
						FeatTable.setSelection(FeatTable.getItemCount()-1);
                    }
				} 
				catch (SQLException vEx) 
				{
					errorPrint(vEx);
				}
			}
		});
		
		vX+=70+cXshift;	
		Button vDelete = new Button(vComposite,SWT.PUSH);
		vDelete.setBounds(vX,vY,70,25);
		vDelete.setText("Удалить");
		vDelete.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{			
				int vSelectedItem = FeatTable.getSelectionIndex();
				try 
				{						
					if ((vSelectedItem+1)<FeatTable.getItemCount() && vSelectedItem!=-1)
					{
						MessageBox vBox = new MessageBox(pShell, SWT.YES|SWT.NO);
						vBox.setMessage("Вы уверены, что хотите удалить достижение?");
						if (vBox.open()==SWT.YES)
						{
							TableItem vItem = FeatTable.getItem(vSelectedItem);
							sStmFeatDelete.setInt(1, Integer.valueOf(vItem.getText(0)));
							sStmFeatDelete.executeUpdate();
							FillFeatTable();
							if (vSelectedItem>1)
							{
								vSelectedItem--;
                    		}
							FeatTable.setSelection(vSelectedItem);
                    	}
					} 
				} 
				catch (SQLException vEx) 
				{
					errorPrint(vEx);
				}					
			}
		});
	
	}
	private static void Content(final Shell pShell)
	{   
		final TabFolder vTabFolder = new TabFolder (pShell, SWT.NONE);
		vTabFolder.setBounds(13,15,cTabWidth,cTabHeight);
		
		TabItem vTabGoals = new TabItem (vTabFolder,SWT.NULL);
		vTabGoals.setText ("Цели");

		TabItem vTabFeats = new TabItem (vTabFolder, SWT.NULL);
		vTabFeats.setText ("Достижения");

		ContentGoals(pShell, vTabFolder, vTabGoals);
		ContentFeats(pShell, vTabFolder, vTabFeats);
	}
	
	private static void DialogAbout(Display pDisplay)
	{
		final Shell vSAbout = new Shell(pDisplay, SWT.CLOSE | SWT.TITLE| SWT.APPLICATION_MODAL);
		vSAbout.setBounds(100, 100, 400, 170);
		vSAbout.setText("О программе");
		vSAbout.setModified(false);
		
		int vX=30;
		int vY=20;
		Label vName = new Label(vSAbout,SWT.CENTER);
		vName.setBounds(vX, vY, 300, 30);
		vName.setText("\"Цели и достижения\" v.0.1");
		vY+=30;
		Label vAutor = new Label(vSAbout,SWT.CENTER);
		vAutor.setBounds(vX, vY, 300, 30);
		vAutor.setText("Автор: Петрелевич Сергей");
		vY+=30;
		Link vSite = new Link(vSAbout, SWT.CENTER);
		vSite.setBounds(vX+60, vY, 300, 30);
		vSite.setText("Сайт программы: <a href=\"http://www.smartyIT.ru\">www.smartyIT.ru</a>");
		vSite.addSelectionListener(new SelectionAdapter() 
		{
	            public void widgetSelected(SelectionEvent e) 
	            {
	            	try 
	            	{
	            		URI vUri = new URI("http://www.smartyIT.ru");
	            		Desktop vDesktop = Desktop.getDesktop();
	            		if (vDesktop.isSupported(Desktop.Action.BROWSE)) 
	            		{
	            			vDesktop.browse(vUri);
	            		}
	            	}
	            	catch(IOException vEx) 
	            	{
	            		errorPrint(vEx);
	            	}
	            	catch(URISyntaxException vEx) 
	            	{
	            		errorPrint(vEx); 
	            	}
	            }
	        });            		
		vSAbout.open();		
	}

		
	private static void FillGoalTable()
	{
		GoalTable.removeAll();
		try 
		{
			ResultSet vGoals = sStmGoalSelectAll.executeQuery();
			TableItem vItem;
			while (vGoals.next())
			{
				vItem = new TableItem(GoalTable,SWT.NONE);
				vItem.setText(new String[] 
				                         {  vGoals.getString(1) ,
											vGoals.getString(2),
											d2s(vGoals.getDate(3)),
											d2s(vGoals.getDate(4)),
											d2s(vGoals.getDate(5))
				                         });        	  
			}
			vGoals.close();
		} 
		catch (SQLException vEx) 
        {
			errorPrint(vEx);
        }
		AddNextItem(GoalTable);
	}

	private static void FillFeatTable()
	{
		FeatTable.removeAll();
		try 
		{
			ResultSet vFeats = sStmFeatSelectAll.executeQuery();
			TableItem vItem;
			while (vFeats.next())
			{
				vItem = new TableItem(FeatTable,SWT.NONE);
				vItem.setText(new String[] 
				                         {  vFeats.getString(1) ,
											vFeats.getString(2),
											d2s(vFeats.getDate(3))
				                         });        	  
			}
			vFeats.close();
		} 
		catch (SQLException vEx) 
        {
			errorPrint(vEx);
        }
		AddNextItem(FeatTable);
	}	
	
	
	private static void AddNextItem(Table pTable)
	{
		@SuppressWarnings("unused")
		TableItem vNextItem = new TableItem(pTable,SWT.NONE);  
	}
	
	private static String d2s(Date pDate)
	{
		if (pDate==null)
		{
			return "";
		}
		else
		{
			DateFormat vDF = new SimpleDateFormat("dd.MM.yyyy");
			return vDF.format(pDate);
			  
		}
	}	

	private static void errorPrint(Throwable e) 
    {
    	if (e instanceof SQLException)
    	{
    		SQLExceptionPrint((SQLException)e);
    	}
        else 
        {
           System.out.println("A non SQL error occured.");
           e.printStackTrace();
        } 	
    }	

	private static void SQLExceptionPrint(SQLException sqle) 
    {
    	while (sqle != null) 
    	{
           System.out.println("\n---SQLException Caught---\n");
           System.out.println("SQLState: " + (sqle).getSQLState());
           System.out.println("Severity: " + (sqle).getErrorCode());
           System.out.println("Message:  " + (sqle).getMessage()); 
           sqle.printStackTrace();  
           sqle = sqle.getNextException();
       }
    }     
    
	private static boolean checkTable() throws SQLException 
    {
        try 
        {
        	Statement vStm = sConnection.createStatement();
        	vStm.execute("update tGoals set Content = 'check' where 1=3");
        }
        catch (SQLException vEx) 
        {
           String vError = vEx.getSQLState();
           /** If table exists will get -  WARNING 02000: No row was found **/
           if (vError.equals("42X05"))   
           {  
        	   // Table does not exist
        	   return false;
           }  
           else if (vError.equals("42X14")||vError.equals("42821"))  
           {
               System.out.println("Incorrect table definition");
               throw vEx;   
           } 
           else 
           { 
               System.out.println("Unhandled SQLException" );
               throw vEx; 
           }
        }
        System.out.println("Just got the warning - table exists OK ");
        return true;
     }   	
	
	
	
}




