Search in sources :

Example 1 with InstanceDataObject

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

the class PersistenceManager method propertyChange.

/**
 * Listens to property changes in InstanceDataObject. Used to clean top component
 * cache when module owning given top component is disabled.
 */
public void propertyChange(PropertyChangeEvent evt) {
    if (DataObject.PROP_COOKIE.equals(evt.getPropertyName())) {
        Object obj = evt.getSource();
        removeTopComponentForDataObject((DataObject) obj);
    }
}
Also used : InstanceDataObject(org.openide.loaders.InstanceDataObject) FileObject(org.openide.filesystems.FileObject) DataObject(org.openide.loaders.DataObject)

Example 2 with InstanceDataObject

use of org.openide.loaders.InstanceDataObject 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

FileObject (org.openide.filesystems.FileObject)2 DataObject (org.openide.loaders.DataObject)2 InstanceDataObject (org.openide.loaders.InstanceDataObject)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)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 InstanceCookie (org.openide.cookies.InstanceCookie)1 DataLoader (org.openide.loaders.DataLoader)1 NbPreferences (org.openide.util.NbPreferences)1 TopComponent (org.openide.windows.TopComponent)1