javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/s60/org/eclipse/swt/internal/qt/s60/S60CommandPresentationStrategy.java
changeset 78 71ad690e91f5
parent 72 1f0034e370aa
child 80 d6dafc5d983f
equal deleted inserted replaced
72:1f0034e370aa 78:71ad690e91f5
     1 package org.eclipse.swt.internal.qt.s60;
       
     2 
       
     3 import org.eclipse.ercp.swt.mobile.Command;
       
     4 import org.eclipse.swt.internal.qt.CommandPresentationStrategy;
       
     5 import org.eclipse.swt.internal.qt.CommandUtils;
       
     6 import org.eclipse.swt.internal.qt.OS;
       
     7 import org.eclipse.swt.internal.qt.CommandArranger.CommandCollection;
       
     8 import org.eclipse.swt.widgets.Control;
       
     9 import org.eclipse.swt.widgets.Internal_PackageSupport;
       
    10 import org.eclipse.swt.widgets.Widget;
       
    11 
       
    12 public class S60CommandPresentationStrategy extends CommandPresentationStrategy {
       
    13     private Command[] positiveKeyCommands;
       
    14     private Command negativeKeyCommand;
       
    15     private Command defaultCommand;
       
    16 
       
    17     public S60CommandPresentationStrategy() {
       
    18         super();
       
    19     }
       
    20 
       
    21     protected void dispose() {
       
    22         positiveKeyCommands = null;
       
    23         negativeKeyCommand = null;
       
    24         defaultCommand =null;
       
    25     }
       
    26 
       
    27 
       
    28     protected void handleCommandListChange(Command added, Command removed,
       
    29             CommandCollection commands) {
       
    30         cleanNegativeCommand();
       
    31         cleanPositiveCommands();
       
    32         updateCommandPositions(commands.getCommands(null));
       
    33         placeNegativeCommand();
       
    34         placePositiveCommands();
       
    35     }
       
    36 
       
    37     protected void handleDefaultCommandChange(Command defaultCommand) {
       
    38         this.defaultCommand = defaultCommand;
       
    39         cleanPositiveCommands();
       
    40         placePositiveCommands();
       
    41     }
       
    42 
       
    43 
       
    44     protected void handleFocusChange(Control focused,
       
    45             CommandCollection oldCommands, CommandCollection commands) {
       
    46 
       
    47         //Clean-up the existing commands
       
    48         cleanPositiveCommands();
       
    49         cleanNegativeCommand();
       
    50         //determine where the commands go
       
    51         if( commands != null && commands.getSize()>0 ){
       
    52             Command[] add = commands.getCommands(null);
       
    53             updateCommandPositions(add);
       
    54         }
       
    55         //Place them to their places
       
    56         placePositiveCommands();
       
    57         placeNegativeCommand();
       
    58 
       
    59     }
       
    60 
       
    61     private void updateCommandPositions(Command[] commands) {
       
    62         positiveKeyCommands = new Command[commands.length];
       
    63         int  positiveKeyIndex=0;
       
    64         for (int i = 0; i < commands.length; i++) {
       
    65             Command cmd = commands[i];
       
    66             if ( cmd.isDefaultCommand()){
       
    67                 defaultCommand = cmd;
       
    68                 continue;
       
    69             }
       
    70             if( CommandUtils.isNegativeType(cmd.type) ){
       
    71                 if (negativeKeyCommand == null || negativeKeyCommand.isDisposed()){
       
    72                     negativeKeyCommand = cmd;
       
    73                 }else
       
    74                 if( negativeKeyCommand.getPriority() <= cmd.getPriority() ){
       
    75                     positiveKeyCommands[positiveKeyIndex] = negativeKeyCommand;
       
    76                     positiveKeyIndex++;
       
    77                     negativeKeyCommand = cmd;
       
    78                 }else{
       
    79                     positiveKeyCommands[positiveKeyIndex] = cmd;
       
    80                     positiveKeyIndex++;
       
    81                 }
       
    82                 continue;
       
    83             }
       
    84             positiveKeyCommands[positiveKeyIndex]=cmd;
       
    85             positiveKeyIndex++;
       
    86         }
       
    87         if ((positiveKeyIndex) < positiveKeyCommands.length ){// needs to shrink
       
    88             Command[] rightSized = new Command[positiveKeyIndex];
       
    89             System.arraycopy(positiveKeyCommands, 0, rightSized, 0, rightSized.length);
       
    90             positiveKeyCommands = rightSized;
       
    91         }
       
    92     }
       
    93 
       
    94 
       
    95     private void cleanPositiveCommands() {
       
    96         boolean useBar = false;
       
    97         if ( (positiveKeyCommands != null && positiveKeyCommands.length >1) ||
       
    98                 (defaultCommand != null && positiveKeyCommands != null) ){
       
    99             useBar = true;
       
   100         }
       
   101         if (defaultCommand != null && !defaultCommand.isDisposed()
       
   102                 && !defaultCommand.control.isDisposed()) {
       
   103             if (useBar) {
       
   104                 OS.QWidget_removeAction(defaultCommand.control.getShell()
       
   105                         .internal_getOwnMenuBar(), topHandle(defaultCommand));
       
   106             }
       
   107             else{
       
   108                  OS.QWidget_removeAction(topHandle(defaultCommand.control), topHandle(defaultCommand));
       
   109             }
       
   110         }
       
   111         if (positiveKeyCommands != null) {
       
   112             for (int i = 0; i < positiveKeyCommands.length; i++) {
       
   113                 Command cmd = positiveKeyCommands[i];
       
   114                 if (cmd == null || cmd.isDisposed()  || cmd.control.isDisposed()){
       
   115                 	continue;
       
   116                 }
       
   117                 int handle = 0;
       
   118                 if (useBar) {
       
   119                     handle = cmd.control.getShell().internal_getOwnMenuBar();
       
   120                 } else {
       
   121                     handle = topHandle(positiveKeyCommands[0].control);
       
   122                 }
       
   123                 OS.QWidget_removeAction(handle, topHandle(cmd));
       
   124 
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129    private void cleanNegativeCommand() {
       
   130         if(negativeKeyCommand != null && !negativeKeyCommand.isDisposed() && !negativeKeyCommand.control.isDisposed() ){
       
   131             OS.QWidget_removeAction(topHandle(negativeKeyCommand.control), topHandle(negativeKeyCommand));
       
   132         }
       
   133     }
       
   134 
       
   135     private void placeNegativeCommand() {
       
   136         if(negativeKeyCommand != null ){
       
   137             OS.QWidget_addAction(Internal_PackageSupport.topHandle(negativeKeyCommand.control),
       
   138                     topHandle(negativeKeyCommand));
       
   139         }
       
   140     }
       
   141 
       
   142     private void placePositiveCommands() {
       
   143         if (defaultCommand != null ) {
       
   144             int defaultCmdHandle = topHandle(defaultCommand);
       
   145             if (positiveKeyCommands != null) {
       
   146                 OS.QMenuBar_addAction(defaultCommand.control.getShell()
       
   147                         .internal_getOwnMenuBar(), defaultCmdHandle);
       
   148             } else {
       
   149                 OS.QWidget_addAction(Internal_PackageSupport
       
   150                         .topHandle(defaultCommand.control), defaultCmdHandle);
       
   151             }
       
   152         }
       
   153         if (positiveKeyCommands != null) {
       
   154             if (positiveKeyCommands.length == 1 && defaultCommand == null) {
       
   155                 OS.QWidget_addAction(Internal_PackageSupport
       
   156                         .topHandle(positiveKeyCommands[0].control),
       
   157                         topHandle(positiveKeyCommands[0]));
       
   158             } else {
       
   159                 CommandUtils.sort(positiveKeyCommands);
       
   160                 for (int i = 0; i < positiveKeyCommands.length; i++) {
       
   161                     OS.QMenuBar_addAction(positiveKeyCommands[i].control
       
   162                             .getShell().internal_getOwnMenuBar(),
       
   163                             topHandle(positiveKeyCommands[i]));
       
   164                 }
       
   165             }
       
   166         }
       
   167     }
       
   168 
       
   169     protected void handleMenuBarChanged(int newMenuBar,
       
   170             CommandCollection commands) {
       
   171         placePositiveCommands();
       
   172     }
       
   173 
       
   174 
       
   175     private static final int topHandle(Widget w) {
       
   176         return Internal_PackageSupport.topHandle(w);
       
   177     }
       
   178 }