blob: a8b08be2a98db772ba249776e3d2dc136ed6ae41 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.editor.settings.storage;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.netbeans.modules.editor.settings.storage.SettingsType.DefaultLocator.MODULE_FILES_FOLDER;
import org.netbeans.modules.editor.settings.storage.SettingsType.LocatorFactory;
import org.netbeans.modules.editor.settings.storage.fontscolors.ColoringStorage;
import org.netbeans.modules.editor.settings.storage.keybindings.KeyMapsStorage;
import org.netbeans.modules.editor.settings.storage.spi.StorageDescription;
import org.openide.filesystems.FileObject;
import org.openide.util.lookup.ServiceProvider;
/**
*
* @author sdedic
*/
@ServiceProvider(service = LocatorFactory.class)
public class EditorLocatorFactory implements LocatorFactory {
private static final Logger LOG = Logger.getLogger(EditorLocatorFactory.class.getName());
@Override
public SettingsType.Locator createLocator(StorageDescription sd) {
if (ColoringStorage.ID.equals(sd.getId())) {
return new FontsColorsLocator(sd.getId(), sd.isUsingProfiles(), sd.getMimeType(), sd.getLegacyFileName());
} else if (KeyMapsStorage.ID.equals(sd.getId())) {
return new LegacyTextBaseLocator(sd.getId(), sd.isUsingProfiles(), sd.getMimeType(), sd.getLegacyFileName());
}
return null;
}
private static final class FontsColorsLocator extends SettingsType.DefaultLocator {
private static final String [] M_LEGACY_FILE_NAMES = new String [] {
MODULE_FILES_FOLDER + "/defaultColoring.xml", // NOI18N
MODULE_FILES_FOLDER + "/coloring.xml", // NOI18N
MODULE_FILES_FOLDER + "/editorColoring.xml", // NOI18N
};
private static final String [] U_LEGACY_FILE_NAMES = new String [] {
"defaultColoring.xml", // NOI18N
"coloring.xml", // NOI18N
"editorColoring.xml", // NOI18N
};
public FontsColorsLocator(String settingTypeId, boolean hasProfiles, String mimeType, String legacyFileName) {
super(settingTypeId, hasProfiles, mimeType, legacyFileName);
}
@Override
protected void addModulesLegacyFiles(
FileObject mimeFolder,
String profileId,
boolean fullScan,
Map<String, List<Object []>> files
) {
addFiles(mimeFolder, profileId, fullScan, M_LEGACY_FILE_NAMES, files, true);
}
@Override
protected void addUsersLegacyFiles(
FileObject mimeFolder,
String profileId,
boolean fullScan,
Map<String, List<Object []>> files
) {
addFiles(mimeFolder, profileId, fullScan, U_LEGACY_FILE_NAMES, files, false);
}
private void addFiles(
FileObject mimeFolder,
String profileId,
boolean fullScan,
String [] filePaths,
Map<String, List<Object []>> files,
boolean moduleFiles
) {
if (profileId == null) {
FileObject [] profileHomes = mimeFolder.getChildren();
for(FileObject f : profileHomes) {
if (!f.isFolder()) {
continue;
}
String id = f.getNameExt();
addFiles(f, filePaths, fullScan, files, id, f, moduleFiles); //NOI18N
}
} else {
FileObject profileHome = mimeFolder.getFileObject(profileId);
if (profileHome != null && profileHome.isFolder()) {
addFiles(profileHome, filePaths, fullScan, files, profileId, profileHome, moduleFiles);
}
}
}
private void addFiles(FileObject folder, String [] filePaths, boolean fullScan, Map<String, List<Object []>> files, String profileId, FileObject profileHome, boolean moduleFiles) {
for(String filePath : filePaths) {
FileObject f = folder.getFileObject(filePath);
if (f != null) {
List<Object []> pair = files.get(profileId);
if (pair == null) {
pair = new ArrayList<>();
files.put(profileId, pair);
}
pair.add(new Object [] { profileHome, f, moduleFiles, null, true });
if (LOG.isLoggable(Level.INFO)) {
Utils.logOnce(LOG, Level.INFO, settingTypeId + " settings " + //NOI18N
"should reside in '" + settingTypeId + "' subfolder, " + //NOI18N
"see #90403 for details. Offending file '" + f.getPath() + "'", null); //NOI18N
}
if (!fullScan) {
break;
}
}
}
}
} // End of FontsColorsLocator class
private static final class LegacyTextBaseLocator extends SettingsType.DefaultLocator {
public LegacyTextBaseLocator(String settingTypeId, boolean hasProfiles, String mimeType, String legacyFileName) {
super(settingTypeId, hasProfiles, mimeType, legacyFileName);
}
@Override
protected FileObject getLegacyMimeFolder(FileObject baseFolder, String mimeType) {
if (mimeType == null || mimeType.length() == 0) {
return baseFolder.getFileObject(Utils.TEXT_BASE_MIME_TYPE);
} else {
return super.getMimeFolder(baseFolder, mimeType);
}
}
} // End of KeybindingsLocator class
}