|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing.glass; |
|
27 |
|
28 import java.awt.*; |
|
29 import javax.swing.*; |
|
30 import javax.swing.border.*; |
|
31 import org.jdesktop.animation.timing.Animator; |
|
32 import com.oracle.solaris.vp.util.misc.finder.Finder; |
|
33 import com.oracle.solaris.vp.util.swing.*; |
|
34 |
|
35 @SuppressWarnings({"serial"}) |
|
36 public class BusyGlassPane extends BlockingGlassPane { |
|
37 // |
|
38 // Static data |
|
39 // |
|
40 |
|
41 private static final int _DELAY = 2000; |
|
42 private static final String _MESSAGE = Finder.getString("busy.message"); |
|
43 |
|
44 // |
|
45 // Instance data |
|
46 // |
|
47 |
|
48 private String message; |
|
49 private JProgressBar progressBar; |
|
50 private WrappingText messageLabel; |
|
51 private FadablePanel fadePanel; |
|
52 private SettingsPanel mainPanel; |
|
53 private JButton[] buttons; |
|
54 private Action[] actions; |
|
55 |
|
56 // |
|
57 // Constructors |
|
58 // |
|
59 |
|
60 public BusyGlassPane() { |
|
61 progressBar = new JProgressBar(); |
|
62 progressBar.setIndeterminate(true); |
|
63 progressBar.setOpaque(false); |
|
64 progressBar.setBorderPainted(false); |
|
65 |
|
66 // A WrappingText must be used here instead of a FlowTextArea because |
|
67 // the latter requires a pack() before it can correctly return its |
|
68 // preferred size (thus two pack()s are needed for it to display |
|
69 // at the correct size). |
|
70 messageLabel = new WrappingText(); |
|
71 |
|
72 createMainPanel(); |
|
73 |
|
74 JPanel shadowPanel = new ShadowPanel(); |
|
75 shadowPanel.setOpaque(false); |
|
76 shadowPanel.setLayout(new BorderLayout()); |
|
77 shadowPanel.add(mainPanel, BorderLayout.CENTER); |
|
78 int gap = GUIUtil.getGap(); |
|
79 Border border = BorderFactory.createEmptyBorder(gap, gap, gap, gap); |
|
80 shadowPanel.setBorder(border); |
|
81 |
|
82 fadePanel = new FadablePanel(shadowPanel); |
|
83 fadePanel.setSyncFadeWithVisibility(true); |
|
84 |
|
85 setLayout(new GridBagLayout()); |
|
86 add(fadePanel); |
|
87 |
|
88 reset(); |
|
89 } |
|
90 |
|
91 // |
|
92 // SimpleGlassPane methods |
|
93 // |
|
94 |
|
95 @Override |
|
96 public void componentHidden() { |
|
97 reset(); |
|
98 } |
|
99 |
|
100 // |
|
101 // BusyGlassPane methods |
|
102 // |
|
103 |
|
104 public Action[] getActions() { |
|
105 return actions; |
|
106 } |
|
107 |
|
108 public int getDelay() { |
|
109 return fadePanel.getAnimator().getStartDelay(); |
|
110 } |
|
111 |
|
112 public String getMessage() { |
|
113 return message; |
|
114 } |
|
115 |
|
116 public JProgressBar getProgressBar() { |
|
117 return progressBar; |
|
118 } |
|
119 |
|
120 protected void reset() { |
|
121 setActions(); |
|
122 setDelay(_DELAY); |
|
123 setMessage(null); |
|
124 progressBar.setIndeterminate(true); |
|
125 fadePanel.setFaded(true, false); |
|
126 } |
|
127 |
|
128 public void setActions(Action... actions) { |
|
129 SettingsButtonBar buttonBar = mainPanel.getButtonBar(); |
|
130 if (buttons != null) { |
|
131 for (JButton button : buttons) { |
|
132 buttonBar.remove(button); |
|
133 } |
|
134 } |
|
135 |
|
136 buttons = new JButton[actions.length]; |
|
137 for (int i = 0; i < actions.length; i++) { |
|
138 buttons[i] = new JButton(actions[i]); |
|
139 buttonBar.add(buttons[i]); |
|
140 } |
|
141 |
|
142 this.actions = actions; |
|
143 } |
|
144 |
|
145 public void setDelay(int delay) { |
|
146 Animator animator = fadePanel.getAnimator(); |
|
147 animator.stop(); |
|
148 animator.setStartDelay(delay); |
|
149 } |
|
150 |
|
151 public void setMessage(String message) { |
|
152 this.message = message; |
|
153 |
|
154 String m = message == null ? _MESSAGE : message; |
|
155 messageLabel.setText(m); |
|
156 messageLabel.setPreferredWidthInColumns(25); |
|
157 messageLabel.setVisible(m != null && !m.isEmpty()); |
|
158 } |
|
159 |
|
160 // |
|
161 // Private methods |
|
162 // |
|
163 |
|
164 private void createMainPanel() { |
|
165 // Override paintComponent and set non-opaque to avoid bug wherein the |
|
166 // progress bar appears well before the rest of the panel has faded in |
|
167 mainPanel = new SettingsPanel() { |
|
168 @Override |
|
169 public void paintComponent(Graphics g) { |
|
170 int w = getWidth(); |
|
171 int h = getHeight(); |
|
172 |
|
173 g.setColor(getBackground()); |
|
174 g.fillRect(0, 0, w, h); |
|
175 } |
|
176 }; |
|
177 mainPanel.setOpaque(false); |
|
178 |
|
179 mainPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
|
180 |
|
181 Border empty = GUIUtil.getEmptyBorder(); |
|
182 Color color = ColorUtil.darker(getBackground(), .5f); |
|
183 Border compound = BorderFactory.createCompoundBorder( |
|
184 new LineBorder(color), empty); |
|
185 compound = BorderFactory.createCompoundBorder( |
|
186 compound, new EtchedBorder()); |
|
187 compound = BorderFactory.createCompoundBorder( |
|
188 compound, empty); |
|
189 mainPanel.setBorder(compound); |
|
190 |
|
191 JPanel helpPane = mainPanel.getHelpPane(); |
|
192 helpPane.removeAll(); |
|
193 helpPane.add(progressBar, BorderLayout.CENTER); |
|
194 |
|
195 mainPanel.getContentPane().add(messageLabel, BorderLayout.CENTER); |
|
196 } |
|
197 } |