001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.internal.edit;
014:
015: import javax.swing.JPanel;
016: import javax.swing.JLabel;
017: import javax.swing.JComponent;
018:
019: import javax.swing.event.ChangeListener;
020: import javax.swing.event.ChangeEvent;
021:
022: import org.rapla.framework.RaplaContext;
023: import org.rapla.framework.RaplaException;
024: import org.rapla.gui.EditComponent;
025: import org.rapla.gui.RaplaGUIComponent;
026: import org.rapla.gui.toolkit.RaplaWidget;
027: import org.rapla.components.layout.TableLayout;
028:
029: /**
030: */
031: public abstract class AbstractEditUI extends RaplaGUIComponent
032: implements EditComponent, ChangeListener, RaplaWidget {
033:
034: protected JPanel editPanel = new JPanel();
035: protected Object o;
036: protected EditField[] fields = new EditField[0];
037:
038: public AbstractEditUI(RaplaContext sm) throws RaplaException {
039: super (sm);
040: }
041:
042: protected void setFields(EditField[] fields) {
043: for (int i = 0; i < fields.length; i++) {
044: fields[i].removeChangeListener(this );
045: }
046: this .fields = fields;
047: for (int i = 0; i < fields.length; i++) {
048: fields[i].addChangeListener(this );
049: }
050: editPanel.removeAll();
051: layout();
052: editPanel.revalidate();
053: }
054:
055: protected void layout() {
056: TableLayout tableLayout = new TableLayout();
057: editPanel.setLayout(tableLayout);
058: tableLayout.insertColumn(0, 5);
059: tableLayout.insertColumn(1, TableLayout.PREFERRED);
060: tableLayout.insertColumn(2, 5);
061: tableLayout.insertColumn(3, TableLayout.FILL);
062: tableLayout.insertColumn(4, 5);
063: int variableSizedBlocks = 0;
064: for (int i = 0; i < fields.length; i++)
065: if (fields[i].isVariableSized())
066: variableSizedBlocks++;
067:
068: int row = 0;
069: for (int i = 0; i < fields.length; i++) {
070: tableLayout.insertRow(row, 5);
071: row++;
072: if (fields[i].isVariableSized())
073: tableLayout.insertRow(row,
074: 0.99 / ((double) variableSizedBlocks));
075: else
076: tableLayout.insertRow(row, TableLayout.PREFERRED);
077: if (fields[i].isBlock()) {
078: editPanel.add("1," + row + ",3," + row + ",l",
079: fields[i].getComponent());
080: } else {
081: editPanel.add("1," + row + ",l,c", new JLabel(fields[i]
082: .getName()
083: + ":"));
084: editPanel.add("3," + row + ",l,c", fields[i]
085: .getComponent());
086: }
087: row++;
088: }
089: if (variableSizedBlocks == 0) {
090: tableLayout.insertRow(row, TableLayout.FILL);
091: editPanel.add("0," + row + ",4," + row, new JLabel(""));
092: }
093: }
094:
095: public void setObject(Object o) throws RaplaException {
096: this .o = o;
097: for (int i = 0; i < fields.length; i++) {
098: fields[i].mapFrom(o);
099: }
100: }
101:
102: public Object getObject() {
103: return o;
104: }
105:
106: public boolean isBlock() {
107: return false;
108: }
109:
110: public JComponent getComponent() {
111: return editPanel;
112: }
113:
114: public void mapToObject() throws RaplaException {
115: if (o == null)
116: return;
117: for (int i = 0; i < fields.length; i++) {
118: fields[i].mapTo(o);
119: }
120: }
121:
122: public void stateChanged(ChangeEvent evt) {
123: }
124:
125: }
|