001: package com.xoetrope.carousel.visualizer;
002:
003: import java.awt.BorderLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import javax.swing.BorderFactory;
007: import javax.swing.JButton;
008: import javax.swing.JPanel;
009: import javax.swing.JScrollPane;
010: import javax.swing.event.ListSelectionEvent;
011: import javax.swing.event.ListSelectionListener;
012: import net.xoetrope.editor.ui.KalideoscopeToolbar;
013: import net.xoetrope.xui.data.XBaseModel;
014: import net.xoetrope.xui.data.XModel;
015:
016: /**
017: * The panel of the table data model visualization.
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.16 $</p>
023: */
024: public class XTableModelPanel extends JPanel implements ActionListener {
025: private XTableModelView table;
026: private XModel rootNode;
027: private KalideoscopeToolbar visualizerToolbar;
028: private ModelVisualiserPanel visualiserPanel;
029: private JButton addRowBtn, deleteRowBtn;
030: private JButton moveRowUpBtn, moveRowDownBtn;
031: private boolean debugView;
032:
033: //private XEditorProject editorProject;
034:
035: public XTableModelPanel(XModel node, ModelVisualiserPanel panel) {
036: rootNode = node;
037: visualiserPanel = panel;
038: debugView = panel.debugView();
039: setLayout(new BorderLayout());
040:
041: visualizerToolbar = new KalideoscopeToolbar();
042: addRowBtn = visualizerToolbar.addTool(this , "addAttribute.gif",
043: "Add", "Add row");
044: deleteRowBtn = visualizerToolbar.addTool(this ,
045: "deleteAttribute.gif", "Delete", "Delete row");
046: deleteRowBtn.setEnabled(false);
047: if (!debugView) {
048: visualizerToolbar.addSeparator();
049: moveRowUpBtn = visualizerToolbar.addTool(this ,
050: "addAttribute.gif", "MoveUp", "Move up");
051: moveRowDownBtn = visualizerToolbar.addTool(this ,
052: "deleteAttribute.gif", "MoveDown", "Move down");
053: moveRowUpBtn.setEnabled(false);
054: moveRowDownBtn.setEnabled(false);
055: }
056:
057: table = new XTableModelView();
058: table.setModel(rootNode);
059: table.setBorder(BorderFactory.createEmptyBorder());
060:
061: JScrollPane scrollPane = new JScrollPane(table);
062: add(visualizerToolbar, BorderLayout.NORTH);
063: add(scrollPane, BorderLayout.CENTER);
064:
065: table.getSelectionModel().addListSelectionListener(
066: new ListSelectionListener() {
067: public void valueChanged(ListSelectionEvent e) {
068: boolean isDbTableNodeSelected = visualiserPanel
069: .isDbTableNodeSelected();
070: boolean state = (table.getSelectedRow() != -1)
071: && (!isDbTableNodeSelected);
072: if (addRowBtn != null)
073: addRowBtn
074: .setEnabled(!isDbTableNodeSelected);
075: if (deleteRowBtn != null)
076: deleteRowBtn.setEnabled(state);
077: if (moveRowUpBtn != null)
078: moveRowUpBtn.setEnabled(state);
079: if (moveRowDownBtn != null)
080: moveRowDownBtn.setEnabled(state);
081: }
082: });
083:
084: table.update();
085: }
086:
087: public void actionPerformed(ActionEvent ae) {
088: String cmd = ae.getActionCommand();
089: if (cmd.equals("Add")) {
090: addNewRow();
091: } else if (cmd.equals("Delete")) {
092: deleteRow();
093: } else if (cmd.equals("MoveUp")) {
094: moveSelectedRow(-1);
095: } else if (cmd.equals("MoveDown")) {
096: moveSelectedRow(1);
097: }
098: }
099:
100: private int getNewRowId() {
101: int nextId = 0;
102: int rowCount = table.getRowCount();
103: int offset = (hasHeaderRow() ? 1 : 0);
104: for (int i = 0; i < rowCount; i++) {
105: XModel rowModel = rootNode.get(i + offset);
106: try {
107: int rowModelId = (new Integer(rowModel.getId()))
108: .intValue();
109: nextId = Math.max(nextId, rowModelId);
110: } catch (NumberFormatException e) {
111: }
112: }
113: return (nextId + 1);
114: }
115:
116: /**
117: * adds a new empty row to the table
118: */
119: protected void addNewRow() {
120: int newRowId = getNewRowId();
121: int colCount = table.getModel().getColumnCount();
122: XModel rowNode = (XModel) rootNode.append(String
123: .valueOf(newRowId));
124:
125: rowNode.setTagName("tr");
126: rowNode.set("");
127:
128: for (int i = 1; i <= colCount; i++) {
129: XModel ceilNode = (XModel) rowNode
130: .append(String.valueOf(i));
131: ceilNode.setTagName("td");
132: ceilNode.set("");
133: }
134: table.update();
135: refreshTree();
136: // editorProject.setModified( true );
137: }
138:
139: /**
140: * delete selected row from the table model
141: */
142: protected void deleteRow() {
143: int rowIdx = table.getSelectedRow();
144: if ((rowIdx < 0) || (rowIdx > table.getRowCount()))
145: return;
146: rowIdx += (hasHeaderRow() ? 1 : 0);
147: XModel rowModel = rootNode.get(rowIdx);
148: String rowModelId = rowModel.getId();
149:
150: if (rootNode instanceof XBaseModel)
151: ((XBaseModel) rootNode).removeChild(rowModelId);
152: else if (rootNode instanceof VisualiserDebuggerModel)
153: ((VisualiserDebuggerModel) rootNode)
154: .removeChild(rowModelId);
155:
156: table.update();
157: refreshTree();
158: // editorProject.setModified( true );
159: }
160:
161: /**
162: * move selected row by the given number of positions in a table
163: */
164: protected void moveSelectedRow(int d) {
165: int rowIdx = table.getSelectedRow();
166: int newRowIdx = rowIdx + d;
167: int offset = (hasHeaderRow() ? 1 : 0);
168: if ((newRowIdx < 0) || (newRowIdx > table.getRowCount() - 1))
169: return;
170: rowIdx += offset;
171: newRowIdx += offset;
172: XModel rowModel = rootNode.get(rowIdx);
173:
174: if (rootNode instanceof XBaseModel) {
175: ((XBaseModel) rootNode).remove(rowModel);
176: ((XBaseModel) rootNode).insertChildAt(rowModel, newRowIdx);
177: refreshTree();
178: table.update();
179: table.setSelectedRow(newRowIdx - offset);
180: //editorProject.setModified( true );
181: }
182: }
183:
184: /**
185: * checks whether table model has a table header model node
186: */
187: protected boolean hasHeaderRow() {
188: XModel row0 = rootNode.get(0);
189: return ((row0 != null) && (row0.getTagName().equals("th")));
190: }
191:
192: /**
193: * refresh tree model representation
194: */
195: protected void refreshTree() {
196: ModelVisualiserTree tree = visualiserPanel
197: .getModelVisualiserTree();
198: if (tree != null) {
199: String path = tree.getStrippedPath();
200: tree.removeTreeSelectionListener(visualiserPanel);
201: tree.removeMouseListener(visualiserPanel);
202: tree.createTreeComp(null);
203: if (path != null)
204: tree.setSelectedPath(path);
205: tree.addTreeSelectionListener(visualiserPanel);
206: tree.addMouseListener(visualiserPanel);
207: }
208: }
209:
210: }
|