org.apache.cayenne.pref.CayennePreferenceEditor Maven / Gradle / Ivy
/*****************************************************************
* 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
*
* https://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.apache.cayenne.pref;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import org.apache.cayenne.modeler.Application;
import org.apache.cayenne.modeler.pref.DBConnectionInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An editor for modifying CayennePreferenceService.
*
*/
public abstract class CayennePreferenceEditor implements PreferenceEditor {
protected boolean restartRequired;
protected CayenneProjectPreferences cayenneProjectPreferences;
private Map> changedPreferences;
private Map> removedPreferences;
private Map> changedBooleanPreferences;
private List removedNode;
private List addedNode;
private static Logger logger = LoggerFactory.getLogger(CayennePreferenceEditor.class);
public CayennePreferenceEditor(CayenneProjectPreferences cayenneProjectPreferences) {
this.cayenneProjectPreferences = cayenneProjectPreferences;
this.changedPreferences = new HashMap<>();
this.removedPreferences = new HashMap<>();
this.changedBooleanPreferences = new HashMap<>();
this.removedNode = new ArrayList<>();
this.addedNode = new ArrayList<>();
}
public List getAddedNode() {
return addedNode;
}
public List getRemovedNode() {
return removedNode;
}
public Map> getRemovedPreferences() {
return removedPreferences;
}
public Map> getChangedPreferences() {
return changedPreferences;
}
public Map> getChangedBooleanPreferences() {
return changedBooleanPreferences;
}
protected boolean isRestartRequired() {
return restartRequired;
}
protected void setRestartRequired(boolean restartOnSave) {
this.restartRequired = restartOnSave;
}
public void save() {
cayenneProjectPreferences.getDetailObject(DBConnectionInfo.class).save();
if (restartRequired) {
restart();
}
// update boolean preferences
for (Map.Entry> entry : changedBooleanPreferences.entrySet()) {
Preferences pref = entry.getKey();
for (Map.Entry en : entry.getValue().entrySet()) {
pref.putBoolean(en.getKey(), en.getValue());
}
}
// update string preferences
for (Map.Entry> entry : changedPreferences.entrySet()) {
Preferences pref = entry.getKey();
for (Map.Entry en : entry.getValue().entrySet()) {
pref.put(en.getKey(), en.getValue());
}
}
// remove string preferences
for (Map.Entry> entry : removedPreferences.entrySet()) {
Preferences pref = entry.getKey();
for (Map.Entry en : entry.getValue().entrySet()) {
pref.remove(en.getKey());
}
}
// remove preferences node
for (Preferences pref : removedNode) {
try {
pref.removeNode();
} catch (BackingStoreException e) {
logger.warn("Error removing preferences");
}
}
Application.getInstance().initClassLoader();
}
public void revert() {
// remove added preferences node
for (Preferences pref : addedNode) {
try {
pref.removeNode();
} catch (BackingStoreException ignored) {
}
}
cayenneProjectPreferences.getDetailObject(DBConnectionInfo.class).cancel();
restartRequired = false;
}
protected abstract void restart();
}