View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.dialogs;
23  
24  import java.util.Iterator;
25  
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.events.SelectionAdapter;
28  import org.eclipse.swt.events.SelectionEvent;
29  import org.eclipse.swt.graphics.Color;
30  import org.eclipse.swt.layout.GridData;
31  import org.eclipse.swt.layout.GridLayout;
32  import org.eclipse.swt.widgets.Button;
33  import org.eclipse.swt.widgets.Composite;
34  import org.eclipse.swt.widgets.Control;
35  import org.eclipse.swt.widgets.Label;
36  import org.eclipse.swt.widgets.List;
37  import org.eclipse.swt.widgets.Shell;
38  import org.fosstrak.llrp.commander.check.CheckEclipseProject;
39  import org.fosstrak.llrp.commander.check.CheckRepository;
40  import org.fosstrak.llrp.commander.check.HealthCheck;
41  
42  /**
43  * Dialog displaying the results of the sanity-checks. 
44  * @author zhanghao
45  * @author sawielan
46  *
47  */
48  public class HealthCheckDialog extends org.eclipse.jface.dialogs.Dialog {
49  	
50  	private HealthCheck healthCheck;
51  	
52  	/**
53  	 * Default Constructor
54  	 * 
55  	 * @param aShell
56  	 */
57  	public HealthCheckDialog(Shell aShell) {
58  		super(aShell);
59  	}
60  	
61  	/**
62  	 * Initialize the GUI components.
63  	 */
64  	protected Control createContents(Composite parent) {
65  		
66  		parent.getShell().setLayout(new GridLayout(3, false));
67  		parent.getShell().setText("LLRP Client Health Check");
68  		parent.setSize(600, 230);
69  		
70  		GridData gridText = new GridData(GridData.FILL_HORIZONTAL);
71  		gridText.horizontalSpan = 3;
72  		gridText.heightHint = 15;
73  		
74  		GridData gridOutput = new GridData(GridData.FILL_HORIZONTAL);
75  		gridOutput.horizontalSpan = 3;
76  		gridOutput.heightHint = 130;
77  		
78  		GridData gridButton = new GridData(GridData.FILL_BOTH);
79  		gridButton.horizontalSpan = 1;
80  		gridButton.heightHint = 25;
81  		
82          final Label lblCheck = new Label(parent, SWT.NONE);
83          lblCheck.setLayoutData(gridText);
84          lblCheck.setText("Check Progress:");
85          
86          final List outputList = new List(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
87          outputList.setLayoutData(gridOutput);
88          outputList.setBackground(new Color(parent.getDisplay(), 255, 255, 255));
89          outputList.setForeground(new Color(parent.getDisplay(), 0, 0, 0));
90          
91  		final Button btnCheck = new Button(parent, SWT.PUSH);
92  		btnCheck.setText("Check Again");
93  		btnCheck.setLayoutData(gridButton);
94  		
95  		final Button btnFix = new Button(parent, SWT.PUSH);
96  		btnFix.setText("Fix it!");
97  		btnFix.setLayoutData(gridButton);
98  		
99  		final Button btnClose = new Button(parent, SWT.PUSH);
100 		btnClose.setText("Close");
101 		btnClose.setLayoutData(gridButton);
102 		
103 		btnCheck.addSelectionListener(new SelectionAdapter() {
104 		      public void widgetSelected(SelectionEvent e) {
105 		    	  
106 		    	  btnCheck.setEnabled(false);
107 		    	  btnFix.setEnabled(false);
108 		    	  btnClose.setEnabled(false);
109 		    	  
110 		    	  outputList.removeAll();
111 		    	  
112 		    	  if (!getHealthCheck().validate()) {
113 		    		  Iterator<String> i = getHealthCheck().getReport().iterator();
114 		    		  while (i.hasNext()) {
115 		    			  outputList.add(i.next());
116 		    		  }
117 		    		  btnFix.setEnabled(true);
118 		    	  }
119 
120 		    	  btnCheck.setEnabled(true);
121 		    	  btnClose.setEnabled(true);
122 		      }
123 		    });
124 		
125 		
126 		btnFix.addSelectionListener(new SelectionAdapter() {
127 		      public void widgetSelected(SelectionEvent e) {
128 		    	  
129 		    	  btnCheck.setEnabled(false);
130 		    	  btnFix.setEnabled(false);
131 		    	  btnClose.setEnabled(false);
132 		    	  
133 		    	  outputList.removeAll();
134 		    	  
135 		    	  if (!getHealthCheck().validate()) {
136 		    		  
137 		    		  getHealthCheck().fix();
138 		    		  
139 		    		  Iterator<String> i = getHealthCheck().getReport().iterator();
140 		    		  while (i.hasNext()) {
141 		    			  outputList.add(i.next());
142 		    		  }
143 		    		  btnFix.setEnabled(true);
144 		    	  }
145 
146 		    	  btnCheck.setEnabled(true);
147 		    	  btnFix.setEnabled(false);
148 		    	  btnClose.setEnabled(true);
149 		      }
150 		    });
151 		
152 		
153 		btnClose.addSelectionListener(new SelectionAdapter() {
154 		      public void widgetSelected(SelectionEvent e) {
155 		    	  close();
156 		      }
157 		    });
158 		
159 		return parent;
160 		
161 	}
162 	
163 	public HealthCheck getHealthCheck() {
164 		if (null == healthCheck) {
165 			healthCheck = new HealthCheck();
166 			healthCheck.registerCheckItem(new CheckEclipseProject());
167 			healthCheck.registerCheckItem(new CheckRepository());
168 		}
169 		return healthCheck;
170 	}
171 
172 }