source: trunk/org.modelica.mdt.core/src/org/modelica/mdt/core/preferences/PreferenceManager.java @ 1577

Last change on this file since 1577 was 1577, checked in by jgoppert, 12 years ago

Added ability to include modelica libraries in preferences page for auto-complete.

File size: 8.6 KB
Line 
1/*
2 * This file is part of Modelica Development Tooling.
3 *
4 * Copyright (c) 2005, Linköpings universitet, Department of
5 * Computer and Information Science, PELAB
6 *
7 * All rights reserved.
8 *
9 * (The new BSD license, see also
10 * http://www.opensource.org/licenses/bsd-license.php)
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 *   notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 *   notice, this list of conditions and the following disclaimer in
22 *   the documentation and/or other materials provided with the
23 *   distribution.
24 *
25 * * Neither the name of Linköpings universitet nor the names of its
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42package org.modelica.mdt.core.preferences;
43
44import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
45import org.eclipse.jface.preference.IPreferenceStore;
46import org.eclipse.jface.preference.PreferenceConverter;
47import org.eclipse.swt.graphics.RGB;
48
49import org.modelica.mdt.internal.core.CorePlugin;
50import org.modelica.mdt.internal.core.ErrorManager;
51
52/**
53 * This class manages the settings for the omc plugin (and at the moment the omc
54 * and ui plugin as well, but that's just uggly)
55 *
56 * The class provides two services, it sets the default settings and
57 * gives an easy access to current settings via the get<Setting_Name> methods.
58 *
59 * This class does not provides means for changing settings. To modify the
60 * settings the preference store object should be fetched from the core plugin.
61 *
62 * Following settings are available:
63 *
64 * DisplayCompErrors:
65 *  if set to false then compatibility error dialogs will not be displayed when
66 *  omc replys with something unexpected, the error will only be logged
67 * 
68 * UseStandardOmcPath:
69 *  if set to true the OPENMODELICAHOME environment variable will be used to
70 *  find the omc binary. if set to false the CustomOmcPath settings value will
71 *  be used instead, se below.
72 * 
73 * CustomOmcPath:
74 *  the value of this setting can be used as alternative path to find omc binary.
75 *  used if UseStandardOmcPath settings is set to false, se above.
76 */
77public class PreferenceManager extends AbstractPreferenceInitializer
78{
79    /* settings tags in the preference store */
80    public static final String DISPLAY_COMPATIBILTY_ERRORS = "DisplayCompatibilityErrors";
81    public static final String USE_STANDARD_OMC_PATH = "UseStandardOmcPath";
82    public static final String CUSTOM_OMC_PATH = "CustomOmcPath";   
83    public static final String START_OMC = "StartOMC";
84    public static final String OMC_IGNORED_DIRECTORIES_AND_FILES = "OMCIgnoredDirectoriesAndFiles";
85    public static final String OMC_COMMAND_LINE_PARAMETERS = "OMCCommandLineParameters";
86    public static final String OMC_LIBRARIES = "OMCLibraries";
87   
88    protected final static String EDITOR_MATCHING_BRACKETS="matchingBrackets";
89    protected final static String EDITOR_MATCHING_BRACKETS_COLOR="matchingBracketsColor";
90    protected final static String EDITOR_TAB_WIDTH = "tabWidth";   
91   
92    private static IPreferenceStore store = null;
93   
94    public enum osType { WINDOWS, UNIX };
95   
96    public  static osType getOs()
97    {
98        String osName = System.getProperty("os.name");
99        if(osName.contains("Linux"))
100        {
101            return osType.UNIX;
102        }
103        else if(osName.contains("Windows"))
104        {
105            return osType.WINDOWS;
106        }
107        else
108        {
109            ErrorManager.logWarning("'" + osName + "' not officialy supported OS");
110            /* If the OS is not GNU/Linux or Windows, default to Unix */
111            return osType.UNIX;
112        }
113    }
114   
115    /**
116     * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
117     */
118    public void initializeDefaultPreferences() 
119    {
120        /*
121         * setup default settings
122         */
123        IPreferenceStore store = getStore();
124
125        store.setDefault(DISPLAY_COMPATIBILTY_ERRORS, false);
126        store.setDefault(USE_STANDARD_OMC_PATH, true);
127        store.setDefault(CUSTOM_OMC_PATH, "");
128        store.setDefault(START_OMC, true);
129        String ignored = "";
130        if (getOs() == osType.UNIX)
131            ignored = "omc_release:omc_debug:omc_frontend:omc_profiler:testsuite:bin:pde:modelica_parser:tools:mingw:template_precompile:susan_codegen:build:test_files:libraries:c_runtime:Examples:test_codegen:ModelicaBuiltin.mo:MetaModelicaBuiltin.mo:";
132        else
133            ignored = "omc_release;omc_debug;omc_frontend;omc_profiler;testsuite;bin;pde;modelica_parser;tools;mingw;template_precompile;susan_codegen;build;test_files;libraries;c_runtime;Examples;test_codegen;ModelicaBuiltin.mo;MetaModelicaBuiltin.mo;";
134        store.setDefault(OMC_IGNORED_DIRECTORIES_AND_FILES, ignored);
135       
136        store.setDefault(OMC_COMMAND_LINE_PARAMETERS, "+g=MetaModelica");
137        store.setDefault(OMC_LIBRARIES, "Modelica");
138       
139        store.setDefault(EDITOR_MATCHING_BRACKETS, true);
140        PreferenceConverter.setDefault(store, EDITOR_MATCHING_BRACKETS_COLOR, new RGB(192, 192, 192));     
141        store.setDefault(EDITOR_TAB_WIDTH, 2);
142    }
143   
144    private static IPreferenceStore getStore() 
145    {
146        if (store == null)
147        {
148            store = CorePlugin.getDefault().getPreferenceStore();
149        }
150        return store;
151    }
152
153    /**
154     * @return current setting for 'display compatibility errors'
155     */
156    public static boolean getDisplayCompErrors()
157    {
158        return getStore().getBoolean(DISPLAY_COMPATIBILTY_ERRORS);
159    }
160   
161    /**
162     * @return current settings for 'use standard omc path (that is $OPENMODELICAHOME)'
163     */
164    public static boolean getUseStandardOmcPath()
165    {
166        return getStore().getBoolean(USE_STANDARD_OMC_PATH);
167    }
168   
169    /**
170     * @return current setting for 'custom omc path'
171     */
172    public static String getCustomOmcPath()
173    {
174        return getStore().getString(CUSTOM_OMC_PATH);
175    }
176   
177    /**
178     * @return current setting for   
179     */
180    public static boolean getStartOMC()
181    {
182        return getStore().getBoolean(START_OMC);
183    }
184
185    /**
186     * @return current setting for   
187     */
188    public static String getOMCIgnoredDirectoriesAndFiles()
189    {
190        return getStore().getString(OMC_IGNORED_DIRECTORIES_AND_FILES);
191    }
192
193    /**
194     * @return current setting for   
195     */
196    public static String getOMCCommandLineParameters()
197    {
198        return getStore().getString(OMC_COMMAND_LINE_PARAMETERS);
199    }
200
201    /**
202     * @return current setting for   
203     */
204    public static String[] getOMCCommandLineParametersArray()
205    {
206        String x = getStore().getString(OMC_COMMAND_LINE_PARAMETERS);
207        String splitAt = " ";
208        return x.split(splitAt);
209    }   
210   
211    /**
212     * @return current setting for   
213     */
214    public static String getOMCLibraries()
215    {
216        return getStore().getString(OMC_LIBRARIES);
217    }
218   
219    /**
220     * @return current setting for   
221     */
222    public static String[] getOMCLibrariesArray()
223    {
224        String x = getStore().getString(OMC_LIBRARIES);
225        String splitAt = " ";
226        return x.split(splitAt);
227    }   
228   
229    /**
230     * @return current setting for   
231     */
232    public static String[] getOMCIgnoredDirectoryAndFileArray()
233    {
234        String x = getStore().getString(OMC_IGNORED_DIRECTORIES_AND_FILES);
235        String splitAt = "";
236        if (getOs() == osType.UNIX) splitAt = ":"; else splitAt = ";";
237        return x.split(splitAt);       
238    }   
239   
240    /**
241     * @return current setting for   
242     */
243    public static boolean isIgnoredDirectoryOrFile(String dir) 
244    {
245        String[] x = getOMCIgnoredDirectoryAndFileArray();
246        for (int i = 0; i < x.length; i++)
247        {
248            if (dir.equalsIgnoreCase(x[i]))
249                return true;
250        }
251        return false;
252    }   
253
254    /**
255     * @return current setting for   
256     */
257    public static boolean isInIgnoredDirectoryOrFile(String file)   
258    {
259        String[] x = getOMCIgnoredDirectoryAndFileArray();
260        for (int i = 0; i < x.length; i++)
261        {
262            if (file.contains(x[i]))
263                return true;
264        }
265        return false;
266    }       
267
268    /**
269     * @return current setting for   
270     */
271    public static boolean isAnIgnoredDirectoryOrFile(String file)   
272    {
273        String[] x = getOMCIgnoredDirectoryAndFileArray();
274        for (int i = 0; i < x.length; i++)
275        {
276            if (file.endsWith(x[i]))
277                return true;
278        }
279        return false;
280    }   
281}
Note: See TracBrowser for help on using the repository browser.