| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.library.ui.preferences; |
| |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.epf.common.utils.FileUtil; |
| import org.eclipse.epf.common.utils.StrUtil; |
| import org.eclipse.epf.library.LibraryService; |
| import org.eclipse.epf.library.ui.LibraryUIPlugin; |
| import org.eclipse.epf.persistence.MultiFileSaveUtil; |
| import org.eclipse.epf.uma.MethodLibrary; |
| import org.eclipse.jface.dialogs.MessageDialogWithToggle; |
| import org.eclipse.jface.preference.IPreferenceStore; |
| |
| import com.ibm.icu.util.StringTokenizer; |
| |
| /** |
| * Manages the Library preferences. |
| * |
| * @author Kelvin Low |
| * @since 1.0 |
| * |
| * fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=154638 |
| */ |
| public class LibraryUIPreferences { |
| |
| /** |
| * The Library UI preference keys. |
| */ |
| public static final String PROMPT_FOR_LIBRARY_AT_STARTUP = "promptForLibraryAtStartup"; //$NON-NLS-1$ |
| |
| public static final String SAVED_LIBRARY_PATH = "savedLibraryPath"; //$NON-NLS-1$ |
| |
| public static final String PUBLISH_UNOPEN_ACTIVITY_DD = "publishUnopenActivityDetailDiagram"; //$NON-NLS-1$ |
| |
| public static final String PREF_SWITCH_CONFIG = "switchConfigurationOnProcessActivate"; //$NON-NLS-1$ |
| |
| public static final String PUBLISH_AD_FOR_ACTIVITY_EXTENSION = "publishActivityDiagramforActivityExtension"; //$NON-NLS-1$ |
| |
| public static final String APPLICATION_SHORT_NAME = "appname"; //$NON-NLS-1$ |
| |
| private static final String RECENTLY_OPENED_LIBRARIES = "recentlyOpenedLibraries"; //$NON-NLS-1$ |
| |
| // The recently opened libraries preference delimiter. |
| private static final String PREFERENCE_DELIMITER = ";"; //$NON-NLS-1$ |
| |
| static { |
| // Initialize the default preference values. |
| IPreferenceStore store = LibraryUIPlugin.getDefault() |
| .getPreferenceStore(); |
| store.setDefault(PROMPT_FOR_LIBRARY_AT_STARTUP, true); |
| store.setDefault(SAVED_LIBRARY_PATH, ""); //$NON-NLS-1$ |
| store.setDefault(PREF_SWITCH_CONFIG, MessageDialogWithToggle.PROMPT); |
| store.setDefault(RECENTLY_OPENED_LIBRARIES, ""); //$NON-NLS-1$ |
| } |
| |
| /** |
| * Returns the prompt for Method Library at startup preference. |
| * |
| * @return <code>true</code> is the preference is set. |
| */ |
| public static boolean getPromptForMethodLibraryAtStartup() { |
| return LibraryUIPlugin.getDefault().getPreferenceStore().getBoolean( |
| PROMPT_FOR_LIBRARY_AT_STARTUP); |
| } |
| |
| /** |
| * Sets the prompt for Method Library at startup preference. |
| * |
| * @param enabled |
| * |
| */ |
| public static void setPromptForMethodLibraryAtStartup(boolean enabled) { |
| LibraryUIPlugin.getDefault().getPreferenceStore().setValue( |
| PROMPT_FOR_LIBRARY_AT_STARTUP, enabled); |
| } |
| |
| public static void setPublishUnopenActivitydd(boolean enabled) { |
| LibraryUIPlugin.getDefault().getPreferenceStore().setValue( |
| PUBLISH_UNOPEN_ACTIVITY_DD, enabled); |
| } |
| |
| public static boolean getPublishUnopenActivitydd() { |
| return LibraryUIPlugin.getDefault().getPreferenceStore().getBoolean( |
| PUBLISH_UNOPEN_ACTIVITY_DD); |
| } |
| |
| /** |
| * Setter method for Publish Activity Diagram for Activity Extension, |
| * |
| * @param enabled |
| */ |
| public static void setPublishADForActivityExtension(boolean enabled) { |
| LibraryUIPlugin.getDefault().getPreferenceStore().setValue( |
| PUBLISH_AD_FOR_ACTIVITY_EXTENSION, enabled); |
| } |
| |
| /** |
| * getter method for Publish Activity Diagram for Activity Extension flag |
| * from preference store, |
| */ |
| public static boolean getPublishADForActivityExtension() { |
| return LibraryUIPlugin.getDefault().getPreferenceStore().getBoolean( |
| PUBLISH_AD_FOR_ACTIVITY_EXTENSION); |
| } |
| |
| /** |
| * Returns switch configuration value store in preference store |
| * |
| * @return value - could be ALWAYS, NEVER, PROMPT |
| */ |
| public static String getSwitchConfig() { |
| return LibraryUIPlugin.getDefault().getPreferenceStore().getString( |
| PREF_SWITCH_CONFIG); |
| } |
| |
| /** |
| * Saves switch configuration value in preference store |
| * |
| * @param value |
| * Value could be ALWAYS, NEVER, PROMPT |
| */ |
| public static void setSwitchConfig(String value) { |
| LibraryUIPlugin.getDefault().getPreferenceStore().setValue( |
| PREF_SWITCH_CONFIG, value); |
| } |
| |
| /** |
| * Returns the Method Library path that was saved in a previous session. |
| * |
| * @return The saved library path. |
| */ |
| public static String getSavedLibraryPath() { |
| return LibraryUIPlugin.getDefault().getPreferenceStore().getString( |
| SAVED_LIBRARY_PATH); |
| } |
| |
| /** |
| * Saves a Method Library path to the Library UI preference store. |
| * |
| * @param libPath |
| * Path to a Method Library. |
| */ |
| public static void setSavedLibraryPath(String libPath) { |
| String path = libPath; |
| if (path.endsWith(MultiFileSaveUtil.DEFAULT_LIBRARY_MODEL_FILENAME)) { |
| path = FileUtil.getParentDirectory(path); |
| } |
| LibraryUIPlugin.getDefault().getPreferenceStore().setValue( |
| SAVED_LIBRARY_PATH, path); |
| LibraryUIPlugin.getDefault().savePluginPreferences(); |
| } |
| |
| /** |
| * Returns the default Method Library path. |
| * |
| * @return The default Method Library path. |
| */ |
| public static String getDefaultLibraryPath() { |
| String userHome = System.getProperty("user.home").replace('\\', '/'); //$NON-NLS-1$ |
| String libraryPath = LibraryUIPlugin.getDefault().getString( |
| "libraryPath"); //$NON-NLS-1$ |
| if (libraryPath == null || libraryPath.length() == 0 |
| || libraryPath.startsWith("[")) { //$NON-NLS-1$ |
| libraryPath = userHome + "/Method Libraries/Library"; //$NON-NLS-1$ |
| } else if (libraryPath.startsWith("<user.home>")) { //$NON-NLS-1$ |
| libraryPath = userHome + libraryPath.substring(11); |
| } |
| if (System.getProperty("file.separator").equals("\\")) { //$NON-NLS-1$ //$NON-NLS-2$ |
| libraryPath = libraryPath.replace('/', '\\'); |
| } |
| |
| // extract the 3rd segment of the application name as the default |
| // segment in the default lib path |
| // int idx = -1; |
| // if ((idx=libraryPath.indexOf("<app.name>")) >= 0) { //$NON-NLS-1$ |
| // String appNameProper = "composer"; //$NON-NLS-1$ |
| // String appName = Platform.getProduct().getApplication(); |
| // StringTokenizer st = new StringTokenizer(appName, "."); //$NON-NLS-1$ |
| // int i=0; |
| // while(st.hasMoreTokens()) { |
| // i++; |
| // if (i != 3) { |
| // st.nextToken(); |
| // } else { |
| // appNameProper = st.nextToken(); |
| // break; |
| // } |
| // } |
| // libraryPath = libraryPath.substring(0, idx) + appNameProper + |
| // libraryPath.substring(idx+10); |
| // } |
| |
| int idx = -1; |
| if ((idx = libraryPath.indexOf("<app.name>")) >= 0) { //$NON-NLS-1$ |
| // String appNameProper = LibraryUIManager.getAppName(); |
| String appNameProper = LibraryUIPreferences |
| .getApplicationShortName(); |
| libraryPath = libraryPath.substring(0, idx) + appNameProper |
| + libraryPath.substring(idx + 10); |
| } |
| |
| return libraryPath; |
| } |
| |
| /** |
| * Returns the application short name passed in the main feature's |
| * plugin_customization.ini. |
| * |
| * @return The passed-in application short name. |
| */ |
| public static String getApplicationShortName() { |
| String appname = LibraryUIPlugin.getDefault().getPreferenceStore() |
| .getString(APPLICATION_SHORT_NAME); |
| return appname; |
| } |
| |
| /** |
| * Gets the recently opened method libraries preference. |
| * |
| * @return a collection of <code>RecentlyOpenedLibrary</code> objects |
| */ |
| public static List getRecentlyOpenedLibraries() { |
| List libraries = getPropertyList(RECENTLY_OPENED_LIBRARIES); |
| List result = new ArrayList(); |
| if (libraries.size() > 0) { |
| for (Iterator it = libraries.iterator(); it.hasNext();) { |
| String libraryURI = (String) it.next(); |
| result.add(new RecentlyOpenedLibrary(libraryURI)); |
| } |
| } |
| return result; |
| } |
| |
| /** |
| * Sets the recently opened method libraries preference. |
| * |
| * @param library |
| * a method library |
| */ |
| public static void addRecentlyOpenedLibrary(MethodLibrary library) { |
| if (library == null) { |
| return; |
| } |
| |
| List libraries = getRecentlyOpenedLibraries(); |
| List libraryURIs = new ArrayList(); |
| for (Iterator it = libraries.iterator(); it.hasNext();) { |
| RecentlyOpenedLibrary lib = (RecentlyOpenedLibrary) it.next(); |
| libraryURIs.add(lib.getURI().toString()); |
| } |
| |
| String libraryURI = LibraryService.getInstance().getLibraryManager(library) |
| .getMethodLibraryURI().toString(); |
| if (!libraryURIs.contains(libraryURI)) { |
| libraryURIs.add(libraryURI); |
| setPropertyList(RECENTLY_OPENED_LIBRARIES, StrUtil |
| .convertListToStrArray(libraryURIs)); |
| } |
| } |
| |
| /** |
| * Save all preferences |
| * |
| */ |
| public static void saveAllPreferences() { |
| LibraryUIPlugin.getDefault().savePluginPreferences(); |
| } |
| |
| /** |
| * Returns the string values associated with the named preference as a |
| * <ocde>List</code>. |
| */ |
| private static List getPropertyList(String name) { |
| return convertToList(LibraryUIPlugin.getDefault().getPreferenceStore() |
| .getString(name)); |
| } |
| |
| /** |
| * Saves the given string values associated with the named preference. |
| */ |
| private static void setPropertyList(String name, String[] items) { |
| StringBuffer buffer = new StringBuffer(); |
| for (int i = 0; i < items.length; i++) { |
| buffer.append(items[i]); |
| buffer.append(PREFERENCE_DELIMITER); |
| } |
| LibraryUIPlugin.getDefault().getPreferenceStore().setValue(name, |
| buffer.toString()); |
| } |
| |
| /** |
| * Converts the supplied PREFERENCE_DELIMITER delimited string into a |
| * <code>List</code> object. |
| */ |
| private static ArrayList convertToList(String preferenceValue) { |
| ArrayList topics = new ArrayList(); |
| StringTokenizer tokenizer = new StringTokenizer(preferenceValue, |
| PREFERENCE_DELIMITER); |
| int tokenCount = tokenizer.countTokens(); |
| for (int i = 0; i < tokenCount; i++) { |
| topics.add(tokenizer.nextToken()); |
| } |
| return topics; |
| } |
| |
| } |