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
20
21
22
23
24
25 public final class GuiUtil {
26
27 private static final Log LOG = LogFactory.getLog(GuiUtil.class);
28
29
30
31
32 private GuiUtil() {
33 super();
34 }
35
36
37
38
39
40
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
57
58
59
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
73
74
75
76
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
96
97
98
99
100
101 public static Frame findOwnerFrame(Object obj) {
102 return (obj instanceof Component) ? findComponentOwnerFrame((Component)obj) : findComponentOwnerFrame(null);
103 }
104
105
106
107
108
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) {
118 if ((col >= 0) && (row >= 0)) {
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 }