001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.log4j.lf5.viewer.categoryexplorer;
018
019import java.awt.Component;
020import java.awt.event.MouseEvent;
021import java.util.EventObject;
022
023import javax.swing.JTable;
024import javax.swing.JTree;
025import javax.swing.event.CellEditorListener;
026import javax.swing.event.ChangeEvent;
027import javax.swing.event.EventListenerList;
028import javax.swing.table.TableCellEditor;
029import javax.swing.tree.TreeCellEditor;
030
031/**
032 * CategoryAbstractCellEditor.  Base class to handle the some common
033 * details of cell editing.
034 *
035 * @author Michael J. Sikorsky
036 * @author Robert Shaw
037 */
038
039// Contributed by ThoughtWorks Inc.
040
041public class CategoryAbstractCellEditor implements TableCellEditor, TreeCellEditor {
042  //--------------------------------------------------------------------------
043  //   Constants:
044  //--------------------------------------------------------------------------
045
046  //--------------------------------------------------------------------------
047  //   Protected Variables:
048  //--------------------------------------------------------------------------
049  protected EventListenerList _listenerList = new EventListenerList();
050  protected Object _value;
051  protected ChangeEvent _changeEvent = null;
052  protected int _clickCountToStart = 1;
053
054  //--------------------------------------------------------------------------
055  //   Private Variables:
056  //--------------------------------------------------------------------------
057
058  //--------------------------------------------------------------------------
059  //   Constructors:
060  //--------------------------------------------------------------------------
061
062  //--------------------------------------------------------------------------
063  //   Public Methods:
064  //--------------------------------------------------------------------------
065
066  public Object getCellEditorValue() {
067    return _value;
068  }
069
070  public void setCellEditorValue(Object value) {
071    _value = value;
072  }
073
074  public void setClickCountToStart(int count) {
075    _clickCountToStart = count;
076  }
077
078  public int getClickCountToStart() {
079    return _clickCountToStart;
080  }
081
082  public boolean isCellEditable(EventObject anEvent) {
083    if (anEvent instanceof MouseEvent) {
084      if (((MouseEvent) anEvent).getClickCount() < _clickCountToStart) {
085        return false;
086      }
087    }
088    return true;
089  }
090
091  public boolean shouldSelectCell(EventObject anEvent) {
092    if (this.isCellEditable(anEvent)) {
093      if (anEvent == null ||
094          ((MouseEvent) anEvent).getClickCount() >= _clickCountToStart) {
095        return true;
096      }
097    }
098    return false;
099  }
100
101  public boolean stopCellEditing() {
102    fireEditingStopped();
103    return true;
104  }
105
106  public void cancelCellEditing() {
107    fireEditingCanceled();
108  }
109
110  public void addCellEditorListener(CellEditorListener l) {
111    _listenerList.add(CellEditorListener.class, l);
112  }
113
114  public void removeCellEditorListener(CellEditorListener l) {
115    _listenerList.remove(CellEditorListener.class, l);
116  }
117
118  public Component getTreeCellEditorComponent(
119      JTree tree, Object value,
120      boolean isSelected,
121      boolean expanded,
122      boolean leaf, int row) {
123    return null;
124  }
125
126  public Component getTableCellEditorComponent(
127      JTable table, Object value,
128      boolean isSelected,
129      int row, int column) {
130    return null;
131  }
132
133  //--------------------------------------------------------------------------
134  //   Protected Methods:
135  //--------------------------------------------------------------------------
136  protected void fireEditingStopped() {
137    Object[] listeners = _listenerList.getListenerList();
138
139    for (int i = listeners.length - 2; i >= 0; i -= 2) {
140      if (listeners[i] == CellEditorListener.class) {
141        if (_changeEvent == null) {
142          _changeEvent = new ChangeEvent(this);
143        }
144
145        ((CellEditorListener) listeners[i + 1]).editingStopped(_changeEvent);
146      }
147    }
148  }
149
150  protected void fireEditingCanceled() {
151    Object[] listeners = _listenerList.getListenerList();
152
153    for (int i = listeners.length - 2; i >= 0; i -= 2) {
154      if (listeners[i] == CellEditorListener.class) {
155        if (_changeEvent == null) {
156          _changeEvent = new ChangeEvent(this);
157        }
158
159        ((CellEditorListener) listeners[i + 1]).editingCanceled(_changeEvent);
160      }
161    }
162  }
163
164  //--------------------------------------------------------------------------
165  //   Private Methods:
166  //--------------------------------------------------------------------------
167
168  //--------------------------------------------------------------------------
169  //   Nested Top-Level Classes or Interfaces:
170  //--------------------------------------------------------------------------
171
172}