Search in sources :

Example 6 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method add.

/**
 * Adds new loader when previous and following are specified.
 * An attempt will be made to (re-)order the loader pool according to specified
 * dependencies.
 * <p>If a loader of the same class already existed in the pool, that will be <b>removed</b>
 * and replaced with the new one.
 * @param s adds loader section
 */
public static void add(ManifestSection.LoaderSection s) throws Exception {
    // the instantiation of the loader is done outside of synchronized block,
    // because foreign code is called and can cause deadlocks
    DataLoader l = (DataLoader) s.getInstance();
    doAdd(l, s, getNbLoaderPool());
}
Also used : DataLoader(org.openide.loaders.DataLoader)

Example 7 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class NbLoaderPool method update.

/**
 * Notification that the state of pool has changed
 */
private static synchronized void update(NbLoaderPool lp) {
    ;
    if (err.isLoggable(Level.FINE))
        err.fine("update");
    // clear the cache of loaders
    loadersArray = null;
    if (lp != null && installationFinished) {
        lp.superFireChangeEvent();
    }
    if (lp != null) {
        Enumeration e = lp.allLoaders();
        while (e.hasMoreElements()) {
            DataLoader l = (DataLoader) e.nextElement();
            // so the pool is there only once
            l.removePropertyChangeListener(lp);
            l.addPropertyChangeListener(lp);
        }
    }
}
Also used : DataLoader(org.openide.loaders.DataLoader) Enumeration(java.util.Enumeration)

Example 8 with DataLoader

use of org.openide.loaders.DataLoader in project netbeans-rcp-lite by outersky.

the class GroupsManager method openGroup.

