View Javadoc

1   /********************************************************************************
2    * Copyright (c) 2001, 2004 IBM Corporation and others.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    * 
8    * Contributors:
9    *     IBM Corporation - initial API and implementation
10   *******************************************************************************/
11  package net.sf.glxdesktop.gui.glxinfo;
12  
13  import net.sf.glxdesktop.gui.glxinfo.dialog.GlxSearchDialog;
14  import net.sf.glxdesktop.gui.glxinfo.dialog.RunGlxinfoCmdDialog;
15  import net.sf.glxdesktop.parser.ConfigFileTools;
16  
17  import org.gnu.gtk.CellRenderer;
18  import org.gnu.gtk.CellRendererText;
19  import org.gnu.gtk.DataColumn;
20  import org.gnu.gtk.DataColumnString;
21  import org.gnu.gtk.Expander;
22  import org.gnu.gtk.Label;
23  import org.gnu.gtk.TreeIter;
24  import org.gnu.gtk.TreeStore;
25  import org.gnu.gtk.TreeViewColumn;
26  
27  
28  public class GlxInfoWindowActions {
29  
30  	private static final String[] frameBufferTitles = { "Vis\nid", "Vis\ndepth", "Vis\ntype", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
31  			"trans\nparent", "buff\nsize", "lev\nel", "render\ntype", "DB", "ste\nreo", "r\nsz", "g\nsz", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
32  			"b\nsz", "a\nsz", "aux\nbuf", "dep\nth", "ste\nncl", "abr", "abg", "abb", "aba", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
33  			"MS\nnum", "MS\nbufs" }; //$NON-NLS-1$ //$NON-NLS-2$
34  
35  	private TreeViewColumn[] frameBufferColumn;
36  
37  	private String glxinfo;
38  
39  	private GlxInfoWindowImpl glxInfoDialog;
40  
41  	private GlxSearchDialog searchDialog;
42  
43  	public GlxInfoWindowActions(GlxInfoWindowImpl glxInfoDialog) {
44  		this.glxInfoDialog = glxInfoDialog;
45  	}
46  	
47  	public void setGlxinfo(String glxinfo) {
48  		this.glxinfo = glxinfo;
49  	}
50  
51  	/***
52  	 * Get a i18n direct redering status string
53  	 * 
54  	 * @return i18n string
55  	 */
56  	public String getI18nDirectRendering() {
57  		String render = getGlxinfoText("direct rendering:", "server glx vendor string:"); //$NON-NLS-1$ //$NON-NLS-2$
58  		if (render.equalsIgnoreCase("No")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
59  			return Messages.getString("GlxInfoWindowImpl.drendering_false"); //$NON-NLS-1$
60  		} else if (render.equalsIgnoreCase("Yes")) { //$NON-NLS-1$
61  			return Messages.getString("GlxInfoWindowImpl.drendering_true"); //$NON-NLS-1$
62  		} else {
63  			return Messages.getString("GlxInfoWindowActions.no_dr_info"); //$NON-NLS-1$
64  		}
65  	}
66  
67  	/***
68  	 * 
69  	 * Get Glx information between two regexp
70  	 * 
71  	 * @param beginRegexp
72  	 * @param endRegexp
73  	 * @return the String between the two regexp
74  	 */
75  	public String getGlxinfoText(String beginRegexp, String endRegexp) {
76  		String glxinfoStr = ConfigFileTools.findTextInInterval(glxinfo,
77  				beginRegexp, endRegexp).trim();
78  		if (!glxinfoStr.trim().equals("")) //$NON-NLS-1$
79  			return glxinfoStr;
80  		else
81  			return Messages.getString("GlxInfoWindowActions.no_info_for_diplay"); //$NON-NLS-1$
82  	}
83  
84  	/***
85  	 * Get Display Name.
86  	 * 
87  	 * @return the DISPLAY name.
88  	 */
89  	public String getDisplayName() {
90  		String glxinfoStr = ConfigFileTools.findTextInInterval(glxinfo,
91  				"name of display:", "display.*screen").trim(); //$NON-NLS-1$ //$NON-NLS-2$
92  		if (!glxinfoStr.trim().equals("")) //$NON-NLS-1$
93  			return glxinfoStr;
94  		else
95  			return glxInfoDialog.entryGlxDisplay.getText();
96  	}
97  
98  	/***
99  	 * Get glx client extensions.
100 	 * 
101 	 * @return glx client extensions string.
102 	 */
103 	public String getGlxClientExtensions() {
104 		return cleanExtensionsString(ConfigFileTools.findTextInInterval(
105 				glxinfo, "client glx extensions:", "GLX version")); //$NON-NLS-1$ //$NON-NLS-2$
106 	}
107 
108 	/***
109 	 * Get glx server extensions.
110 	 * 
111 	 * @return glx server extensions string.
112 	 */
113 	public String getGlxServerExtensions() {
114 		return cleanExtensionsString(ConfigFileTools.findTextInInterval(
115 				glxinfo, "server glx extensions:", "client glx")); //$NON-NLS-1$ //$NON-NLS-2$
116 	}
117 
118 	/***
119 	 * Get GLX extensions.
120 	 * 
121 	 * @return GLX extensions string.
122 	 */
123 	public String getGLXExtensions() {
124 		return cleanExtensionsString(ConfigFileTools.findTextInInterval(
125 				glxinfo, "GLX extensions:", "OpenGL vendor")); //$NON-NLS-1$ //$NON-NLS-2$
126 	}
127 
128 	/***
129 	 * Get OpenGL extensions.
130 	 * 
131 	 * @return OpenGL extensions string.
132 	 */
133 	public String getOpenGLExtensions() {
134 		return cleanExtensionsString(ConfigFileTools.findTextInInterval(
135 				glxinfo, "OpenGL extensions:", "glu version| visual|Vis")); //$NON-NLS-1$ //$NON-NLS-2$
136 	}
137 
138 	/***
139 	 * Get glu extensions.
140 	 * 
141 	 * @return glu extensions string.
142 	 */
143 	public String getGLUExtensions() {
144 		return cleanExtensionsString(ConfigFileTools.findTextInInterval(
145 				glxinfo, "glu extensions:", " visual|Vis")); //$NON-NLS-1$ //$NON-NLS-2$
146 	}
147 
148 	public void openSearchDialog() {
149 		if (searchDialog == null) {
150 			searchDialog = new GlxSearchDialog(this);
151 		} else {
152 			refreshGlxinfo();
153 		}
154 		expandedAllExtensions(false);
155 		searchDialog.show();
156 	}
157 
158 	public void expandedAllExtensions(boolean bool) {
159 		glxInfoDialog.expanderGlxClientExtensions.setExpanded(bool);
160 		glxInfoDialog.expanderGlxServerExtensions.setExpanded(bool);
161 		glxInfoDialog.expanderGlxExtensions.setExpanded(bool);
162 		glxInfoDialog.expanderGluExtensions.setExpanded(bool);
163 		glxInfoDialog.expanderOpenGLExtensions.setExpanded(bool);
164 		glxInfoDialog.expanderGlxClientExtensions.unHighlight();
165 		glxInfoDialog.expanderGlxServerExtensions.unHighlight();
166 		glxInfoDialog.expanderGlxExtensions.unHighlight();
167 		glxInfoDialog.expanderGluExtensions.unHighlight();
168 		glxInfoDialog.expanderOpenGLExtensions.unHighlight();
169 	}
170 
171 	public void refreshGlxinfo() {
172 		String display;
173 		if (!glxInfoDialog.entryGlxDisplay.getText().equals("")) { //$NON-NLS-1$
174 			display = glxInfoDialog.entryGlxDisplay.getText();
175 		} else {
176 			display = System.getenv("DISPLAY"); //$NON-NLS-1$
177 		}
178 		RunGlxinfoCmdDialog glxinfoCmd = new RunGlxinfoCmdDialog("glxinfo -t -display " //$NON-NLS-1$
179 				+ display, glxInfoDialog, this);
180 		glxinfoCmd.execute();
181 	}
182 
183 	/***
184 	 * Set Frame Buffer configuration Tree viewer
185 	 */
186 	public void setGlxTableTreeViewer() {
187 		// String[] frameBufferTitles = getGlxinfoText("cav\n","--").split("//
188 		// |// "); work only without glxinfo "-t" option.
189 		String[] rowsData = getGlxinfoText("--\n", "EOF").split("\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
190 		// Construct a new store for the tree data.
191 		DataColumn[] columns = new DataColumn[frameBufferTitles.length];
192 		for (int i = 0; i < frameBufferTitles.length; i++) {
193 			DataColumnString dataString = new DataColumnString();
194 			columns[i] = dataString;
195 		}
196 		TreeStore store = new TreeStore(columns);
197 		// Create rows.
198 		for (int row = 0; row < rowsData.length; row++) {
199 			String[] tableFields = rowsData[row]
200 					.split("//     |//    |//   |//  |// "); //$NON-NLS-1$
201 			TreeIter iter = store.appendRow(null);
202 			for (int field = 0; field < tableFields.length; field++) {
203 				store.setValue(iter, (DataColumnString) columns[field],
204 						tableFields[field].trim());
205 			}
206 		}
207 		// Clean TreeViewer if it's requiered
208 		if (frameBufferColumn != null) {
209 			for (int i = 0; i < frameBufferColumn.length; i++) {
210 				glxInfoDialog.treeViewGlxTable
211 						.removeColumn(frameBufferColumn[i]);
212 			}
213 			frameBufferColumn = null;
214 		}
215 		glxInfoDialog.treeViewGlxTable.setModel(store);
216 		frameBufferColumn = new TreeViewColumn[frameBufferTitles.length];
217 		// Create and configure all columns.
218 		for (int i = 0; i < frameBufferTitles.length; i++) {
219 			frameBufferColumn[i] = new TreeViewColumn();
220 			frameBufferColumn[i].setTitle(frameBufferTitles[i]);
221 			frameBufferColumn[i].setSortColumn(columns[i]);
222 			glxInfoDialog.treeViewGlxTable.appendColumn(frameBufferColumn[i]);
223 			CellRenderer renderer = new CellRendererText();
224 			frameBufferColumn[i].packStart(renderer, true);
225 			frameBufferColumn[i].addAttributeMapping(renderer,
226 					CellRendererText.Attribute.TEXT,
227 					(DataColumnString) columns[i]);
228 		}
229 	}
230 	
231 	/***
232 	 * Play with the exander visible propreties.
233 	 * 
234 	 * @param expander
235 	 *            to set.
236 	 * @param isVisible
237 	 *            visibility stat to set.
238 	 */
239 	public void setVisibility(Expander expander, boolean isVisible) {
240 		if (isVisible) {
241 			expander.show();
242 		} else {
243 			expander.hide();
244 		}
245 	}
246 	
247 	/***
248 	 * This method expand and highlight ALL extentions Expander, if the given
249 	 * regexp search string is found. see {@link GlxInfoWindowActions}
250 	 * <code>makeFindResult</code> method for more informations.
251 	 * 
252 	 * @param regexp
253 	 *            to search.
254 	 * @param caseSensitive
255 	 *            set this to true to make the search case sensitive.
256 	 */
257 	public void searchExtensions(String regexp, boolean caseSensitive) {
258 		makeSearchResult(regexp, caseSensitive,
259 				glxInfoDialog.expanderGlxExtensions,
260 				glxInfoDialog.labelGlxExtensions);
261 		makeSearchResult(regexp, caseSensitive,
262 				glxInfoDialog.expanderGluExtensions,
263 				glxInfoDialog.labelGluExtensions);
264 		makeSearchResult(regexp, caseSensitive,
265 				glxInfoDialog.expanderGlxServerExtensions,
266 				glxInfoDialog.labelServerGlxExtensions);
267 		makeSearchResult(regexp, caseSensitive,
268 				glxInfoDialog.expanderGlxClientExtensions,
269 				glxInfoDialog.labelClientGlxExtensions);
270 		makeSearchResult(regexp, caseSensitive,
271 				glxInfoDialog.expanderOpenGLExtensions,
272 				glxInfoDialog.labelOpenGLExtensions);
273 	}
274 	
275 	/***
276 	 * This method expand and highlight a extentions Expander, if the given
277 	 * regexp search string is found.
278 	 * 
279 	 * @param regexp
280 	 *            to search.
281 	 * @param caseSensitive
282 	 *            set this to true to make the search case sensitive.
283 	 * @param expander
284 	 *            the Exander that contain the glx extensions.
285 	 * @param label
286 	 *            the Label that contain the glx extensions string.
287 	 */
288 	private void makeSearchResult(String regexp, boolean caseSensitive,
289 			Expander expander, Label label) {
290 		String glxExtensions;
291 		if (!caseSensitive) {
292 			glxExtensions = label.getText().toUpperCase();
293 			regexp = regexp.toUpperCase();
294 		} else
295 			glxExtensions = label.getText();
296 		
297 		if (glxExtensions.contains(regexp)) {
298 			String txt = label.getText().replaceAll(regexp,
299 					"<b>" + regexp + "</b>"); //$NON-NLS-1$ //$NON-NLS-2$
300 			txt = txt.replaceAll(regexp.toLowerCase(), "<b>" //$NON-NLS-1$
301 					+ regexp.toLowerCase() + "</b>"); //$NON-NLS-1$
302 			label.setMarkup(txt);
303 			expander.setExpanded(true);
304 			expander.highlight();
305 		} else
306 			expander.unHighlight();
307 	}
308 
309 	private String cleanExtensionsString(String extentionsStr) {
310 		extentionsStr = extentionsStr.replaceAll(" ", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$
311 		return extentionsStr.replaceAll(",", ", ").trim(); //$NON-NLS-1$ //$NON-NLS-2$
312 	}
313 }