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.views;
23  
24  import org.apache.log4j.Logger;
25  import org.eclipse.jface.action.*;
26  import org.eclipse.jface.viewers.*;
27  import org.eclipse.swt.SWT;
28  import org.eclipse.swt.widgets.*;
29  import org.eclipse.ui.*;
30  import org.eclipse.ui.part.ViewPart;
31  
32  
33  /**
34  * Prepares the ViewPart for the table viewer.
35  * @author zhanghao
36  *
37  */
38  public class TableViewPart extends ViewPart {
39  
40  	/**
41  	 * Log4j instance.
42  	 */
43  	private static Logger log = Logger.getLogger(TableViewPart.class);
44  	
45  	protected static final String TAG_COLUMN = "column";
46  	protected static final String TAG_NUMBER = "number";
47  	protected static final String TAG_WIDTH = "width";
48  
49  	private String columnHeaders[];
50  	private ColumnLayoutData columnLayouts[];
51  	private IAction doubleClickAction;
52  	private IMemento memento;
53  
54  	private Table table;
55  	private TableViewer viewer;
56  
57  	public void createPartControl(Composite parent) {
58  		viewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION
59  				| SWT.H_SCROLL | SWT.V_SCROLL);
60  
61  		table = viewer.getTable();
62  		table.setHeaderVisible(true);
63  		table.setLinesVisible(true);
64  
65  		createColumns();
66  		createActions();
67  		hookMenus();
68  		hookEvents();
69  		contributeToActionBars();
70  	}
71  
72  	protected void createColumns() {
73  		if (memento != null) {
74  			restoreColumnWidths(memento);
75  		}
76  
77  		TableLayout layout = new TableLayout();
78  		table.setLayout(layout);
79  
80  		for (int i = 0; i < columnHeaders.length; i++) {
81  			TableColumn tc = new TableColumn(table, SWT.NONE, i);
82  			
83  			log.debug("Creating Column " + columnHeaders[i]);
84  			
85  			tc.setText(columnHeaders[i]);
86  			tc.setResizable(columnLayouts[i].resizable);
87  			layout.addColumnData(columnLayouts[i]);
88  		}
89  		
90  		//table.pack();
91  	}
92  
93  	protected void restoreColumnWidths(IMemento memento) {
94  		IMemento children[] = memento.getChildren(TAG_COLUMN);
95  		if (children != null) {
96  			for (int i = 0; i < children.length; i++) {
97  				Integer val = children[i].getInteger(TAG_NUMBER);
98  				if (val != null) {
99  					int index = val.intValue();
100 					val = children[i].getInteger(TAG_WIDTH);
101 					if (val != null) {
102 						columnLayouts[index] = new ColumnPixelData(val
103 								.intValue(), true);
104 					}
105 				}
106 			}
107 		}
108 	}
109 
110 	protected void saveColumnWidths(IMemento memento) {
111 		Table table = viewer.getTable();
112 		TableColumn columns[] = table.getColumns();
113 
114 		for (int i = 0; i < columns.length; i++) {
115 			if (columnLayouts[i].resizable) {
116 				IMemento child = memento.createChild(TAG_COLUMN);
117 				child.putInteger(TAG_NUMBER, i);
118 				child.putInteger(TAG_WIDTH, columns[i].getWidth());
119 			}
120 		}
121 	}
122 
123 	protected void hookMenus() {
124 		MenuManager menuMgr = new MenuManager("#PopupMenu");
125 		menuMgr.setRemoveAllWhenShown(true);
126 		menuMgr.addMenuListener(new IMenuListener() {
127 			public void menuAboutToShow(IMenuManager manager) {
128 				TableViewPart.this.fillContextMenu(manager);
129 			}
130 		});
131 		Menu menu = menuMgr.createContextMenu(viewer.getControl());
132 		viewer.getControl().setMenu(menu);
133 		getSite().registerContextMenu(menuMgr, viewer);
134 	}
135 
136 	protected void hookEvents() {
137 		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
138 			public void selectionChanged(SelectionChangedEvent event) {
139 				if (event.getSelection() != null)
140 					TableViewPart.this.selectionChanged(event);
141 			}
142 		});
143 		viewer.addDoubleClickListener(new IDoubleClickListener() {
144 			public void doubleClick(DoubleClickEvent event) {
145 				try {
146 					doubleClickAction.run();
147 				} catch (Exception e) {
148 					
149 				}
150 			}
151 		});
152 	}
153 
154 	protected void contributeToActionBars() {
155 		IActionBars bars = getViewSite().getActionBars();
156 		fillLocalPullDown(bars.getMenuManager());
157 		fillLocalToolBar(bars.getToolBarManager());
158 	}
159 
160 	public void saveState(IMemento memento) {
161 		if (viewer == null) {
162 			if (this.memento != null) // Keep the old state;
163 				memento.putMemento(this.memento);
164 			return;
165 		}
166 
167 		saveColumnWidths(memento);
168 	}
169 
170 	public void init(IViewSite site, IMemento memento) throws PartInitException {
171 		super.init(site, memento);
172 		this.memento = memento;
173 	}
174 
175 	public void setFocus() {
176 		viewer.getControl().setFocus();
177 	}
178 
179 	public Table getTable() {
180 		return table;
181 	}
182 
183 	public TableViewer getViewer() {
184 		return viewer;
185 	}
186 
187 	public void setColumnHeaders(String[] strings) {
188 		columnHeaders = strings;
189 	}
190 
191 	public void setColumnLayouts(ColumnLayoutData[] data) {
192 		columnLayouts = data;
193 	}
194 
195 	public void setDoubleClickAction(IAction action) {
196 		doubleClickAction = action;
197 	}
198 
199 	protected void fillContextMenu(IMenuManager manager) {
200 	}
201 
202 	protected void fillLocalPullDown(IMenuManager manager) {
203 	}
204 
205 	protected void fillLocalToolBar(IToolBarManager manager) {
206 	}
207 
208 	protected void selectionChanged(SelectionChangedEvent event) {
209 	}
210 
211 	protected void createActions() {
212 	}
213 }