Skip to main content
summaryrefslogtreecommitdiffstats
blob: 78a97072a6fdb38159fb5543c7c8e89961dc3cac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2009 Oracle Corporation.
 * 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
 *******************************************************************************/ 
package org.eclipse.jst.pagedesigner.ui.preferences;

import java.util.Arrays;

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.editors.HTMLEditor;

/**
 * Utility class for handling preferences related to the Web Page Editor plug-in.
 * <br>
 * <p><b>Provisional API - subject to change</b></p>
 */
public class PDPreferences extends AbstractPreferenceInitializer {

    /**
     * Preference used to set modes for the page designer when displayed in a
     * sash editor.
     */
    public static final String SASH_EDITOR_MODE_PREF = PDPreferences.class
            .getName()
            + ".sash_editor_mode"; //$NON-NLS-1$
    /**
     * The number of pixels of artificial cell padding to use Default = 0
     */
    public final static String CSS_USE_ARTIFICAL_CELL_PADDING = PDPreferences.class
            .getName()
            + ".CSSArtificalCellPadding"; //$NON-NLS-1$
    /**
     * Whether or not to enable absolute positioning Default = false
     */
    public final static String CSS_ENABLE_ABSOLUTE_POSITIONING = PDPreferences.class
            .getName()
            + ".CSSEnableAbsolutePositioning"; //$NON-NLS-1$
    /**
     * Content types (as comma-delimited String) for which to hide preview page (default is the empty String).
     */
    public static final String HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES = PDPreferences.class.
    		getName() +
    		".hidePreviewPageForContentTypes"; //$NON-NLS-1$
    /**
     * The default value for absolute positioning enablement
     */
    public static final boolean DEFAULT_CSS_ENABLE_ABSOLUTE_POSITIONING = false;
    /**
     * The default value for artificial cell padding.
     */
    public static final int DEFAULT_CSS_USE_ARTIFICIAL_CELL_PADDING = 4;
    /**
     * Default value for content types for which to hide preview page.
     */
    public static final String DEFAULT_HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES = ""; //$NON-NLS-1$

    @Override
    public void initializeDefaultPreferences()
    {
        // Set default HTML editor split vertical (i.e. with top and bottom
        // pane)
        IPreferenceStore store = getPreferenceStore();
        store.setDefault(SASH_EDITOR_MODE_PREF, HTMLEditor.MODE_SASH_VERTICAL);
        store.setDefault(CSS_USE_ARTIFICAL_CELL_PADDING,
                DEFAULT_CSS_USE_ARTIFICIAL_CELL_PADDING);
        store.setDefault(CSS_ENABLE_ABSOLUTE_POSITIONING,
                DEFAULT_CSS_ENABLE_ABSOLUTE_POSITIONING);
        store.setDefault(HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES,
        		DEFAULT_HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES);
    }

    /**
     * @return the css absolute positioning enablement flag.
     */
    public boolean isCssAbsolutePositioningEnabled()
    {
        final IPreferenceStore store = getPreferenceStore();
        return store.getBoolean(CSS_ENABLE_ABSOLUTE_POSITIONING);
    }

    /**
     * @return the css artificial cell padding preference
     */
    public int getCssArtificialCellPadding()
    {
        final IPreferenceStore store = getPreferenceStore();
        return store.getInt(CSS_USE_ARTIFICAL_CELL_PADDING);
    }

    /**
     * Get list of content types for which to hide preview page. 
     * @return Content types (as String array) for which to hide preview page. 
     */
    public static String[] getHiddenPreviewPageContentTypes()
    {
    	String[] hiddenTypes = new String[0];
    	final IPreferenceStore store = getPreferenceStore();
    	final String hiddenTypesString = store.getString(HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES);
    	if (hiddenTypesString != null && hiddenTypesString.length() > 0)
    	{
    		hiddenTypes = hiddenTypesString.split(","); //$NON-NLS-1$
    	}
    	return hiddenTypes;
    }

    /**
     * Adds content type to list of types for which to hide preview page.
     * @param contentTypeId Content type to add to list.
     */
    public static void addHiddenPreviewPageContentType(String contentTypeId)
    {
    	if (contentTypeId != null && contentTypeId.length() > 0)
    	{
	    	final String[] hiddenContentTypes = getHiddenPreviewPageContentTypes();
	    	if (Arrays.binarySearch(hiddenContentTypes, contentTypeId) < 0)
	    	{
	    		String hiddenTypesString = arrayToString(hiddenContentTypes);
	    		if (hiddenTypesString.length() > 0)
	    		{
	    			hiddenTypesString = hiddenTypesString.concat(","); //$NON-NLS-1$
	    		}
	    		hiddenTypesString = hiddenTypesString.concat(contentTypeId.trim());
	        	final IPreferenceStore store = getPreferenceStore();
	        	store.setValue(HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES, hiddenTypesString);
	    	}
    	}
    }

    /**
     * Removes content type from list of types for which to hide preview page.
     * @param contentTypeId Content type to remove from list.
     */
    public static void removeHiddenPreviewPageContentType(String contentTypeId)
    {
    	if (contentTypeId != null && contentTypeId.length() > 0) {
    		final String[] hiddenContentTypes = getHiddenPreviewPageContentTypes();
    		if (Arrays.binarySearch(hiddenContentTypes, contentTypeId) >= 0)
    		{
    			final String hiddenTypesString = arrayToString(hiddenContentTypes);
    			hiddenTypesString.replace(contentTypeId, ""); //$NON-NLS-1$
    			hiddenTypesString.replace(",,", ","); //$NON-NLS-1$ //$NON-NLS-2$
	        	final IPreferenceStore store = getPreferenceStore();
	        	store.setValue(HIDE_PREVIEW_PAGE_FOR_CONTENT_TYPES, hiddenTypesString);
    		}
    	}
    }

    private static IPreferenceStore getPreferenceStore()
    {
        return PDPlugin.getDefault().getPreferenceStore();
    }

    private static String arrayToString(String[] array)
    {
    	String string = ""; //$NON-NLS-1$
    	if (array != null && array.length > 0)
    	{
    		StringBuilder builder = new StringBuilder();
    		for (int i = 0; i < array.length; i++)
    		{
    			if (i > 0)
    			{
    				builder.append(',');
    			}
    			builder.append(array[i]);
    		}
    		string = builder.toString();
    	}
    	return string;
    }

}

Back to the top