001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.awt.Component;
036: import java.awt.Dimension;
037: import java.awt.event.MouseEvent;
038: import java.util.EventObject;
039:
040: import javax.swing.DefaultCellEditor;
041: import javax.swing.JLabel;
042: import javax.swing.JTextField;
043: import javax.swing.JTree;
044: import javax.swing.SwingUtilities;
045: import javax.swing.event.CellEditorListener;
046: import javax.swing.tree.TreeCellEditor;
047:
048: /**
049: * Implements a tree cell editor for the Layer tree.
050: */
051:
052: public class LayerTreeCellEditor implements TreeCellEditor {
053: private JTextField textField = new JTextField();
054: private DefaultCellEditor editor = new DefaultCellEditor(textField);
055: private JTree tree;
056:
057: public LayerTreeCellEditor(JTree tree) {
058: this .tree = tree;
059:
060: //A font that is reasonable for both categories and layers. [Jon Aquino]
061: textField.setFont(new JLabel().getFont());
062: }
063:
064: private void changeWidthUsing(JTree tree, int row) {
065: //Make editor as wide as possible, so user input doesn't get chopped off. [Jon Aquino]
066: int width = (int) (tree.getWidth() - tree.getUI()
067: .getPathBounds(tree,
068: tree.getUI().getPathForRow(tree, row))
069: .getLocation().getX());
070: textField.setPreferredSize(new Dimension(width, (int) textField
071: .getPreferredSize().getHeight()));
072: }
073:
074: public Component getTreeCellEditorComponent(JTree tree,
075: Object value, boolean isSelected, boolean expanded,
076: boolean leaf, int row) {
077: changeWidthUsing(tree, row);
078:
079: return editor.getTreeCellEditorComponent(tree, value,
080: isSelected, expanded, leaf, row);
081: }
082:
083: public Object getCellEditorValue() {
084: return editor.getCellEditorValue();
085: }
086:
087: public boolean isCellEditable(EventObject anEvent) {
088: MouseEvent e = (MouseEvent) anEvent;
089: if (SwingUtilities.isRightMouseButton(e)) {
090: return false;
091: }
092: return editor.isCellEditable(anEvent);
093: }
094:
095: public boolean shouldSelectCell(EventObject anEvent) {
096: return false;
097: }
098:
099: public boolean stopCellEditing() {
100: return editor.stopCellEditing();
101: }
102:
103: public void cancelCellEditing() {
104: editor.cancelCellEditing();
105: }
106:
107: public void addCellEditorListener(CellEditorListener l) {
108: editor.addCellEditorListener(l);
109: }
110:
111: public void removeCellEditorListener(CellEditorListener l) {
112: editor.removeCellEditorListener(l);
113: }
114: }
|