sdkcreationmw/sdkconnectivityfw/emuconnectmanager/epdt_java/src/com/nokia/epdt/plugins/s60/pan/PanPluginJPanel.java
changeset 0 b26acd06ea60
child 1 ac50fd48361b
equal deleted inserted replaced
-1:000000000000 0:b26acd06ea60
       
     1 /*
       
     2 * Copyright (c) 2000 - 2006 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 
       
    19 package com.nokia.epdt.plugins.s60.pan;
       
    20 
       
    21 /* java.awt */
       
    22 import java.awt.Component;
       
    23 import java.awt.BorderLayout;
       
    24 import java.awt.GridBagLayout;
       
    25 import java.awt.GridBagConstraints;
       
    26 
       
    27 /* java.awt.event */
       
    28 import java.awt.event.ActionEvent;
       
    29 import java.awt.event.ActionListener;
       
    30 
       
    31 /* javax.swing */
       
    32 import javax.swing.Icon;
       
    33 import javax.swing.JOptionPane;
       
    34 import javax.swing.JPanel;
       
    35 import javax.swing.JLabel;
       
    36 import javax.swing.JComboBox;
       
    37 import javax.swing.JRadioButton;
       
    38 import javax.swing.ButtonGroup;
       
    39 
       
    40 /* javax.swing.border */
       
    41 import javax.swing.border.EmptyBorder;
       
    42 import javax.swing.border.TitledBorder;
       
    43 
       
    44 /* com.nokia.wtk.util.gui */
       
    45 import com.nokia.wtk.util.gui.GUI;
       
    46 import com.nokia.wtk.util.gui.BorderPanel;
       
    47 
       
    48 /**
       
    49  * PanPluginJPanel
       
    50  *
       
    51  * @version 1.0
       
    52  */
       
    53 class PanPluginJPanel extends JPanel
       
    54 {
       
    55 	private static final int GAP = 6;
       
    56 	private static final String AUTO_PORT = "auto";
       
    57 	private static final String[] HCI_MODULES = new String[]{"BCSP", "H4",
       
    58 			"USB"};
       
    59 	private static final int HCI_USB_INDEX = 2;
       
    60 	private static final int MAX_COM_PORTS = 16;
       
    61 
       
    62 	private PanModel panModel;
       
    63 	private PanPlugin panPlugin;
       
    64 
       
    65 	private boolean updateInProgress = false;
       
    66 	private JComboBox bluetoothComPortJComboBox;
       
    67 	private JRadioButton bluetoothDisableJRadioButton;
       
    68 	private JRadioButton bluetoothEnableJRadioButton;
       
    69 	private JComboBox bluetoothHciJComboBox;
       
    70 	private JComboBox irdaComPortJComboBox;
       
    71 	private JRadioButton irdaDisableJRadioButton;
       
    72 	private JRadioButton irdaEnableJRadioButton;
       
    73 
       
    74 	/**
       
    75 	 * Creates new form PanPluginJPanel
       
    76 	 * @param plugin the plugin that creates this panel
       
    77 	 * @param connected <code>true</code> if we are connected to the emulator
       
    78 	 */
       
    79 	PanPluginJPanel(PanPlugin plugin)
       
    80 	{
       
    81 		super(new GridBagLayout());
       
    82 		panPlugin = plugin;
       
    83 		panModel = plugin.getModel();
       
    84 		initComponents();
       
    85 	}
       
    86 
       
    87 	private static String getResString(String key)
       
    88 	{
       
    89 		return PanResourceBundle.getInstance().getValue(key);
       
    90 	}
       
    91 
       
    92 	JPanel buildOnePanel(String t, JRadioButton b1, JRadioButton b2, JPanel c)
       
    93 	{
       
    94 		BorderPanel bp = new BorderPanel(b2);
       
    95 		bp.getContentPanel().add(c);
       
    96 
       
    97 		JPanel p = new JPanel(new GridBagLayout());
       
    98 		GridBagConstraints gbc = new GridBagConstraints();
       
    99 
       
   100 		gbc.weightx = 1.0;
       
   101 		gbc.gridwidth = GridBagConstraints.REMAINDER;
       
   102 		gbc.fill = GridBagConstraints.HORIZONTAL;
       
   103 		gbc.anchor = GridBagConstraints.NORTH;
       
   104 		gbc.insets.left = bp.getBorder().getBorderInsets(bp).left
       
   105 				+ BorderPanel.COMPONENT_INSET_H;
       
   106 		p.add(b1, gbc);
       
   107 
       
   108 		gbc.weighty = 1.0;
       
   109 		gbc.insets.left = 0;
       
   110 		gbc.fill = GridBagConstraints.BOTH;
       
   111 		gbc.gridheight = GridBagConstraints.REMAINDER;
       
   112 		p.add(bp, gbc);
       
   113 
       
   114 		int left = BorderPanel.COMPONENT_INSET_H + GAP;
       
   115 		c.setBorder(new EmptyBorder(0, left, GAP, GAP));
       
   116 		p.setBorder(new TitledBorder(t));
       
   117 		return p;
       
   118 	}
       
   119 
       
   120 	/** This method is called from within the constructor to
       
   121 	 * initialize the form.
       
   122 	 */
       
   123 	private void initComponents()
       
   124 	{
       
   125 		Object[] btComPorts = new Object[MAX_COM_PORTS];
       
   126 		Object[] irdaComPorts = new Object[MAX_COM_PORTS];
       
   127 		for (int i = 0; i < MAX_COM_PORTS; i++)
       
   128 		{
       
   129 			String label = Integer.toString(i + 1);
       
   130 			btComPorts[i] = new ComPortItem(label);
       
   131 			irdaComPorts[i] = label;
       
   132 		}
       
   133 
       
   134 		bluetoothDisableJRadioButton = new JRadioButton(
       
   135 				getResString(PanConstants.BLUETOOTH_DISABLE_LABEL));
       
   136 		bluetoothEnableJRadioButton = new JRadioButton(
       
   137 				getResString(PanConstants.BLUETOOTH_ENABLE_LABEL));
       
   138 		bluetoothComPortJComboBox = new JComboBox(btComPorts);
       
   139 		bluetoothHciJComboBox = new JComboBox(HCI_MODULES);
       
   140 		irdaDisableJRadioButton = new JRadioButton(
       
   141 				getResString(PanConstants.IRDA_DISABLE_LABEL));
       
   142 		irdaEnableJRadioButton = new JRadioButton(
       
   143 				getResString(PanConstants.IRDA_ENABLE_LABEL));
       
   144 		irdaComPortJComboBox = new JComboBox(irdaComPorts);
       
   145 
       
   146 		// Bluetooth
       
   147 		JPanel bluetoothEnableJPanel = GUI
       
   148 				.createRowPanel(new Component[]{
       
   149 						new JLabel(
       
   150 								getResString(PanConstants.BLUETOOTH_COM_PORT_LABEL)),
       
   151 						bluetoothComPortJComboBox,
       
   152 						new JLabel(
       
   153 								getResString(PanConstants.BLUETOOTH_COM_HCI_LABEL)),
       
   154 						bluetoothHciJComboBox});
       
   155 
       
   156 		JPanel bluetoothJPanel = buildOnePanel(
       
   157 				getResString(PanConstants.BLUETOOTH_BORDER_TITLE),
       
   158 				bluetoothDisableJRadioButton, bluetoothEnableJRadioButton,
       
   159 				bluetoothEnableJPanel);
       
   160 
       
   161 		ButtonGroup bluetoothButtonGroup = new ButtonGroup();
       
   162 		bluetoothButtonGroup.add(bluetoothDisableJRadioButton);
       
   163 		bluetoothDisableJRadioButton.addActionListener(new ActionListener()
       
   164 		{
       
   165 			public void actionPerformed(ActionEvent evt)
       
   166 			{
       
   167 				bluetoothDisableJRadioButtonActionPerformed(evt);
       
   168 			}
       
   169 		});
       
   170 
       
   171 		bluetoothButtonGroup.add(bluetoothEnableJRadioButton);
       
   172 		bluetoothEnableJRadioButton.addActionListener(new ActionListener()
       
   173 		{
       
   174 			public void actionPerformed(ActionEvent evt)
       
   175 			{
       
   176 				bluetoothEnableJRadioButtonActionPerformed(evt);
       
   177 			}
       
   178 		});
       
   179 
       
   180 		bluetoothComPortJComboBox.addActionListener(new ActionListener()
       
   181 		{
       
   182 			public void actionPerformed(ActionEvent evt)
       
   183 			{
       
   184 				bluetoothComPortJComboBoxActionPerformed(evt);
       
   185 			}
       
   186 		});
       
   187 
       
   188 		bluetoothHciJComboBox.addActionListener(new ActionListener()
       
   189 		{
       
   190 			public void actionPerformed(ActionEvent evt)
       
   191 			{
       
   192 				bluetoothHciJComboBoxActionPerformed(evt);
       
   193 			}
       
   194 		});
       
   195 
       
   196 		// IRDA
       
   197 		JPanel irdaEnableJPanel = GUI.createRowPanel(new Component[]{
       
   198 				new JLabel(getResString(PanConstants.IRDA_COM_PORT_LABEL)),
       
   199 				irdaComPortJComboBox});
       
   200 
       
   201 		JPanel irdaJPanel = buildOnePanel(
       
   202 				getResString(PanConstants.IRDA_BORDER_TITLE),
       
   203 				irdaDisableJRadioButton, irdaEnableJRadioButton,
       
   204 				irdaEnableJPanel);
       
   205 
       
   206 		ButtonGroup irdaButtonGroup = new ButtonGroup();
       
   207 		irdaButtonGroup.add(irdaDisableJRadioButton);
       
   208 		irdaDisableJRadioButton.addActionListener(new ActionListener()
       
   209 		{
       
   210 			public void actionPerformed(ActionEvent evt)
       
   211 			{
       
   212 				irdaDisableJRadioButtonActionPerformed(evt);
       
   213 			}
       
   214 		});
       
   215 
       
   216 		irdaButtonGroup.add(irdaEnableJRadioButton);
       
   217 		irdaEnableJRadioButton.addActionListener(new ActionListener()
       
   218 		{
       
   219 			public void actionPerformed(ActionEvent evt)
       
   220 			{
       
   221 				irdaEnableJRadioButtonActionPerformed(evt);
       
   222 			}
       
   223 		});
       
   224 
       
   225 		irdaComPortJComboBox.addActionListener(new ActionListener()
       
   226 		{
       
   227 			public void actionPerformed(ActionEvent evt)
       
   228 			{
       
   229 				irdaComPortJComboBoxActionPerformed(evt);
       
   230 			}
       
   231 		});
       
   232 
       
   233 		JPanel row = new JPanel(new GridBagLayout());
       
   234 		GridBagConstraints gbc = new GridBagConstraints();
       
   235 		gbc.gridheight = GridBagConstraints.REMAINDER;
       
   236 		gbc.fill = GridBagConstraints.BOTH;
       
   237 		gbc.insets.right = GAP;
       
   238 		gbc.weightx = 1.0;
       
   239 		gbc.weighty = 1.0;
       
   240 		row.add(bluetoothJPanel, gbc);
       
   241 
       
   242 		gbc.insets.right = 0;
       
   243 		gbc.gridwidth = GridBagConstraints.REMAINDER;
       
   244 		row.add(irdaJPanel, gbc);
       
   245 
       
   246 		gbc.gridheight = 1;
       
   247 		gbc.fill = GridBagConstraints.HORIZONTAL;
       
   248 		gbc.anchor = GridBagConstraints.NORTH;
       
   249 		add(row, gbc);
       
   250 
       
   251 		gbc.gridheight = GridBagConstraints.REMAINDER;
       
   252 		gbc.anchor = GridBagConstraints.SOUTH;
       
   253 
       
   254 		setBorder(new EmptyBorder(GAP, GAP, GAP, GAP));
       
   255 
       
   256 		//Set the initial values, obtained from .esk files:
       
   257 		update();
       
   258 	}
       
   259 
       
   260 	/**
       
   261 	 * Update UI components from Model:
       
   262 	 */
       
   263 	public void update()
       
   264 	{
       
   265 		// Update Bluetooth panel
       
   266 		if (!updateInProgress)
       
   267 		{//If the function is not called as the result of components updates,
       
   268 			//i.e. there is no update ongiong already
       
   269 			updateInProgress = true;
       
   270 			boolean bluetoothEnabled = panModel.isBluetoothEnabled();
       
   271 			int hci = panModel.getBluetoothHci();
       
   272 			boolean usb = (hci == HCI_USB_INDEX);
       
   273 			bluetoothDisableJRadioButton.setSelected(!bluetoothEnabled);
       
   274 			bluetoothEnableJRadioButton.setSelected(bluetoothEnabled);
       
   275 			bluetoothHciJComboBox.setSelectedIndex(bluetoothEnabled ? hci : -1);
       
   276 			bluetoothHciJComboBox.setEnabled(bluetoothEnabled);
       
   277 			bluetoothComPortJComboBox.setEnabled(bluetoothEnabled);
       
   278 			bluetoothComPortJComboBox.setSelectedIndex(panModel
       
   279 					.getBluetoothComPort());
       
   280 			bluetoothComPortJComboBox.setEnabled(bluetoothEnabled && !usb);
       
   281 
       
   282 			// Update IRDA panel
       
   283 			boolean irdaEnabled = panModel.isIrdaEnabled();
       
   284 			irdaDisableJRadioButton.setSelected(!irdaEnabled);
       
   285 			irdaEnableJRadioButton.setSelected(irdaEnabled);
       
   286 			irdaComPortJComboBox.setEnabled(irdaEnabled);
       
   287 			irdaComPortJComboBox.setSelectedIndex(panModel.getIrdaComPort());
       
   288 			updateInProgress = false;
       
   289 		}
       
   290 	}
       
   291 
       
   292 	private void irdaComPortJComboBoxActionPerformed(ActionEvent evt)
       
   293 	{
       
   294 		if (!updateInProgress)
       
   295 		{//I.e. if update came from UI
       
   296 			if (panModel.getBluetoothHci() == panModel.BT_HCI_USB_INDEX || 
       
   297 					//HCI is not USB, where com port is selected automatically
       
   298 				irdaComPortJComboBox.getSelectedIndex() != panModel
       
   299 					.getBluetoothComPort())
       
   300 				//or com ports are different
       
   301 			{
       
   302 				panModel
       
   303 						.setIrdaComPort(irdaComPortJComboBox.getSelectedIndex());
       
   304 				panPlugin.pluginModelChangedAction();
       
   305 			} else
       
   306 			{
       
   307 				irdaComPortJComboBox
       
   308 						.setSelectedIndex(panModel.getIrdaComPort());
       
   309 				JOptionPane.showMessageDialog(this,
       
   310 						getResString("comPortEqualWarning"),
       
   311 						getResString("dialogTitle"), JOptionPane.ERROR_MESSAGE);
       
   312 			}
       
   313 		}
       
   314 	}
       
   315 
       
   316 	private void irdaEnableJRadioButtonActionPerformed(ActionEvent evt)
       
   317 	{
       
   318 		if (!updateInProgress)
       
   319 		{//I.e. if update came from UI
       
   320 			//We set the default values of com ports (COM 1) when switching
       
   321 			//from disabled mode
       
   322 			panModel.setIrdaComPort(0);
       
   323 			panPlugin.pluginModelChangedAction();
       
   324 			update();
       
   325 		}
       
   326 	}
       
   327 
       
   328 	private void irdaDisableJRadioButtonActionPerformed(ActionEvent evt)
       
   329 	{
       
   330 		if (!updateInProgress)
       
   331 		{//I.e. if update came from UI
       
   332 			//Setting com port to -1 means disabling it
       
   333 			panModel.setIrdaComPort(-1);
       
   334 			panPlugin.pluginModelChangedAction();
       
   335 			update();
       
   336 		}
       
   337 	}
       
   338 
       
   339 	private void bluetoothEnableJRadioButtonActionPerformed(ActionEvent evt)
       
   340 	{
       
   341 		if (!updateInProgress)
       
   342 		{//I.e. if update came from UI
       
   343 			//We set the default values of com ports (COM 1) when switching
       
   344 			//from disabled mode
       
   345 			panModel.setBluetoothComPort(0);
       
   346 			panPlugin.pluginModelChangedAction();
       
   347 			update();
       
   348 		}
       
   349 	}
       
   350 
       
   351 	private void bluetoothDisableJRadioButtonActionPerformed(ActionEvent evt)
       
   352 	{
       
   353 		if (!updateInProgress)
       
   354 		{//I.e. if update came from UI
       
   355 			//Setting com port to -1 means disabling it
       
   356 			panModel.setBluetoothComPort(-1);
       
   357 			panPlugin.pluginModelChangedAction();
       
   358 			update();
       
   359 		}
       
   360 	}
       
   361 
       
   362 	private void bluetoothHciJComboBoxActionPerformed(ActionEvent evt)
       
   363 	{
       
   364 		if (!updateInProgress)
       
   365 		{//I.e. if update came from UI
       
   366 			int index = bluetoothHciJComboBox.getSelectedIndex();
       
   367 			if (index >= 0)
       
   368 			{
       
   369 				panModel.setBluetoothHci(index);
       
   370 				panPlugin.pluginModelChangedAction();
       
   371 				update();
       
   372 			}
       
   373 		}
       
   374 	}
       
   375 
       
   376 	private void bluetoothComPortJComboBoxActionPerformed(ActionEvent evt)
       
   377 	{
       
   378 		if (!updateInProgress)
       
   379 		{//I.e. if update came from UI
       
   380 			if (bluetoothComPortJComboBox.getSelectedIndex() != panModel
       
   381 					.getIrdaComPort())
       
   382 			{
       
   383 				panModel.setBluetoothComPort(bluetoothComPortJComboBox
       
   384 						.getSelectedIndex());
       
   385 				panPlugin.pluginModelChangedAction();
       
   386 			} else
       
   387 			{
       
   388 				bluetoothComPortJComboBox.setSelectedIndex(panModel
       
   389 						.getBluetoothComPort());
       
   390 				JOptionPane.showMessageDialog(this,
       
   391 						getResString("comPortEqualWarning"),
       
   392 						getResString("dialogTitle"), JOptionPane.ERROR_MESSAGE);
       
   393 			}
       
   394 		}
       
   395 	}
       
   396 
       
   397 	private class ComPortItem
       
   398 	{
       
   399 		private String comPortNumber;
       
   400 		ComPortItem(String label)
       
   401 		{
       
   402 			comPortNumber = label;
       
   403 		}
       
   404 		public String toString()
       
   405 		{
       
   406 			return panModel.getBluetoothHci() == HCI_USB_INDEX
       
   407 					? AUTO_PORT
       
   408 					: comPortNumber;
       
   409 		}
       
   410 	}
       
   411 
       
   412 }