/** * */ package com.onaro.sanscreen.client.prefs; import com.onaro.client.swing.IdleDetector; import com.onaro.commons.beans.IPropertyChangeSupport; import com.onaro.commons.prefs.PreferencesWrapper; import com.onaro.sanscreen.client.ejb.SessionsManager; import com.onaro.sanscreen.client.ejb.logging.UsageLog; import com.onaro.sanscreen.client.ejb.logging.UsageLog.MessageBuilder; import com.onaro.sanscreen.client.ejb.logging.UsageLogType; import com.onaro.sanscreen.client.view.topology.TopologyWatermark; import com.onaro.sanscreen.server.interfaces.data.ServerInterfaceException; import com.onaro.sanscreen.server.sessions.ParamSession; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; public final class GeneralPreferenceManager implements IPropertyChangeSupport { public static final String PROP_PROGRESS_DIALOG_ENABLED = "PROP_PROGRESS_DIALOG_ENABLED"; //$NON-NLS-1$ public static final String PROP_CONFIRM_EXIT_ENABLED = "PROP_CONFIRM_EXIT_ENABLED"; //$NON-NLS-1$ public static final String PROP_RESTORE_TABS_ENABLED = "PROP_RESTORE_TABS_ENABLED"; //$NON-NLS-1$ public static final String PROP_LIMIT_TABS_ENABLED = "PROP_LIMIT_TABS_ENABLED"; //$NON-NLS-1$ public static final String PROP_LIMIT_TABS_QUANTITY = "PROP_LIMIT_TABS_QUANTITY"; //$NON-NLS-1$ public static final String PROP_LEGACY_MODE_ENABLED = "PROP_LEGACY_MODE_ENABLED"; //$NON-NLS-1$ public static final String PROP_TRUNCATE_TOPOLOGY_LABELS_ENABLED = "PROP_TRUNCATE_TOPOLOGY_LABELS_ENABLED"; //$NON-NLS-1$ public static final String PROP_PERCENT_BARS_ENABLED = "PROP_PERCENT_BARS_ENABLED"; //$NON-NLS-1$ public static final String PROP_SHOW_PENDING_VIOLATIONS = "PROP_SHOW_PENDING_VIOLATIONS";//$NON-NLS-1$ /* Preferences that are stored on the server. These should match what is created in createdb.sql * on the server side since these are persisted on the server */ public static final String SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED = "TOPOLOGY_WATERMARK_ENABLED"; //$NON-NLS-1$ public static final String SERVER_PREF_TOPOLOGY_WATERMARK_TEXT = "TOPOLOGY_WATERMARK_TEXT"; //$NON-NLS-1$ public static final String SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED = "IDLE_SESSION_TERMINATION_ENABLED"; //$NON-NLS-1$ public static final String SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES = "IDLE_SESSION_TERMINATION_MINUTES"; //$NON-NLS-1$ private static final String PREF_PROGRESS_DIALOG_KEY = "progress.dialog.popup"; //$NON-NLS-1$ private static final String PREF_CONFIRM_EXIT_KEY = "confirm.exit"; //$NON-NLS-1$ private static final String PREF_RESTORE_TABS_KEY = "restore.view.tabs"; //$NON-NLS-1$ private static final String PREF_LIMIT_TABS_KEY = "limit.open.tabs"; //$NON-NLS-1$ private static final String PREF_TAB_COUNT_LIMIT_KEY = "tab.count.limit"; //$NON-NLS-1$ private static final String PREF_LEGACY_MODE_KEY = "legacy_mode"; //$NON-NLS-1$ private static final String PREF_TRUNCATE_TOPOLOGY_LABELS_KEY = "truncate.topology.labels"; //$NON-NLS-1$ private static final String PREF_PERCENT_BARS_KEY = "percent.bars"; //$NON-NLS-1$ private static final String PREF_SHOW_PENDING_VIOLATIONS_KEY = "show.pending.violations";//$NON-NLS-1$ /* Use for UsageLog. Values are resource keys */ private static final String SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED_KEY = "truncate.watermark.enabled"; //$NON-NLS-1$ private static final String SERVER_PREF_TOPOLOGY_WATERMARK_TEXT_KEY = "truncate.watermark.text"; //$NON-NLS-1$ private static final String SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED_KEY = "idle.session.termination.enabled"; //$NON-NLS-1$ private static final String SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES_KEY = "idle.session.termination.minutes"; //$NON-NLS-1$ static final Logger logger = LogManager.getLogger(GeneralPreferenceManager.class); // Initialize preference values with their defaults private boolean progressDialogEnabled = true; private boolean confirmExitEnabled = false; private boolean restoreTabsEnabled = true; private boolean limitTabsEnabled = false; private Integer limitTabsQuantity = Integer.valueOf(5); private boolean legacyModeEnabled = false; private boolean truncateTopologyLabelsEnabled = false; private boolean percentBarsEnabled = true; /** Holds TRUE is the check-box has been selected, otherwise false */ private boolean isTopologyWatermarkEnabled = false; /** Holds the Text that is going to show up as watermark */ private String topologyWatermarkText; private boolean isIdleSessionTerminationEnabled = true; private int idleSessionTerminationMinutes = 120; private boolean isShowPendingViolationsSelected = false; private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); private static final GeneralPreferenceManager INSTANCE = new GeneralPreferenceManager(); public static GeneralPreferenceManager getInstance() { return INSTANCE; } private GeneralPreferenceManager() { registerListeners(); readPreferences(); } private void registerListeners() { addPropertyChangeListener(PROP_PROGRESS_DIALOG_ENABLED, new PropertyChangeListener() { // TODO: Determine a non-deprecated substitute for setAutoPopupProgressDialog(). @SuppressWarnings("deprecation") public void propertyChange(PropertyChangeEvent evt) { com.onaro.client.leekui.ui.compatibility.CompatibilityUtils.setAutoPopupProgressDialog(isProgressDialogEnabled()); } }); } public boolean isProgressDialogEnabled() { return progressDialogEnabled; } void setProgressDialogEnabled(boolean progressDialogEnabled) { boolean oldValue = this.progressDialogEnabled; this.progressDialogEnabled = progressDialogEnabled; firePropertyChange(PROP_PROGRESS_DIALOG_ENABLED, oldValue, progressDialogEnabled); } public boolean isConfirmExitEnabled() { return confirmExitEnabled; } public void setConfirmExitEnabled(boolean confirmExitEnabled, boolean saveImmediately) { boolean oldValue = this.confirmExitEnabled; this.confirmExitEnabled = confirmExitEnabled; firePropertyChange(PROP_CONFIRM_EXIT_ENABLED, oldValue, confirmExitEnabled); if(saveImmediately) { writePreferences(); } } public boolean isRestoreTabsEnabled() { return restoreTabsEnabled; } void setRestoreTabsEnabled(boolean restoreTabsEnabled) { boolean oldValue = this.restoreTabsEnabled; this.restoreTabsEnabled = restoreTabsEnabled; firePropertyChange(PROP_RESTORE_TABS_ENABLED, oldValue, restoreTabsEnabled); } public boolean isLimitTabsEnabled() { return limitTabsEnabled; } void setLimitTabsEnabled(boolean limitTabsEnabled) { boolean oldValue = this.limitTabsEnabled; this.limitTabsEnabled = limitTabsEnabled; firePropertyChange(PROP_LIMIT_TABS_ENABLED, oldValue, limitTabsEnabled); } public Integer getLimitTabsQuantity() { return limitTabsQuantity; } void setLimitTabsQuantity(Integer limitTabsQuantity) { Integer oldValue = this.limitTabsQuantity; this.limitTabsQuantity = limitTabsQuantity; firePropertyChange(PROP_LIMIT_TABS_QUANTITY, oldValue, limitTabsQuantity); } public boolean isLegacyModeEnabled() { return legacyModeEnabled; } void setLegacyModeEnabled(boolean legacyModeEnabled) { boolean oldValue = this.legacyModeEnabled; this.legacyModeEnabled = legacyModeEnabled; firePropertyChange(PROP_LEGACY_MODE_ENABLED, oldValue, legacyModeEnabled); } public void setShowPendingViolationsSelected(boolean isSelected){ boolean oldValue = this.isShowPendingViolationsSelected; this.isShowPendingViolationsSelected = isSelected; firePropertyChange(PROP_SHOW_PENDING_VIOLATIONS, oldValue, isShowPendingViolationsSelected); } public boolean isTruncateTopologyLabelsEnabled() { return truncateTopologyLabelsEnabled; } public boolean isPercentBarsEnabled() { return percentBarsEnabled; } public boolean isTopologyWatermarkEnabled() { return isTopologyWatermarkEnabled; } public String getTopologyWatermarkText() { return topologyWatermarkText; } public boolean isShowPendingViolationsSelected(){ return isShowPendingViolationsSelected; } public void setTruncateTopologyLabelsEnabled(boolean truncateTopologyLabelsEnabled) { boolean oldValue = this.truncateTopologyLabelsEnabled; this.truncateTopologyLabelsEnabled = truncateTopologyLabelsEnabled; firePropertyChange(PROP_TRUNCATE_TOPOLOGY_LABELS_ENABLED, oldValue, truncateTopologyLabelsEnabled); } void setPercentBarsEnabled(boolean percentBarsEnabled) { boolean oldValue = this.percentBarsEnabled; this.percentBarsEnabled = percentBarsEnabled; firePropertyChange(PROP_PERCENT_BARS_ENABLED, oldValue, percentBarsEnabled); } /** * Fires property change of {@link GeneralPreferenceManager#SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED} * * @see {@link TopologyWatermark} as an example of listeners reacting to this property change. * * @param topologyWatermarkEnabled flag that indicates if watermark is enabled. */ void setTopologyWatermarkEnabled(final boolean topologyWatermarkEnabled) { boolean oldValue = this.isTopologyWatermarkEnabled; this.isTopologyWatermarkEnabled = topologyWatermarkEnabled; firePropertyChange(SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED, oldValue, topologyWatermarkEnabled); } /** * Fires property change of {@link GeneralPreferenceManager#SERVER_PREF_TOPOLOGY_WATERMARK_TEXT} * * @see {@link TopologyWatermark} as an example of listeners reacting to this property change. * * @param topologyWatermarkText watermark's text */ void setTopologyWatermarkText(final String topologyWatermarkText) { // final String oldValue = this.topologyWatermarkText; this.topologyWatermarkText = topologyWatermarkText; firePropertyChange(SERVER_PREF_TOPOLOGY_WATERMARK_TEXT, null, topologyWatermarkText); } public boolean isIdleSessionTerminationEnabled() { return isIdleSessionTerminationEnabled; } public int getIdleSessionTerminationMinutes() { return idleSessionTerminationMinutes; } /** * Fires property change of {@link GeneralPreferenceManager#SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED} * * @param idleSessionTerminationEnabled flag that indicates if idle session termination is enabled. */ void setIdleSessionTerminationEnabled(final boolean idleSessionTerminationEnabled) { if (IdleDetector.isSupported()) { boolean oldValue = this.isIdleSessionTerminationEnabled; this.isIdleSessionTerminationEnabled = idleSessionTerminationEnabled; firePropertyChange(SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED, oldValue, idleSessionTerminationEnabled); } } /** * Fires property change of {@link GeneralPreferenceManager#SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES} * * @param idleSessionTerminationMinutes the idle session minutes value */ void setIdleSessionTerminationMinutes(final int idleSessionTerminationMinutes) { if (IdleDetector.isSupported()) { int oldValue = this.idleSessionTerminationMinutes; this.idleSessionTerminationMinutes = idleSessionTerminationMinutes; firePropertyChange(SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES, oldValue, idleSessionTerminationMinutes); } } void readPreferences() { PreferencesWrapper prefs = ClientPrefs.getClient(); setProgressDialogEnabled(prefs.getBoolean(PREF_PROGRESS_DIALOG_KEY, progressDialogEnabled)); setConfirmExitEnabled(prefs.getBoolean(PREF_CONFIRM_EXIT_KEY, confirmExitEnabled), false); setRestoreTabsEnabled(prefs.getBoolean(PREF_RESTORE_TABS_KEY, restoreTabsEnabled)); setLimitTabsEnabled(prefs.getBoolean(PREF_LIMIT_TABS_KEY, limitTabsEnabled)); int tabCount = prefs.getInt(PREF_TAB_COUNT_LIMIT_KEY, limitTabsQuantity.intValue()); setLimitTabsQuantity(Integer.valueOf(tabCount)); setLegacyModeEnabled(prefs.getBoolean(PREF_LEGACY_MODE_KEY, legacyModeEnabled)); setTruncateTopologyLabelsEnabled(prefs.getBoolean(PREF_TRUNCATE_TOPOLOGY_LABELS_KEY, truncateTopologyLabelsEnabled)); setPercentBarsEnabled(prefs.getBoolean(PREF_PERCENT_BARS_KEY, percentBarsEnabled)); setShowPendingViolationsSelected(prefs.getBoolean(PREF_SHOW_PENDING_VIOLATIONS_KEY, isShowPendingViolationsSelected)); /* Preferences on server */ ParamSession paramSession = SessionsManager.getInstance().getSession(ParamSession.class); setTopologyWatermarkEnabled(paramSession.get(SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED, isTopologyWatermarkEnabled())); setTopologyWatermarkText(paramSession.get(SERVER_PREF_TOPOLOGY_WATERMARK_TEXT, getTopologyWatermarkText())); setIdleSessionTerminationEnabled(paramSession.get(SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED, isIdleSessionTerminationEnabled())); setIdleSessionTerminationMinutes(paramSession.get(SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES, getIdleSessionTerminationMinutes())); } @SuppressWarnings("boxing") void writePreferences() { try { /* Save server preferences */ ParamSession paramSession = SessionsManager.getInstance().getSession(ParamSession.class); paramSession.set(SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED, isTopologyWatermarkEnabled()); paramSession.set(SERVER_PREF_TOPOLOGY_WATERMARK_TEXT, getTopologyWatermarkText()); // Don't save idle session parameters from unsupported platform. Don't want to affect supported platforms. if (IdleDetector.isSupported()) { paramSession.set(SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED, isIdleSessionTerminationEnabled()); paramSession.set(SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES, getIdleSessionTerminationMinutes()); } } catch(ServerInterfaceException e) { logger.error("Failed to update server parameters", e); //$NON-NLS-1$ } PreferencesWrapper prefs = ClientPrefs.getClient(); prefs.putBoolean(PREF_PROGRESS_DIALOG_KEY, isProgressDialogEnabled()); prefs.putBoolean(PREF_CONFIRM_EXIT_KEY, isConfirmExitEnabled()); prefs.putBoolean(PREF_RESTORE_TABS_KEY, isRestoreTabsEnabled()); prefs.putBoolean(PREF_LIMIT_TABS_KEY, isLimitTabsEnabled()); prefs.putInt(PREF_TAB_COUNT_LIMIT_KEY, getLimitTabsQuantity().intValue()); prefs.putBoolean(PREF_LEGACY_MODE_KEY, isLegacyModeEnabled()); prefs.putBoolean(PREF_TRUNCATE_TOPOLOGY_LABELS_KEY, isTruncateTopologyLabelsEnabled()); prefs.putBoolean(PREF_PERCENT_BARS_KEY, isPercentBarsEnabled()); prefs.putBoolean(PREF_SHOW_PENDING_VIOLATIONS_KEY, isShowPendingViolationsSelected()); if (UsageLog.isEnabled()) { MessageBuilder builder = UsageLog.builder(UsageLogType.GENERAL_PREFERENCES_CHANGE); builder.append(PREF_PROGRESS_DIALOG_KEY, isProgressDialogEnabled()); builder.append(PREF_CONFIRM_EXIT_KEY, isConfirmExitEnabled()); builder.append(PREF_RESTORE_TABS_KEY, isRestoreTabsEnabled()); builder.append(PREF_LIMIT_TABS_KEY, isLimitTabsEnabled()); builder.append(PREF_TAB_COUNT_LIMIT_KEY, getLimitTabsQuantity().intValue()); builder.append(PREF_LEGACY_MODE_KEY, isLegacyModeEnabled()); builder.append(PREF_TRUNCATE_TOPOLOGY_LABELS_KEY, isTruncateTopologyLabelsEnabled()); builder.append(PREF_PERCENT_BARS_KEY, isPercentBarsEnabled()); builder.append(SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED_KEY, isTopologyWatermarkEnabled()); builder.append(SERVER_PREF_TOPOLOGY_WATERMARK_TEXT_KEY, getTopologyWatermarkText()); builder.append(SERVER_PREF_IDLE_SESSION_TERMINATION_ENABLED_KEY, isIdleSessionTerminationEnabled()); builder.append(SERVER_PREF_IDLE_SESSION_TERMINATION_MINUTES_KEY, getIdleSessionTerminationMinutes()); builder.log(); } } private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); } private void firePropertyChange(String propertyName, int oldValue, int newValue) { propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); } private void firePropertyChange(String propertyName, Object oldValue, Object newValue) { propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); } public void addPropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(listener); } public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(propertyName, listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(listener); } public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(propertyName, listener); } }