From cc41c2badfefd45906cc83b6f06a93cc5ef1554e Mon Sep 17 00:00:00 2001 From: "hyunsik.noh" Date: Tue, 12 Jul 2011 14:35:53 +0900 Subject: [PATCH] [Conn]Add and Fix highlighting bug in DeviceView --- .../ide/connection/FreshConnectionActivator.java | 13 +- .../ide/connection/device/DeviceChecker.java | 56 +++-- .../connection/handler/RemoveDeviceHandler.java | 4 +- .../com/samsung/ide/connection/ui/DeviceView.java | 255 ++++++++++++--------- .../com/samsung/ide/connection/ui/ImageLoader.java | 65 ++++++ 5 files changed, 265 insertions(+), 128 deletions(-) create mode 100644 com.samsung.ide.connection/src/com/samsung/ide/connection/ui/ImageLoader.java diff --git a/com.samsung.ide.connection/src/com/samsung/ide/connection/FreshConnectionActivator.java b/com.samsung.ide.connection/src/com/samsung/ide/connection/FreshConnectionActivator.java index f1a955b..fcac0e5 100644 --- a/com.samsung.ide.connection/src/com/samsung/ide/connection/FreshConnectionActivator.java +++ b/com.samsung.ide.connection/src/com/samsung/ide/connection/FreshConnectionActivator.java @@ -2,7 +2,6 @@ package com.samsung.ide.connection; import java.util.Properties; -import org.eclipse.core.internal.net.ProxySelector; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -13,6 +12,7 @@ import org.osgi.framework.BundleContext; import com.samsung.ide.connection.device.DeviceChecker; import com.samsung.ide.connection.machine.IMachine; import com.samsung.ide.connection.machine.LocalMachine; +import com.samsung.ide.connection.ui.ImageLoader; /** * The activator class controls the plug-in life cycle @@ -28,6 +28,8 @@ public class FreshConnectionActivator extends AbstractUIPlugin { public static final int LINUX = 0; public static final int WINDOWS = 1; + /** Image loader object */ + private ImageLoader mLoader; // The shared instance private static FreshConnectionActivator plugin; @@ -42,7 +44,6 @@ public class FreshConnectionActivator extends AbstractUIPlugin { * (non-Javadoc) * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) */ - @SuppressWarnings("restriction") public void start(BundleContext context) throws Exception { super.start(context); plugin = this; @@ -55,7 +56,9 @@ public class FreshConnectionActivator extends AbstractUIPlugin { else this.OS = this.LINUX; + mLoader = new ImageLoader(this); new DeviceChecker().start(); + UIJob job = new UIJob("Connect LocalMachine...") { public IStatus runInUIThread(IProgressMonitor monitor) { @@ -91,4 +94,10 @@ public class FreshConnectionActivator extends AbstractUIPlugin { public static FreshConnectionActivator getDefault() { return plugin; } + public static ImageLoader getImageLoader() { + if (plugin != null) { + return plugin.mLoader; + } + return null; + } } diff --git a/com.samsung.ide.connection/src/com/samsung/ide/connection/device/DeviceChecker.java b/com.samsung.ide.connection/src/com/samsung/ide/connection/device/DeviceChecker.java index 23d35fa..4b2c4a4 100644 --- a/com.samsung.ide.connection/src/com/samsung/ide/connection/device/DeviceChecker.java +++ b/com.samsung.ide.connection/src/com/samsung/ide/connection/device/DeviceChecker.java @@ -70,31 +70,40 @@ public class DeviceChecker extends Thread{ if( hasActiveRealDevice(realDeviceTestId)) { - activeDeviceList.put(realDeviceTestId, IMachine.REAL_DEVICE ); + testConnection( realDeviceTestId, IMachine.REAL_DEVICE ); } if( hasActiveVirtualDevice(emulatorTestId)) { - VirtualDeviceLauncher launcher = (VirtualDeviceLauncher)DeviceLauncherManager.getlauncherList().get( emulatorTestId ); - if( launcher != null ) + testConnection( emulatorTestId, IMachine.VIRTUAL_DEVICE ); + } + + } + + private void testConnection( String id, String type) + { + if( type.equals( IMachine.VIRTUAL_DEVICE )) + { + VirtualDeviceLauncher launcher = (VirtualDeviceLauncher)DeviceLauncherManager.getlauncherList().get( id ); + + if( launcher != null ) + { + if( launcher.getVirtualDeviceState() != IDeviceListener.RUNNING) { - if( launcher.getVirtualDeviceState() != IDeviceListener.RUNNING) - { - return; - } - } - else - { - DeviceMachine temp = DeviceManager.getDevice(emulatorTestId, IMachine.VIRTUAL_DEVICE); - try { - SessionManager.getSession( emulatorTestId +"check_temp", temp, new NullProgressMonitor()); - } catch (Exception e) { - e.printStackTrace(); - } -// SessionManager.removeSession( emulatorTestId +"check_temp"); + return; } - activeDeviceList.put(emulatorTestId, IMachine.VIRTUAL_DEVICE ); + } } - +// else +// { +// DeviceMachine tempMachine = DeviceManager.getDevice( id, type ); +// try { +// SessionManager.getSession( id +"_testConnection", tempMachine , new NullProgressMonitor()); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// SessionManager.removeSession( id +"_testConnection" ); +// } + activeDeviceList.put( id, type ); } synchronized private boolean compareList() @@ -195,11 +204,20 @@ public class DeviceChecker extends Thread{ else { DeviceMachine machine = DeviceManager.getDevice(deviceId, deviceType); + if( !machine.isReachable() && INTERFACE_NAME != null) { String command = InstallPathConfig.getUACPath() + File.separatorChar + "uaccmd.exe " + INTERFACE_NAME ; HostUtil.execute(command); } + + try { + SessionManager.getSession( deviceId +"_testConnection", machine , new NullProgressMonitor()); + } catch (Exception e) { + e.printStackTrace(); + } + SessionManager.removeSession( deviceId +"_testConnection" ); + DeviceLauncherManager.getDeviceLauncher(deviceId, deviceType); } } diff --git a/com.samsung.ide.connection/src/com/samsung/ide/connection/handler/RemoveDeviceHandler.java b/com.samsung.ide.connection/src/com/samsung/ide/connection/handler/RemoveDeviceHandler.java index 22d0677..302fdfc 100644 --- a/com.samsung.ide.connection/src/com/samsung/ide/connection/handler/RemoveDeviceHandler.java +++ b/com.samsung.ide.connection/src/com/samsung/ide/connection/handler/RemoveDeviceHandler.java @@ -19,9 +19,7 @@ public class RemoveDeviceHandler extends AbstractHandler { public Object execute(ExecutionEvent event) throws ExecutionException { - IStructuredSelection selection = (IStructuredSelection)HandlerUtil.getCurrentSelection(event); - Device deviceModel = (Device)selection.getFirstElement(); - DeviceMachine device = DeviceManager.getDevice( deviceModel.getDeviceId(), deviceModel.getDeviceType()); + DeviceMachine device = DeviceManager.getSelectedDevice(); if(DeviceChecker.hasActiveVirtualDevice( device.getMachineId() ) && device.getMachineType().equals( IMachine.VIRTUAL_DEVICE)) { diff --git a/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/DeviceView.java b/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/DeviceView.java index 5e264fa..e65d575 100644 --- a/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/DeviceView.java +++ b/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/DeviceView.java @@ -2,16 +2,14 @@ package com.samsung.ide.connection.ui; import java.util.concurrent.CopyOnWriteArrayList; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; import org.eclipse.core.runtime.ListenerList; -import org.eclipse.jface.action.MenuManager; -import org.eclipse.jface.action.Separator; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ColumnLabelProvider; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.ISelectionChangedListener; -import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; @@ -19,41 +17,125 @@ import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; -import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.IActionBars; import org.eclipse.ui.part.ViewPart; +import com.samsung.ide.connection.FreshConnectionActivator; import com.samsung.ide.connection.device.Device; import com.samsung.ide.connection.device.DeviceLauncherManager; import com.samsung.ide.connection.device.DeviceManager; import com.samsung.ide.connection.device.DeviceModelProvider; import com.samsung.ide.connection.device.IDeviceListener; +import com.samsung.ide.connection.handler.CreateDeviceHandler; +import com.samsung.ide.connection.handler.RemoveDeviceHandler; +import com.samsung.ide.connection.machine.IMachine; -public class DeviceView extends ViewPart implements IDeviceListener, ISelectionProvider{ - +public class DeviceView extends ViewPart { + private TableViewer Connections; + private Action startAction; + private Action stopAction; + private ImageLoader loader; - private String selection; ListenerList listeners = new ListenerList(); + + + private IDeviceListener dListener = new IDeviceListener() + { + + @Override + public void ConnectionChanged() { + CopyOnWriteArrayList deviceList = DeviceModelProvider.INSTANCE.getDevices(); + + boolean hasNoEmulator = true; + //There are one more devices at least + if( deviceList.size() != 0) + { + for( Device device: deviceList) + { + if( device.getDeviceType().equals(IMachine.VIRTUAL_DEVICE)) + hasNoEmulator =false; + } + + if( DeviceManager.getSelectedDevice() == null ) + { + for( Device device: deviceList) + { + device.setDeviceActivation( false ); + } + + Device justOneDeviceModel = deviceList.get(0); + DeviceManager.setSelectedDevice( justOneDeviceModel.getDeviceId() ); + justOneDeviceModel.setDeviceActivation( true ); +// Connections.getTable().select( 0 ); +// setSelection(new StructuredSelection(justOneDeviceModel.getDeviceId())); + } + +// if( !DeviceManager.isReachable(DeviceManager.getSelectedDevice())) +// DeviceManager.setSelectedDevice(null); + + activateActions( DeviceManager.getSelectedDevice().getMachineType() , hasNoEmulator); + } + //if, There is no device + else + DeviceManager.setSelectedDevice( null ); + + Connections.refresh(); + } + }; @Override public void dispose() { super.dispose(); - DeviceLauncherManager.removeDeviceListener(this); + DeviceLauncherManager.removeDeviceListener(dListener); } @Override public void createPartControl(Composite parent) { + loader = FreshConnectionActivator.getImageLoader(); createViewer(parent); - createContextMenu(); - DeviceLauncherManager.addDeviceListener(this); + createAction(); + DeviceLauncherManager.addDeviceListener(dListener); } - + + public void createAction() + { + final IHandler startHandler = new CreateDeviceHandler(); + final IHandler stopHandler = new RemoveDeviceHandler(); + + startAction = new Action("Start Emulator"){ + + public void run(){ + try { + startHandler.execute(null); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }; + startAction.setImageDescriptor(loader.loadDescriptor("Emulator_start.gif")); + + stopAction = new Action("Stop Emulator"){ + + public void run(){ + try { + stopHandler.execute(null); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }; + stopAction.setImageDescriptor(loader.loadDescriptor("Emulator_stop.gif")); + stopAction.setEnabled(false); + placeActions(); + } + public void createViewer(Composite parent){ Connections = @@ -66,8 +148,8 @@ public class DeviceView extends ViewPart implements IDeviceListener, ISelectionP table.setLayoutData(new GridData(GridData.FILL_BOTH)); Connections.setContentProvider(new ArrayContentProvider()); - Connections.setInput(DeviceModelProvider.INSTANCE.getDevices()); - getSite().setSelectionProvider(Connections); + Connections.setInput(DeviceModelProvider.INSTANCE.getDevices()); + getSite().setSelectionProvider(Connections); table.addMouseListener( new MouseListener(){ @@ -77,46 +159,64 @@ public class DeviceView extends ViewPart implements IDeviceListener, ISelectionP @Override public void mouseDown(MouseEvent e) { - IStructuredSelection selection = (IStructuredSelection) Connections.getSelection(); - CopyOnWriteArrayList deviceList = DeviceModelProvider.INSTANCE.getDevices(); - + IStructuredSelection selection = (IStructuredSelection)Connections.getSelection(); if( !selection.toString().equals("")) { - Device selectedDeviceModel = (Device)selection.getFirstElement(); + Device device = (Device)selection.getFirstElement(); + String id = device.getDeviceId(); + String type = device.getDeviceType(); + boolean hasNoEmulator = true; + +// Connections.setSelection( selection ); + DeviceManager.setSelectedDevice( id ); + + CopyOnWriteArrayList deviceList = DeviceModelProvider.INSTANCE.getDevices(); + for( int i = 0; i < deviceList.size() ; i++) { - if( selectedDeviceModel.getDeviceId().equals( deviceList.get(i).getDeviceId())) + if( id.equals( deviceList.get(i).getDeviceId())) deviceList.get(i).setDeviceActivation( true ); else deviceList.get(i).setDeviceActivation( false ); - } - DeviceManager.setSelectedDevice( selectedDeviceModel.getDeviceId()); - setSelection(new StructuredSelection(selectedDeviceModel.getDeviceId())); - } - //Nothing is selected - else - { - if( deviceList.size() != 0) - { - for( int i = 0; i < deviceList.size() ; i++) + + if( hasNoEmulator ) { - deviceList.get(i).setDeviceActivation( false ); - } - Device device = deviceList.get(0); - DeviceManager.setSelectedDevice( device.getDeviceId() ); - device.setDeviceActivation( true ); -// setSelection(new StructuredSelection(device.getDeviceId())); -// table.select(0); + if( IMachine.VIRTUAL_DEVICE.equals( deviceList.get(i).getDeviceType())) + hasNoEmulator = false; + } } + activateActions( type, hasNoEmulator); } + Connections.refresh(); - } @Override public void mouseUp(MouseEvent e) { } }); + + } + + public void activateActions( String type, boolean hasNoEmulator) + { + boolean start = false; + boolean stop = false; + + if( type.equals(IMachine.VIRTUAL_DEVICE)) + { + stop = true; + } + else + { + if( hasNoEmulator) + { + start = true; + } + } + + startAction.setEnabled(start); + stopAction.setEnabled(stop); } public TableViewer getViewer(){ @@ -162,72 +262,19 @@ public class DeviceView extends ViewPart implements IDeviceListener, ISelectionP return viewerColumn; } - private void createContextMenu() { - MenuManager menuManager = new MenuManager("#popup"); - menuManager.setRemoveAllWhenShown(true); - menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); - - Control control = Connections.getControl(); - Menu menu = menuManager.createContextMenu(control); - control.setMenu(menu); - getSite().registerContextMenu(menuManager, Connections); - } + private void placeActions() { + IActionBars actionBars = getViewSite().getActionBars(); + + IToolBarManager toolBarManager = actionBars.getToolBarManager(); + toolBarManager.add(startAction); + startAction.setEnabled(true); + toolBarManager.add(stopAction); + stopAction.setEnabled(false); + } + @Override public void setFocus() { Connections.getControl().setFocus(); } - - @Override - public void ConnectionChanged() { - CopyOnWriteArrayList deviceList = DeviceModelProvider.INSTANCE.getDevices(); - - if( deviceList.size() != 0) - { - if( DeviceManager.getSelectedDevice() == null ) - { - for( Device device: deviceList) - { - device.setDeviceActivation( false ); - } - - Device justOneDeviceModel = deviceList.get(0); - DeviceManager.setSelectedDevice( justOneDeviceModel.getDeviceId() ); - justOneDeviceModel.setDeviceActivation( true ); - Connections.getTable().select( 0 ); -// setSelection(new StructuredSelection(justOneDeviceModel.getDeviceId())); - } - } - else - DeviceManager.setSelectedDevice( null ); - - if( DeviceManager.getSelectedDevice() != null ) - { - if( !DeviceManager.isReachable(DeviceManager.getSelectedDevice())) - DeviceManager.setSelectedDevice(null); - } - Connections.refresh(); - } - - @Override - public void addSelectionChangedListener(ISelectionChangedListener listener) { - listeners.add( listener ); - - } - - @Override - public ISelection getSelection() { - return new StructuredSelection(selection); - } - - @Override - public void removeSelectionChangedListener( - ISelectionChangedListener listener) { - listeners.remove( listener ); - - } - - @Override - public void setSelection(ISelection selection) { - } } diff --git a/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/ImageLoader.java b/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/ImageLoader.java new file mode 100644 index 0000000..0ff7c77 --- /dev/null +++ b/com.samsung.ide.connection/src/com/samsung/ide/connection/ui/ImageLoader.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.samsung.ide.connection.ui; + +import org.eclipse.core.runtime.Plugin; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Display; + +import java.net.MalformedURLException; +import java.net.URL; + +/** + * Implementation of the IImageLoader interface for the eclipse plugin. + */ +public class ImageLoader{ + + private URL mBaseUrl; + + public ImageLoader(Plugin plugin) { + mBaseUrl = plugin.getBundle().getEntry("/"); // $NON-NLS-1$ + } + + /** + * default method. only need a filename. the 2 interface methods call this one. + * @param filename the filename of the image to load. The filename is searched for under /icons. + * @return + */ + public ImageDescriptor loadDescriptor(String filename) { + try { + URL newUrl = new URL(mBaseUrl, "/icons/" + filename); // $NON-NLS-1$ + return ImageDescriptor.createFromURL(newUrl); + } catch (MalformedURLException e) { + // we'll just return null; + } + return null; + } + + public ImageDescriptor loadDescriptor(String filename, Display display) { + return loadDescriptor(filename); + } + + + public Image loadImage(String filename, Display display) { + ImageDescriptor descriptor = loadDescriptor(filename); + if (descriptor !=null) { + return descriptor.createImage(); + } + return null; + } + +} -- 2.7.4