boolean openGroup(DocumentGroupImpl group) {
    DocumentGroupImpl current = getCurrentGroup();
    if (null != current && !current.close()) {
        return false;
    }
    // close documents not in our list
    WindowManagerImpl wmi = WindowManagerImpl.getInstance();
    for (TopComponent tc : TopComponent.getRegistry().getOpened()) {
        if (wmi.isEditorTopComponent(tc)) {
            if (!tc.close()) {
                return false;
            }
        }
    }
    // prune empty modes
    ArrayList<ModeImpl> emptyModes = new ArrayList<>(10);
    for (ModeImpl mode : wmi.getModes()) {
        if (mode.isPermanent() || mode.getKind() != Constants.MODE_KIND_EDITOR)
            continue;
        if (mode.getOpenedTopComponentsIDs().isEmpty()) {
            emptyModes.add(mode);
        }
    }
    for (ModeImpl mode : emptyModes) {
        wmi.removeMode(mode);
    }
    String name = group.getName();
    Preferences prefs = getPreferences().node(name);
    File userDir = Places.getUserDirectory();
    // NOI18N
    File root = new File(new File(userDir, "config"), DOCUMENT_GROUPS);
    File groupDir = new File(root, name);
    FileObject groupFO = FileUtil.toFileObject(groupDir);
    if (null != groupFO) {
        // load mode configs
        Map<ModeImpl, ModeConfig> mode2config = new HashMap<>(10);
        Map<String, ModeImpl> tcid2mode = new HashMap<>(50);
        for (FileObject fo : groupFO.getChildren()) {
            if (fo.isData() && WSMODE.equals(fo.getExt())) {
                try {
                    ModeConfig config = WindowManagerParser.loadModeConfigFrom(fo);
                    // NOI18N
                    String idList = prefs.get(config.name, "");
                    if (idList.isEmpty())
                        // ignore empty modes
                        continue;
                    ModeImpl mode = (ModeImpl) wmi.findMode(config.name);
                    if (null == mode) {
                        mode = createMode(config);
                    } else {
                        mode.setConstraints(config.constraints);
                    }
                    mode2config.put(mode, config);
                    String[] ids = idList.split(ID_SEPARATOR);
                    for (String id : ids) {
                        tcid2mode.put(id, mode);
                        mode.addUnloadedTopComponent(id);
                    }
                } catch (IOException ex) {
                    LOG.log(Level.INFO, null, ex);
                }
            }
        }
        DataLoader settingsLoader = null;
        Enumeration<DataLoader> loaders = DataLoaderPool.getDefault().producersOf(InstanceDataObject.class);
        while (loaders.hasMoreElements()) {
            settingsLoader = loaders.nextElement();
        }
        // restore TCs
        Map<String, TopComponent> id2tc = new HashMap<>(50);
        for (FileObject fo : groupFO.getChildren()) {
            if (fo.isData() && SETTINGS.equals(fo.getExt())) {
                DataObject dob;
                try {
                    // for some reason the DataLoader infrastructure insists that the first FileObject is an XMLDataObject
                    // using preferred loader ensures we always get the expected InstanceDataObject with proper instance cookies
                    DataLoaderPool.setPreferredLoader(fo, settingsLoader);
                    dob = DataObject.find(fo);
                    DataLoaderPool.setPreferredLoader(fo, null);
                    InstanceCookie ic = dob.getCookie(InstanceCookie.class);
                    if (null != ic) {
                        TopComponent tc = (TopComponent) ic.instanceCreate();
                        id2tc.put(fo.getName(), tc);
                    } else {
                        // no instance cookie, which means that module which owned top
                        // component is gone or versions of data and module are incompatible
                        String excAnnotation = NbBundle.getMessage(// NOI18N
                        PersistenceManager.class, // NOI18N
                        "EXC_BrokenTCSetting", fo.getName());
                        // resultExc = new SafeException(new IOException(excAnnotation));
                        LOG.log(Level.INFO, // NOI18N
                        "[PersistenceManager.getTopComponentForID]" + " Problem when deserializing TopComponent for tcID:'" + fo.getName() + // NOI18N //NOI18N
                        "'. Reason: " + excAnnotation);
                    }
                } catch (Exception ex) {
                    LOG.log(Level.INFO, null, ex);
                }
            }
        }
        Map<String, String> oldId2newId = new HashMap<>(id2tc.size());
        for (String oldId : id2tc.keySet()) {
            TopComponent tc = id2tc.get(oldId);
            ModeImpl mode = tcid2mode.get(oldId);
            if (null != mode) {
                mode.dockInto(tc);
            }
            tc.open();
            oldId2newId.put(oldId, wmi.findTopComponentID(tc));
        }
        // restore selection in all modes
        for (ModeImpl mode : wmi.getModes()) {
            ModeConfig config = mode2config.get(mode);
            if (null == config)
                continue;
            String selectedId = config.selectedTopComponentID;
            if (null != selectedId)
                selectedId = oldId2newId.get(selectedId);
            if (null != selectedId) {
                if (mode.getOpenedTopComponentsIDs().contains(selectedId)) {
                    TopComponent tc = wmi.findTopComponent(selectedId);
                    if (null != tc)
                        mode.setSelectedTopComponent(tc);
                }
            }
        }
    }
    // TODO restore recent view list
    getPreferences().put(SEL_GROUP, group.getName());
    return true;
}
Also used : WindowManagerImpl(org.netbeans.core.windows.WindowManagerImpl) HashMap(java.util.HashMap) ModeImpl(org.netbeans.core.windows.ModeImpl) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BackingStoreException(java.util.prefs.BackingStoreException) IOException(java.io.IOException) DataLoader(org.openide.loaders.DataLoader) InstanceDataObject(org.openide.loaders.InstanceDataObject) DataObject(org.openide.loaders.DataObject) ModeConfig(org.netbeans.core.windows.persistence.ModeConfig) InstanceCookie(org.openide.cookies.InstanceCookie) FileObject(org.openide.filesystems.FileObject) Preferences(java.util.prefs.Preferences) NbPreferences(org.openide.util.NbPreferences) File(java.io.File) TopComponent(org.openide.windows.TopComponent)

Aggregations

DataLoader (org.openide.loaders.DataLoader)8 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Iterator (java.util.Iterator)3 FileObject (org.openide.filesystems.FileObject)3 Enumeration (java.util.Enumeration)2 HashSet (java.util.HashSet)2 ModuleInfo (org.openide.modules.ModuleInfo)2 SpecificationVersion (org.openide.modules.SpecificationVersion)2 NbMarshalledObject (org.openide.util.io.NbMarshalledObject)2 File (java.io.File)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 BackingStoreException (java.util.prefs.BackingStoreException)1 Preferences (java.util.prefs.Preferences)1 ModeImpl (org.netbeans.core.windows.ModeImpl)1 WindowManagerImpl (org.netbeans.core.windows.WindowManagerImpl)1 ModeConfig (org.netbeans.core.windows.persistence.ModeConfig)1