KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > viewer > LF5SwingUtils


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5.viewer;
17
18 import javax.swing.*;
19 import javax.swing.table.TableModel JavaDoc;
20 import java.awt.*;
21
22 /**
23  * Provides methods to accomplish common yet non-trivial tasks
24  * with Swing. Obvious implementations of these methods have been
25  * tried and failed.
26  *
27  * @author Richard Wan
28  */

29
30 // Contributed by ThoughtWorks Inc.
31

32 public class LF5SwingUtils {
33   //--------------------------------------------------------------------------
34
// Constants:
35
//--------------------------------------------------------------------------
36

37   //--------------------------------------------------------------------------
38
// Protected Variables:
39
//--------------------------------------------------------------------------
40

41   //--------------------------------------------------------------------------
42
// Private Variables:
43
//--------------------------------------------------------------------------
44

45   //--------------------------------------------------------------------------
46
// Constructors:
47
//--------------------------------------------------------------------------
48

49   //--------------------------------------------------------------------------
50
// Public Methods:
51
//--------------------------------------------------------------------------
52

53   /**
54    * Selects a the specified row in the specified JTable and scrolls
55    * the specified JScrollpane to the newly selected row. More importantly,
56    * the call to repaint() delayed long enough to have the table
57    * properly paint the newly selected row which may be offscre
58    * @param table should belong to the specified JScrollPane
59    */

60   public static void selectRow(int row, JTable table, JScrollPane pane) {
61     if (table == null || pane == null) {
62       return;
63     }
64     if (contains(row, table.getModel()) == false) {
65       return;
66     }
67     moveAdjustable(row * table.getRowHeight(), pane.getVerticalScrollBar());
68     selectRow(row, table.getSelectionModel());
69     // repaint must be done later because moveAdjustable
70
// posts requests to the swing thread which must execute before
71
// the repaint logic gets executed.
72
repaintLater(table);
73   }
74
75   /**
76    * Makes the specified Adjustable track if the view area expands and
77    * the specified Adjustable is located near the of the view.
78    */

79   public static void makeScrollBarTrack(Adjustable scrollBar) {
80     if (scrollBar == null) {
81       return;
82     }
83     scrollBar.addAdjustmentListener(new TrackingAdjustmentListener());
84   }
85
86   /**
87    * Makes the vertical scroll bar of the specified JScrollPane
88    * track if the view expands (e.g. if rows are added to an underlying
89    * table).
90    */

91   public static void makeVerticalScrollBarTrack(JScrollPane pane) {
92     if (pane == null) {
93       return;
94     }
95     makeScrollBarTrack(pane.getVerticalScrollBar());
96   }
97
98   //--------------------------------------------------------------------------
99
// Protected Methods:
100
//--------------------------------------------------------------------------
101
protected static boolean contains(int row, TableModel JavaDoc model) {
102     if (model == null) {
103       return false;
104     }
105     if (row < 0) {
106       return false;
107     }
108     if (row >= model.getRowCount()) {
109       return false;
110     }
111     return true;
112   }
113
114   protected static void selectRow(int row, ListSelectionModel model) {
115     if (model == null) {
116       return;
117     }
118     model.setSelectionInterval(row, row);
119   }
120
121   protected static void moveAdjustable(int location, Adjustable scrollBar) {
122     if (scrollBar == null) {
123       return;
124     }
125     scrollBar.setValue(location);
126   }
127
128   /**
129    * Work around for JTable/viewport bug.
130    * @link http://developer.java.sun.com/developer/bugParade/bugs/4205145.html
131    */

132   protected static void repaintLater(final JComponent component) {
133     SwingUtilities.invokeLater(new Runnable JavaDoc() {
134       public void run() {
135         component.repaint();
136       }
137     });
138   }
139   //--------------------------------------------------------------------------
140
// Private Methods:
141
//--------------------------------------------------------------------------
142

143   //--------------------------------------------------------------------------
144
// Nested Top-Level Classes or Interfaces
145
//--------------------------------------------------------------------------
146
}
147
148
Popular Tags