2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.progress.ui;
44
import java.awt.Color;
45
import java.awt.Component;
46
import java.awt.Cursor;
47
import java.awt.Dimension;
49
import java.awt.Image;
50
import java.awt.LayoutManager;
51
import java.awt.event.ActionEvent;
52
import java.awt.event.FocusListener;
53
import java.awt.event.KeyEvent;
54
import java.awt.event.MouseAdapter;
55
import java.awt.event.MouseEvent;
56
import javax.swing.AbstractAction;
57
import javax.swing.Action;
58
import javax.swing.BorderFactory;
59
import javax.swing.ImageIcon;
60
import javax.swing.JButton;
61
import javax.swing.JLabel;
62
import javax.swing.JPanel;
63
import javax.swing.JPopupMenu;
64
import javax.swing.KeyStroke;
65
import javax.swing.UIManager;
66
import org.netbeans.progress.spi.InternalHandle;
67
import org.netbeans.progress.spi.ProgressEvent;
68
import org.openide.DialogDisplayer;
69
import org.openide.NotifyDescriptor;
70
import org.openide.util.NbBundle;
71
import org.openide.util.Utilities;
77
public class ListComponent extends JPanel {
78
private NbProgressBar bar;
79
private JLabel mainLabel;
80
private JLabel dynaLabel;
81
private JButton closeButton;
82
private InternalHandle handle;
83
private boolean watched;
84
private Action cancelAction;
85
private Color selectBgColor;
86
private Color selectFgColor;
87
private Color bgColor;
88
private Color fgColor;
89
// private Color dynaFgColor;
90
private int mainHeight;
91
private int dynaHeight;
93
/** Creates a new instance of ListComponent */
94
public ListComponent(InternalHandle hndl) {
96
setRequestFocusEnabled(true);
97
// setVerifyInputWhenFocusTarget(false);
98
mainLabel = new JLabel();
99
dynaLabel = new JLabel();
100
// in gtk, the panel is non-opague, meaning we cannot color background
103
dynaLabel.setFont(dynaLabel.getFont().deriveFont((float) (dynaLabel.getFont().getSize() - 2)));
104
bar = new NbProgressBar();
106
Color bg = UIManager.getColor("nbProgressBar.popupText.background");
109
mainLabel.setBackground(bg);
110
dynaLabel.setBackground(bg);
112
bgColor = getBackground();
113
Color dynaFg = UIManager.getColor("nbProgressBar.popupDynaText.foreground");
114
if (dynaFg != null) {
115
dynaLabel.setForeground(dynaFg);
117
// dynaFgColor = dynaLabel.getForeground();
118
fgColor = UIManager.getColor("nbProgressBar.popupText.foreground");
119
if (fgColor != null) {
120
mainLabel.setForeground(fgColor);
122
fgColor = mainLabel.getForeground();
123
selectBgColor = UIManager.getColor("nbProgressBar.popupText.selectBackground");
124
if (selectBgColor == null) {
125
selectBgColor = UIManager.getColor("List.selectionBackground");
127
selectFgColor = UIManager.getColor("nbProgressBar.popupText.selectForeground");
128
if (selectFgColor == null) {
129
selectFgColor = UIManager.getColor("List.selectionForeground");
131
bar.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.bar.tooltip"));
132
bar.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
133
//start figure out height
134
mainLabel.setText("XYZ");
135
dynaLabel.setText("XYZ");
136
mainHeight = mainLabel.getPreferredSize().height;
137
dynaHeight = dynaLabel.getPreferredSize().height;
138
mainLabel.setText(null);
139
dynaLabel.setText(null);
140
//end figure out height
142
setLayout(new CustomLayout());
145
MListener list = new MListener();
146
if (handle.isAllowCancel()) {
147
cancelAction = new CancelAction(false);
148
closeButton = new JButton(cancelAction);
149
closeButton.setBorderPainted(false);
150
closeButton.setBorder(BorderFactory.createEmptyBorder());
151
closeButton.setOpaque(false);
152
closeButton.setContentAreaFilled(false);
153
closeButton.setFocusable(false);
155
Image img = (Image)UIManager.get("nb.progress.cancel.icon");
157
closeButton.setIcon( new ImageIcon( img ) );
159
img = (Image)UIManager.get("nb.progress.cancel.icon.mouseover");
161
closeButton.setRolloverEnabled(true);
162
closeButton.setRolloverIcon( new ImageIcon( img ) );
164
img = (Image)UIManager.get("nb.progress.cancel.icon.pressed");
166
closeButton.setPressedIcon( new ImageIcon( img ) );
169
closeButton.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.btnClose.tooltip"));
171
if (handle.getState() != InternalHandle.STATE_RUNNING) {
172
closeButton.setEnabled(false);
176
setBorder(BorderFactory.createEmptyBorder());
177
addMouseListener(list);
178
bar.addMouseListener(list);
179
mainLabel.addMouseListener(list);
180
dynaLabel.addMouseListener(list);
181
if (handle.isAllowCancel()) {
182
closeButton.addMouseListener(list);
185
mainLabel.setText(handle.getDisplayName());
186
NbProgressBar.setupBar(handle, bar);
187
addFocusListener(new FocusListener() {
188
public void focusGained(java.awt.event.FocusEvent e) {
189
if (!e.isTemporary()) {
190
setBackground(selectBgColor);
191
mainLabel.setBackground(selectBgColor);
192
dynaLabel.setBackground(selectBgColor);
193
mainLabel.setForeground(selectFgColor);
194
// TODO assuming now that dynalabel has always the same foreground color
195
// seems to be the case according to the spec
196
scrollRectToVisible(getBounds());
200
public void focusLost(java.awt.event.FocusEvent e) {
201
if (!e.isTemporary()) {
202
setBackground(bgColor);
203
mainLabel.setBackground(bgColor);
204
dynaLabel.setBackground(bgColor);
205
mainLabel.setForeground(fgColor);
206
// TODO assuming now that dynalabel has always the same foreground color
207
// seems to be the case according to the spec
217
Action getCancelAction() {
221
InternalHandle getHandle() {
225
void processProgressEvent(ProgressEvent event) {
226
if (event.getType() == ProgressEvent.TYPE_PROGRESS ||
227
event.getType() == ProgressEvent.TYPE_SWITCH ||
228
event.getType() == ProgressEvent.TYPE_SILENT) {
229
if (event.getSource() != handle) {
230
throw new IllegalStateException();
232
if (event.isSwitched()) {
233
NbProgressBar.setupBar(event.getSource(), bar);
235
if (event.getWorkunitsDone() > 0) {
236
bar.setValue(event.getWorkunitsDone());
238
bar.setString(StatusLineComponent.getBarString(event.getPercentageDone(), event.getEstimatedCompletion()));
239
if (event.getMessage() != null) {
240
dynaLabel.setText(event.getMessage());
242
if (event.getSource().getState() == InternalHandle.STATE_REQUEST_STOP) {
243
closeButton.setEnabled(false);
245
if (event.getDisplayName() != null) {
246
mainLabel.setText(event.getDisplayName());
249
throw new IllegalStateException();
253
void markAsActive(boolean sel) {
254
if (sel == watched) {
259
mainLabel.setFont(mainLabel.getFont().deriveFont(Font.BOLD));
261
mainLabel.setFont(mainLabel.getFont().deriveFont(Font.PLAIN));
263
if (mainLabel.isVisible()) {
268
private class MListener extends MouseAdapter {
269
public void mouseClicked(MouseEvent e) {
270
if (e.getSource() == bar) {
271
handle.requestExplicitSelection();
272
// markAsSelected(true);
274
if (e.getClickCount() > 1 && (e.getSource() == mainLabel || e.getSource() == dynaLabel)) {
275
handle.requestView();
277
if (e.getButton() != e.BUTTON1) {
280
ListComponent.this.requestFocus();
283
// System.out.println("list component requesting focus..");
287
private void showMenu(MouseEvent e) {
288
JPopupMenu popup = new JPopupMenu();
289
//mark teh popup for the status line awt listener
290
popup.setName("progresspopup");
291
popup.add(new ViewAction());
292
popup.add(new WatchAction());
293
popup.add(new CancelAction(true));
294
popup.show((Component)e.getSource(), e.getX(), e.getY());
297
private class CancelAction extends AbstractAction {
298
CancelAction(boolean text) {
300
putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.Cancel"));
301
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
303
Image icon = (Image)UIManager.get("nb.progress.cancel.icon");
306
icon = Utilities.loadImage("org/netbeans/progress/module/resources/buton.png");
308
putValue(Action.SMALL_ICON, new ImageIcon(icon));
310
setEnabled(handle == null ? false : handle.isAllowCancel());
313
public void actionPerformed(ActionEvent actionEvent) {
314
if (handle.getState() == InternalHandle.STATE_RUNNING) {
315
String message = NbBundle.getMessage(ListComponent.class, "Cancel_Question", handle.getDisplayName());
316
String title = NbBundle.getMessage(ListComponent.class, "Cancel_Question_Title");
317
NotifyDescriptor dd = new NotifyDescriptor(message, title,
318
NotifyDescriptor.YES_NO_OPTION,
319
NotifyDescriptor.QUESTION_MESSAGE, null, null);
320
Object retType = DialogDisplayer.getDefault().notify(dd);
321
if (retType == NotifyDescriptor.YES_OPTION) {
322
handle.requestCancel();
328
private class ViewAction extends AbstractAction {
329
public ViewAction() {
330
putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.View"));
331
setEnabled(handle == null ? false : handle.isAllowView());
332
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
334
public void actionPerformed(ActionEvent actionEvent) {
335
if (handle != null) {
336
handle.requestView();
341
private class WatchAction extends AbstractAction {
342
public WatchAction() {
343
putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "ListComponent.Watch"));
344
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0));
347
public void actionPerformed(ActionEvent actionEvent) {
348
if (handle != null) {
349
handle.requestExplicitSelection();
355
private static final int UPPERMARGIN = 3;
356
private static final int LEFTMARGIN = 2;
357
private static final int BOTTOMMARGIN = 2;
358
private static final int BETWEENTEXTMARGIN = 3;
360
static final int ITEM_WIDTH = 400;
362
private class CustomLayout implements LayoutManager {
365
* If the layout manager uses a per-component string,
366
* adds the component <code>comp</code> to the layout,
368
* with the string specified by <code>name</code>.
370
* @param name the string to be associated with the component
371
* @param comp the component to be added
373
public void addLayoutComponent(String name, java.awt.Component comp) {
377
* Calculates the preferred size dimensions for the specified
378
* container, given the components it contains.
379
* @param parent the container to be laid out
381
* @see #minimumLayoutSize
383
public Dimension preferredLayoutSize(java.awt.Container parent) {
384
int height = UPPERMARGIN + mainHeight + BETWEENTEXTMARGIN + dynaHeight + BOTTOMMARGIN;
385
return new Dimension(ITEM_WIDTH, height);
390
* Lays out the specified container.
391
* @param parent the container to be laid out
393
public void layoutContainer(java.awt.Container parent) {
394
int parentWidth = parent.getWidth();
395
int parentHeight = parent.getHeight();
396
int offset = parentWidth - 18;
397
if (closeButton != null) {
398
closeButton.setBounds(offset, UPPERMARGIN, 18, mainHeight);
400
// have the bar approx 30 percent of the width
401
int barOffset = offset - (ITEM_WIDTH / 3);
402
bar.setBounds(barOffset, UPPERMARGIN, offset - barOffset, mainHeight);
403
mainLabel.setBounds(LEFTMARGIN, UPPERMARGIN, barOffset - LEFTMARGIN, mainHeight);
404
dynaLabel.setBounds(LEFTMARGIN, mainHeight + UPPERMARGIN + BETWEENTEXTMARGIN,
405
parentWidth - LEFTMARGIN, dynaHeight);
410
* Calculates the minimum size dimensions for the specified
411
* container, given the components it contains.
412
* @param parent the component to be laid out
413
* @see #preferredLayoutSize
415
public Dimension minimumLayoutSize(java.awt.Container parent) {
416
return preferredLayoutSize(parent);
420
* Removes the specified component from the layout.
421
* @param comp the component to be removed
423
public void removeLayoutComponent(java.awt.Component comp) {
b'\\ No newline at end of file'