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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils.helper.swing;
038:
039: import java.awt.event.KeyEvent;
040: import java.awt.event.MouseAdapter;
041: import java.awt.event.MouseEvent;
042: import java.awt.event.MouseListener;
043: import javax.swing.JTable;
044: import javax.swing.ListSelectionModel;
045: import javax.swing.tree.TreePath;
046: import org.netbeans.installer.utils.helper.ErrorLevel;
047: import org.netbeans.installer.utils.LogManager;
048:
049: /**
050: *
051: * @author Kirill Sorokin
052: */
053: public class NbiTreeTable extends JTable {
054: private NbiTreeTableModel model;
055:
056: private NbiTreeTableColumnRenderer treeRenderer;
057:
058: private boolean mousePressedEventConsumed = false;
059:
060: public NbiTreeTable(final NbiTreeTableModel model) {
061: this .model = model;
062:
063: setTreeColumnRenderer(new NbiTreeTableColumnRenderer(this ));
064: model.setTree(treeRenderer);
065:
066: super .setModel(model);
067:
068: getColumnModel().getColumn(model.getTreeColumnIndex())
069: .setCellRenderer(treeRenderer);
070:
071: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
072: }
073:
074: public void updateUI() {
075: super .updateUI();
076:
077: if (treeRenderer != null) {
078: treeRenderer.updateUI();
079: }
080: }
081:
082: public void setRowHeight(int height) {
083: super .setRowHeight(height);
084:
085: if (treeRenderer != null) {
086: treeRenderer.setRowHeight(height);
087: }
088: }
089:
090: public NbiTreeTableModel getModel() {
091: return model;
092: }
093:
094: public NbiTreeTableColumnRenderer getTreeColumnRenderer() {
095: return treeRenderer;
096: }
097:
098: public void setTreeColumnRenderer(
099: NbiTreeTableColumnRenderer renderer) {
100: treeRenderer = renderer;
101:
102: model.setTree(renderer);
103: model.setTreeModel(renderer.getModel());
104:
105: treeRenderer.setRowHeight(getRowHeight());
106: }
107:
108: public NbiTreeTableColumnCellRenderer getTreeColumnCellRenderer() {
109: return treeRenderer.getTreeColumnCellRenderer();
110: }
111:
112: public void setTreeColumnCellRenderer(
113: NbiTreeTableColumnCellRenderer renderer) {
114: treeRenderer.setTreeColumnCellRenderer(renderer);
115: }
116:
117: protected void processMouseEvent(MouseEvent event) {
118: int column = columnAtPoint(event.getPoint());
119: int row = rowAtPoint(event.getPoint());
120:
121: if ((event.getID() == MouseEvent.MOUSE_RELEASED)
122: && mousePressedEventConsumed) {
123: mousePressedEventConsumed = false;
124: event.consume();
125: return;
126: }
127:
128: if (mouseEventHitTreeHandle(event)) {
129: mousePressedEventConsumed = true;
130: event.consume();
131: sendTreeHandleEvents(event);
132: return;
133: }
134:
135: mousePressedEventConsumed = false;
136: super .processMouseEvent(event);
137: }
138:
139: protected void processKeyEvent(KeyEvent event) {
140: if (event.getID() == KeyEvent.KEY_RELEASED) {
141: int row = getSelectedRow();
142:
143: if (row != -1) {
144: if (event.getKeyCode() == KeyEvent.VK_LEFT) {
145: if (treeRenderer.isExpanded(row)) {
146: treeRenderer.collapseRow(row);
147: } else {
148: int parentRow = treeRenderer
149: .getRowForPath(treeRenderer
150: .getPathForRow(row)
151: .getParentPath());
152:
153: treeRenderer.collapseRow(parentRow);
154: getSelectionModel().setSelectionInterval(
155: parentRow, parentRow);
156: }
157: event.consume();
158: return;
159: }
160: if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
161: if (treeRenderer.isCollapsed(row)) {
162: treeRenderer.expandRow(row);
163: }
164: event.consume();
165: return;
166: }
167: }
168: }
169:
170: super .processKeyEvent(event);
171: }
172:
173: private boolean mouseEventHitTreeHandle(MouseEvent event) {
174: if ((event.getID() != MouseEvent.MOUSE_PRESSED)) {
175: return false;
176: }
177:
178: int column = columnAtPoint(event.getPoint());
179: int row = rowAtPoint(event.getPoint());
180:
181: if (column == model.getTreeColumnIndex()) {
182: MouseEvent mousePressed = new MouseEvent(treeRenderer,
183: MouseEvent.MOUSE_PRESSED, event.getWhen(), event
184: .getModifiers(), event.getX()
185: - getCellRect(row, column, true).x, event
186: .getY(), event.getClickCount(), event
187: .isPopupTrigger());
188: MouseEvent mouseReleased = new MouseEvent(treeRenderer,
189: MouseEvent.MOUSE_RELEASED, event.getWhen(), event
190: .getModifiers(), event.getX()
191: - getCellRect(row, column, true).x, event
192: .getY(), event.getClickCount(), event
193: .isPopupTrigger());
194:
195: TreePath targetPath = treeRenderer.getPathForRow(row);
196:
197: boolean currentState = treeRenderer.isExpanded(targetPath);
198:
199: // dispatch the event and see whether the node changed its state
200: model.consumeNextExpansionEvent();
201: treeRenderer.dispatchEvent(mousePressed);
202: treeRenderer.dispatchEvent(mouseReleased);
203:
204: if (treeRenderer.isExpanded(targetPath) == currentState) {
205: model.cancelConsume();
206: return false;
207: } else {
208: model.consumeNextExpansionEvent();
209: treeRenderer.dispatchEvent(mousePressed);
210: treeRenderer.dispatchEvent(mouseReleased);
211: return true;
212: }
213: } else {
214: return false;
215: }
216: }
217:
218: private void sendTreeHandleEvents(MouseEvent event) {
219: int column = model.getTreeColumnIndex();
220: int row = rowAtPoint(event.getPoint());
221:
222: MouseEvent mousePressed = new MouseEvent(treeRenderer,
223: MouseEvent.MOUSE_PRESSED, event.getWhen(), event
224: .getModifiers(), event.getX()
225: - getCellRect(row, column, true).x, event
226: .getY(), event.getClickCount(), event
227: .isPopupTrigger());
228: MouseEvent mouseReleased = new MouseEvent(treeRenderer,
229: MouseEvent.MOUSE_RELEASED, event.getWhen(), event
230: .getModifiers(), event.getX()
231: - getCellRect(row, column, true).x, event
232: .getY(), event.getClickCount(), event
233: .isPopupTrigger());
234:
235: treeRenderer.dispatchEvent(mousePressed);
236: treeRenderer.dispatchEvent(mouseReleased);
237: }
238: }
|