001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.graphic.network.rendering;
051:
052: import java.awt.Color;
053: import java.awt.Component;
054: import java.awt.Dimension;
055: import java.awt.Font;
056: import java.awt.Graphics;
057: import java.awt.Graphics2D;
058: import java.util.ArrayList;
059: import java.util.HashMap;
060: import java.util.Iterator;
061: import java.util.List;
062: import java.util.Map;
063:
064: import javax.swing.JComponent;
065: import javax.swing.JLabel;
066: import javax.swing.JPanel;
067: import javax.swing.JTextField;
068:
069: import com.jgoodies.forms.builder.DefaultFormBuilder;
070: import com.jgoodies.forms.factories.Borders;
071: import com.jgoodies.forms.layout.CellConstraints;
072: import com.jgoodies.forms.layout.FormLayout;
073: import com.projity.configuration.Configuration;
074: import com.projity.field.Field;
075: import com.projity.field.FieldConverter;
076: import com.projity.graphic.configuration.BarFormat;
077: import com.projity.graphic.configuration.FormBox;
078: import com.projity.graphic.configuration.FormBoxLayout;
079: import com.projity.graphic.configuration.FormFormat;
080: import com.projity.grouping.core.Node;
081: import com.projity.grouping.core.model.NodeModel;
082: import com.projity.pm.graphic.ChangeAwareComponent;
083: import com.projity.pm.graphic.ChangeAwareTextField;
084: import com.projity.strings.Messages;
085: import com.projity.util.Alert;
086:
087: public class FormComponent extends JPanel {
088: protected int maxRows = 3;
089: protected int maxCols = 2;
090: protected Map fieldComponents;
091: protected List selectedFormats;
092: protected boolean editor;
093: protected int zoom;
094: protected boolean texture = true;
095:
096: public FormComponent(List selectedFormats, int zoom,
097: boolean editor, boolean texture) {
098: super ();
099: fieldComponents = new HashMap();
100: this .selectedFormats = selectedFormats;
101: this .editor = editor;
102: this .zoom = zoom;
103: this .texture = texture;
104: BarFormat format;
105: if (selectedFormats == null || selectedFormats.size() == 0)
106: format = null;
107: else
108: format = (BarFormat) selectedFormats.get(0);
109: init(format);
110: setOpaque(false);
111: setForeground(Color.BLACK);
112: setBackground(Color.WHITE);
113: }
114:
115: public boolean isEditor() {
116: return editor;
117: }
118:
119: public void setEditor(boolean editor) {
120: this .editor = editor;
121: }
122:
123: public void init(BarFormat format) {
124: if (format == null)
125: return;
126: FormFormat form = format.getForm();
127: if (form == null)
128: return;
129: List boxes = form.getBoxes();
130: if (boxes == null || boxes.size() == 0) {
131: return;
132: }
133: FormBoxLayout formBoxLayout = form.getLayout(zoom);
134: FormLayout layout = new FormLayout(formBoxLayout
135: .getColumnGrid(), formBoxLayout.getRowGrid());
136: DefaultFormBuilder builder = new DefaultFormBuilder(this ,
137: layout);
138: if (formBoxLayout.getBorder() == null)
139: builder.setDefaultDialogBorder();
140: else
141: builder.setBorder(Borders.createEmptyBorder(formBoxLayout
142: .getBorder()));
143: CellConstraints cc = new CellConstraints();
144: for (Iterator i = boxes.iterator(); i.hasNext();) {
145: FormBox box = (FormBox) i.next();
146: if (zoom < box.getMinZoom())
147: return;
148: JComponent component;
149: if (box.getFieldId() == null)
150: component = new JLabel(Messages.getString(box
151: .getTextId()));
152: else {
153: if (editor && !box.getField().isReadOnly()) {
154: component = new ChangeAwareTextField();
155: component.setBorder(null);
156: //component.setOpaque(false);
157: } else
158: component = new JLabel();
159:
160: //if (box.getRow()==1&&!editor) ((JLabel)component).setHorizontalAlignment(SwingConstants.CENTER);
161: //bug workaround, not possible to center with classic method when rowSpan>1
162:
163: fieldComponents.put(box.getFieldId(), component);
164: }
165: Font font = formBoxLayout.getFont(box.getFont());
166: if (font != null)
167: component.setFont(font);
168: builder.add(component, (box.getAlignment() == null) ? cc
169: .xywh(box.getColumn(), box.getRow(), box
170: .getColumnSpan(), box.getRowSpan()) : cc
171: .xywh(box.getColumn(), box.getRow(), box
172: .getColumnSpan(), box.getRowSpan(), box
173: .getAlignment()));
174: }
175: }
176:
177: public Component getComponent(String fieldId) {
178: return (Component) fieldComponents.get(fieldId);
179: }
180:
181: public void setFields(Node node, NodeModel model) {
182: for (Iterator i = fieldComponents.keySet().iterator(); i
183: .hasNext();) {
184: String fieldId = (String) i.next();
185: Field field = Configuration.getFieldFromId(fieldId);
186: Object value = field.getValue(node, model, null);
187:
188: String stringValue = "";
189: if (value != null)
190: stringValue = FieldConverter.toString(value);
191: Object textComp = fieldComponents.get(fieldId);
192: if (textComp instanceof JLabel)
193: ((JLabel) textComp).setText(stringValue);
194: else {
195: ((ChangeAwareTextField) textComp).setText(stringValue);
196: ((ChangeAwareComponent) textComp).resetChange();
197: }
198: }
199: }
200:
201: public List getChange() {
202: ArrayList change = new ArrayList();
203: for (Iterator i = fieldComponents.keySet().iterator(); i
204: .hasNext();) {
205: String fieldId = (String) i.next();
206: Object component = fieldComponents.get(fieldId);
207: if (component instanceof ChangeAwareComponent
208: && ((ChangeAwareComponent) component).hasChanged()) {
209: String stringValue;
210: if (component instanceof JTextField)
211: stringValue = ((JTextField) component).getText();
212: //hangle other components here
213: else
214: continue;
215:
216: Field field = Configuration.getFieldFromId(fieldId);
217: try {
218: Object value = FieldConverter.fromString(
219: stringValue, field.getDisplayType());
220: change.add(new FieldChange(field, value));
221: } catch (Exception e) {
222: Alert.error(e.getMessage());
223: }
224: }
225: }
226: return change;
227: }
228:
229: void paintSelectedBars(Graphics2D g2, double width, double height) {
230: for (Iterator i = selectedFormats.iterator(); i.hasNext();) {
231: BarFormat format = (BarFormat) i.next();
232: if (format.getMiddle() != null)
233: format.getMiddle().draw(g2, width, height, 0,
234: +height / 2, texture);
235: if (format.getStart() != null)
236: format.getStart().draw(g2, width, height, 0,
237: +height / 2, texture);
238: if (format.getEnd() != null)
239: format.getEnd().draw(g2, width, height, 0, +height / 2,
240: texture);
241: }
242:
243: }
244:
245: public void paint(Graphics g) {
246: /*CommonGraphCell cell=(CommonGraphCell)view.getCell();
247: if (cell.getNode().isVoid()) return;*/
248:
249: Graphics2D g2 = (Graphics2D) g;
250: Dimension d = getSize();
251: double w = d.getWidth();
252: double h = d.getHeight();
253:
254: paintSelectedBars(g2, w - 1, h - 1);
255: // ImageIcon link=IconManager.getIcon("common.link.image");
256: // g2.drawImage(link.getImage(),(int)(w-link.getIconWidth()),(int)(h-link.getIconHeight()),this);
257: //x=w and y=h are outside
258:
259: try {
260: //if (preview && !isDoubleBuffered)
261: // setOpaque(false);
262: super .paint(g);
263: //paintSelectionBorder(g);
264: } catch (IllegalArgumentException e) {
265: // JDK Bug: Zero length string passed to TextLayout constructor
266: }
267: }
268:
269: /**
270: * Provided for subclassers to paint a selection border.
271: */
272: /*protected void paintSelectionBorder(Graphics g) {
273: ((Graphics2D) g).setStroke(GraphConstants.SELECTION_STROKE);
274: if (childrenSelected)
275: g.setColor(graph.getGridColor());
276: else if (hasFocus && selected)
277: g.setColor(graph.getLockedHandleColor());
278: else if (selected)
279: g.setColor(graph.getHighlightColor());
280: if (childrenSelected || selected) {
281: Dimension d = getSize();
282: g.drawRect(0, 0, d.width - 1, d.height - 1);
283: }
284: }*/
285: /**
286: * Overridden for performance reasons.
287: * See the <a href="#override">Implementation Note</a>
288: * for more information.
289: */
290: protected void firePropertyChange(String propertyName,
291: Object oldValue, Object newValue) {
292: if (propertyName == "text")
293: super .firePropertyChange(propertyName, oldValue, newValue);
294: }
295:
296: /**
297: * Overridden for performance reasons.
298: * See the <a href="#override">Implementation Note</a>
299: * for more information.
300: */
301: public void firePropertyChange(String propertyName, byte oldValue,
302: byte newValue) {
303: }
304:
305: /**
306: * Overridden for performance reasons.
307: * See the <a href="#override">Implementation Note</a>
308: * for more information.
309: */
310: public void firePropertyChange(String propertyName, char oldValue,
311: char newValue) {
312: }
313:
314: /**
315: * Overridden for performance reasons.
316: * See the <a href="#override">Implementation Note</a>
317: * for more information.
318: */
319: public void firePropertyChange(String propertyName, short oldValue,
320: short newValue) {
321: }
322:
323: /**
324: * Overridden for performance reasons.
325: * See the <a href="#override">Implementation Note</a>
326: * for more information.
327: */
328: public void firePropertyChange(String propertyName, int oldValue,
329: int newValue) {
330: }
331:
332: /**
333: * Overridden for performance reasons.
334: * See the <a href="#override">Implementation Note</a>
335: * for more information.
336: */
337: public void firePropertyChange(String propertyName, long oldValue,
338: long newValue) {
339: }
340:
341: /**
342: * Overridden for performance reasons.
343: * See the <a href="#override">Implementation Note</a>
344: * for more information.
345: */
346: public void firePropertyChange(String propertyName, float oldValue,
347: float newValue) {
348: }
349:
350: /**
351: * Overridden for performance reasons.
352: * See the <a href="#override">Implementation Note</a>
353: * for more information.
354: */
355: public void firePropertyChange(String propertyName,
356: double oldValue, double newValue) {
357: }
358:
359: /**
360: * Overridden for performance reasons.
361: * See the <a href="#override">Implementation Note</a>
362: * for more information.
363: */
364: public void firePropertyChange(String propertyName,
365: boolean oldValue, boolean newValue) {
366: }
367:
368: }
|