Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 38507143b6396b7881edc56d260e3fa5ea85f31c (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
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *    
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.properties.runtime.view.content;

import org.eclipse.papyrus.properties.runtime.Activator;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * Utility class to parse layouts. <BR>
 * Nota: Entry point for future possible extension point</BR>
 */
public class LayoutParser {

	/** kind for grid layout (default value) */
	public static final String GRID_KIND = "Grid";

	/**
	 * Parses the content of the layout node and returns the descriptor
	 * 
	 * @param layoutNode
	 *        the node to parse
	 * @return the created layout descriptor
	 */
	public static LayoutDescriptor parseLayoutNode(Node layoutNode) {
		// default layout
		if(layoutNode == null) {
			return new GridLayoutDescriptor();
		}
		String type = getType(layoutNode);
		if(GRID_KIND.equals(type)) {
			NamedNodeMap attributes = layoutNode.getAttributes();
			if(attributes == null) {
				Activator.log.warn("Impossible to parse the attribute set for grid layout: " + layoutNode);
				return new GridLayoutDescriptor();
			}

			int numColumns = 2;
			Node numColumnsNode = attributes.getNamedItem("numColumns");
			if(numColumnsNode != null) {
				try {
					numColumns = Integer.parseInt(numColumnsNode.getNodeValue());
				} catch (NumberFormatException e) {
					Activator.log.error(e);
				}
			}

			boolean sameWidth = true;
			Node sameWidthNode = attributes.getNamedItem("sameWidth");
			if(sameWidthNode != null) {
				sameWidth = Boolean.parseBoolean(sameWidthNode.getNodeValue());
			}

			return new GridLayoutDescriptor(numColumns, sameWidth);
		}

		return new GridLayoutDescriptor();
	}

	/**
	 * Returns the kind of layout
	 * 
	 * @param layoutNode
	 *        the node to parse
	 * @return the kind of layout
	 */
	private static String getType(Node layoutNode) {
		NamedNodeMap attributes = layoutNode.getAttributes();
		if(attributes == null) {
			Activator.log.warn("Impossible to parse the attribute set for layout node: " + layoutNode);
			return GRID_KIND;
		}
		Node kindNode = attributes.getNamedItem("kind");
		if(kindNode == null) {
			Activator.log.warn("Impossible to find the kind attribute for the layout node: " + layoutNode);
			return GRID_KIND;
		}

		return kindNode.getNodeValue();
	}
}

Back to the top