1 /******************************************************************************* |
|
2 * Copyright (c) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
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 * Nokia Corporation - initial implementation |
|
10 *******************************************************************************/ |
|
11 package org.eclipse.swt.internal.qt; |
|
12 |
|
13 import org.eclipse.ercp.swt.mobile.Command; |
|
14 import org.eclipse.swt.internal.qt.CommandArranger.CommandCollection; |
|
15 import org.eclipse.swt.widgets.Control; |
|
16 import org.eclipse.swt.widgets.Internal_PackageSupport; |
|
17 import org.eclipse.swt.widgets.Shell; |
|
18 import org.eclipse.swt.widgets.Widget; |
|
19 |
|
20 public class DesktopCommandPresentationStrategy extends |
|
21 CommandPresentationStrategy { |
|
22 |
|
23 private int commandsMenu; |
|
24 private int commandsMenuAction; |
|
25 private Control previousFocus; |
|
26 |
|
27 public DesktopCommandPresentationStrategy(){ |
|
28 super(); |
|
29 initCommandsMenu(); |
|
30 } |
|
31 |
|
32 protected void handleCommandListChange(Command added, Command removed, |
|
33 CommandCollection commands) { |
|
34 |
|
35 Shell shell = null; |
|
36 if ( added != null ){ |
|
37 shell = added.control.getShell(); |
|
38 OS.QMenu_addAction(commandsMenu, handle(added) ); |
|
39 } |
|
40 else{ |
|
41 shell = removed.control.getShell(); |
|
42 OS.QWidget_removeAction(commandsMenu, handle(removed) ); |
|
43 } |
|
44 |
|
45 OS.QMenuBar_addAction(shell.internal_getOwnMenuBar(), commandsMenuAction ); |
|
46 } |
|
47 |
|
48 protected void handleFocusChange(Control focused, |
|
49 CommandCollection oldCommands, CommandCollection commands) { |
|
50 Shell shell = focused.getShell(); |
|
51 |
|
52 if( oldCommands != null && oldCommands.getSize() >0 ){ |
|
53 Command[] remove = oldCommands.getCommands(null); |
|
54 for(int i=0; i< remove.length; i++){ |
|
55 if (previousFocus!= null && remove[i].control == previousFocus){ |
|
56 OS.QWidget_removeAction(handle(previousFocus), handle(remove[i])); |
|
57 }else{ |
|
58 OS.QWidget_removeAction(commandsMenu, handle(remove[i])); |
|
59 } |
|
60 } |
|
61 } |
|
62 |
|
63 if ( commands != null && commands.getSize() > 0 ){ |
|
64 OS.QMenuBar_addAction(shell.internal_getOwnMenuBar(), commandsMenuAction ); |
|
65 Command[] add = commands.getCommands(null); |
|
66 CommandUtils.sort(add); |
|
67 for(int i=0 ; i < add.length; i++){ |
|
68 if ( add[i].control != focused ){ |
|
69 OS.QMenu_addAction( commandsMenu, handle(add[i]) ); |
|
70 } |
|
71 else{ |
|
72 OS.QWidget_addAction(handle(focused), handle(add[i])); |
|
73 } |
|
74 if (add[i].isDefaultCommand() )OS.QMenu_setDefaultAction( commandsMenu, handle(add[i]) ); |
|
75 } |
|
76 } |
|
77 //Store for use next time. |
|
78 previousFocus=focused; |
|
79 } |
|
80 |
|
81 protected void handleMenuBarChanged(int newMenuBar, |
|
82 CommandCollection commands) { |
|
83 OS.QMenuBar_addAction(newMenuBar, commandsMenuAction ); |
|
84 } |
|
85 |
|
86 |
|
87 protected void dispose() { |
|
88 if (commandsMenu != 0 ){ |
|
89 QObjectDeleteWrapper.deleteSafely( commandsMenu ); |
|
90 commandsMenu = 0; |
|
91 } |
|
92 if (commandsMenuAction != 0 ){ |
|
93 QObjectDeleteWrapper.deleteSafely( commandsMenuAction ); |
|
94 commandsMenuAction = 0; |
|
95 } |
|
96 } |
|
97 |
|
98 private void initCommandsMenu (){ |
|
99 commandsMenuAction = OS.QAction_new( 0 ); |
|
100 OS.QAction_setText( commandsMenuAction, "Commands"); |
|
101 commandsMenu = OS.QMenu_new( 0 ); |
|
102 OS.QAction_setMenu( commandsMenuAction, commandsMenu ); |
|
103 } |
|
104 |
|
105 protected void handleDefaultCommandChange(Command defaultCommand) { |
|
106 int handle = 0; |
|
107 if (defaultCommand != null ) handle = handle(defaultCommand); |
|
108 OS.QMenu_setDefaultAction( commandsMenu, handle ); |
|
109 } |
|
110 |
|
111 |
|
112 private final int handle(Widget w) { |
|
113 return Internal_PackageSupport.handle(w); |
|
114 } |
|
115 } |
|