alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.windows.persistence.convert;

import org.netbeans.core.projects.SessionManager;
import org.netbeans.core.windows.Debug;
import org.netbeans.core.windows.ModeImpl;
import org.netbeans.core.windows.WindowManagerImpl;
import org.netbeans.core.windows.persistence.*;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.Repository;
import org.openide.util.NbBundle;
import org.openide.windows.WindowManager;

import java.io.IOException;
import java.util.*;

/**
 * Handle conversion (import) of old configuration winsys data. (Version < 2.0.)
 *
 * @author Marek Slama
 */

public class ImportManager {
    
    /** Root folder for old configuration data. */
    static final String ROOT_FOLDER          = "Windows"; // NOI18N
    static final String WINDOWMANAGER_FOLDER = "WindowManager"; // NOI18N
    static final String WORKSPACE_EXT        = "wswksp"; // NOI18N
    private static final String COMPS_FOLDER = "Components"; // NOI18N
    
    //Frame types (old data from mode)
    static final int INTERNAL_FRAME = 0;
    static final int DESKTOP_FRAME  = 1;
    static final int TOP_FRAME      = 2;
    
    static final int MULTI_TABBED_CONTAINER = 0;
    static final int SPLIT_CONTAINER        = 1;
    
    //Constraints of PerimeterLayout
    /** Represents the top side. */
    static final String TOP = "TOP"; // NOI18N
    
    /** Represents the bottom side. */
    static final String BOTTOM = "BOTTOM"; // NOI18N
    
    /** Represents the right side. */
    static final String RIGHT = "RIGHT"; // NOI18N
    
    /** Represents the left side. */
    static final String LEFT = "LEFT"; // NOI18N
    
    /** Represents the center area. */
    static final String  CENTER = "CENTER"; // NOI18N
    
    /** Singleton instance of ImportManager */
    private static ImportManager defaultInstance;
    
    /** Loading/saving of old window system configuration data */
    private WindowManagerParserOld wmParserOld;
    
    /** A map of imported TcIds. */
    private Map importedTcIds = new HashMap(10); // 
    
    /** Handling module enable/disable. */
    private ModuleChangeHandlerOld changeHandler = new ModuleChangeHandlerOld();
    
    private ImportManager () {
    }
    
    /** Returns reference to singleton instance of PersistenceManager.
     */
    public static synchronized ImportManager getDefault() {
        if (defaultInstance == null) {
            defaultInstance = new ImportManager();
        }
        return defaultInstance;
    }
    
    /** Reset internal data when project is switched. */
    public void reset () {
        importedTcIds.clear();
        wmParserOld = null;
    }
    
    /** Returns map of imported tcref items. Used only to serialize/deserialize
     * by WindowManagerParser. */
    public Map getImportedTCRefs () {
        return importedTcIds;
    }
    
    /** Sets map of imported tcref items. Used only to serialize/deserialize
     * by WindowManagerParser. */
    public void setImportedTCRefs (Map importedTcIds) {
        this.importedTcIds = importedTcIds;
    }
    
    /** Returns true if given tc_id was imported from old configuration. */
    public boolean isImportedTcId (String tc_id) {
        return importedTcIds.containsKey(tc_id);
    }
    
    /** Instance of FileObject is NOT cached because it can be completely
     * destroyed when module is disabled. So it is necessary to create
     * new instance to get its children correctly.
     * @return Root folder of old window system configuration data. */
    FileObject getRootFolder () {
        try {
            FileObject rootFolder = FileUtil.createFolder(
                Repository.getDefault().getDefaultFileSystem().getRoot(), ROOT_FOLDER
            );
            return rootFolder;
        } catch (IOException exc) {
            String annotation = NbBundle.getMessage(ImportManager.class,
                "EXC_RootFolder", ROOT_FOLDER);
            ErrorManager.getDefault().annotate(exc, annotation);
            ErrorManager.getDefault().notify(ErrorManager.ERROR, exc);
        }
        return null;
    }
    
    /** Instance of FileObject is NOT cached because it can be completely
     * destroyed when module is disabled. So it is necessary to create
     * new instance to get its children correctly.
     * @return Window manager folder of old window system configuration data. */
    public FileObject getWindowManagerFolder () {
        try {
            FileObject wmFolder = FileUtil.createFolder(
                getRootFolder(), WINDOWMANAGER_FOLDER
            );
            return wmFolder;
        } catch (IOException exc) {
            String annotation = NbBundle.getMessage(ImportManager.class,
                "EXC_WindowManagerFolder", WINDOWMANAGER_FOLDER);
            ErrorManager.getDefault().annotate(exc, annotation);
            ErrorManager.getDefault().notify(ErrorManager.ERROR, exc);
        }
        return null;
    }
    
