001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.vmd.midpnb.screen.display;
043:
044: import org.netbeans.modules.vmd.api.model.DesignComponent;
045: import org.netbeans.modules.vmd.api.model.PropertyValue;
046: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
047: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
048: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyEditor;
049: import org.netbeans.modules.vmd.midp.components.MidpTypes;
050: import org.netbeans.modules.vmd.midp.screen.display.ItemDisplayPresenter;
051: import org.netbeans.modules.vmd.midp.screen.display.ScreenSupport;
052: import org.netbeans.modules.vmd.midp.screen.display.property.ResourcePropertyEditor;
053: import org.netbeans.modules.vmd.midpnb.components.items.TableItemCD;
054: import org.netbeans.modules.vmd.midpnb.components.resources.SimpleTableModelCD;
055: import javax.swing.*;
056: import java.awt.*;
057: import java.util.ArrayList;
058: import java.util.Collection;
059: import java.util.List;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Anton Chechel
065: * @version 1.0
066: */
067: public class TableItemDisplayPresenter extends ItemDisplayPresenter {
068:
069: private static final int BORDER_LINE_WIDTH = 1;
070: private static final int CELL_INSETS = 2;
071: private static final int DOUBLE_CELL_INSETS = CELL_INSETS * 2;
072: private static final Stroke BORDER_STROKE = new BasicStroke(1,
073: BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 0f,
074: new float[] { 3f, 3f }, 0f);
075: private static JLabel fontLabel = new JLabel();
076:
077: private JPanel tablePanel;
078: private boolean hasModel;
079: private boolean modelIsUserCode;
080: private boolean drawBorders = true;
081: private String[] columnNames;
082: private String[][] values;
083:
084: public TableItemDisplayPresenter() {
085: tablePanel = new JPanel() {
086:
087: @Override
088: public void paint(Graphics g) {
089: super .paint(g);
090: paintTable(g);
091: }
092: };
093: tablePanel.setOpaque(false);
094: tablePanel.setPreferredSize(new Dimension(200, 40)); // TODO compute it from fontSize
095: setContentComponent(tablePanel);
096: }
097:
098: private void paintTable(Graphics g) {
099: Font headersFont = fontLabel.getFont().deriveFont(Font.BOLD);
100: Font valuesFont = fontLabel.getFont();
101: int cummulativeY = 0;
102:
103: if (modelIsUserCode) {
104: cummulativeY += ScreenSupport.getFontHeight(g, valuesFont);
105: g.drawString(NbBundle.getMessage(
106: TableItemDisplayPresenter.class,
107: "DISP_table_is_user_code"), CELL_INSETS,
108: cummulativeY); // NOI18N
109: } else if (!hasModel) {
110: cummulativeY += ScreenSupport.getFontHeight(g, valuesFont);
111: g.drawString(NbBundle.getMessage(
112: TableItemDisplayPresenter.class,
113: "DISP_no_table_model_specified"), CELL_INSETS,
114: cummulativeY); // NOI18N
115: } else if (values == null || values.length < 1) {
116: cummulativeY += ScreenSupport.getFontHeight(g, valuesFont);
117: g.drawString(NbBundle.getMessage(
118: TableItemDisplayPresenter.class,
119: "DISP_empty_table_model"), CELL_INSETS,
120: cummulativeY); // NOI18N
121: } else {
122: Graphics2D g2D = (Graphics2D) g;
123: Dimension oldSize = tablePanel.getSize();
124: final int width = oldSize.width;
125: final int height = oldSize.height;
126:
127: int headersY = 0;
128:
129: int[] colWidths = getColWidths(g, values, columnNames,
130: headersFont, valuesFont);
131:
132: if (columnNames != null) {
133: g.setFont(headersFont);
134: headersY = cummulativeY;
135: cummulativeY += ScreenSupport.getFontHeight(g,
136: headersFont);
137: int cummulativeX = CELL_INSETS + BORDER_LINE_WIDTH;
138: // draw headers ...
139: for (int i = 0; (i < columnNames.length)
140: && (cummulativeX < width); i++) {
141: String name = columnNames[i];
142: if (name != null) {
143: g.drawString(name, cummulativeX, cummulativeY);
144: }
145: if (colWidths != null) {
146: cummulativeX += colWidths[i];
147: }
148: }
149: cummulativeY += DOUBLE_CELL_INSETS + BORDER_LINE_WIDTH;
150: }
151:
152: if (values != null && values.length > 0) {
153: g.setFont(valuesFont);
154: for (int i = 0; (i < values.length)
155: && (cummulativeY < height); i++) {
156: String[] row = values[i];
157: cummulativeY += ScreenSupport.getFontHeight(g,
158: valuesFont);
159: int cummulativeX = CELL_INSETS + BORDER_LINE_WIDTH;
160: for (int j = 0; (j < row.length)
161: && (cummulativeX < width); j++) {
162: String cell = row[j];
163: if (cell != null) {
164: g.drawString(cell, cummulativeX,
165: cummulativeY);
166: }
167: if (colWidths != null) {
168: cummulativeX += colWidths[j];
169: }
170: }
171: cummulativeY += DOUBLE_CELL_INSETS
172: + BORDER_LINE_WIDTH;
173: }
174: }
175:
176: // draw borders
177: if (drawBorders) {
178: g2D.setStroke(BORDER_STROKE);
179: g.drawRect(0, 0, width - 1, height - 1);
180: g.drawLine(0, cummulativeY, width, cummulativeY);
181: int borderY = 0;
182: if (columnNames != null) {
183: borderY += ScreenSupport.getFontHeight(g,
184: headersFont)
185: + DOUBLE_CELL_INSETS;
186: g.drawLine(0, borderY, width, borderY);
187: borderY++;
188: }
189: if (values != null && values.length > 0) {
190: // horizontal lines
191: for (int i = 0; (i < values.length)
192: && (borderY < height); i++) {
193: borderY += ScreenSupport.getFontHeight(g,
194: valuesFont)
195: + DOUBLE_CELL_INSETS;
196: g.drawLine(0, borderY, width, borderY);
197: borderY++;
198: }
199:
200: // vertical lines
201: int borderX = 0;
202: int rows = values[0].length;
203: for (int i = 0; (i < rows) && (borderX < width); i++) {
204: g.drawLine(borderX, headersY, borderX,
205: height - 1);
206: borderX += colWidths[i];
207: }
208: }
209: }
210: }
211: }
212:
213: @Override
214: public void reload(ScreenDeviceInfo deviceInfo) {
215: super .reload(deviceInfo);
216:
217: PropertyValue value = getComponent().readProperty(
218: TableItemCD.PROP_MODEL);
219: modelIsUserCode = PropertyValue.Kind.USERCODE.equals(value
220: .getKind());
221: if (!modelIsUserCode) {
222: DesignComponent tableModelComponent = value.getComponent();
223: hasModel = tableModelComponent != null;
224:
225: if (hasModel) {
226: value = getComponent().readProperty(
227: TableItemCD.PROP_BORDERS);
228: if (!PropertyValue.Kind.USERCODE
229: .equals(value.getKind())) {
230: drawBorders = MidpTypes.getBoolean(value);
231: }
232:
233: PropertyValue columnsProperty = tableModelComponent
234: .readProperty(SimpleTableModelCD.PROP_COLUMN_NAMES);
235: List<PropertyValue> list = columnsProperty.getArray();
236: if (list != null) {
237: columnNames = new java.lang.String[list.size()];
238: for (int i = 0; i < list.size(); i++) {
239: columnNames[i] = MidpTypes.getString(list
240: .get(i));
241: }
242: } else {
243: columnNames = null;
244: }
245:
246: PropertyValue valuesProperty = tableModelComponent
247: .readProperty(SimpleTableModelCD.PROP_VALUES);
248: list = valuesProperty.getArray();
249: if (list != null) {
250: values = new String[list.size()][];
251: for (int i = 0; i < list.size(); i++) {
252: List<String> row = gatherStringValues(list.get(
253: i).getArray());
254: values[i] = row.toArray(new String[row.size()]);
255: }
256: } else {
257: values = null;
258: }
259: }
260: } else {
261: hasModel = false;
262: }
263:
264: tablePanel.setPreferredSize(calculatePrefferedSize());
265: tablePanel.repaint();
266: }
267:
268: // TODO compute 14 from fontSize
269: private Dimension calculatePrefferedSize() {
270: final Dimension oldSize = tablePanel.getPreferredSize();
271: if (!hasModel || values == null) {
272: return oldSize;
273: }
274:
275: int height = 0;
276: if (columnNames != null) {
277: height += CELL_INSETS + 14 + BORDER_LINE_WIDTH;
278: }
279: if (values != null) {
280: height += (DOUBLE_CELL_INSETS + 14 + BORDER_LINE_WIDTH)
281: * values.length;
282: }
283: return new Dimension(oldSize.width, height);
284: }
285:
286: // TODO make parameter generic and move to ArraySupport class (gatherPrimitiveValues)
287: private static List<String> gatherStringValues(
288: List<PropertyValue> propertyValues) {
289: List<String> list = new ArrayList<String>(propertyValues.size());
290: for (PropertyValue pv : propertyValues) {
291: list.add(MidpTypes.getString(pv));
292: }
293: return list;
294: }
295:
296: private int[] getColWidths(Graphics g, String[][] values,
297: String[] headers, Font headersFont, Font valuesFont) {
298: if (values == null || values.length == 0) {
299: return null;
300: }
301:
302: final int tableCols = values[0].length;
303:
304: final int[] colWidths = new int[tableCols];
305: for (int i = 0; i < tableCols; i++) {
306: colWidths[i] = tablePanel.getSize().width / tableCols;
307: }
308:
309: return colWidths;
310: }
311:
312: @Override
313: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
314: List<ScreenPropertyDescriptor> descriptors = new ArrayList<ScreenPropertyDescriptor>(
315: super .getPropertyDescriptors());
316: PropertyValue value = getComponent().readProperty(
317: TableItemCD.PROP_MODEL);
318: DesignComponent tableModel = null;
319: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
320: tableModel = value.getComponent();
321: }
322: ScreenPropertyEditor tableModelDescriptor = null;
323: if (tableModel == null) {
324: tableModelDescriptor = new ResourcePropertyEditor(
325: TableItemCD.PROP_MODEL, getComponent());
326: } else {
327: tableModelDescriptor = new ResourcePropertyEditor(
328: SimpleTableModelCD.PROP_VALUES, tableModel);
329: }
330: descriptors.add(new ScreenPropertyDescriptor(getComponent(),
331: tablePanel, tableModelDescriptor));
332: return descriptors;
333: }
334: }
|