View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.lf5.viewer.categoryexplorer;
18  
19  import java.awt.Component;
20  import java.awt.event.MouseEvent;
21  import java.util.EventObject;
22  
23  import javax.swing.JTable;
24  import javax.swing.JTree;
25  import javax.swing.event.CellEditorListener;
26  import javax.swing.event.ChangeEvent;
27  import javax.swing.event.EventListenerList;
28  import javax.swing.table.TableCellEditor;
29  import javax.swing.tree.TreeCellEditor;
30  
31  /**
32   * CategoryAbstractCellEditor.  Base class to handle the some common
33   * details of cell editing.
34   *
35   * @author Michael J. Sikorsky
36   * @author Robert Shaw
37   */
38  
39  // Contributed by ThoughtWorks Inc.
40  
41  public class CategoryAbstractCellEditor implements TableCellEditor, TreeCellEditor {
42    //--------------------------------------------------------------------------
43    //   Constants:
44    //--------------------------------------------------------------------------
45  
46    //--------------------------------------------------------------------------
47    //   Protected Variables:
48    //--------------------------------------------------------------------------
49    protected EventListenerList _listenerList = new EventListenerList();
50    protected Object _value;
51    protected ChangeEvent _changeEvent = null;
52    protected int _clickCountToStart = 1;
53  
54    //--------------------------------------------------------------------------
55    //   Private Variables:
56    //--------------------------------------------------------------------------
57  
58    //--------------------------------------------------------------------------
59    //   Constructors:
60    //--------------------------------------------------------------------------
61  
62    //--------------------------------------------------------------------------
63    //   Public Methods:
64    //--------------------------------------------------------------------------
65  
66    public Object getCellEditorValue() {
67      return _value;
68    }
69  
70    public void setCellEditorValue(Object value) {
71      _value = value;
72    }
73  
74    public void setClickCountToStart(int count) {
75      _clickCountToStart = count;
76    }
77  
78    public int getClickCountToStart() {
79      return _clickCountToStart;
80    }
81  
82    public boolean isCellEditable(EventObject anEvent) {
83      if (anEvent instanceof MouseEvent) {
84        if (((MouseEvent) anEvent).getClickCount() < _clickCountToStart) {
85          return false;
86        }
87      }
88      return true;
89    }
90  
91    public boolean shouldSelectCell(EventObject anEvent) {
92      if (this.isCellEditable(anEvent)) {
93        if (anEvent == null ||
94            ((MouseEvent) anEvent).getClickCount() >= _clickCountToStart) {
95          return true;
96        }
97      }
98      return false;
99    }
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 }