intial source from http://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / source / DefaultSourceViewerConfiguration.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://solareclipse.sourceforge.net/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  * 
11  * $Id: DefaultSourceViewerConfiguration.java,v 1.1 2004-09-02 18:26:32 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.text.source;
15
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.text.source.ISourceViewer;
18 import org.eclipse.jface.text.source.SourceViewerConfiguration;
19
20 /**
21  * @author Igor Malinin
22  */
23 public class DefaultSourceViewerConfiguration extends SourceViewerConfiguration {
24
25         /** Preference key used to look up display tab width. */
26         public static final String PREFERENCE_TAB_WIDTH =
27                 "net.sourceforge.phpeclipse.ui.editor.tabWidth"; //$NON-NLS-1$
28
29         /** Preference key for inserting spaces rather than tabs. */
30         public static final String PREFERENCE_SPACES_FOR_TABS =
31                 "net.sourceforge.phpeclipse.ui.editor.spacesForTabs"; //$NON-NLS-1$
32
33         private IPreferenceStore store;
34
35         public DefaultSourceViewerConfiguration(IPreferenceStore store) {
36                 this.store = store;
37         }
38
39         /*
40          * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
41          */
42         public String[] getIndentPrefixes(ISourceViewer sourceViewer,
43                         String contentType) {
44                 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
45
46                 int tabWidth = store.getInt(PREFERENCE_TAB_WIDTH);
47                 boolean useSpaces = store.getBoolean(PREFERENCE_SPACES_FOR_TABS);
48
49                 String[] prefixes = new String[tabWidth + 1];
50
51                 for (int i = 0; i <= tabWidth; i++) {
52                         StringBuffer prefix = new StringBuffer(tabWidth - 1);
53
54                         if (useSpaces) {
55                                 for (int j = 0; j + i < tabWidth; j++) {
56                                         prefix.append(' ');
57                                 }
58
59                                 if (i != 0) {
60                                         prefix.append('\t');
61                                 }
62                         } else {
63                                 for (int j = 0; j < i; j++) {
64                                         prefix.append(' ');
65                                 }
66
67                                 if (i != tabWidth) {
68                                         prefix.append('\t');
69                                 }
70                         }
71
72                         prefixes[i] = prefix.toString();
73                 }
74
75                 prefixes[tabWidth] = ""; //$NON-NLS-1$
76
77                 return prefixes;
78         }
79 }