/******************************************************************************* * Copyright (c) 2010 Intel Corporation. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Intel - initial API and implementation *******************************************************************************/ package org.yocto.sdk.remotetools.views; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.part.*; import org.eclipse.jface.viewers.*; import org.eclipse.swt.graphics.Image; import org.eclipse.ui.*; import org.eclipse.swt.SWT; /** * This sample class demonstrates how to plug-in a new * workbench view. The view shows data obtained from the * model. The sample creates a dummy model on the fly, * but a real implementation would connect to the model * available either in this or another plug-in (e.g. the workspace). * The view is connected to the model using a content provider. *

* The view uses a label provider to define how model * objects should be presented in the view. Each * view can present the same model objects using * different labels and icons, if needed. Alternatively, * a single label provider can be shared between views * in order to ensure that objects of the same type are * presented in the same way everywhere. *

*/ public class BaseFileView extends ViewPart { /** * The ID of the view as specified by the extension. */ public static final String ID = "org.yocto.sdk.remotetools.views.BaseFileView"; private TableViewer viewer; private String filename; /* * The content provider class is responsible for * providing objects to the view. It can wrap * existing objects in adapters or simply return * objects as-is. These objects may be sensitive * to the current input of the view, or ignore * it and always show the same content * (like Task List, for example). */ class ViewContentProvider implements IStructuredContentProvider { public void inputChanged(Viewer v, Object oldInput, Object newInput) { if(newInput instanceof String) filename=(String)newInput; } public void dispose() { } public Object[] getElements(Object parent) { ArrayList elements=new ArrayList (); BufferedReader in; String line; try { in=new BufferedReader(new FileReader(filename)); }catch (Exception e) { return new String [] {"Invalid file " + filename}; } try { do { line=in.readLine(); if(line!=null) elements.add(line); }while(line!=null); }catch (Exception e) { e.printStackTrace(); } return (String[]) elements.toArray(new String[elements.size()]); } } class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { return getText(obj); } public Image getColumnImage(Object obj, int index) { //return getImage(obj); return null; } public Image getImage(Object obj) { return PlatformUI.getWorkbench(). getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); } } /** * The constructor. */ public BaseFileView() { } public BaseFileView(String file) { this(); this.filename=file; } /** * This is a callback that will allow us * to create the viewer and initialize it. */ public void createPartControl(Composite parent) { viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setInput(filename); } public void setInput(String filename) { viewer.setInput(filename); } /** * Passing the focus request to the viewer's control. */ public void setFocus() { viewer.getControl().setFocus(); } }