001: package org.enhydra.jawe.base.panel.panels;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Vector;
009:
010: import javax.swing.JScrollPane;
011: import javax.swing.JTable;
012: import javax.swing.ListSelectionModel;
013: import javax.swing.table.DefaultTableModel;
014: import javax.swing.table.TableColumn;
015:
016: import org.enhydra.jawe.base.panel.PanelContainer;
017: import org.enhydra.jawe.base.panel.PanelSettings;
018: import org.enhydra.shark.xpdl.XMLCollection;
019: import org.enhydra.shark.xpdl.XMLComplexElement;
020: import org.enhydra.shark.xpdl.XMLElement;
021:
022: /**
023: * Creates a table panel.
024: * @author Sasa Bojanic
025: * @author Zoran Milakovic
026: * @author Miroslav Popov
027: */
028: public class XMLSimpleTablePanel extends XMLBasicPanel {
029:
030: protected static Dimension miniTableDimension = new Dimension(450,
031: 125);
032: protected static Dimension smallTableDimension = new Dimension(450,
033: 200);
034: protected static Dimension mediumTableDimension = new Dimension(
035: 550, 200);
036: protected static Dimension largeTableDimension = new Dimension(650,
037: 200);
038:
039: protected JTable allItems;
040:
041: protected Vector columnNames;
042: protected List columnsToShow;
043:
044: public XMLSimpleTablePanel(PanelContainer pc,
045: XMLCollection myOwner, List columnsToShow,
046: List elementsToShow, String title, boolean hasBorder,
047: boolean hasEmptyBorder, boolean automaticWidth) {
048: super (pc, myOwner, title, true, hasBorder, hasEmptyBorder);
049:
050: columnNames = getColumnNames(columnsToShow);
051: this .columnsToShow = columnsToShow;
052: allItems = new JTable(new Vector(), columnNames) {
053: public boolean isCellEditable(int row, int col) {
054: return false;
055: }
056: };
057:
058: Color bkgCol = new Color(245, 245, 245);
059: if (pc.getSettings() instanceof PanelSettings) {
060: bkgCol = ((PanelSettings) pc.getSettings())
061: .getBackgroundColor();
062: }
063: allItems.setBackground(bkgCol);
064:
065: setupTable(automaticWidth);
066: fillTableContent(elementsToShow);
067:
068: add(createScrollPane());
069:
070: }
071:
072: public JTable getTable() {
073: return allItems;
074: }
075:
076: public XMLElement getSelectedElement() {
077: int row = allItems.getSelectedRow();
078: if (row >= 0) {
079: return (XMLElement) allItems.getValueAt(row, 0);
080: }
081: return null;
082:
083: }
084:
085: public void addRow(XMLElement e) {
086: int rowpos = allItems.getRowCount();
087: DefaultTableModel dtm = (DefaultTableModel) allItems.getModel();
088: Vector v = getRow(e);
089: dtm.insertRow(rowpos, v);
090: }
091:
092: public void removeRow(XMLElement e) {
093: int row = getElementRow(e);
094: if (row >= 0) {
095: DefaultTableModel dtm = (DefaultTableModel) allItems
096: .getModel();
097: dtm.removeRow(row);
098: }
099: }
100:
101: public List getElements() {
102: List els = new ArrayList();
103: for (int i = 0; i < allItems.getRowCount(); i++) {
104: els.add(allItems.getValueAt(i, 0));
105: }
106: return els;
107: }
108:
109: protected Vector getColumnNames(List columnsToShow) {
110: // creating a table which do not allow cell editing
111: Vector cnames = new Vector();
112: cnames.add("Object");
113: XMLElement cel = ((XMLCollection) getOwner())
114: .generateNewElement();
115: if (cel instanceof XMLComplexElement) {
116: Iterator it = columnsToShow.iterator();
117: while (it.hasNext()) {
118: String elemName = (String) it.next();
119: XMLElement el = ((XMLComplexElement) cel).get(elemName);
120: if (el != null) {
121: cnames.add(pc.getLabelGenerator().getLabel(el));
122: } else {
123: it.remove();
124: }
125: }
126: } else {
127: cnames.add(pc.getLabelGenerator().getLabel(cel));
128: }
129: return cnames;
130: }
131:
132: protected void setupTable(boolean automaticWidth) {
133: TableColumn column;
134: // setting the first column (object column) to be invisible
135: column = allItems.getColumnModel().getColumn(0);
136: column.setMinWidth(0);
137: column.setMaxWidth(0);
138: column.setPreferredWidth(0);
139: column.setResizable(false);
140: // setting fields that will not be displayed within the table
141:
142: // setting some table properties
143: allItems.setColumnSelectionAllowed(false);
144: allItems.setRowSelectionAllowed(true);
145: allItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
146: allItems.getTableHeader().setReorderingAllowed(false);
147:
148: Dimension tDim;
149: int noOfVisibleColumns = columnNames.size() - 1;
150: if (noOfVisibleColumns <= 3) {
151: tDim = new Dimension(smallTableDimension);
152: } else if (noOfVisibleColumns <= 5) {
153: tDim = new Dimension(mediumTableDimension);
154: } else {
155: tDim = new Dimension(largeTableDimension);
156: }
157:
158: if (automaticWidth) {
159: tDim.width = allItems.getPreferredScrollableViewportSize().width;
160: }
161: allItems
162: .setPreferredScrollableViewportSize(new Dimension(tDim));
163:
164: }
165:
166: protected void fillTableContent(List elementsToShow) {
167: DefaultTableModel dtm = (DefaultTableModel) allItems.getModel();
168: Iterator it = elementsToShow.iterator();
169:
170: while (it.hasNext()) {
171: XMLElement elem = (XMLElement) it.next();
172: Vector v = getRow(elem);
173: dtm.addRow(v);
174: }
175: }
176:
177: protected Vector getRow(XMLElement elem) {
178: Vector v = new Vector();
179: if (elem instanceof XMLComplexElement) {
180: Iterator itAllElems = columnsToShow.iterator();
181: v = new Vector();
182: XMLComplexElement cmel = (XMLComplexElement) elem;
183: while (itAllElems.hasNext()) {
184: String elName = (String) itAllElems.next();
185: XMLElement el = cmel.get(elName);
186: if (el != null) {
187: v.add(new XMLElementView(pc, el,
188: XMLElementView.TOVALUE));
189: }
190: }
191: } else {
192: v.add(new XMLElementView(pc, elem, XMLElementView.TOVALUE));
193: }
194: v.add(0, elem);
195: return v;
196: }
197:
198: protected JScrollPane createScrollPane() {
199: // creates panel
200: JScrollPane allItemsPane = new JScrollPane();
201: allItemsPane.setViewportView(allItems);
202: return allItemsPane;
203: }
204:
205: protected int getElementRow(XMLElement el) {
206: int row = -1;
207: for (int i = 0; i < allItems.getRowCount(); i++) {
208: XMLElement toCompare = (XMLElement) allItems.getValueAt(i,
209: 0);
210: if (el == toCompare) {
211: row = i;
212: break;
213: }
214: }
215: return row;
216: }
217:
218: public void cleanup() {
219: }
220: }
|