javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/qt/org/eclipse/swt/internal/qt/CommandArranger.java
changeset 80 d6dafc5d983f
parent 78 71ad690e91f5
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
     9  *     Nokia Corporation - initial implementation
     9  *     Nokia Corporation - initial implementation
    10  *******************************************************************************/
    10  *******************************************************************************/
    11 package org.eclipse.swt.internal.qt;
    11 package org.eclipse.swt.internal.qt;
    12 
    12 
    13 import org.eclipse.ercp.swt.mobile.Command;
    13 import org.eclipse.ercp.swt.mobile.Command;
       
    14 import org.eclipse.ercp.swt.mobile.Internal_MobilePackageSupport;
    14 import org.eclipse.swt.widgets.Control;
    15 import org.eclipse.swt.widgets.Control;
    15 import org.eclipse.swt.widgets.Decorations;
    16 import org.eclipse.swt.widgets.Decorations;
    16 import org.eclipse.swt.widgets.Display;
    17 import org.eclipse.swt.widgets.Display;
    17 import org.eclipse.swt.widgets.Internal_PackageSupport;
    18 import org.eclipse.swt.widgets.Internal_PackageSupport;
    18 import org.eclipse.swt.widgets.Menu;
    19 import org.eclipse.swt.widgets.Menu;
    29  * 
    30  * 
    30  * @see Command
    31  * @see Command
    31  * @see CommandCollection
    32  * @see CommandCollection
    32  * @see CommandPresentationStrategy
    33  * @see CommandPresentationStrategy
    33  */
    34  */
    34 public class CommandArranger {
    35 public class CommandArranger
    35 
    36 {
    36 /**
    37 
    37  * A helper for keeping an ordered list of {@link Command}s in the current focus
    38 /**
    38  * context. The Commands are ordered according to their proximity to the
    39  * A helper for keeping an ordered list of {@link Command}s in the current
    39  * currently focused control. The ordered list starts with Commands from the
    40  * focus context. The Commands are ordered according to their proximity to
    40  * currently focused Control if any and ends with the active Shell and includes
    41  * the currently focused control. The ordered list starts with Commands from
    41  * all the Commands in between. If a Control has more than one Command they are
    42  * the currently focused Control if any and ends with the active Shell and
    42  * ordered among themselves according to creation order.
    43  * includes all the Commands in between. If a Control has more than one
    43  */
    44  * Command they are ordered among themselves according to creation order.
    44 public class CommandCollection {
    45  */
       
    46 public class CommandCollection
       
    47 {
    45 
    48 
    46 private Command[] fCommands;
    49 private Command[] fCommands;
    47 
    50 
    48 CommandCollection() {
    51 CommandCollection() {
    49     super();
    52     super();
    50 }
    53 }
    51 
    54 
    52 /**
    55 /**
    53  * Adds the command to the collection. Warning: This does not make duplicate
    56  * Adds the command to the collection. Warning: This does not make
    54  * control.
    57  * duplicate control.
    55  * 
    58  * 
    56  * @param command
    59  * @param command
    57  */
    60  */
    58 void addCommand(Command command) {
    61 void addCommand(Command command) {
    59     if (command == null)
    62     if (command == null)
    68     // find the insertion point so that the order is correct
    71     // find the insertion point so that the order is correct
    69     int insertPoint = 0;
    72     int insertPoint = 0;
    70     Shell activeShell = display.getActiveShell();
    73     Shell activeShell = display.getActiveShell();
    71     Control ctrl = display.getFocusControl();
    74     Control ctrl = display.getFocusControl();
    72     while (ctrl != null && ctrl != activeShell) {
    75     while (ctrl != null && ctrl != activeShell) {
    73         if (ctrl == command.control) {
    76         if (ctrl == Internal_MobilePackageSupport.control(command)) {
    74             // Adding a command to focused control increment by one.
    77             // Adding a command to focused control increment by one.
    75             // adding Command.internal_getCommands(ctrl).length will
    78             // adding Command.internal_getCommands(ctrl).length will
    76             // just duplicate the count for Commands already in array.
    79             // just duplicate the count for Commands already in array.
    77             insertPoint++;
    80             insertPoint++;
    78             break;
    81             break;
    79         }
    82         }
    80         insertPoint += Command.internal_getCommands(ctrl).length;
    83 
       
    84         insertPoint += Internal_PackageSupport.getCommands(ctrl).length;
    81         ctrl = ctrl.getParent();
    85         ctrl = ctrl.getParent();
    82     }
    86     }
    83     System.arraycopy(fCommands, 0, newList, 0, insertPoint);
    87     System.arraycopy(fCommands, 0, newList, 0, insertPoint);
    84     System.arraycopy(fCommands, insertPoint, newList, insertPoint + 1, fCommands.length
    88     System.arraycopy(fCommands, insertPoint, newList, insertPoint + 1, fCommands.length
    85         - insertPoint);
    89         - insertPoint);
   143         size = fCommands.length;
   147         size = fCommands.length;
   144     return size;
   148     return size;
   145 }
   149 }
   146 
   150 
   147 /**
   151 /**
   148  * Retrieves the Commands of the types indicated by the commandTypes array. The
   152  * Retrieves the Commands of the types indicated by the commandTypes
   149  * order of the commandTypes array has no significance. Passing a null parameter
   153  * array. The order of the commandTypes array has no significance.
   150  * or an empty array retrieves all the available Commands.
   154  * Passing a null parameter or an empty array retrieves all the
       
   155  * available Commands.
   151  * 
   156  * 
   152  * @param commandTypes
   157  * @param commandTypes
   153  * @return Command list
   158  * @return Command list
   154  */
   159  */
   155 Command[] getCommands(int[] commandTypes) {
   160 Command[] getCommands(int[] commandTypes) {
   159     int size = getSize();
   164     int size = getSize();
   160     Command[] filteredCommands = new Command[size];
   165     Command[] filteredCommands = new Command[size];
   161     int index = 0;
   166     int index = 0;
   162     for (int i = 0; i < fCommands.length; i++) {
   167     for (int i = 0; i < fCommands.length; i++) {
   163         for (int j = 0; j < commandTypes.length; j++) {
   168         for (int j = 0; j < commandTypes.length; j++) {
   164             if (fCommands[i].type == commandTypes[j]) {
   169             if (Internal_MobilePackageSupport.type(fCommands[i]) == commandTypes[j]) {
   165                 filteredCommands[index] = fCommands[i];
   170                 filteredCommands[index] = fCommands[i];
   166                 index++;
   171                 index++;
   167                 break;
   172                 break;
   168             }
   173             }
   169         }
   174         }
   184 Control focusedControl;
   189 Control focusedControl;
   185 Shell lastKnownActiveShell;
   190 Shell lastKnownActiveShell;
   186 private Command[] positiveKeyCommands;
   191 private Command[] positiveKeyCommands;
   187 private Command negativeKeyCommand;
   192 private Command negativeKeyCommand;
   188 
   193 
   189 
       
   190 public CommandArranger(Display display) {
   194 public CommandArranger(Display display) {
   191     super();
   195     super();
   192     this.display = display;
   196     this.display = display;
   193     currentCommands = new CommandCollection();
   197     currentCommands = new CommandCollection();
   194 }
   198 }
   195 
   199 
   196 /**
   200 /**
   197  * Called when the application changes the QMenuBar. This method does not handle
   201  * Called when the application changes the QMenuBar. This method does not
   198  * the cases when the QMenuBar may change when the active top-level Shell
   202  * handle the cases when the QMenuBar may change when the active top-level
   199  * changes. Since this does not cause a menu bar change on all platforms.
   203  * Shell changes. Since this does not cause a menu bar change on all
       
   204  * platforms.
   200  * 
   205  * 
   201  * @see org.eclipse.swt.widgets.Decorations#setMenuBar(Menu)
   206  * @see org.eclipse.swt.widgets.Decorations#setMenuBar(Menu)
   202  * 
   207  * 
   203  **/
   208  **/
   204 public void menuBarChanged(Decorations decorations) {
   209 public void menuBarChanged(Decorations decorations) {
   225     if (display == null) {
   230     if (display == null) {
   226         return;
   231         return;
   227     }
   232     }
   228 
   233 
   229     Shell activeShell = display.getActiveShell();
   234     Shell activeShell = display.getActiveShell();
   230     
   235 
   231     if (activeShell == lastKnownActiveShell) {
   236     if (activeShell == lastKnownActiveShell) {
   232         return;
   237         return;
   233     }
   238     }
   234     lastKnownActiveShell = activeShell;
   239     lastKnownActiveShell = activeShell;
   235     
   240 
   236     cleanPositiveCommands();
   241     cleanPositiveCommands();
   237     cleanNegativeCommand();
   242     cleanNegativeCommand();
   238     
   243 
   239     currentCommands = new CommandCollection();
   244     currentCommands = new CommandCollection();
   240 
   245 
   241     if (activeShell != null && Command.internal_getCommands(activeShell).length > 0) {
   246     if (activeShell != null && Internal_PackageSupport.getCommands(activeShell).length > 0) {
   242         currentCommands.addCommand(Command.internal_getCommands(activeShell));
   247         currentCommands.addCommand(Internal_PackageSupport.getCommands(activeShell));
   243     }
   248     }
   244 
   249 
   245     // Determine where the commands go
   250     // Determine where the commands go
   246     if (currentCommands.getSize() > 0) {
   251     if (currentCommands.getSize() > 0) {
   247         Command[] add = currentCommands.getCommands(null);
   252         Command[] add = currentCommands.getCommands(null);
   255  * Called when a new Command is created
   260  * Called when a new Command is created
   256  * 
   261  * 
   257  * @param command
   262  * @param command
   258  */
   263  */
   259 public void commandAdded(Command command) {
   264 public void commandAdded(Command command) {
   260     if (isInFocusContext(command.control)) {
   265     if (isInFocusContext(Internal_MobilePackageSupport.control(command))) {
   261         currentCommands.addCommand(command);
   266         currentCommands.addCommand(command);
   262         handleCommandListChange(command, null, currentCommands);
   267         handleCommandListChange(command, null, currentCommands);
   263     }
   268     }
   264 }
   269 }
   265 
   270 
   269  * @param command
   274  * @param command
   270  */
   275  */
   271 public void commandRemoved(Command command) {
   276 public void commandRemoved(Command command) {
   272     if (command == defaultCommand)
   277     if (command == defaultCommand)
   273         defaultCommand = null;
   278         defaultCommand = null;
   274     if (isInFocusContext(command.control)) {
   279     if (isInFocusContext(Internal_MobilePackageSupport.control(command))) {
   275         currentCommands.removeCommand(command);
   280         currentCommands.removeCommand(command);
   276         handleCommandListChange(null, command, currentCommands);
   281         handleCommandListChange(null, command, currentCommands);
   277     }
   282     }
   278 }
   283 }
   279 
   284 
   295  * @param command
   300  * @param command
   296  * @see Command#setDefaultCommand();
   301  * @see Command#setDefaultCommand();
   297  */
   302  */
   298 public void setDefaultCommand(Command command) {
   303 public void setDefaultCommand(Command command) {
   299     defaultCommand = command;
   304     defaultCommand = command;
   300     if (isInFocusContext(command.control)) {
   305     if (isInFocusContext(Internal_MobilePackageSupport.control(command))) {
   301         handleDefaultCommandChange(command);
   306         handleDefaultCommandChange(command);
   302     }
   307     }
   303 }
   308 }
   304 
   309 
   305 /**
   310 /**
   309  * @see Command#isDefaultCommand()
   314  * @see Command#isDefaultCommand()
   310  */
   315  */
   311 public Command getDefaultCommand() {
   316 public Command getDefaultCommand() {
   312     return defaultCommand;
   317     return defaultCommand;
   313 }
   318 }
   314 
       
   315 
   319 
   316 private boolean isInFocusContext(Control control) {
   320 private boolean isInFocusContext(Control control) {
   317     Display display = control.getDisplay();
   321     Display display = control.getDisplay();
   318     Shell activeShell = display.getActiveShell();
   322     Shell activeShell = display.getActiveShell();
   319     return control == activeShell;
   323     return control == activeShell;
   340         Command cmd = commands[i];
   344         Command cmd = commands[i];
   341         if (cmd.isDefaultCommand()) {
   345         if (cmd.isDefaultCommand()) {
   342             defaultCommand = cmd;
   346             defaultCommand = cmd;
   343             continue;
   347             continue;
   344         }
   348         }
   345         if (CommandUtils.isNegativeType(cmd.type)) {
   349         if (CommandUtils.isNegativeType(Internal_MobilePackageSupport.type(cmd))) {
   346             if (negativeKeyCommand == null || negativeKeyCommand.isDisposed()) {
   350             if (negativeKeyCommand == null || negativeKeyCommand.isDisposed()) {
   347                 negativeKeyCommand = cmd;
   351                 negativeKeyCommand = cmd;
   348             } else if (negativeKeyCommand.getPriority() <= cmd.getPriority()) {
   352             }
       
   353             else if (negativeKeyCommand.getPriority() <= cmd.getPriority()) {
   349                 positiveKeyCommands[positiveKeyIndex] = negativeKeyCommand;
   354                 positiveKeyCommands[positiveKeyIndex] = negativeKeyCommand;
   350                 positiveKeyIndex++;
   355                 positiveKeyIndex++;
   351                 negativeKeyCommand = cmd;
   356                 negativeKeyCommand = cmd;
   352             } else {
   357             }
       
   358             else {
   353                 positiveKeyCommands[positiveKeyIndex] = cmd;
   359                 positiveKeyCommands[positiveKeyIndex] = cmd;
   354                 positiveKeyIndex++;
   360                 positiveKeyIndex++;
   355             }
   361             }
   356             continue;
   362             continue;
   357         }
   363         }
   370     if ((positiveKeyCommands != null && positiveKeyCommands.length > 1)
   376     if ((positiveKeyCommands != null && positiveKeyCommands.length > 1)
   371         || (defaultCommand != null && positiveKeyCommands != null)) {
   377         || (defaultCommand != null && positiveKeyCommands != null)) {
   372         useBar = true;
   378         useBar = true;
   373     }
   379     }
   374     if (defaultCommand != null && !defaultCommand.isDisposed()
   380     if (defaultCommand != null && !defaultCommand.isDisposed()
   375         && !defaultCommand.control.isDisposed()) {
   381         && !Internal_MobilePackageSupport.control(defaultCommand).isDisposed()) {
   376         if (useBar) {
   382         if (useBar) {
   377             OS.QWidget_removeAction(defaultCommand.control.getShell().internal_getOwnMenuBar(),
   383             OS.QWidget_removeAction(Internal_MobilePackageSupport.control(defaultCommand)
       
   384                 .getShell().internal_getOwnMenuBar(), topHandle(defaultCommand));
       
   385         }
       
   386         else {
       
   387             OS.QWidget_removeAction(
       
   388                 topHandle(Internal_MobilePackageSupport.control(defaultCommand)),
   378                 topHandle(defaultCommand));
   389                 topHandle(defaultCommand));
   379         } else {
       
   380             OS.QWidget_removeAction(topHandle(defaultCommand.control), topHandle(defaultCommand));
       
   381         }
   390         }
   382     }
   391     }
   383     if (positiveKeyCommands != null) {
   392     if (positiveKeyCommands != null) {
   384         for (int i = 0; i < positiveKeyCommands.length; i++) {
   393         for (int i = 0; i < positiveKeyCommands.length; i++) {
   385             Command cmd = positiveKeyCommands[i];
   394             Command cmd = positiveKeyCommands[i];
   386             if (cmd == null || cmd.isDisposed() || cmd.control.isDisposed()) {
   395             if (cmd == null || cmd.isDisposed()
       
   396                 || Internal_MobilePackageSupport.control(cmd).isDisposed()) {
   387                 continue;
   397                 continue;
   388             }
   398             }
   389             int handle = 0;
   399             int handle = 0;
   390             if (useBar) {
   400             if (useBar) {
   391                 handle = cmd.control.getShell().internal_getOwnMenuBar();
   401                 handle = Internal_MobilePackageSupport.control(cmd).getShell()
   392             } else {
   402                     .internal_getOwnMenuBar();
   393                 handle = topHandle(positiveKeyCommands[0].control);
   403             }
       
   404             else {
       
   405                 handle = topHandle(Internal_MobilePackageSupport
       
   406                     .control(positiveKeyCommands[0]));
   394             }
   407             }
   395             OS.QWidget_removeAction(handle, topHandle(cmd));
   408             OS.QWidget_removeAction(handle, topHandle(cmd));
   396 
   409 
   397         }
   410         }
   398     }
   411     }
   399 }
   412 }
   400 
   413 
   401 private void cleanNegativeCommand() {
   414 private void cleanNegativeCommand() {
   402     if (negativeKeyCommand != null && !negativeKeyCommand.isDisposed()
   415     if (negativeKeyCommand != null && !negativeKeyCommand.isDisposed()
   403         && !negativeKeyCommand.control.isDisposed()) {
   416         && !Internal_MobilePackageSupport.control(negativeKeyCommand).isDisposed()) {
   404         OS.QWidget_removeAction(topHandle(negativeKeyCommand.control),
   417         OS.QWidget_removeAction(
       
   418             topHandle(Internal_MobilePackageSupport.control(negativeKeyCommand)),
   405             topHandle(negativeKeyCommand));
   419             topHandle(negativeKeyCommand));
   406     }
   420     }
   407 }
   421 }
   408 
   422 
   409 private void placeNegativeCommand() {
   423 private void placeNegativeCommand() {
   410     if (negativeKeyCommand != null) {
   424     if (negativeKeyCommand != null) {
   411         OS.QWidget_addAction(Internal_PackageSupport.topHandle(negativeKeyCommand.control),
   425         OS.QWidget_addAction(Internal_PackageSupport.topHandle(Internal_MobilePackageSupport
   412             topHandle(negativeKeyCommand));
   426             .control(negativeKeyCommand)), topHandle(negativeKeyCommand));
   413     }
   427     }
   414 }
   428 }
   415 
   429 
   416 private void placePositiveCommands() {
   430 private void placePositiveCommands() {
   417     if (defaultCommand != null) {
   431     if (defaultCommand != null) {
   418         int defaultCmdHandle = topHandle(defaultCommand);
   432         int defaultCmdHandle = topHandle(defaultCommand);
   419         if (positiveKeyCommands != null) {
   433         if (positiveKeyCommands != null) {
   420             OS.QMenuBar_addAction(defaultCommand.control.getShell().internal_getOwnMenuBar(),
   434             OS.QMenuBar_addAction(Internal_MobilePackageSupport.control(defaultCommand)
   421                 defaultCmdHandle);
   435                 .getShell().internal_getOwnMenuBar(), defaultCmdHandle);
   422         } else {
   436         }
   423             OS.QWidget_addAction(Internal_PackageSupport.topHandle(defaultCommand.control),
   437         else {
       
   438             OS.QWidget_addAction(Internal_PackageSupport
       
   439                 .topHandle(Internal_MobilePackageSupport.control(defaultCommand)),
   424                 defaultCmdHandle);
   440                 defaultCmdHandle);
   425         }
   441         }
   426     }
   442     }
   427     if (positiveKeyCommands != null) {
   443     if (positiveKeyCommands != null) {
   428         if (positiveKeyCommands.length == 1 && defaultCommand == null) {
   444         if (positiveKeyCommands.length == 1 && defaultCommand == null) {
   429             OS.QWidget_addAction(Internal_PackageSupport.topHandle(positiveKeyCommands[0].control),
   445             OS.QWidget_addAction(Internal_PackageSupport
       
   446                 .topHandle(Internal_MobilePackageSupport.control(positiveKeyCommands[0])),
   430                 topHandle(positiveKeyCommands[0]));
   447                 topHandle(positiveKeyCommands[0]));
   431         } else {
   448         }
       
   449         else {
   432             CommandUtils.sort(positiveKeyCommands);
   450             CommandUtils.sort(positiveKeyCommands);
   433             for (int i = 0; i < positiveKeyCommands.length; i++) {
   451             for (int i = 0; i < positiveKeyCommands.length; i++) {
   434                 OS.QMenuBar_addAction(positiveKeyCommands[i].control.getShell()
   452                 OS.QMenuBar_addAction(
   435                     .internal_getOwnMenuBar(), topHandle(positiveKeyCommands[i]));
   453                     Internal_MobilePackageSupport.control(positiveKeyCommands[i]).getShell()
       
   454                         .internal_getOwnMenuBar(), topHandle(positiveKeyCommands[i]));
   436             }
   455             }
   437         }
   456         }
   438     }
   457     }
   439 }
   458 }
   440 
   459