    /** Instance of FileObject is NOT cached because it can be completely
     * destroyed when module is disabled. So it is necessary to create
     * new instance to get its children correctly.
     * @return Folder for TopComponent settings files */
    FileObject getComponentsFolder () {
        try {
            FileObject componentsFolder = FileUtil.createFolder(
                getRootFolder(), COMPS_FOLDER
            );
            return componentsFolder;
        } catch (IOException exc) {
            String annotation = NbBundle.getMessage(PersistenceManager.class,
                "EXC_CompsFolder", COMPS_FOLDER);
            ErrorManager.getDefault().annotate(exc, annotation);
            ErrorManager.getDefault().notify(ErrorManager.ERROR, exc);
        }
        return null;
    }
    
    WindowManagerParserOld getWindowManagerParserOld () {
        if (wmParserOld == null) {
            wmParserOld = new WindowManagerParserOld(this, WINDOWMANAGER_FOLDER);
        }
        return wmParserOld;
    }

    /** Starts handling of file events from old window system configuration folder
     * Windows. */
    public void startHandling () {
        changeHandler.startHandling();
    }
    
    /** Stops handling of file events from old window system configuration folder
     * Windows. Handling must be stopped when project layer is switched during projects
     * switch ie. 1.Stop handling. 2.Switch project layer. 3. Start handling. */
    public void stopHandling () {
        changeHandler.stopHandling();
    }
    
    /** Loads old window system configuration from installation layer.
     * @return window system configuration
     */
    WindowManagerConfigOld loadWindowSystemOld () {
        long start = System.currentTimeMillis();
        
        WindowManagerParserOld wmParser = getWindowManagerParserOld();
        WindowManagerConfigOld wmc = null;
        try {
            wmc = wmParser.load();
        } catch (IOException exc) {
            ErrorManager.getDefault().notify(
                ErrorManager.WARNING, exc
            );
        }
        
        changeHandler.startHandling();
        
        long end = System.currentTimeMillis();
        long diff = end - start;
        
        //System.out.println("## Loading of old window system takes " + diff + " ms");
        
        return wmc;
    }

