View Javadoc

1   package com.melloware.jukes.util;
2   
3   import java.awt.Component;
4   import java.awt.Cursor;
5   import java.awt.Frame;
6   import java.awt.Insets;
7   
8   import javax.swing.JScrollBar;
9   import javax.swing.JScrollPane;
10  import javax.swing.JTable;
11  import javax.swing.table.TableCellEditor;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import com.jgoodies.uif.application.Application;
17  
18  /**
19   * Swing Utilities used throughout the application.
20   * <p>
21   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
22   * @author Emil A. Lefkof III <info@melloware.com>
23   * @version 4.0
24   */
25  public final class GuiUtil {
26  
27      private static final Log LOG = LogFactory.getLog(GuiUtil.class);
28  
29      /**
30       * Private constructor, no instatiation.
31       */
32      private GuiUtil() {
33          super();
34      }
35  
36      /**
37       * Gets the preferred width of a JScrollpane.
38       * <p>
39       * @param aScrollPane the scrollpane to get the width for
40       * @return the width of the scrollpaneas an int
41       */
42      public static final int getPreferredWidth(JScrollPane aScrollPane) {
43          final JScrollBar scrollBar = aScrollPane.getVerticalScrollBar();
44          int scrollWidth = scrollBar.getUI().getPreferredSize(scrollBar).width;
45  
46          final Insets insets = aScrollPane.getInsets();
47          int insetWidth = insets.left + insets.right;
48  
49          final Component view = aScrollPane.getViewport().getView();
50          int viewWidth = view.getPreferredSize().width;
51  
52          return viewWidth + insetWidth + scrollWidth;
53      }
54  
55      /**
56       * Sets the cursor to hourglass for true and default for false.  Used for
57       * long operations such as saves.
58       * <p>
59       * @param aBusy true for busy cursor, false for default
60       */
61      public static void setBusyCursor(Component aComponent, boolean aBusy) {
62          if (aBusy) {
63              LOG.debug("CURSOR: Set to WAIT");
64              aComponent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
65          } else {
66              LOG.debug("CURSOR: Set to DEFAULT");
67              aComponent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
68          }
69      }
70  
71      /**
72       * Goes to the root of component hierarchy until finds Frame or null.
73       *
74       * @param component start of search.
75       *
76       * @return owner frame.
77       */
78      public static Frame findComponentOwnerFrame(Component component) {
79          Frame owner;
80  
81          if (component == null) {
82              owner = Application.getDefaultParentFrame();
83          } else {
84              if (component instanceof Frame) {
85                  owner = (Frame)component;
86              } else {
87                  owner = findComponentOwnerFrame(component.getParent());
88              }
89          }
90  
91          return owner;
92      }
93  
94      /**
95       * Goes to the root of component hierarchy until finds Frame or null.
96       *
97       * @param obj object to start searching.
98       *
99       * @return owner frame.
100      */
101     public static Frame findOwnerFrame(Object obj) {
102         return (obj instanceof Component) ? findComponentOwnerFrame((Component)obj) : findComponentOwnerFrame(null);
103     }
104 
105     /**
106      * Find and stop a table's editing.
107      * <p>
108      * @param aTable the JTable to stop editing
109      */
110     public static void stopTableEditing(JTable aTable) {
111 
112         TableCellEditor cellEditor = null;
113         int col = aTable.getEditingColumn();
114         int row = aTable.getEditingRow();
115 
116         cellEditor = aTable.getCellEditor();
117         if (cellEditor == null) {    // NOPMD
118             if ((col >= 0) && (row >= 0)) {    // NOPMD
119                 cellEditor = aTable.getColumnModel().getColumn(col).getCellEditor();
120                 if (cellEditor == null) {
121                     cellEditor = aTable.getDefaultEditor(aTable.getColumnClass(col));
122                 }
123             }
124         }
125         if (cellEditor != null) {
126             try {
127                 cellEditor.stopCellEditing();
128             } catch (Exception e) {
129                 LOG.warn("failed to stop cell editing " + e, e);
130             }
131         }
132     }
133 
134 }