View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ModelInfo.java
13   * Created: 2007-00-00
14   * Author: entrop
15   * $Id: ModelInfo.java 2668 2009-03-27 14:12:00Z bmilos $
16   */
17  
18  package pl.edu.agh.cast.data.ui.template;
19  
20  import org.eclipse.swt.SWT;
21  import org.eclipse.swt.graphics.Font;
22  import org.eclipse.swt.graphics.FontData;
23  import org.eclipse.swt.layout.GridData;
24  import org.eclipse.swt.layout.GridLayout;
25  import org.eclipse.swt.widgets.Composite;
26  import org.eclipse.swt.widgets.Label;
27  
28  import pl.edu.agh.cast.data.util.Messages;
29  import pl.edu.agh.cast.model.ModelColumn;
30  import pl.edu.agh.cast.model.base.BasePlugin;
31  
32  /**
33   * Holds and maintains the information about a specific model. It is used in the template creation wizard in order to
34   * display the data format column details.
35   *
36   * @author AGH CAST Team
37   */
38  public class ModelInfo {
39  	private static final int COLUMNS_COUNT = 3;
40  
41  	private String modelId;
42  
43  	private ModelColumn[] modelColumns;
44  
45  	/**
46  	 * The default constructor.
47  	 *
48  	 * @param modelId
49  	 *            the model identifier
50  	 */
51  	public ModelInfo(String modelId) {
52  		this.modelId = modelId;
53  	}
54  
55  	/**
56  	 * Fills in the specified composite, with the information about the model.
57  	 *
58  	 * @param modelInfoComposite
59  	 *            the model information composite
60  	 * @param modelCols
61  	 *            the model columns
62  	 */
63  	public void fillModelInfoComposite(Composite modelInfoComposite, ModelColumn[] modelCols) {
64  		this.modelColumns = modelCols;
65  
66  		GridLayout modelInfoLayout = new GridLayout(COLUMNS_COUNT, false);
67  		modelInfoComposite.setLayout(modelInfoLayout);
68  		modelInfoLayout.verticalSpacing = 1;
69  
70  		createAndAddCaptionsRow(modelInfoComposite);
71  
72  		for (ModelColumn modelColumn : modelColumns) {
73  			addLabel(modelInfoComposite, modelColumn.getName());
74  			addLabel(modelInfoComposite, translateDataType(modelColumn.getType()));
75  			addLabel(modelInfoComposite, translateUseValue(modelColumn.isRequired()));
76  		}
77  	}
78  
79  	private void createAndAddCaptionsRow(Composite modelInfoComposite) {
80  		GridData gd = new GridData();
81  		gd.grabExcessHorizontalSpace = true;
82  		gd.horizontalAlignment = SWT.FILL;
83  
84  		Label nameColumnName = addLabel(modelInfoComposite, Messages.ModelInfoColumn_Name);
85  		Label typeColumnName = addLabel(modelInfoComposite, Messages.ModelInfoColumn_Type);
86  		Label useColumnName = addLabel(modelInfoComposite, Messages.ModelInfoColumn_Use);
87  
88  		FontData fd = new FontData();
89  		fd.setStyle(SWT.BOLD);
90  		fd.setHeight(9);
91  		Font font = new Font(modelInfoComposite.getDisplay(), fd);
92  
93  		nameColumnName.setLayoutData(gd);
94  		typeColumnName.setLayoutData(gd);
95  		useColumnName.setLayoutData(gd);
96  
97  		nameColumnName.setFont(font);
98  		typeColumnName.setFont(font);
99  		useColumnName.setFont(font);
100 	}
101 
102 	private Label addLabel(Composite composite, String text) {
103 		Label label = new Label(composite, SWT.NULL);
104 		if (text != null) {
105 			label.setText(text);
106 		}
107 		return label;
108 	}
109 
110 	/**
111 	 * Returns the number of columns that are required.
112 	 *
113 	 * @return the number of required columns
114 	 */
115 	public int getRequiredColumnsCount() {
116 		int requiredCount = 0;
117 		for (ModelColumn column : modelColumns) {
118 			if (column.isRequired()) {
119 				requiredCount++;
120 			}
121 		}
122 		return requiredCount;
123 	}
124 
125 	private static String translateUseValue(boolean required) {
126 		if (required) {
127 			return Messages.ModelInfo_ColumnRequired;
128 		} else {
129 			return Messages.ModelInfo_ColumnOptional;
130 		}
131 	}
132 
133 	/**
134 	 * Localizes the data types, so that they can be displayed in different languages.
135 	 *
136 	 * @param type
137 	 *            the type to localize
138 	 * @return the localized type
139 	 */
140 	private static String translateDataType(String type) {
141 		if (type == null || type.length() == 0) {
142 			return ""; //$NON-NLS-1$
143 		}
144 
145 		String result = type;
146 		if (type.equals(BasePlugin.COLUMN_TYPE_DATE)) {
147 			result = Messages.ModelInfo_TypeDate;
148 		} else if (type.equals(BasePlugin.COLUMN_TYPE_LONG)) {
149 			result = Messages.ModelInfo_TypeLong;
150 		} else if (type.equals(BasePlugin.COLUMN_TYPE_STRING)) {
151 			result = Messages.ModelInfo_TypeString;
152 		} else if (type.equals(BasePlugin.COLUMN_TYPE_BOOLEAN)) {
153 			result = Messages.ModelInfo_TypeBoolean;
154 		} else if (type.equals(BasePlugin.COLUMN_TYPE_DOUBLE)) {
155 			result = Messages.ModelInfo_TypeDouble;
156 		}
157 		return result;
158 	}
159 }