javatools/javacontrolpanel/controlpanel/javasrc/com/nokia/mj/impl/javacontrolpanel/JavaControlPanelUi.java
branchRCL_3
changeset 83 26b2b12093af
parent 77 7cee158cb8cd
child 84 0553e2305d00
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.mj.impl.javacontrolpanel;
       
    19 
       
    20 import com.nokia.mj.impl.rt.support.Jvm;
       
    21 import org.eclipse.swt.layout.*;
       
    22 import org.eclipse.ercp.swt.mobile.*;
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.events.*;
       
    25 import org.eclipse.swt.widgets.*;
       
    26 import org.eclipse.swt.graphics.*;
       
    27 
       
    28 import com.nokia.mj.impl.utils.Logger;
       
    29 
       
    30 class JavaControlPanelUi
       
    31 {
       
    32     private JavaCaptain iCaptain;
       
    33     private Redirector iRedirector;
       
    34     private SecurityConfig iSecurity;
       
    35 
       
    36     private Shell iShell;
       
    37     private Button iJavaCaptain;
       
    38     private Button iPrewarm;
       
    39     private Button iThreadDump;
       
    40     private Button iJavaDebug;
       
    41     private Button iRedirect;
       
    42     private Combo iSecurityWarnings;
       
    43     private Combo iSecurityPolicy;
       
    44 
       
    45     static
       
    46     {
       
    47         Jvm.loadSystemLibrary("javacontrolpanel");
       
    48     }
       
    49 
       
    50     public JavaControlPanelUi()
       
    51     {
       
    52         iCaptain = new JavaCaptain();
       
    53         iRedirector = new Redirector();
       
    54         iSecurity = new SecurityConfig();
       
    55     }
       
    56 
       
    57     public void show()
       
    58     {
       
    59         Display display = new Display();
       
    60         iShell = new Shell(display);
       
    61 
       
    62         createUi();
       
    63         iShell.pack();
       
    64         iShell.open();
       
    65         handleOrientationChange();
       
    66 
       
    67         while (!iShell.isDisposed())
       
    68         {
       
    69             if (!display.readAndDispatch())
       
    70             {
       
    71                 display.sleep();
       
    72             }
       
    73         }
       
    74         display.dispose();
       
    75     }
       
    76 
       
    77     private void exitEventLoop()
       
    78     {
       
    79         iRedirector.stop();
       
    80         iShell.dispose();
       
    81     }
       
    82 
       
    83     private void createUi()
       
    84     {
       
    85         try
       
    86         {
       
    87             setLayout();
       
    88             createJavaCaptainUi();
       
    89             createPrewarmUi();
       
    90             createThreadDumpUi();
       
    91             createJavaDebugUi();
       
    92             createSecurityWarningsUi();
       
    93             createSecurityPolicyUi();
       
    94             createRedirectLogsUi();
       
    95             createCommands();
       
    96             platformAdaptation();
       
    97         }
       
    98         catch (Throwable e)
       
    99         {
       
   100             Logger.ELOG(Logger.EUtils, "createUi failed", e);
       
   101             exitEventLoop();
       
   102         }
       
   103     }
       
   104 
       
   105     private void setLayout()
       
   106     {
       
   107         iShell.setText("Java Control Panel");
       
   108         iShell.setLayout(new GridLayout(2, false));
       
   109     }
       
   110 
       
   111     private void createJavaCaptainUi()
       
   112     {
       
   113         Label label = new Label(iShell, SWT.NONE);
       
   114         label.setText("Java Captain");
       
   115         label.setLayoutData(new GridData(SWT.CENTER));
       
   116 
       
   117         iJavaCaptain = new Button(iShell, SWT.TOGGLE);
       
   118         iJavaCaptain.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   119         iJavaCaptain.addSelectionListener(new SelectionListener()
       
   120         {
       
   121             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   122             {
       
   123                 widgetSelected(aEvent);
       
   124             }
       
   125             public void widgetSelected(SelectionEvent event)
       
   126             {
       
   127                 javaCaptainPressed();
       
   128             }
       
   129         });
       
   130         toggleJavaCaptain(iCaptain.isRunning());
       
   131     }
       
   132 
       
   133     private void createPrewarmUi()
       
   134     {
       
   135         Label label = new Label(iShell, SWT.NONE);
       
   136         label.setText("Prewarm");
       
   137         label.setLayoutData(new GridData(SWT.CENTER));
       
   138 
       
   139         iPrewarm = new Button(iShell, SWT.TOGGLE);
       
   140         iPrewarm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   141         iPrewarm.addSelectionListener(new SelectionListener()
       
   142         {
       
   143             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   144             {
       
   145                 widgetSelected(aEvent);
       
   146             }
       
   147             public void widgetSelected(SelectionEvent event)
       
   148             {
       
   149                 prewarmPressed();
       
   150             }
       
   151         });
       
   152         togglePrewarm(iCaptain.isPrewarmEnabled());
       
   153     }
       
   154 
       
   155     private void createThreadDumpUi()
       
   156     {
       
   157         Label label = new Label(iShell, SWT.NONE);
       
   158         label.setText("Thread Dump");
       
   159         label.setLayoutData(new GridData(SWT.CENTER));
       
   160 
       
   161         iThreadDump = new Button(iShell, SWT.PUSH);
       
   162         iThreadDump.setText("Dump");
       
   163         iThreadDump.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   164         iThreadDump.addSelectionListener(new SelectionListener()
       
   165         {
       
   166             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   167             {
       
   168                 widgetSelected(aEvent);
       
   169             }
       
   170             public void widgetSelected(SelectionEvent event)
       
   171             {
       
   172                 threadDumpPressed();
       
   173             }
       
   174         });
       
   175     }
       
   176 
       
   177     private void createJavaDebugUi()
       
   178     {
       
   179         Label label = new Label(iShell, SWT.NONE);
       
   180         label.setText("Java Debug");
       
   181         label.setLayoutData(new GridData(SWT.CENTER));
       
   182 
       
   183         iJavaDebug = new Button(iShell, SWT.TOGGLE);
       
   184         iJavaDebug.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   185         iJavaDebug.addSelectionListener(new SelectionListener()
       
   186         {
       
   187             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   188             {
       
   189                 widgetSelected(aEvent);
       
   190             }
       
   191             public void widgetSelected(SelectionEvent event)
       
   192             {
       
   193                 javaDebugPressed();
       
   194             }
       
   195         });
       
   196         toggleJavaDebug(iCaptain.isDebugEnabled());
       
   197     }
       
   198 
       
   199     private void createSecurityWarningsUi()
       
   200     {
       
   201         Label label = new Label(iShell, SWT.NONE);
       
   202         label.setText("Security Warnings");
       
   203         label.setLayoutData(new GridData(SWT.CENTER));
       
   204 
       
   205         iSecurityWarnings = new Combo(iShell, SWT.DROP_DOWN | SWT.READ_ONLY);
       
   206         for(int i = 0; i < SecurityConfig.MODES.length; i++)
       
   207         {
       
   208             iSecurityWarnings.add(SecurityConfig.MODES[i]);
       
   209         }
       
   210         iSecurityWarnings.select(iSecurity.getWarningsMode());
       
   211         iSecurityWarnings.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   212         iSecurityWarnings.addSelectionListener(new SelectionListener()
       
   213         {
       
   214             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   215             {
       
   216                 widgetSelected(aEvent);
       
   217             }
       
   218             public void widgetSelected(SelectionEvent event)
       
   219             {
       
   220                 securityWarningsSelected();
       
   221             }
       
   222         });
       
   223     }
       
   224 
       
   225     private void createSecurityPolicyUi()
       
   226     {
       
   227         Label label = new Label(iShell, SWT.NONE);
       
   228         label.setText("Security Policy");
       
   229         label.setLayoutData(new GridData(SWT.CENTER));
       
   230 
       
   231         iSecurityPolicy = new Combo(iShell, SWT.DROP_DOWN | SWT.READ_ONLY);
       
   232         for(int i = 0; i < SecurityConfig.POLICIES.length; i++)
       
   233         {
       
   234             iSecurityPolicy.add(SecurityConfig.POLICIES[i]);
       
   235         }
       
   236         iSecurityPolicy.select(iSecurity.getSecurityPolicy());
       
   237         iSecurityPolicy.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   238         iSecurityPolicy.addSelectionListener(new SelectionListener()
       
   239         {
       
   240             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   241             {
       
   242                 widgetSelected(aEvent);
       
   243             }
       
   244             public void widgetSelected(SelectionEvent event)
       
   245             {
       
   246                 securityPolicySelected();
       
   247             }
       
   248         });
       
   249     }
       
   250 
       
   251     private void createRedirectLogsUi()
       
   252     {
       
   253         Label label = new Label(iShell, SWT.NONE);
       
   254         label.setText("Redirect Logs");
       
   255         label.setLayoutData(new GridData(SWT.CENTER));
       
   256 
       
   257         iRedirect = new Button(iShell, SWT.TOGGLE);
       
   258         iRedirect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   259         iRedirect.addSelectionListener(new SelectionListener()
       
   260         {
       
   261             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   262             {
       
   263                 widgetSelected(aEvent);
       
   264             }
       
   265             public void widgetSelected(SelectionEvent event)
       
   266             {
       
   267                 redirectPressed();
       
   268             }
       
   269         });
       
   270         toggleRedirect(iRedirector.isRedirecting());
       
   271     }
       
   272 
       
   273     private void createCommands()
       
   274     {
       
   275         Command exit = new Command(iShell, Command.EXIT, 0);
       
   276         exit.setText("Exit");
       
   277         exit.addSelectionListener(new SelectionListener()
       
   278         {
       
   279             public void widgetDefaultSelected(SelectionEvent aEvent)
       
   280             {
       
   281                 widgetSelected(aEvent);
       
   282             }
       
   283             public void widgetSelected(SelectionEvent aEvent)
       
   284             {
       
   285                 exitEventLoop();
       
   286             }
       
   287         });
       
   288     }
       
   289 
       
   290     private void platformAdaptation()
       
   291     {
       
   292         if (isLinux())
       
   293         {
       
   294             configureUi();
       
   295         }
       
   296     }
       
   297 
       
   298     private void configureUi()
       
   299     {
       
   300         // add second dummy command so that menu bar is visible
       
   301         new Command(iShell, Command.GENERAL, 0).setText("");
       
   302 
       
   303         // disable not supported services
       
   304         iRedirect.setEnabled(false);
       
   305         iSecurityPolicy.setEnabled(false);
       
   306         iSecurityWarnings.setEnabled(false);
       
   307     }
       
   308 
       
   309     private void handleOrientationChange()
       
   310     {
       
   311         iShell.addControlListener(new ControlListener()
       
   312         {
       
   313             private boolean handleEvent = false;
       
   314             public void controlMoved(ControlEvent e)
       
   315             {
       
   316             }
       
   317             public void controlResized(ControlEvent e)
       
   318             {
       
   319                 // ignore first event that comes during startup
       
   320                 if (handleEvent)
       
   321                 {
       
   322                     // avoid flickering during orientation change
       
   323                     iShell.setVisible(false);
       
   324                     iShell.layout();
       
   325                     iShell.setVisible(true);
       
   326                 }
       
   327                 handleEvent = true;
       
   328             }
       
   329         });
       
   330     }
       
   331 
       
   332 
       
   333     private void javaCaptainPressed()
       
   334     {
       
   335         if (iJavaCaptain.getSelection())
       
   336         {
       
   337             iCaptain.start();
       
   338             javaCaptainStarted();
       
   339         }
       
   340         else
       
   341         {
       
   342             iCaptain.stop();
       
   343         }
       
   344         toggleJavaCaptain(iJavaCaptain.getSelection());
       
   345     }
       
   346 
       
   347     private void toggleJavaCaptain(boolean aSelected)
       
   348     {
       
   349         iJavaCaptain.setText( aSelected ? "Running" : "Stopped");
       
   350         iJavaCaptain.setSelection(aSelected);
       
   351     }
       
   352 
       
   353     private void prewarmPressed()
       
   354     {
       
   355         iCaptain.enablePrewarm(iPrewarm.getSelection());
       
   356         togglePrewarm(iPrewarm.getSelection());
       
   357     }
       
   358 
       
   359     private void togglePrewarm(boolean aSelected)
       
   360     {
       
   361         iPrewarm.setText( aSelected ? "Enabled" : "Disabled");
       
   362         iPrewarm.setSelection(aSelected);
       
   363     }
       
   364 
       
   365     private void threadDumpPressed()
       
   366     {
       
   367         iCaptain.doThreadDump();
       
   368     }
       
   369 
       
   370     private void javaDebugPressed()
       
   371     {
       
   372         iCaptain.enableDebug(iJavaDebug.getSelection());
       
   373         toggleJavaDebug(iJavaDebug.getSelection());
       
   374     }
       
   375 
       
   376     private void toggleJavaDebug(boolean aSelected)
       
   377     {
       
   378         iJavaDebug.setText( aSelected ? "Enabled" : "Disabled");
       
   379         iJavaDebug.setSelection(aSelected);
       
   380     }
       
   381 
       
   382     private void securityWarningsSelected()
       
   383     {
       
   384         iSecurity.setWarningsMode(iSecurityWarnings.getSelectionIndex());
       
   385     }
       
   386 
       
   387     private void securityPolicySelected()
       
   388     {
       
   389         iSecurity.setSecurityPolicy(iSecurityPolicy.getSelectionIndex());
       
   390     }
       
   391 
       
   392     private void redirectPressed()
       
   393     {
       
   394         if (iRedirect.getSelection())
       
   395         {
       
   396             iRedirector.start();
       
   397         }
       
   398         else
       
   399         {
       
   400             iRedirector.stop();
       
   401         }
       
   402         toggleRedirect(iRedirect.getSelection());
       
   403     }
       
   404 
       
   405     private void toggleRedirect(boolean aSelected)
       
   406     {
       
   407         iRedirect.setText( aSelected ? "Yes" : "No");
       
   408         iRedirect.setSelection(aSelected);
       
   409     }
       
   410 
       
   411     private void javaCaptainStarted()
       
   412     {
       
   413         // some JavaCaptain services return to default settings
       
   414         // when JC is restarted so refresh states
       
   415         togglePrewarm(iCaptain.isPrewarmEnabled());
       
   416         toggleJavaDebug(iCaptain.isDebugEnabled());
       
   417     }
       
   418 
       
   419     private boolean isLinux()
       
   420     {
       
   421         String platform = System.getProperty("os.name");
       
   422         if (platform != null && platform.equalsIgnoreCase("linux"))
       
   423         {
       
   424             return true;
       
   425         }
       
   426         return false;
       
   427     }
       
   428 
       
   429 }
       
   430 
       
   431 
       
   432 
       
   433 
       
   434