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 javax.swing.*;
20  import javax.swing.event.CellEditorListener;
21  import javax.swing.event.ChangeEvent;
22  import javax.swing.event.EventListenerList;
23  import javax.swing.table.TableCellEditor;
24  import javax.swing.tree.TreeCellEditor;
25  import java.awt.*;
26  import java.awt.event.MouseEvent;
27  import java.util.EventObject;
28  
29  /***
30   * CategoryAbstractCellEditor.  Base class to handle the some common
31   * details of cell editing.
32   *
33   * @author Michael J. Sikorsky
34   * @author Robert Shaw
35   */
36  
37  // Contributed by ThoughtWorks Inc.
38  
39  public class CategoryAbstractCellEditor implements TableCellEditor, TreeCellEditor {
40    //--------------------------------------------------------------------------
41    //   Constants:
42    //--------------------------------------------------------------------------
43  
44    //--------------------------------------------------------------------------
45    //   Protected Variables:
46    //--------------------------------------------------------------------------
47    protected EventListenerList _listenerList = new EventListenerList();
48    protected Object _value;
49    protected ChangeEvent _changeEvent = null;
50    protected int _clickCountToStart = 1;
51  
52    //--------------------------------------------------------------------------
53    //   Private Variables:
54    //--------------------------------------------------------------------------
55  
56    //--------------------------------------------------------------------------
57    //   Constructors:
58    //--------------------------------------------------------------------------
59  
60    //--------------------------------------------------------------------------
61    //   Public Methods:
62    //--------------------------------------------------------------------------
63  
64    public Object getCellEditorValue() {
65      return _value;
66    }
67  
68    public void setCellEditorValue(Object value) {
69      _value = value;
70    }
71  
72    public void setClickCountToStart(int count) {
73      _clickCountToStart = count;
74    }
75  
76    public int getClickCountToStart() {
77      return _clickCountToStart;
78    }
79  
80    public boolean isCellEditable(EventObject anEvent) {
81      if (anEvent instanceof MouseEvent) {
82        if (((MouseEvent) anEvent).getClickCount() < _clickCountToStart) {
83          return false;
84        }
85      }
86      return true;
87    }
88  
89    public boolean shouldSelectCell(EventObject anEvent) {
90      if (this.isCellEditable(anEvent)) {
91        if (anEvent == null ||
92            ((MouseEvent) anEvent).getClickCount() >= _clickCountToStart) {
93          return true;
94        }
95      }
96      return false;
97    }
98  
99    public boolean stopCellEditing() {
100     fireEditingStopped();
101     return true;
102   }
103 
104   public void cancelCellEditing() {
105     fireEditingCanceled();
106   }
107 
108   public void addCellEditorListener(CellEditorListener l) {
109     _listenerList.add(CellEditorListener.class, l);
110   }
111 
112   public void removeCellEditorListener(CellEditorListener l) {
113     _listenerList.remove(CellEditorListener.class, l);
114   }
115 
116   public Component getTreeCellEditorComponent(
117       JTree tree, Object value,
118       boolean isSelected,
119       boolean expanded,
120       boolean leaf, int row) {
121     return null;
122   }
123 
124   public Component getTableCellEditorComponent(
125       JTable table, Object value,
126       boolean isSelected,
127       int row, int column) {
128     return null;
129   }
130 
131   //--------------------------------------------------------------------------
132   //   Protected Methods:
133   //--------------------------------------------------------------------------
134   protected void fireEditingStopped() {
135     Object[] listeners = _listenerList.getListenerList();
136 
137     for (int i = listeners.length - 2; i >= 0; i -= 2) {
138       if (listeners[i] == CellEditorListener.class) {
139         if (_changeEvent == null) {
140           _changeEvent = new ChangeEvent(this);
141         }
142 
143         ((CellEditorListener) listeners[i + 1]).editingStopped(_changeEvent);
144       }
145     }
146   }
147 
148   protected void fireEditingCanceled() {
149     Object[] listeners = _listenerList.getListenerList();
150 
151     for (int i = listeners.length - 2; i >= 0; i -= 2) {
152       if (listeners[i] == CellEditorListener.class) {
153         if (_changeEvent == null) {
154           _changeEvent = new ChangeEvent(this);
155         }
156 
157         ((CellEditorListener) listeners[i + 1]).editingCanceled(_changeEvent);
158       }
159     }
160   }
161 
162   //--------------------------------------------------------------------------
163   //   Private Methods:
164   //--------------------------------------------------------------------------
165 
166   //--------------------------------------------------------------------------
167   //   Nested Top-Level Classes or Interfaces:
168   //--------------------------------------------------------------------------
169 
170 }