Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 6164c952583c3d609714994e0dc94bf40a042d5c (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*****************************************************************************
 * Copyright (c) 2004 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 API and implementation
 ****************************************************************************/
package org.eclipse.wst.css.ui.internal.contentassist;



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.wst.css.core.internal.metamodel.CSSMMNode;
import org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil;
import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSCharsetRule;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.css.core.internal.provisional.preferences.CSSPreferenceHelper;
import org.eclipse.wst.css.core.internal.util.SelectionCollector;
import org.eclipse.wst.css.ui.internal.image.CSSImageType;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;

class CSSProposalGeneratorForAtmarkRule extends CSSProposalGenerator {

	private boolean fUseUpperCase = false;
	private static final String CHARSET = "@charset";//$NON-NLS-1$
	private static final String FONT_FACE = "@font-face";//$NON-NLS-1$
	private static final String IMPORT = "@import";//$NON-NLS-1$
	private static final String MEDIA = "@media";//$NON-NLS-1$
	private static final String PAGE = "@page";//$NON-NLS-1$

	/**
	 * CSSAtmarkRuleProposalGenerator constructor comment.
	 * 
	 * @param context
	 *            com.ibm.sed.contentassist.old.css.CSSContentAssistContext
	 */
	CSSProposalGeneratorForAtmarkRule(CSSContentAssistContext context) {
		super(context);
		fUseUpperCase = CSSPreferenceHelper.getInstance().isIdentUpperCase();
	}

	/**
	 *  
	 */
	private CSSCACandidate getCandidateCharsetRule() {
		// check content model
		CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
		if (!util.collectNodesByType(CSSMMNode.TYPE_CHARSET_RULE).hasNext()) {
			return null;
		}

		// check if embedded or not
		if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
			return null;
		}

		// check if caret precede all other rules.
		int offset = fContext.getCursorPos();
		if (0 < offset) {
			SelectionCollector trav = new SelectionCollector();
			trav.setRegion(0, offset - 1);
			trav.apply(fContext.getModel().getDocument());
			Iterator i = trav.getSelectedNodes().iterator();
			while (i.hasNext()) {
				Object obj = i.next();
				if (obj instanceof ICSSNode && !(obj instanceof ICSSDocument)) {
					return null;
				}
			}
		}

		int cursorPos = 0;
		String ident = (fUseUpperCase) ? CHARSET.toUpperCase() : CHARSET.toLowerCase();
		StringBuffer buf = new StringBuffer();
		buf.append(ident);
		buf.append(" ");//$NON-NLS-1$
		cursorPos = buf.length();
		StringAndOffset sao;
		sao = generateQuotes();
		buf.append(sao.fString);
		cursorPos += sao.fOffset;
		sao = generateSemicolon();
		buf.append(sao.fString);

		String text = buf.toString();

		if (isMatch(text)) {
			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(text);
			item.setCursorPosition(cursorPos);
			item.setDisplayString(ident);
			item.setImageType(CSSImageType.RULE_CHARSET);
			return item;
		} else {
			return null;
		}
	}

	/**
	 *  
	 */
	private CSSCACandidate getCandidateFontFaceRule() {
		CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
		if (!util.collectNodesByType(CSSMMNode.TYPE_FONT_FACE_RULE).hasNext()) {
			return null;
		}

		int cursorPos = 0;
		String ident = (fUseUpperCase) ? FONT_FACE.toUpperCase() : FONT_FACE.toLowerCase();
		StringBuffer buf = new StringBuffer();
		buf.append(ident);
		buf.append(" ");//$NON-NLS-1$
		cursorPos = buf.length();
		StringAndOffset sao;
		sao = generateBraces();
		buf.append(sao.fString);
		cursorPos += sao.fOffset;

		String text = buf.toString();

		if (isMatch(text)) {
			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(buf.toString());
			item.setCursorPosition(cursorPos);
			item.setDisplayString(ident);
			item.setImageType(CSSImageType.RULE_FONTFACE);
			return item;
		} else {
			return null;
		}
	}

	/**
	 *  
	 */
	private CSSCACandidate getCandidateImportRule() {
		// check content model
		CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
		if (!util.collectNodesByType(CSSMMNode.TYPE_IMPORT_RULE).hasNext()) {
			return null;
		}

		// charset and import can precede import rule.
		int offset = fContext.getCursorPos();
		if (0 < offset) {
			SelectionCollector trav = new SelectionCollector();
			trav.setRegion(0, offset - 1);
			trav.apply(fContext.getModel().getDocument());
			Iterator i = trav.getSelectedNodes().iterator();
			while (i.hasNext()) {
				Object obj = i.next();
				if (obj instanceof ICSSNode && !(obj instanceof ICSSDocument || obj instanceof ICSSCharsetRule || obj instanceof ICSSImportRule)) {
					return null;
				}
			}
		}

		int cursorPos = 0;
		String ident = (fUseUpperCase) ? IMPORT.toUpperCase() : IMPORT.toLowerCase();
		StringBuffer buf = new StringBuffer();
		buf.append(ident);
		buf.append(" ");//$NON-NLS-1$
		cursorPos = buf.length();
		StringAndOffset sao;
		sao = generateURI();
		buf.append(sao.fString);
		cursorPos += sao.fOffset;
		sao = generateSemicolon();
		buf.append(sao.fString);

		String text = buf.toString();

		if (isMatch(text)) {
			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(buf.toString());
			item.setCursorPosition(cursorPos);
			item.setDisplayString(ident);
			item.setImageType(CSSImageType.RULE_IMPORT);
			return item;
		} else {
			return null;
		}
	}

	/**
	 *  
	 */
	private CSSCACandidate getCandidateMediaRule() {
		CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
		if (!util.collectNodesByType(CSSMMNode.TYPE_MEDIA_RULE).hasNext()) {
			return null;
		}

		int cursorPos = 0;
		String ident = (fUseUpperCase) ? MEDIA.toUpperCase() : MEDIA.toLowerCase();
		StringBuffer buf = new StringBuffer();
		buf.append(ident);
		buf.append("  ");//$NON-NLS-1$
		cursorPos = buf.length() - 1;
		StringAndOffset sao;
		sao = generateBraces();
		buf.append(sao.fString);

		String text = buf.toString();

		if (isMatch(text)) {
			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(buf.toString());
			item.setCursorPosition(cursorPos);
			item.setDisplayString(ident);
			item.setImageType(CSSImageType.RULE_MEDIA);
			return item;
		} else {
			return null;
		}
	}

	/**
	 *  
	 */
	private CSSCACandidate getCandidatePageRule() {
		CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
		if (!util.collectNodesByType(CSSMMNode.TYPE_PAGE_RULE).hasNext()) {
			return null;
		}

		int cursorPos = 0;
		String ident = (fUseUpperCase) ? PAGE.toUpperCase() : PAGE.toLowerCase();
		StringBuffer buf = new StringBuffer();
		buf.append(ident);
		buf.append(" ");//$NON-NLS-1$
		cursorPos = buf.length();
		StringAndOffset sao;
		sao = generateBraces();
		buf.append(sao.fString);
		cursorPos += sao.fOffset;

		String text = buf.toString();

		if (isMatch(text)) {
			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(buf.toString());
			item.setCursorPosition(cursorPos);
			item.setDisplayString(ident);
			item.setImageType(CSSImageType.RULE_PAGE);
			return item;
		} else {
			return null;
		}
	}

	/**
	 * getCandidates method comment.
	 */
	protected Iterator getCandidates() {
		List candidates = new ArrayList();

		ITextRegion region = fContext.getTargetRegionPrevious();
		//	ITextRegion region = fContext.getSignificantTargetRegion();
		if (region != null) {
			String type = region.getType();
			if (type != CSSRegionContexts.CSS_RBRACE && type != CSSRegionContexts.CSS_DELIMITER) {
				return candidates.iterator();
			}
		}

		CSSCACandidate item;
		if ((item = getCandidateImportRule()) != null) {
			candidates.add(item);
		}
		if ((item = getCandidateCharsetRule()) != null) {
			candidates.add(item);
		}
		if ((item = getCandidateMediaRule()) != null) {
			candidates.add(item);
		}
		if ((item = getCandidatePageRule()) != null) {
			candidates.add(item);
		}
		if ((item = getCandidateFontFaceRule()) != null) {
			candidates.add(item);
		}
		return candidates.iterator();
	}
}

Back to the top