[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[dsdp-mtj-commit] r1486 - in trunk/plugins: org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/sdk/device org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui

Author: dmadruga
Date: 2009-09-03 17:29:06 -0400 (Thu, 03 Sep 2009)
New Revision: 1486

Modified:
   trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/sdk/device/DeviceRegistry.java
   trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIMessages.java
   trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIPlugin.java
   trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/messages.properties
Log:
[282738][contributed]  -  [fiximprove][SDK]: MTJ keep references of removed SDKs 

Modified: trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/sdk/device/DeviceRegistry.java
===================================================================
--- trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/sdk/device/DeviceRegistry.java	2009-09-03 21:21:45 UTC (rev 1485)
+++ trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/sdk/device/DeviceRegistry.java	2009-09-03 21:29:06 UTC (rev 1486)
@@ -13,6 +13,7 @@
  *     Hugo Raniere (Motorola)  - Storing default preverifier
  *     David Aragao (Motorola)  - Fixing device removal.
  *     Fernando Rocha(Motorola) - Loading the default devices [Bug 261873].
+ *     Rafael Amaral (Motorola) - Fixing management of SDK references [Bug 282738].
  */
 package org.eclipse.mtj.internal.core.sdk.device;
 
@@ -35,6 +36,7 @@
 import org.eclipse.mtj.core.sdk.device.IDevice;
 import org.eclipse.mtj.core.sdk.device.IDeviceRegistry;
 import org.eclipse.mtj.core.sdk.device.IDeviceRegistryListener;
+import org.eclipse.mtj.core.sdk.device.ILibrary;
 import org.eclipse.mtj.internal.core.persistence.XMLPersistenceProvider;
 import org.eclipse.mtj.internal.core.util.migration.DeviceRegistryMigration;
 import org.eclipse.mtj.internal.core.util.migration.IMigration;
@@ -83,7 +85,9 @@
     // Map of device group names that maps to a lower-level
     // map of device instances by name
     private Map<String, Map<String, IDevice>> deviceGroupsMap;
-
+      
+    private Map<String, Map<String, IDevice>> invalidDeviceGroupsMap;
+    
     private boolean fireDeviceAddedEvent = false;
 
     // A list of registry listeners
@@ -92,6 +96,7 @@
     private DeviceRegistry() {
         super();
         listenerList = new ArrayList<IDeviceRegistryListener>();
+        invalidDeviceGroupsMap = new HashMap<String, Map<String, IDevice>>();
         try {
 			this.load();
 		} catch (PersistenceException e) {
@@ -233,6 +238,17 @@
      * @see org.eclipse.mtj.core.sdk.device.IDeviceRegistry#load()
      */
     public void load() throws PersistenceException {
+        load(false);
+    }
+    
+    /**
+     * Similar to load()
+     * 
+     * @param loadOnlyValidDevices - false to load all devices, true to load
+     *            only valid devices
+     * @throws PersistenceException
+     */
+    private void load(boolean loadOnlyValidDevices) throws PersistenceException {
         File storeFile = getComponentStoreFile();
         if (storeFile.exists()) {
             try {
@@ -266,7 +282,7 @@
                 XMLPersistenceProvider pprovider = new XMLPersistenceProvider(
                         document);
 
-                loadUsing(pprovider);
+                loadUsing(pprovider, loadOnlyValidDevices);
             } catch (ParserConfigurationException e) {
                 throw new PersistenceException(e.getMessage(), e);
             } catch (SAXException e) {
@@ -285,9 +301,25 @@
      */
     public void loadUsing(IPersistenceProvider persistenceProvider)
             throws PersistenceException {
+        loadUsing(persistenceProvider, false);
+    }
+    
+    /**
+     * Similar to loadUsing
+     * 
+     * @param persistenceProvider the IPersistenceProvider implementation that
+     *            provides the facilities for storing and retrieving persistable
+     *            objects.
+     * @param loadOnlyValidDevices - false to load all devices, true to load
+     *            only valid devices
+     * @throws PersistenceException
+     */
+    private void loadUsing(IPersistenceProvider persistenceProvider,
+            boolean loadOnlyValidDevices) throws PersistenceException {
 
         enableDeviceAddedEvent(false);
         getDeviceGroupsMap().clear();
+        invalidDeviceGroupsMap.clear();
 
         try {
 
@@ -296,7 +328,35 @@
             for (int i = 0; i < deviceCount; i++) {
                 IDevice device = (IDevice) persistenceProvider
                         .loadPersistable("device" + i); //$NON-NLS-1$
-                addDevice(device);
+
+                List<ILibrary> entries = device.getClasspath().getEntries();
+                boolean isValidDevice = true;
+
+                for (ILibrary library : entries) {
+                    File file = library.toFile();
+                    if (!file.exists()) {
+                        isValidDevice = false;
+                        break;
+                    }
+                }
+
+                if (!loadOnlyValidDevices) {
+                    addDevice(device);
+                    if (!isValidDevice) {
+                        Map<String, IDevice> sdkMap = invalidDeviceGroupsMap
+                                .get(device.getSDKName());
+                        if (sdkMap == null) {
+                            sdkMap = new HashMap<String, IDevice>();
+                            invalidDeviceGroupsMap.put(device.getSDKName(),
+                                    sdkMap);
+                        }
+                        if (!sdkMap.containsKey(device.getName())) {
+                            sdkMap.put(device.getName(), device);
+                        }
+                    }
+                } else if (isValidDevice) {
+                    addDevice(device);
+                }
             }
 
             defaultDevice = (IDevice) persistenceProvider
@@ -308,6 +368,19 @@
             enableDeviceAddedEvent(true);
         }
     }
+    
+    /**
+     * Updates the device's persistence file with only valid devices
+     * 
+     * @throws PersistenceException
+     * @throws TransformerException
+     * @throws IOException
+     */
+    public void updateReferences() throws PersistenceException,
+            TransformerException, IOException {
+        load(true);
+        store();
+    }
 
     /* (non-Javadoc)
      * @see org.eclipse.mtj.core.sdk.device.IDeviceRegistry#removeDevice(org.eclipse.mtj.core.sdk.device.IDevice)
@@ -498,5 +571,17 @@
 
         return deviceGroupsMap;
     }
-
+    
+    /**
+     * Return a map where the key is the SDK name and the associated value is
+     * another map where the key is the device name and the value is a
+     * associated IDevice. The map returned have all invalid devices associated
+     * with its respective SDKs
+     * 
+     * @return map with all invalid devices associated with its respective SDKs
+     * @throws PersistenceException
+     */
+    public Map<String, Map<String, IDevice>> getInvalidDeviceGroupsMap() {
+        return invalidDeviceGroupsMap;
+    }
 }

Modified: trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIMessages.java
===================================================================
--- trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIMessages.java	2009-09-03 21:21:45 UTC (rev 1485)
+++ trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIMessages.java	2009-09-03 21:29:06 UTC (rev 1486)
@@ -605,6 +605,12 @@
     public static String MTJMultiPageContentOutline_toolTipText;
     public static String MTJSourcePage_quickOutlineAction_text;
 
+    public static String MTJUIPlugin_missing_devices_dialog_title;
+
+    public static String MTJUIPlugin_missing_devices_text;
+
+    public static String MTJUIPlugin_remove_invalid_references_question;
+
     public static String NewJ2MEProjectPreferencePage_compliance_label_text;
 
     public static String NewJ2MEProjectPreferencePage_description;

Modified: trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIPlugin.java
===================================================================
--- trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIPlugin.java	2009-09-03 21:21:45 UTC (rev 1485)
+++ trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/MTJUIPlugin.java	2009-09-03 21:29:06 UTC (rev 1486)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2004,2008 Craig Setera and others.
+ * Copyright (c) 2004,2009 Craig Setera and others.
  * 
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -11,14 +11,20 @@
  *     Diego Sandin (Motorola)  - Refactoring package name to follow eclipse
  *                                standards
  *     Diego Sandin (Motorola)  - Adapting code to use Jetty version 5.1.11        
- *     David Marques(Motorola)  - Adding buildGridData functions.                        
+ *     David Marques(Motorola)  - Adding buildGridData functions.     
+ *     Rafael Amaral (Motorola) - Adding SDK references verification [Bug 282738].                   
  */
 package org.eclipse.mtj.internal.ui;
 
+import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
+import javax.xml.transform.TransformerException;
+
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IWorkspace;
 import org.eclipse.core.resources.ProjectScope;
@@ -26,11 +32,16 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.preference.IPersistentPreferenceStore;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.mtj.core.MTJCore;
+import org.eclipse.mtj.core.persistence.PersistenceException;
+import org.eclipse.mtj.core.sdk.device.IDevice;
+import org.eclipse.mtj.core.sdk.device.IDeviceRegistry;
 import org.eclipse.mtj.internal.core.IMTJCoreConstants;
 import org.eclipse.mtj.internal.core.launching.midp.ota.OTAServer;
+import org.eclipse.mtj.internal.core.sdk.device.DeviceRegistry;
 import org.eclipse.mtj.internal.core.util.log.MTJLogger;
 import org.eclipse.mtj.internal.ui.util.PluginPreferenceStore;
 import org.eclipse.swt.SWT;
@@ -480,6 +491,8 @@
         if (startAtStart) {
             startupOTAServer();
         }
+        
+        verifyDevices();
     }
 
     /**
@@ -516,7 +529,69 @@
         try {
             OTAServer.getInstance().start();
         } catch (Exception e) {
-            MTJLogger.log(IStatus.ERROR, "startupOTAServer", e);
+            MTJLogger.log(IStatus.ERROR, "startupOTAServer", e); //$NON-NLS-1$
         }
     }
+    
+
+    /**
+     * Verifies if there are references to invalid devices, if it is true
+     * prompts to the user to choose between remove that references or not
+     */
+    private void verifyDevices() {
+
+        IDeviceRegistry deviceRegistry = MTJCore.getDeviceRegistry();
+
+        if (deviceRegistry instanceof DeviceRegistry) {
+
+            Map<String, Map<String, IDevice>> invalidDeviceGroupsMap = ((DeviceRegistry) deviceRegistry)
+                    .getInvalidDeviceGroupsMap();
+
+            if (!invalidDeviceGroupsMap.isEmpty()) {
+
+                List<String> sdkWithInvalidReferences = new ArrayList<String>(
+                        invalidDeviceGroupsMap.keySet());
+
+                boolean toRemove = showWarningInvalidReferences(sdkWithInvalidReferences);
+
+                if (toRemove) {
+                    try {
+                        ((DeviceRegistry) deviceRegistry).updateReferences();
+                    } catch (PersistenceException e) {
+                        MTJLogger.log(IStatus.ERROR, e);
+                    } catch (TransformerException e) {
+                        MTJLogger.log(IStatus.ERROR, e);
+                    } catch (IOException e) {
+                        MTJLogger.log(IStatus.ERROR, e);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Creates the dialog that prompts to the user to choose between remove the
+     * invalid references or not
+     * 
+     * @param sdkWithInvalidReferences - list of SDK that have invalid
+     *            references
+     * @return true if user chooses yes, false otherwise
+     */
+    private boolean showWarningInvalidReferences(
+            List<String> sdkWithInvalidReferences) {
+
+        String message = MTJUIMessages.MTJUIPlugin_missing_devices_text + "\n";
+
+        for (String removedSDK : sdkWithInvalidReferences) {
+            message += "\n" + removedSDK; //$NON-NLS-1$
+        }
+
+        message += "\n\n"
+                + MTJUIMessages.MTJUIPlugin_remove_invalid_references_question;
+
+        return MessageDialog
+                .openQuestion(Display.getDefault().getActiveShell(),
+                        MTJUIMessages.MTJUIPlugin_missing_devices_dialog_title,
+                        message);
+    }
 }

Modified: trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/messages.properties
===================================================================
--- trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/messages.properties	2009-09-03 21:21:45 UTC (rev 1485)
+++ trunk/plugins/org.eclipse.mtj.ui/src/org/eclipse/mtj/internal/ui/messages.properties	2009-09-03 21:29:06 UTC (rev 1486)
@@ -226,6 +226,9 @@
 MTJMultiPageContentOutline_sortingAction_text=Sort
 MTJMultiPageContentOutline_toolTipText=Sort
 MTJSourcePage_quickOutlineAction_text=Quick Outline
+MTJUIPlugin_missing_devices_dialog_title=MTJ Warning
+MTJUIPlugin_missing_devices_text=Devices from the following SDKs are missing:
+MTJUIPlugin_remove_invalid_references_question=Would you like to remove the references to them?
 ToggleLinkWithEditorAction_description=Link with active editorMTJMultiPageContentOutline.SortingAction.label = Sort
 ToggleLinkWithEditorAction_name=Lin&k with Editor
 ToggleLinkWithEditorAction_toolTipText=Link with Editor