    /** Import old window system configuration data. Called from PersistenceManager
     * when window system is loaded */
    public void importConfigurationData
    (WindowManagerConfig wmc, WindowManagerParser wmParser) {
        log("importConfigurationData ENTER");
        WindowManagerConfigOld wmcOld = loadWindowSystemOld();
        
        //System.out.println(dumpConfig(wmcOld));
        
        //Get list of workspaces, sort it appropriately and process
        //import workspaces one by one.
        List wsList = new ArrayList(wmcOld.workspaces.length);
        //Priority of workspaces is defined by their order for import.
        //"Editing" workspace is imported first it has highest priority.
        //Look for "Editing" workspace
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if ("Editing".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        //Look for "Visual" workspace
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if ("Visual".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        //Look for "Debugging" workspace
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if ("Debugging".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        //All other workspaces
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if (!"Editing".equals(wmcOld.workspaces[i].name) &&
                !"Visual".equals(wmcOld.workspaces[i].name) &&
                !"Debugging".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        
        //Dump workspaces
        for (int i = 0; i < wsList.size(); i++) {
            WorkspaceConfigOld wsCfg = (WorkspaceConfigOld) wsList.get(i);
            log("importConfigurationData ws[" + i + "]: " + wsCfg.name);
        }
        
        //Import workspaces
        for (int i = 0; i < wsList.size(); i++) {
            WorkspaceConfigOld wsCfg = (WorkspaceConfigOld) wsList.get(i);
            importWorkspace(wmc, wmParser, wsCfg);
        }
        log("importConfigurationData LEAVE");
    }
    
    /** Import old window system configuration data. Called from PersistenceManager
     * when window system is loaded */
    private void importWorkspace
    (WindowManagerConfig wmc, WindowManagerParser wmParser, WorkspaceConfigOld wsCfgSrc) {
        //Opened TopComponent is opened only when it is defined in
        boolean open = "Editing".equals(wsCfgSrc.name);
        log("importWorkspace ENTER ws:" + wsCfgSrc.name);
        for (int i = 0; i < wsCfgSrc.modes.length; i++) {
            ModeConfigOld modeCfgSrc = wsCfgSrc.modes[i];
            if (modeCfgSrc.tcRefConfigs.length == 0) {
                continue;
            }
            //Check if mode with the same name is present in new winsys (modes like editor,explorer,...)
            //If found add TopComponent to existing mode otherwise add TopComponent to editor.
            log("importWorkspace -- mo[" + i + "]: " + modeCfgSrc.name);
            ModeConfig modeCfgDest = null;
            //Find mode with the same name
            for (int j = 0; j < wmc.modes.length; j++) {
                if (modeCfgSrc.name.equals(wmc.modes[j].name)) {
                    modeCfgDest = wmc.modes[j];
                    break;
                }
            }
            //Find editor mode
            if (modeCfgDest == null) {
                for (int j = 0; j < wmc.modes.length; j++) {
                    if ("editor".equals(wmc.modes[j].name)) {
                        modeCfgDest = wmc.modes[j];
                        break;
                    }
                }
            }
            //No mode found so far, use first mode if available
            if (modeCfgDest == null) {
                if (wmc.modes.length > 0) {
                    modeCfgDest = wmc.modes[0];
                }
            }
            //No mode defined in new winsys.
            //XXX What to do?? Log it and cancel import for now.
            //It would be possible to create some default mode.
            //Current import specification assumes there is at least editor mode.
            if (modeCfgDest == null) {
                ErrorManager.getDefault().log(ErrorManager.WARNING,
                "[WinSys.ImportManager.importWorkspace]" // NOI18N
                + " Warning: No mode is defined where imported TopComponent could be placed." // NOI18N
                + " Import is cancelled."); // NOI18N
                return;
            }
            log("importWorkspace modeCfgDest:" + modeCfgDest.name);
            
            List tcRefCfgDestList = new ArrayList();
            
            for (int j = 0; j < modeCfgSrc.tcRefConfigs.length; j++) {
                TCRefConfigOld tcRefCfgSrc = modeCfgSrc.tcRefConfigs[j];
                //Check if given TopComponent is not already imported (from previous workspace
                //or from previous import).
                if (importedTcIds.containsKey(tcRefCfgSrc.tc_id)) {
                    log("importWorkspace TopComponent ID '" // NOI18N
                    + tcRefCfgSrc.tc_id + "' is already imported."); // NOI18N
                    continue;
                }
                //Check if given ID is not already used.
                if (PersistenceManager.getDefault().isUsedTCId(tcRefCfgSrc.tc_id)) {
                    log("importWorkspace TopComponent ID" // NOI18N
                    + " '" + tcRefCfgSrc.tc_id + "'" // NOI18N
                    + " is already used in new window system."); // NOI18N
                    continue;
                }
                ImportedItem item = new ImportedItem(wsCfgSrc.name,modeCfgSrc.name,tcRefCfgSrc.tc_id);
                importedTcIds.put(tcRefCfgSrc.tc_id,item);
                
                log("importWorkspace ADD ITEM [" + wsCfgSrc.name + ","
                + modeCfgSrc.name + "," + tcRefCfgSrc.tc_id + "]");
                
                PersistenceManager.getDefault().addUsedTCId(tcRefCfgSrc.tc_id);
                
                log("importWorkspace importing TopComponent ID '" // NOI18N
                + tcRefCfgSrc.tc_id + "'"); // NOI18N
                //Add new tcRef to destination mode
                TCRefConfig tcRefCfgDest = new TCRefConfig();
                tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
                if (open) {
                    tcRefCfgDest.opened = tcRefCfgSrc.opened;
                } else {
                    tcRefCfgDest.opened = false;
                }
                if (copySettingsFile(tcRefCfgSrc.tc_id)) {
                    tcRefCfgDestList.add(tcRefCfgDest);
                }
                //Add new TCRefParser to pass module info
                WindowManagerParserOld wmParserOld = getWindowManagerParserOld();
                TCRefParserOld tcRefParserOld =
                    wmParserOld.findTCRefParser(wsCfgSrc.name,modeCfgSrc.name,tcRefCfgSrc.tc_id);
                wmParser.addTCRefImport(modeCfgDest.name,tcRefCfgDest.tc_id,tcRefParserOld.getInternalConfig());
                
            }
            //Is anything to add?
            if (tcRefCfgDestList.size() > 0) {
                tcRefCfgDestList.addAll(0,Arrays.asList(modeCfgDest.tcRefConfigs));
                modeCfgDest.tcRefConfigs =
                    (TCRefConfig []) tcRefCfgDestList.toArray(new TCRefConfig[tcRefCfgDestList.size()]);
                for (int k = 0; k < modeCfgDest.tcRefConfigs.length; k++) {
                    log("importWorkspace tcRef[" + k + "]: " + modeCfgDest.tcRefConfigs[k].tc_id);
                }
            }
        }
    }
    
    /** Process change (new data added) in old window system configuration data.
     * Called from ModuleChangeHandlerOld when change in module folder is detected. */
    void addConfigurationData () {
        log("addConfigurationData ENTER");
        
        WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
        WindowManagerImpl wm = WindowManagerImpl.getInstance();
        
        WindowManagerConfigOld wmcOld = loadWindowSystemOld();
        
        Set modeSet = wm.getModes();
        
        String [] modeNameArray = new String[modeSet.size()];
        int k = 0;
        for (Iterator it = modeSet.iterator(); it.hasNext(); k++) {
            ModeImpl mo = (ModeImpl) it.next();
            modeNameArray[k] = mo.getName();
        }
        
        //System.out.println(dumpConfig(wmcOld));
        
        //Get list of workspaces, sort it appropriately and process
        //import workspaces one by one.
        List wsList = new ArrayList(wmcOld.workspaces.length);
        //Priority of workspaces is defined by their order for import.
        //"Editing" workspace is imported first it has highest priority.
        //Look for "Editing" workspace
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if ("Editing".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        //Look for "Visual" workspace
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if ("Visual".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        //Look for "Debugging" workspace
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if ("Debugging".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        //All other workspaces
        for (int i = 0; i < wmcOld.workspaces.length; i++) {
            if (!"Editing".equals(wmcOld.workspaces[i].name) &&
                !"Visual".equals(wmcOld.workspaces[i].name) &&
                !"Debugging".equals(wmcOld.workspaces[i].name)) {
                wsList.add(wmcOld.workspaces[i]);
                break;
            }
        }
        
        //Dump workspaces
        for (int i = 0; i < wsList.size(); i++) {
            WorkspaceConfigOld wsCfg = (WorkspaceConfigOld) wsList.get(i);
            log("importConfigurationData ws[" + i + "]: " + wsCfg.name);
        }
        
        //Import workspaces
        for (int i = 0; i < wsList.size(); i++) {
            WorkspaceConfigOld wsCfgSrc = (WorkspaceConfigOld) wsList.get(i);
            addWorkspaceImpl(modeNameArray, wmParser, wsCfgSrc);
        }
        log("addConfigurationData LEAVE");
    }
    
    /** Process change (new data added) in old window system configuration data.
     * Called from ModuleChangeHandlerOld when change in module folder is detected. */
    void addWorkspace (String workspaceName) {
        log("addWorkspace ENTER");
        
        WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
        WindowManagerImpl wm = WindowManagerImpl.getInstance();
        
        WorkspaceConfigOld wsCfgSrc = wmParserOld.addWorkspace(workspaceName);
        
        Set modeSet = wm.getModes();
        
        String [] modeNameArray = new String[modeSet.size()];
        int k = 0;
        for (Iterator it = modeSet.iterator(); it.hasNext(); k++) {
            ModeImpl mo = (ModeImpl) it.next();
            modeNameArray[k] = mo.getName();
        }
        
        //System.out.println(dumpConfig(wmcOld));
        
        addWorkspaceImpl(modeNameArray, wmParser, wsCfgSrc);
        log("addWorkspace LEAVE");
    }
    
    /** Process change (new data added) in old window system configuration data.
     * Called from ModuleChangeHandlerOld when change in module folder is detected. */
    void addMode (String workspaceName, String modeName) {
        log("addMode ENTER" + " ws:" + workspaceName + " mo:" + modeName);
        
        WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
        WindowManagerImpl wm = WindowManagerImpl.getInstance();
        
        ModeConfigOld modeCfgSrc = wmParserOld.addMode(workspaceName,modeName);
        
        Set modeSet = wm.getModes();
        
        String [] modeNameArray = new String[modeSet.size()];
        int k = 0;
        for (Iterator it = modeSet.iterator(); it.hasNext(); k++) {
            ModeImpl mo = (ModeImpl) it.next();
            modeNameArray[k] = mo.getName();
        }
        
        //System.out.println(dumpConfig(wmcOld));
        
        //addWorkspaceImpl(modeNameArray, wmParser, wsCfgSrc);
        boolean open = "Editing".equals(workspaceName);
        //Check if mode with the same name is present in new winsys (modes like editor,explorer,...)
        //If found add TopComponent to existing mode otherwise add TopComponent to editor.
        String modeDest = null;
        //Find mode with the same name
        for (int j = 0; j < modeNameArray.length; j++) {
            if (modeCfgSrc.name.equals(modeNameArray[j])) {
                modeDest = modeNameArray[j];
                break;
            }
        }
        //Find editor mode
        if (modeDest == null) {
            for (int j = 0; j < modeNameArray.length; j++) {
                if ("editor".equals(modeNameArray[j])) {
                    modeDest = modeNameArray[j];
                    break;
                }
            }
        }
        //No mode found so far, use first mode if available
        if (modeDest == null) {
            if (modeNameArray.length > 0) {
                modeDest = modeNameArray[0];
            }
        }
        //No mode defined in new winsys.
        //XXX What to do?? Log it and cancel import for now.
        //It would be possible to create some default mode.
        //Current import specification assumes there is at least editor mode.
        if (modeDest == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING,
            "[WinSys.ImportManager.addMode]" // NOI18N
            + " Warning: No mode is defined where imported TopComponent could be placed." // NOI18N
            + " Import is cancelled."); // NOI18N
            return;
        }
        log("addMode modeDest:" + modeDest);

        List tcRefCfgDestList = new ArrayList();

        for (int j = 0; j < modeCfgSrc.tcRefConfigs.length; j++) {
            TCRefConfigOld tcRefCfgSrc = modeCfgSrc.tcRefConfigs[j];
            //Check if given TopComponent is not already imported (from previous workspace
            //or from previous import).
            //If workspace is Editing import it anyway
            if (importedTcIds.containsKey(tcRefCfgSrc.tc_id)) {
                log("addMode TopComponent ID '" // NOI18N
                + tcRefCfgSrc.tc_id + "' is already imported."); // NOI18N
                //If workspace is Editing import it again to get opened state
                //from Editing workspace
                ImportedItem oldItem = (ImportedItem) importedTcIds.get(tcRefCfgSrc.tc_id);
                if ("Editing".equals(workspaceName) && !"Editing".equals(oldItem.workspaceName)) {
                    ImportedItem newItem = new ImportedItem(workspaceName,oldItem.modeName,tcRefCfgSrc.tc_id);
                    importedTcIds.put(tcRefCfgSrc.tc_id,newItem);
                    TCRefConfig tcRefCfgDest = new TCRefConfig();
                    tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
                    tcRefCfgDest.opened = tcRefCfgSrc.opened;
                    log("addMode ADD TCREF FROM EDITING WORKSPACE # tcRef:" + tcRefCfgDest.tc_id);
                    WindowManagerImpl wmi = (WindowManagerImpl) WindowManager.getDefault();
                    wmi.getPersistenceObserver().topComponentRefConfigAdded
                        (oldItem.modeName, tcRefCfgDest, new String[0]);
                }
                continue;
            }
            //Check if given ID is not already used.
            if (PersistenceManager.getDefault().isUsedTCId(tcRefCfgSrc.tc_id)) {
                log("addMode TopComponent ID" // NOI18N
                + " '" + tcRefCfgSrc.tc_id + "'" // NOI18N
                + " is already used in new window system."); // NOI18N
                continue;
            }
            ImportedItem item = new ImportedItem(workspaceName,modeCfgSrc.name,tcRefCfgSrc.tc_id);
            importedTcIds.put(tcRefCfgSrc.tc_id,item);

            log("addMode ADD ITEM [" + workspaceName + ","
            + modeCfgSrc.name + "," + tcRefCfgSrc.tc_id + "]");

            PersistenceManager.getDefault().addUsedTCId(tcRefCfgSrc.tc_id);

            log("addMode importing TopComponent ID '" // NOI18N
            + tcRefCfgSrc.tc_id + "'"); // NOI18N
            //Add new tcRef to destination mode
            TCRefConfig tcRefCfgDest = new TCRefConfig();
            tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
            if (open) {
                tcRefCfgDest.opened = tcRefCfgSrc.opened;
            } else {
                tcRefCfgDest.opened = false;
            }
            if (copySettingsFile(tcRefCfgSrc.tc_id)) {
                tcRefCfgDestList.add(tcRefCfgDest);
            }
            //Add new TCRefParser to pass module info
            WindowManagerParserOld wmParserOld = getWindowManagerParserOld();
            TCRefParserOld tcRefParserOld =
                wmParserOld.findTCRefParser(workspaceName,modeCfgSrc.name,tcRefCfgSrc.tc_id);
            wmParser.addTCRefImport(modeDest,tcRefCfgDest.tc_id,tcRefParserOld.getInternalConfig());

        }
        //Is anything to add?
        if (tcRefCfgDestList.size() > 0) {
            String [] tcRefNameArray = new String[0];
            for (int l = 0; l < tcRefCfgDestList.size(); l++) {
                log("addMode tcRef[" + l + "]: " + (TCRefConfig) tcRefCfgDestList.get(l));
                WindowManagerImpl wmi = (WindowManagerImpl) WindowManager.getDefault();
                wmi.getPersistenceObserver().topComponentRefConfigAdded
                    (modeDest, (TCRefConfig) tcRefCfgDestList.get(l), tcRefNameArray);
            }
        }
        log("addMode LEAVE");
    }
    
    /** Process change (new data added) in old window system configuration data.
     * Called from ModuleChangeHandlerOld when change in module folder is detected. */
    void addTCRef (String workspaceName, String modeName, String tcRefName) {
        log("addTCRef ENTER" + " ws:" + workspaceName + " mo:" + modeName
        + " tcRef:" + tcRefName);
        
        WindowManagerParser wmParser = PersistenceManager.getDefault().getWindowManagerParser();
        WindowManagerImpl wm = WindowManagerImpl.getInstance();
        
        TCRefConfigOld tcRefCfgSrc = wmParserOld.addTCRef(workspaceName,modeName,tcRefName);
        
        Set modeSet = wm.getModes();
        
        String [] modeNameArray = new String[modeSet.size()];
        int k = 0;
        for (Iterator it = modeSet.iterator(); it.hasNext(); k++) {
            ModeImpl mo = (ModeImpl) it.next();
            modeNameArray[k] = mo.getName();
        }
        
        //System.out.println(dumpConfig(wmcOld));
        
        //addWorkspaceImpl(modeNameArray, wmParser, wsCfgSrc);
        boolean open = "Editing".equals(workspaceName);
        //Check if mode with the same name is present in new winsys (modes like editor,explorer,...)
        //If found add TopComponent to existing mode otherwise add TopComponent to editor.
        String modeDest = null;
        //Find mode with the same name
        for (int j = 0; j < modeNameArray.length; j++) {
            if (modeName.equals(modeNameArray[j])) {
                modeDest = modeNameArray[j];
                break;
            }
        }
        //Find editor mode
        if (modeDest == null) {
            for (int j = 0; j < modeNameArray.length; j++) {
                if ("editor".equals(modeNameArray[j])) {
                    modeDest = modeNameArray[j];
                    break;
                }
            }
        }
        //No mode found so far, use first mode if available
        if (modeDest == null) {
            if (modeNameArray.length > 0) {
                modeDest = modeNameArray[0];
            }
        }
        //No mode defined in new winsys.
        //XXX What to do?? Log it and cancel import for now.
        //It would be possible to create some default mode.
        //Current import specification assumes there is at least editor mode.
        if (modeDest == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING,
            "[WinSys.ImportManager.addTCRef]" // NOI18N
            + " Warning: No mode is defined where imported TopComponent could be placed." // NOI18N
            + " Import is cancelled."); // NOI18N
            return;
        }
        log("addTCRef modeDest:" + modeDest);

        List tcRefCfgDestList = new ArrayList();

        //Check if given TopComponent is not already imported (from previous workspace
        //or from previous import).
        if (importedTcIds.containsKey(tcRefCfgSrc.tc_id)) {
            log("addTCRef TopComponent ID '" // NOI18N
            + tcRefCfgSrc.tc_id + "' is already imported."); // NOI18N
            //If workspace is Editing import it again to get opened state
            //from Editing workspace
            ImportedItem oldItem = (ImportedItem) importedTcIds.get(tcRefCfgSrc.tc_id);
            if ("Editing".equals(workspaceName) && !"Editing".equals(oldItem.workspaceName)) {
                ImportedItem newItem = new ImportedItem(workspaceName,oldItem.modeName,tcRefCfgSrc.tc_id);
                importedTcIds.put(tcRefCfgSrc.tc_id,newItem);
                TCRefConfig tcRefCfgDest = new TCRefConfig();
                tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
                tcRefCfgDest.opened = tcRefCfgSrc.opened;
                log("addTCRef ADD TCREF FROM # EDITING WORKSPACE # tcRef:" + tcRefCfgDest.tc_id);
                WindowManagerImpl wmi = (WindowManagerImpl) WindowManager.getDefault();
                wmi.getPersistenceObserver().topComponentRefConfigAdded
                    (oldItem.modeName, tcRefCfgDest, new String[0]);
            }
            return;
        }
        //Check if given ID is not already used.
        if (PersistenceManager.getDefault().isUsedTCId(tcRefCfgSrc.tc_id)) {
            log("addTCRef TopComponent ID" // NOI18N
            + " '" + tcRefCfgSrc.tc_id + "'" // NOI18N
            + " is already used in new window system."); // NOI18N
            return;
        }
        ImportedItem item = new ImportedItem(workspaceName,modeName,tcRefCfgSrc.tc_id);
        importedTcIds.put(tcRefCfgSrc.tc_id,item);

        log("addTCRef ADD ITEM [" + workspaceName + ","
        + modeName + "," + tcRefCfgSrc.tc_id + "]");

        PersistenceManager.getDefault().addUsedTCId(tcRefCfgSrc.tc_id);

        log("addTCRef importing TopComponent ID '" // NOI18N
        + tcRefCfgSrc.tc_id + "'"); // NOI18N
        //Add new tcRef to destination mode
        TCRefConfig tcRefCfgDest = new TCRefConfig();
        tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
        if (open) {
            tcRefCfgDest.opened = tcRefCfgSrc.opened;
        } else {
            tcRefCfgDest.opened = false;
        }
        if (copySettingsFile(tcRefCfgSrc.tc_id)) {
            tcRefCfgDestList.add(tcRefCfgDest);
        }
        //Add new TCRefParser to pass module info
        WindowManagerParserOld wmParserOld = getWindowManagerParserOld();
        TCRefParserOld tcRefParserOld =
            wmParserOld.findTCRefParser(workspaceName,modeName,tcRefCfgSrc.tc_id);
        wmParser.addTCRefImport(modeDest,tcRefCfgDest.tc_id,tcRefParserOld.getInternalConfig());

        //Is anything to add?
        if (tcRefCfgDestList.size() > 0) {
            //tcRefCfgDestList.addAll(0,Arrays.asList(modeCfgDest.tcRefConfigs));
            //modeCfgDest.tcRefConfigs =
            //    (TCRefConfig []) tcRefCfgDestList.toArray(new TCRefConfig[tcRefCfgDestList.size()]);
            String [] tcRefNameArray = new String[0];
            for (int l = 0; l < tcRefCfgDestList.size(); l++) {
                log("addTCRef tcRef[" + l + "]: " + (TCRefConfig) tcRefCfgDestList.get(l));
                WindowManagerImpl wmi = (WindowManagerImpl) WindowManager.getDefault();
                wmi.getPersistenceObserver().topComponentRefConfigAdded
                    (modeDest, (TCRefConfig) tcRefCfgDestList.get(l), tcRefNameArray);
            }
        }
        log("addTCRef LEAVE" + " ws:" + workspaceName + " mo:" + modeName
        + " tcRef:" + tcRefName);
    }
    
    /** Adds TopComponent from one workspace to new window system.
     * Called from ModuleChangeHandlerOld processes change in module folder. */
    private void addWorkspaceImpl
    (String [] modeNameArray, WindowManagerParser wmParser, WorkspaceConfigOld wsCfgSrc) {
        //Opened TopComponent is opened only when it is defined in
        boolean open = "Editing".equals(wsCfgSrc.name);
        log("addWorkspaceImpl ENTER ws:" + wsCfgSrc.name);
        for (int i = 0; i < wsCfgSrc.modes.length; i++) {
            ModeConfigOld modeCfgSrc = wsCfgSrc.modes[i];
            if (modeCfgSrc.tcRefConfigs.length == 0) {
                continue;
            }
            //Check if mode with the same name is present in new winsys (modes like editor,explorer,...)
            //If found add TopComponent to existing mode otherwise add TopComponent to editor.
            log("addWorkspaceImpl -- mo[" + i + "]: " + modeCfgSrc.name);
            String modeDest = null;
            //Find mode with the same name
            for (int j = 0; j < modeNameArray.length; j++) {
                if (modeCfgSrc.name.equals(modeNameArray[j])) {
                    modeDest = modeNameArray[j];
                    break;
                }
            }
            //Find editor mode
            if (modeDest == null) {
                for (int j = 0; j < modeNameArray.length; j++) {
                    if ("editor".equals(modeNameArray[j])) {
                        modeDest = modeNameArray[j];
                        break;
                    }
                }
            }
            //No mode found so far, use first mode if available
            if (modeDest == null) {
                if (modeNameArray.length > 0) {
                    modeDest = modeNameArray[0];
                }
            }
            //No mode defined in new winsys.
            //XXX What to do?? Log it and cancel import for now.
            //It would be possible to create some default mode.
            //Current import specification assumes there is at least editor mode.
            if (modeDest == null) {
                ErrorManager.getDefault().log(ErrorManager.WARNING,
                "[WinSys.ImportManager.addWorkspaceImpl]" // NOI18N
                + " Warning: No mode is defined where imported TopComponent could be placed." // NOI18N
                + " Import is cancelled."); // NOI18N
                return;
            }
            log("addWorkspaceImpl modeDest:" + modeDest);
            
            List tcRefCfgDestList = new ArrayList();
            
            for (int j = 0; j < modeCfgSrc.tcRefConfigs.length; j++) {
                TCRefConfigOld tcRefCfgSrc = modeCfgSrc.tcRefConfigs[j];
                //Check if given TopComponent is not already imported (from previous workspace
                //or from previous import).
                //If workspace is Editing import it anyway
                if (importedTcIds.containsKey(tcRefCfgSrc.tc_id)) {
                    log("addWorkspaceImpl TopComponent ID '" // NOI18N
                    + tcRefCfgSrc.tc_id + "' is already imported."); // NOI18N
                    //If workspace is Editing import it again to get opened state
                    //from Editing workspace
                    ImportedItem oldItem = (ImportedItem) importedTcIds.get(tcRefCfgSrc.tc_id);
                    if ("Editing".equals(wsCfgSrc.name) && !"Editing".equals(oldItem.workspaceName)) {
                        ImportedItem newItem = new ImportedItem(wsCfgSrc.name,oldItem.modeName,tcRefCfgSrc.tc_id);
                        importedTcIds.put(tcRefCfgSrc.tc_id,newItem);
                        TCRefConfig tcRefCfgDest = new TCRefConfig();
                        tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
                        tcRefCfgDest.opened = tcRefCfgSrc.opened;
                        log("addWorkspaceImpl ADD TCREF FROM # EDITING WORKSPACE # tcRef:" + tcRefCfgDest.tc_id);
                        WindowManagerImpl wmi = (WindowManagerImpl) WindowManager.getDefault();
                        wmi.getPersistenceObserver().topComponentRefConfigAdded
                            (oldItem.modeName, tcRefCfgDest, new String[0]);
                    }
                    continue;
                }
                //Check if given ID is not already used.
                if (PersistenceManager.getDefault().isUsedTCId(tcRefCfgSrc.tc_id)) {
                    log("addWorkspaceImpl TopComponent ID" // NOI18N
                    + " '" + tcRefCfgSrc.tc_id + "'" // NOI18N
                    + " is already used in new window system."); // NOI18N
                    continue;
                }
                ImportedItem item = new ImportedItem(wsCfgSrc.name,modeCfgSrc.name,tcRefCfgSrc.tc_id);
                importedTcIds.put(tcRefCfgSrc.tc_id,item);
                
                log("addWorkspaceImpl ADD ITEM [" + wsCfgSrc.name + ","
                + modeCfgSrc.name + "," + tcRefCfgSrc.tc_id + "]");
                
                PersistenceManager.getDefault().addUsedTCId(tcRefCfgSrc.tc_id);
                
                log("addWorkspaceImpl importing TopComponent ID '" // NOI18N
                + tcRefCfgSrc.tc_id + "'"); // NOI18N
                //Add new tcRef to destination mode
                TCRefConfig tcRefCfgDest = new TCRefConfig();
                tcRefCfgDest.tc_id = tcRefCfgSrc.tc_id;
                if (open) {
                    tcRefCfgDest.opened = tcRefCfgSrc.opened;
                } else {
                    tcRefCfgDest.opened = false;
                }
                if (copySettingsFile(tcRefCfgSrc.tc_id)) {
                    tcRefCfgDestList.add(tcRefCfgDest);
                }
                //Add new TCRefParser to pass module info
                WindowManagerParserOld wmParserOld = getWindowManagerParserOld();
                TCRefParserOld tcRefParserOld =
                    wmParserOld.findTCRefParser(wsCfgSrc.name,modeCfgSrc.name,tcRefCfgSrc.tc_id);
                wmParser.addTCRefImport(modeDest,tcRefCfgDest.tc_id,tcRefParserOld.getInternalConfig());
                
            }
            //Is anything to add?
            if (tcRefCfgDestList.size() > 0) {
                String [] tcRefNameArray = new String[0];
                for (int k = 0; k < tcRefCfgDestList.size(); k++) {
                    log("addWorkspaceImpl tcRef[" + k + "]: " + (TCRefConfig) tcRefCfgDestList.get(k));
                    WindowManagerImpl wmi = (WindowManagerImpl) WindowManager.getDefault();
                    wmi.getPersistenceObserver().topComponentRefConfigAdded
                        (modeDest, (TCRefConfig) tcRefCfgDestList.get(k), tcRefNameArray);
                }
            }
        }
    }
    
    /** Copy settings file from old Components folder to new Components folder. 
     * @return true if copy operation was successful */
    private boolean copySettingsFile (String tc_id) {
        FileObject fo = getComponentsFolder().getFileObject(tc_id,"settings"); // NOI18N
        log("copySettingsFile fo:" + fo);
        if (fo == null) {
            //This should not happen. When wstcref file is defined
            //settings file should be defined too.
            return false;
        }
        //Look for file at install layer
        SessionManager sm = SessionManager.getDefault();
        FileSystem installLayer = sm.getLayer(SessionManager.LAYER_INSTALL);
        FileObject compFO = installLayer.findResource(fo.getPath());
        if (compFO == null) {
            //Again this should not happen. When wstcref file is defined in install layer
            //settings file should be defined too in install layer.
            return false;
        }
        log("copySettingsFile compFO:" + compFO);
        FileObject destFolder = PersistenceManager.getDefault().getComponentsLocalFolder();
        log("copySettingsFile destFolder:" + destFolder);
        try {
            compFO.copy(destFolder,tc_id,"settings"); // NOI18N
        } catch (IOException exc) {
            String annotation = NbBundle.getMessage(ImportManager.class,
                "EXC_CopyFails", destFolder);
            ErrorManager.getDefault().annotate(exc, annotation);
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
            return false;
        }
        return true;
    }
    
    void log (String s) {
        Debug.log(ImportManager.class, s);
    }
    
    /** Dump old window manager configuration data to standard output. */
    private static String dumpConfig (WindowManagerConfigOld wmCfg) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("\n-- wmc: [" + Integer.toHexString(System.identityHashCode(wmCfg)) + "]");
        buffer.append("\n-- init: " + wmCfg.initialized);
        
        buffer.append("\n-- workspaces: " + wmCfg.workspaces + " size " + (wmCfg.workspaces == null ? -1 : wmCfg.workspaces.length));
        for (int i = 0; i < wmCfg.workspaces.length; i++) {
            WorkspaceConfigOld wsCfg = wmCfg.workspaces[i];
            buffer.append("\n-- -- workspace[" + i + "]: " + wsCfg.name);
            buffer.append("\n-- --         init: " + wsCfg.initialized);
            buffer.append("\n-- -- modes: " + wsCfg.modes + " size " + (wsCfg.modes == null ? -1 : wsCfg.modes.length));
            for (int j = 0; j < wsCfg.modes.length; j++) {
                ModeConfigOld mc = wsCfg.modes[j];
                buffer.append("\n-- -- --");
                buffer.append("\n-- -- -- mode[" + j + "]: " + mc.name);
                buffer.append("\n-- -- --          init: " + mc.initialized);
                buffer.append("\n-- -- --     frameType: " + mc.frameType);
                buffer.append("\n-- -- -- containerType: " + mc.containerType);
                buffer.append("\n-- -- -- tcRefs: " + mc.tcRefConfigs + " size " + (mc.tcRefConfigs == null ? -1 : mc.tcRefConfigs.length));
                for (int k = 0; k < mc.tcRefConfigs.length; k++) {
                    TCRefConfigOld tcRefCfg = mc.tcRefConfigs[k];
                    buffer.append("\n++ ++ ++ ++ tcRef[" + k + "]: " + tcRefCfg.tc_id);
                    buffer.append("\n++ ++ ++ ++     init: " + tcRefCfg.initialized);
                    buffer.append("\n++ ++ ++ ++   opened: " + tcRefCfg.opened);
                    buffer.append("\n++ ++ ++ ++   constr: " + tcRefCfg.constraint);
                }
            }
        }
        
        return buffer.toString();
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.