diff options
Diffstat (limited to 'plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/BaseFileView.java')
-rw-r--r-- | plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/BaseFileView.java | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/BaseFileView.java b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/BaseFileView.java new file mode 100644 index 0000000..a5801c7 --- /dev/null +++ b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/BaseFileView.java | |||
@@ -0,0 +1,140 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010 Intel Corporation. | ||
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 | * Intel - initial API and implementation | ||
10 | *******************************************************************************/ | ||
11 | package org.yocto.sdk.remotetools.views; | ||
12 | |||
13 | |||
14 | import java.io.BufferedReader; | ||
15 | import java.io.FileReader; | ||
16 | import java.util.ArrayList; | ||
17 | |||
18 | import org.eclipse.swt.widgets.Composite; | ||
19 | import org.eclipse.ui.part.*; | ||
20 | import org.eclipse.jface.viewers.*; | ||
21 | import org.eclipse.swt.graphics.Image; | ||
22 | import org.eclipse.ui.*; | ||
23 | import org.eclipse.swt.SWT; | ||
24 | |||
25 | |||
26 | /** | ||
27 | * This sample class demonstrates how to plug-in a new | ||
28 | * workbench view. The view shows data obtained from the | ||
29 | * model. The sample creates a dummy model on the fly, | ||
30 | * but a real implementation would connect to the model | ||
31 | * available either in this or another plug-in (e.g. the workspace). | ||
32 | * The view is connected to the model using a content provider. | ||
33 | * <p> | ||
34 | * The view uses a label provider to define how model | ||
35 | * objects should be presented in the view. Each | ||
36 | * view can present the same model objects using | ||
37 | * different labels and icons, if needed. Alternatively, | ||
38 | * a single label provider can be shared between views | ||
39 | * in order to ensure that objects of the same type are | ||
40 | * presented in the same way everywhere. | ||
41 | * <p> | ||
42 | */ | ||
43 | |||
44 | public class BaseFileView extends ViewPart { | ||
45 | |||
46 | /** | ||
47 | * The ID of the view as specified by the extension. | ||
48 | */ | ||
49 | public static final String ID = "org.yocto.sdk.remotetools.views.BaseFileView"; | ||
50 | |||
51 | private TableViewer viewer; | ||
52 | |||
53 | private String filename; | ||
54 | |||
55 | /* | ||
56 | * The content provider class is responsible for | ||
57 | * providing objects to the view. It can wrap | ||
58 | * existing objects in adapters or simply return | ||
59 | * objects as-is. These objects may be sensitive | ||
60 | * to the current input of the view, or ignore | ||
61 | * it and always show the same content | ||
62 | * (like Task List, for example). | ||
63 | */ | ||
64 | |||
65 | class ViewContentProvider implements IStructuredContentProvider { | ||
66 | public void inputChanged(Viewer v, Object oldInput, Object newInput) { | ||
67 | if(newInput instanceof String) | ||
68 | filename=(String)newInput; | ||
69 | } | ||
70 | public void dispose() { | ||
71 | } | ||
72 | public Object[] getElements(Object parent) { | ||
73 | ArrayList <String> elements=new ArrayList <String>(); | ||
74 | BufferedReader in; | ||
75 | String line; | ||
76 | try { | ||
77 | in=new BufferedReader(new FileReader(filename)); | ||
78 | }catch (Exception e) { | ||
79 | return new String [] {"Invalid file " + filename}; | ||
80 | } | ||
81 | |||
82 | try { | ||
83 | do { | ||
84 | line=in.readLine(); | ||
85 | if(line!=null) | ||
86 | elements.add(line); | ||
87 | }while(line!=null); | ||
88 | }catch (Exception e) { | ||
89 | e.printStackTrace(); | ||
90 | } | ||
91 | return (String[]) elements.toArray(new String[elements.size()]); | ||
92 | } | ||
93 | } | ||
94 | class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { | ||
95 | public String getColumnText(Object obj, int index) { | ||
96 | return getText(obj); | ||
97 | } | ||
98 | public Image getColumnImage(Object obj, int index) { | ||
99 | //return getImage(obj); | ||
100 | return null; | ||
101 | } | ||
102 | public Image getImage(Object obj) { | ||
103 | return PlatformUI.getWorkbench(). | ||
104 | getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * The constructor. | ||
110 | */ | ||
111 | public BaseFileView() { | ||
112 | } | ||
113 | |||
114 | public BaseFileView(String file) { | ||
115 | this(); | ||
116 | this.filename=file; | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * This is a callback that will allow us | ||
121 | * to create the viewer and initialize it. | ||
122 | */ | ||
123 | public void createPartControl(Composite parent) { | ||
124 | viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); | ||
125 | viewer.setContentProvider(new ViewContentProvider()); | ||
126 | viewer.setLabelProvider(new ViewLabelProvider()); | ||
127 | viewer.setInput(filename); | ||
128 | } | ||
129 | |||
130 | public void setInput(String filename) { | ||
131 | viewer.setInput(filename); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Passing the focus request to the viewer's control. | ||
136 | */ | ||
137 | public void setFocus() { | ||
138 | viewer.getControl().setFocus(); | ||
139 | } | ||
140 | } \ No newline at end of file | ||