View Javadoc

1   package org.fosstrak.llrp.commander.views.roaccess;
2   
3   import java.util.HashMap;
4   
5   import org.apache.log4j.Logger;
6   import org.eclipse.jface.dialogs.Dialog;
7   import org.eclipse.jface.window.Window;
8   import org.eclipse.swt.SWT;
9   import org.eclipse.swt.events.SelectionAdapter;
10  import org.eclipse.swt.events.SelectionEvent;
11  import org.eclipse.swt.graphics.Color;
12  import org.eclipse.swt.layout.GridData;
13  import org.eclipse.swt.layout.GridLayout;
14  import org.eclipse.swt.widgets.Button;
15  import org.eclipse.swt.widgets.Composite;
16  import org.eclipse.swt.widgets.Control;
17  import org.eclipse.swt.widgets.Label;
18  import org.eclipse.swt.widgets.Shell;
19  import org.eclipse.swt.widgets.Text;
20  import org.epcglobalinc.tdt.LevelTypeList;
21  import org.fosstrak.llrp.client.repository.sql.roaccess.AbstractSQLROAccessReportsRepository;
22  import org.fosstrak.llrp.client.repository.sql.roaccess.DerbyROAccessReportsRepository;
23  import org.fosstrak.llrp.client.repository.sql.roaccess.ROAccessItem;
24  import org.fosstrak.tdt.TDTEngine;
25  
26  public class DetailsDialog extends Dialog {
27  	
28  	final protected ROAccessItem item;
29  	
30  	// the log4j logger.
31  	private static Logger log = Logger.getLogger(DetailsDialog.class);
32  
33  	public DetailsDialog(Shell parent, ROAccessItem item) {
34  		super(parent);
35  		this.item = item;
36  	}
37  
38  	@Override
39  	protected Control createContents(Composite parent) {
40  		GridLayout layout = new GridLayout();
41  		layout.numColumns = 2;
42  		
43  		parent.getShell().setLayout(layout);
44  		parent.getShell().setText("Details page:");
45  		
46  		GridData gridAll = new GridData();
47  		gridAll.horizontalSpan = 2;
48  		
49  		String[][] cnt = DerbyROAccessReportsRepository.COLUMN_NAMES_AND_TYPES;
50  		Color color = getShell().getDisplay().getSystemColor(
51  				SWT.COLOR_WHITE);
52  		for (int i=0; i<cnt.length; i++) {
53  			final Label lbl = new Label(parent, SWT.NONE);
54  			lbl.setText(cnt[i][0] + ": ");
55  			lbl.setFont(parent.getFont());
56  			
57  			final Text txt = new Text(parent, SWT.NONE);
58  			String s = item.getAsString(i + 1);
59  			if (null == s) s = "";
60  			txt.setText(s);
61  			txt.setEditable(false);
62  			txt.setBackground(color);
63  			txt.setFont(parent.getFont());
64  			
65  			if (!s.equalsIgnoreCase("") && 
66  					(AbstractSQLROAccessReportsRepository.CINDEX_EPC == i+1)) {
67  				// try to add the tdt stuff...
68  				try {
69  					TDTEngine tdt = new TDTEngine();
70  					
71  					String binary = tdt.hex2bin(s);
72  					if (binary.startsWith("1")) binary = "00" + binary;
73  					
74  					// try to translate to tag and epc
75  					String asEPC = tdt.convert(
76  							binary, 
77  							new HashMap<String, String> (), 
78  							LevelTypeList.PURE_IDENTITY);
79  					
80  					final Label lblEPC = new Label(parent, SWT.NONE);
81  					lblEPC.setText("   decoded as epc-pure: ");
82  					lblEPC.setFont(parent.getFont());
83  					
84  					final Text txtEPC = new Text(parent, SWT.NONE);
85  					txtEPC.setText(asEPC);
86  					txtEPC.setFont(parent.getFont());
87  					txtEPC.setEditable(false);
88  					txtEPC.setBackground(color);
89  					
90  					String asTag = tdt.convert(
91  							binary, 
92  							new HashMap<String, String> (), 
93  							LevelTypeList.TAG_ENCODING);
94  					
95  					final Label lblTag = new Label(parent, SWT.NONE);
96  					lblTag.setText("   decoded as epc-tag: ");
97  					lblTag.setFont(parent.getFont());
98  					
99  					final Text txtTag = new Text(parent, SWT.NONE);
100 					txtTag.setText(asTag);
101 					txtTag.setFont(parent.getFont());
102 					txtTag.setEditable(false);
103 					txtTag.setBackground(color);
104 				} catch (Exception e) {
105 					log.debug(String.format("Could not translate epc: '%s'",
106 							e.getMessage()));
107 				}
108 			}
109 		}
110 		
111 		// add CSV (for now not active as it destroys the layout)
112 //		final Label lbl = new Label(parent, SWT.NONE);
113 //		lbl.setText("as CSV");
114 //		lbl.setFont(parent.getFont());
115 //		
116 //		final Text txt = new Text(parent, SWT.NONE);
117 //		txt.setFont(parent.getFont());
118 //		txt.setText(item.getAsCSV());
119 //		
120 //		
121 		final Button btnOk = new Button(parent, SWT.PUSH);
122 		btnOk.setText("Close");
123 		btnOk.setFont(parent.getFont());
124 		btnOk.setLayoutData(gridAll);
125 		btnOk.addSelectionListener(new SelectionAdapter() {
126 		      public void widgetSelected(SelectionEvent e) {
127 		    	  setReturnCode(Window.OK);
128 		    	  close();
129 		      }
130 		    });
131 		
132 		parent.pack();
133 		return parent;
134 	}
135 }