001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mappercore.vertexitemeditor;
021:
022: import java.awt.Component;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.FocusEvent;
026: import java.awt.event.FocusListener;
027: import java.awt.event.KeyEvent;
028: import java.util.ArrayList;
029: import java.util.List;
030: import javax.swing.AbstractAction;
031: import javax.swing.Action;
032: import javax.swing.ActionMap;
033: import javax.swing.InputMap;
034: import javax.swing.JTextField;
035: import javax.swing.KeyStroke;
036: import javax.swing.event.CellEditorListener;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.tree.TreePath;
039: import org.netbeans.modules.soa.mappercore.Mapper;
040: import org.netbeans.modules.soa.mappercore.VertexItemEditor;
041: import org.netbeans.modules.soa.mappercore.model.VertexItem;
042: import org.netbeans.modules.soa.mappercore.utils.MetalTextFieldBorder;
043:
044: /**
045: *
046: * @author anjeleevich
047: */
048: public abstract class AbstractTextVertexItemEditor extends JTextField
049: implements FocusListener, ActionListener, VertexItemEditor {
050: private List<CellEditorListener> listeners = null;
051:
052: public AbstractTextVertexItemEditor() {
053: MetalTextFieldBorder.installIfItIsNeeded(this );
054:
055: addFocusListener(this );
056: addActionListener(this );
057:
058: Action cancelAction = new AbstractAction() {
059: public void actionPerformed(ActionEvent e) {
060: fireEditingCanceled();
061: }
062: };
063:
064: InputMap inputMap = getInputMap();
065: ActionMap actionMap = getActionMap();
066:
067: inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
068: "cancel-editing-action");
069: actionMap.put("cancel-editing-action", cancelAction);
070: }
071:
072: public void addEditorListener(CellEditorListener listener) {
073: if (listeners == null) {
074: listeners = new ArrayList<CellEditorListener>();
075: }
076: listeners.add(listener);
077: }
078:
079: public void removeEditorListener(CellEditorListener listener) {
080: if (listeners != null) {
081: int index = listeners.lastIndexOf(listener);
082: if (index >= 0) {
083: listeners.remove(index);
084: if (listeners.isEmpty()) {
085: listeners = null;
086: }
087: }
088: }
089: }
090:
091: protected CellEditorListener[] getEditorListeners() {
092: return (listeners == null) ? null : listeners
093: .toArray(new CellEditorListener[listeners.size()]);
094: }
095:
096: protected void fireEditingStopped() {
097: CellEditorListener[] listeners = getEditorListeners();
098:
099: if (listeners != null) {
100: ChangeEvent event = new ChangeEvent(this );
101: for (int i = listeners.length - 1; i >= 0; i--) {
102: listeners[i].editingStopped(event);
103: }
104: }
105: }
106:
107: protected void fireEditingCanceled() {
108: CellEditorListener[] listeners = getEditorListeners();
109:
110: if (listeners != null) {
111: ChangeEvent event = new ChangeEvent(this );
112: for (int i = listeners.length - 1; i >= 0; i--) {
113: listeners[i].editingCanceled(event);
114: }
115: }
116: }
117:
118: public void focusGained(FocusEvent e) {
119: }
120:
121: public void focusLost(FocusEvent e) {
122: if (getParent() != null) {
123: fireEditingStopped();
124: }
125: }
126:
127: public void actionPerformed(ActionEvent e) {
128: fireEditingStopped();
129: }
130:
131: public abstract Component getVertexItemEditorComponent(
132: Mapper mapper, TreePath treePath, VertexItem vertexItem);
133:
134: public abstract Object getVertexItemEditorValue();
135:
136: public void stopVertexItemEditing() {
137: fireEditingStopped();
138: }
139:
140: public void cancelVertexItemEditing() {
141: fireEditingCanceled();
142: }
143: }
|