|
1 |
|
2 /* |
|
3 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. |
|
5 * This component and the accompanying materials are made available |
|
6 * under the terms of "Eclipse Public License v1.0" |
|
7 * which accompanies this distribution, and is available |
|
8 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 * |
|
10 * Initial Contributors: |
|
11 * Nokia Corporation - initial contribution. |
|
12 * |
|
13 * Contributors: |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 package com.nokia.s60tools.hticonnection.ui.views.main; |
|
20 |
|
21 import org.eclipse.swt.widgets.Composite; |
|
22 import org.eclipse.swt.widgets.Display; |
|
23 import org.eclipse.ui.part.*; |
|
24 import org.eclipse.jface.action.*; |
|
25 import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
26 import org.eclipse.jface.viewers.SelectionChangedEvent; |
|
27 import org.eclipse.ui.*; |
|
28 import org.eclipse.swt.widgets.Menu; |
|
29 import org.eclipse.swt.SWT; |
|
30 |
|
31 import com.nokia.s60tools.hticonnection.HtiApiActivator; |
|
32 import com.nokia.s60tools.hticonnection.HtiConnectionHelpContextIDs; |
|
33 import com.nokia.s60tools.hticonnection.actions.ClearScreenAction; |
|
34 import com.nokia.s60tools.hticonnection.actions.OpenPreferencePageAction; |
|
35 import com.nokia.s60tools.hticonnection.actions.ScrollLockAction; |
|
36 import com.nokia.s60tools.hticonnection.actions.SelectAllAction; |
|
37 import com.nokia.s60tools.hticonnection.actions.StartStopGatewayAction; |
|
38 import com.nokia.s60tools.hticonnection.core.HtiConnection; |
|
39 import com.nokia.s60tools.hticonnection.core.HtiConnection.ConnectionStatus; |
|
40 import com.nokia.s60tools.ui.ICopyActionHandler; |
|
41 import com.nokia.s60tools.ui.IStringProvider; |
|
42 import com.nokia.s60tools.ui.StringArrayClipboardCopyHandler; |
|
43 import com.nokia.s60tools.ui.actions.CopyFromStringProviderToClipboardAction; |
|
44 |
|
45 /** |
|
46 * This class comprises the Main View of the HTI API |
|
47 * application. |
|
48 */ |
|
49 public class MainView extends ViewPart implements IStringProvider { |
|
50 |
|
51 /** |
|
52 * We can get view ID at runtime once the view is instantiated, but we |
|
53 * also need static access to ID in order to be able to invoke the view. |
|
54 */ |
|
55 public static final String ID = "com.nokia.s60tools.hticonnection.ui.views.main.MainView"; //$NON-NLS-1$ |
|
56 /** |
|
57 * Viewer for showing text output. |
|
58 */ |
|
59 private MainTextViewer textViewer; |
|
60 |
|
61 // |
|
62 // Actions. |
|
63 // |
|
64 private StartStopGatewayAction startStopGatewayAction; |
|
65 private Action preferencesAction; |
|
66 private Action clearScreenAction; |
|
67 private Action scrollLockAction; |
|
68 private Action copyAction; |
|
69 private Action selectAllAction; |
|
70 |
|
71 /** |
|
72 * The constructor. |
|
73 */ |
|
74 public MainView() { |
|
75 } |
|
76 |
|
77 /** |
|
78 * This is called by framework when the controls for |
|
79 * the view should be created. |
|
80 */ |
|
81 public void createPartControl(Composite parent) { |
|
82 |
|
83 // |
|
84 // Actions invoked by content providers may set enable/disable |
|
85 // states for the actions, therefore all the action has to be |
|
86 // created before creating the controls. This makes sure that |
|
87 // it is safe to refer to all the actions after this point. |
|
88 // |
|
89 |
|
90 createMainMenuActions(); |
|
91 |
|
92 // |
|
93 // Creating controls |
|
94 // |
|
95 |
|
96 createViewContents(parent); |
|
97 |
|
98 // |
|
99 // Doing other initializations that may refer to the component |
|
100 // that has been created above. |
|
101 // |
|
102 |
|
103 hookContextMenu(); |
|
104 contributeToActionBars(); |
|
105 |
|
106 // Connection status needs to be updated, if view was closed/re-opened. |
|
107 HtiConnection.getInstance().updateConnectionStatus(); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Creates text textViewer for viewing output. |
|
112 * @param parent Parent composite. |
|
113 */ |
|
114 private void createViewContents(Composite parent) { |
|
115 textViewer = new MainTextViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL); |
|
116 textViewer.initializeSettings(); |
|
117 |
|
118 // Creating selection changed listener to enable/disable copy action as needed. |
|
119 ISelectionChangedListener consoleSelectionListener = new ISelectionChangedListener(){ |
|
120 public void selectionChanged(SelectionChangedEvent event) { |
|
121 int length = textViewer.getSelectedRange().y; |
|
122 // Action is enabled when length of selection is different than zero; |
|
123 copyAction.setEnabled(length != 0); |
|
124 }}; |
|
125 textViewer.addSelectionChangedListener(consoleSelectionListener); |
|
126 |
|
127 // Setting context help IDs |
|
128 |
|
129 PlatformUI.getWorkbench().getHelpSystem().setHelp(textViewer.getControl(), |
|
130 HtiConnectionHelpContextIDs.HTI_CONNECTION_MAIN_VIEW); |
|
131 } |
|
132 |
|
133 /** |
|
134 * Hooking context menu. |
|
135 */ |
|
136 private void hookContextMenu() { |
|
137 |
|
138 // |
|
139 // Context menu for text viewer |
|
140 // |
|
141 MenuManager menuMgr = new MenuManager("#TextViewerPopupMenu"); //$NON-NLS-1$ |
|
142 menuMgr.setRemoveAllWhenShown(true); |
|
143 menuMgr.addMenuListener(new IMenuListener() { |
|
144 public void menuAboutToShow(IMenuManager manager) { |
|
145 MainView.this.fillContextMenu(manager); |
|
146 } |
|
147 }); |
|
148 Menu menu = menuMgr.createContextMenu(textViewer.getControl()); |
|
149 textViewer.getControl().setMenu(menu); |
|
150 getSite().registerContextMenu(menuMgr, textViewer); |
|
151 } |
|
152 |
|
153 /** |
|
154 * Filling action bars. |
|
155 */ |
|
156 private void contributeToActionBars() { |
|
157 IActionBars bars = getViewSite().getActionBars(); |
|
158 fillViewPullDownMenu(bars.getMenuManager()); |
|
159 fillViewToolBar(bars.getToolBarManager()); |
|
160 } |
|
161 |
|
162 /** |
|
163 * Filling pull down menu. |
|
164 * @param manager Menu manager. |
|
165 */ |
|
166 private void fillViewPullDownMenu(IMenuManager manager) { |
|
167 manager.add(preferencesAction); |
|
168 } |
|
169 |
|
170 /** |
|
171 * Filling context menu. |
|
172 * @param manager Menu manager. |
|
173 */ |
|
174 private void fillContextMenu(IMenuManager manager) { |
|
175 manager.add(copyAction); |
|
176 manager.add(selectAllAction); |
|
177 manager.add(new Separator()); |
|
178 manager.add(clearScreenAction); |
|
179 manager.add(new Separator()); |
|
180 manager.add(scrollLockAction); |
|
181 // Other plug-ins can contribute there actions here |
|
182 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
|
183 } |
|
184 |
|
185 /** |
|
186 * Filling toolbar. |
|
187 * @param manager Menu manager. |
|
188 */ |
|
189 private void fillViewToolBar(IToolBarManager manager) { |
|
190 manager.add(startStopGatewayAction); |
|
191 manager.add(new Separator()); |
|
192 manager.add(preferencesAction); |
|
193 manager.add(new Separator()); |
|
194 manager.add(clearScreenAction); |
|
195 manager.add(scrollLockAction); |
|
196 } |
|
197 |
|
198 /** |
|
199 * Creating all needed actions. |
|
200 */ |
|
201 private void createMainMenuActions() { |
|
202 preferencesAction = new OpenPreferencePageAction(); |
|
203 clearScreenAction = new ClearScreenAction(this); |
|
204 scrollLockAction = new ScrollLockAction(this); |
|
205 selectAllAction = new SelectAllAction(this); |
|
206 startStopGatewayAction = new StartStopGatewayAction(); |
|
207 // Creator connection menu. |
|
208 IMenuCreator creator = new ConnectionMenuCreator(); |
|
209 startStopGatewayAction.setMenuCreator(creator); |
|
210 |
|
211 StringArrayClipboardCopyHandler copyHandler = new StringArrayClipboardCopyHandler(); |
|
212 copyAction = new CopyFromStringProviderToClipboardAction(this, new ICopyActionHandler[]{ copyHandler }); |
|
213 copyAction.setEnabled(false); |
|
214 } |
|
215 |
|
216 /** |
|
217 * Allows other classes to update content description. |
|
218 * @param newContentDescription New description. |
|
219 */ |
|
220 public void updateDescription(String newContentDescription){ |
|
221 setContentDescription(newContentDescription); |
|
222 IToolBarManager tbManager = getViewSite().getActionBars().getToolBarManager(); |
|
223 tbManager.update(true); |
|
224 } |
|
225 |
|
226 /** |
|
227 * The view should refresh all its UI components in this method. |
|
228 */ |
|
229 public void refresh(){ |
|
230 textViewer.refresh(); |
|
231 } |
|
232 |
|
233 /** |
|
234 * Sets enabled/disabled states for actions commands |
|
235 * on this view, based on the current application state. |
|
236 * This method should be called whenever an operation is |
|
237 * started or stopped that might have effect on action |
|
238 * button states. |
|
239 */ |
|
240 public void updateActionButtonStates(){ |
|
241 // Setting state depending on datagateway status. |
|
242 |
|
243 startStopGatewayAction.setRunning( |
|
244 HtiConnection.getInstance().getConnectionStatus() != ConnectionStatus.SHUTDOWN); |
|
245 } |
|
246 |
|
247 /* (non-Javadoc) |
|
248 * @see org.eclipse.ui.part.WorkbenchPart#dispose() |
|
249 */ |
|
250 public void dispose() { |
|
251 LogDocument.getInstance().removeDocumentListener(textViewer); |
|
252 super.dispose(); |
|
253 } |
|
254 |
|
255 /** |
|
256 * Enables to open main view from classes that don't have |
|
257 * reference to the main view instance and are not UI threads. |
|
258 * This method only opens the view if it was not visible. |
|
259 * @param setFocus True if focus should be set for view. |
|
260 */ |
|
261 public static void openMainViewAsync(final boolean setFocus){ |
|
262 // We want to activate/create the view if it isn't open. |
|
263 // do not need to use the instance after it is returned. |
|
264 Runnable dialogRunnable = new Runnable() { |
|
265 public void run() { |
|
266 try { |
|
267 MainView.getViewInstance(setFocus); |
|
268 } catch (PartInitException e) { |
|
269 e.printStackTrace(); |
|
270 } |
|
271 } |
|
272 }; |
|
273 // Opening main view in default UI thread. |
|
274 Display.getDefault().asyncExec(dialogRunnable); |
|
275 } |
|
276 |
|
277 /** |
|
278 * Enables to get reference of the main view |
|
279 * from the classes that do not actually |
|
280 * have reference to the main view instance. |
|
281 * This method opens opens up the view if it |
|
282 * was not visible. |
|
283 * @param setFocus True if focus should be set for view. |
|
284 * @return Instance of main view. |
|
285 * @throws PartInitException |
|
286 */ |
|
287 public static MainView getViewInstance(boolean setFocus) throws PartInitException{ |
|
288 |
|
289 IWorkbenchPage page = HtiApiActivator.getCurrentlyActivePage(); |
|
290 |
|
291 boolean viewAlreadyVisible = false; |
|
292 IViewPart viewPart = null; |
|
293 |
|
294 // Checking if view is already open |
|
295 IViewReference[] viewRefs = page.getViewReferences(); |
|
296 for (int i = 0; i < viewRefs.length; i++) { |
|
297 IViewReference reference = viewRefs[i]; |
|
298 String id = reference.getId(); |
|
299 if(id.equalsIgnoreCase(MainView.ID)){ |
|
300 viewAlreadyVisible = true; |
|
301 // Found, restoring the view |
|
302 viewPart = reference.getView(true); |
|
303 // Activating the view if wanted. |
|
304 if(setFocus) { |
|
305 page.activate(viewPart); |
|
306 } |
|
307 } |
|
308 } |
|
309 // View was not opened |
|
310 if(! viewAlreadyVisible){ |
|
311 if(setFocus){ |
|
312 // Opening and setting focus for view. |
|
313 viewPart = page.showView(MainView.ID); |
|
314 } else { |
|
315 // Opening the view, but not setting focus. |
|
316 viewPart = page.showView(MainView.ID, null, IWorkbenchPage.VIEW_VISIBLE); |
|
317 } |
|
318 } |
|
319 return ((MainView) viewPart); |
|
320 } |
|
321 |
|
322 /** |
|
323 * Passing the focus request to the textViewer's control. |
|
324 */ |
|
325 public void setFocus() { |
|
326 textViewer.getControl().setFocus(); |
|
327 } |
|
328 |
|
329 /** |
|
330 * Returns console viewer. |
|
331 * @return Viewer used in console. |
|
332 */ |
|
333 public MainTextViewer getMainTextViewer(){ |
|
334 return textViewer; |
|
335 } |
|
336 |
|
337 /* (non-Javadoc) |
|
338 * @see com.nokia.s60tools.ui.IStringProvider#getString() |
|
339 */ |
|
340 public String getString() { |
|
341 // Getting current selection and returning it. |
|
342 return textViewer.getSelectedText(); |
|
343 } |
|
344 } |