KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > progress > ui > ListComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.progress.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.Image JavaDoc;
28 import java.awt.LayoutManager JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.FocusListener JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.awt.event.MouseAdapter JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import javax.swing.AbstractAction JavaDoc;
35 import javax.swing.Action JavaDoc;
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JPopupMenu JavaDoc;
42 import javax.swing.KeyStroke JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import org.netbeans.progress.spi.InternalHandle;
45 import org.netbeans.progress.spi.ProgressEvent;
46 import org.openide.DialogDisplayer;
47 import org.openide.NotifyDescriptor;
48 import org.openide.util.NbBundle;
49 import org.openide.util.Utilities;
50
51 /**
52  *
53  * @author mkleint
54  */

55 public class ListComponent extends JPanel JavaDoc {
56     private NbProgressBar bar;
57     private JLabel JavaDoc mainLabel;
58     private JLabel JavaDoc dynaLabel;
59     private JButton JavaDoc closeButton;
60     private InternalHandle handle;
61     private boolean watched;
62     private Action JavaDoc cancelAction;
63     private Color JavaDoc selectBgColor;
64     private Color JavaDoc selectFgColor;
65     private Color JavaDoc bgColor;
66     private Color JavaDoc fgColor;
67 // private Color dynaFgColor;
68
private int mainHeight;
69     private int dynaHeight;
70     
71     /** Creates a new instance of ListComponent */
72     public ListComponent(InternalHandle hndl) {
73         setFocusable(true);
74         setRequestFocusEnabled(true);
75 // setVerifyInputWhenFocusTarget(false);
76
mainLabel = new JLabel JavaDoc();
77         dynaLabel = new JLabel JavaDoc();
78         // in gtk, the panel is non-opague, meaning we cannot color background
79
// #59419
80
setOpaque(true);
81         dynaLabel.setFont(dynaLabel.getFont().deriveFont((float) (dynaLabel.getFont().getSize() - 2)));
82         bar = new NbProgressBar();
83         handle = hndl;
84         Color JavaDoc bg = UIManager.getColor("nbProgressBar.popupText.background");
85         if (bg != null) {
86             setBackground(bg);
87             mainLabel.setBackground(bg);
88             dynaLabel.setBackground(bg);
89         }
90         bgColor = getBackground();
91         Color JavaDoc dynaFg = UIManager.getColor("nbProgressBar.popupDynaText.foreground");
92         if (dynaFg != null) {
93             dynaLabel.setForeground(dynaFg);
94         }
95 // dynaFgColor = dynaLabel.getForeground();
96
fgColor = UIManager.getColor("nbProgressBar.popupText.foreground");
97         if (fgColor != null) {
98             mainLabel.setForeground(fgColor);
99         }
100         fgColor = mainLabel.getForeground();
101         selectBgColor = UIManager.getColor("nbProgressBar.popupText.selectBackground");
102         if (selectBgColor == null) {
103             selectBgColor = UIManager.getColor("List.selectionBackground");
104         }
105         selectFgColor = UIManager.getColor("nbProgressBar.popupText.selectForeground");
106         if (selectFgColor == null) {
107             selectFgColor = UIManager.getColor("List.selectionForeground");
108         }
109         bar.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.bar.tooltip"));
110         bar.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
111         //start figure out height
112
mainLabel.setText("XYZ");
113         dynaLabel.setText("XYZ");
114         mainHeight = mainLabel.getPreferredSize().height;
115         dynaHeight = dynaLabel.getPreferredSize().height;
116         mainLabel.setText(null);
117         dynaLabel.setText(null);
118         //end figure out height
119

120         setLayout(new CustomLayout());
121         add(mainLabel);
122         add(bar);
123         MListener list = new MListener();
124         if (handle.isAllowCancel()) {
125             cancelAction = new CancelAction(false);
126             closeButton = new JButton JavaDoc(cancelAction);
127             closeButton.setBorderPainted(false);
128             closeButton.setBorder(BorderFactory.createEmptyBorder());
129             closeButton.setOpaque(false);
130             closeButton.setContentAreaFilled(false);
131             closeButton.setFocusable(false);
132             
133             Image JavaDoc img = (Image JavaDoc)UIManager.get("nb.progress.cancel.icon");
134             if( null != img ) {
135                 closeButton.setIcon( new ImageIcon JavaDoc( img ) );
136             }
137             img = (Image JavaDoc)UIManager.get("nb.progress.cancel.icon.mouseover");
138             if( null != img ) {
139                 closeButton.setRolloverEnabled(true);
140                 closeButton.setRolloverIcon( new ImageIcon JavaDoc( img ) );
141             }
142             img = (Image JavaDoc)UIManager.get("nb.progress.cancel.icon.pressed");
143             if( null != img ) {
144                 closeButton.setPressedIcon( new ImageIcon JavaDoc( img ) );
145             }
146             
147             closeButton.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.btnClose.tooltip"));
148             add(closeButton);
149             if (handle.getState() != InternalHandle.STATE_RUNNING) {
150                 closeButton.setEnabled(false);
151             }
152         }
153         add(dynaLabel);
154         setBorder(BorderFactory.createEmptyBorder());
155         addMouseListener(list);
156         bar.addMouseListener(list);
157         mainLabel.addMouseListener(list);
158         dynaLabel.addMouseListener(list);
159         if (handle.isAllowCancel()) {
160             closeButton.addMouseListener(list);
161         }
162         
163         mainLabel.setText(handle.getDisplayName());
164         NbProgressBar.setupBar(handle, bar);
165         addFocusListener(new FocusListener JavaDoc() {
166             public void focusGained(java.awt.event.FocusEvent JavaDoc e) {
167                 if (!e.isTemporary()) {
168                     setBackground(selectBgColor);
169                     mainLabel.setBackground(selectBgColor);
170                     dynaLabel.setBackground(selectBgColor);
171                     mainLabel.setForeground(selectFgColor);
172                     // TODO assuming now that dynalabel has always the same foreground color
173
// seems to be the case according to the spec
174
scrollRectToVisible(getBounds());
175                 }
176             }
177
178             public void focusLost(java.awt.event.FocusEvent JavaDoc e) {
179                 if (!e.isTemporary()) {
180                     setBackground(bgColor);
181                     mainLabel.setBackground(bgColor);
182                     dynaLabel.setBackground(bgColor);
183                     mainLabel.setForeground(fgColor);
184                     // TODO assuming now that dynalabel has always the same foreground color
185
// seems to be the case according to the spec
186

187                 }
188             }
189             
190         });
191         
192     }
193     
194     
195     Action JavaDoc getCancelAction() {
196         return cancelAction;
197     }
198     
199     InternalHandle getHandle() {
200         return handle;
201     }
202     
203     void processProgressEvent(ProgressEvent event) {
204         if (event.getType() == ProgressEvent.TYPE_PROGRESS ||
205                 event.getType() == ProgressEvent.TYPE_SWITCH ||
206                 event.getType() == ProgressEvent.TYPE_SILENT) {
207             if (event.getSource() != handle) {
208                 throw new IllegalStateException JavaDoc();
209             }
210             if (event.isSwitched()) {
211                 NbProgressBar.setupBar(event.getSource(), bar);
212             }
213             if (event.getWorkunitsDone() > 0) {
214                 bar.setValue(event.getWorkunitsDone());
215             }
216             bar.setString(StatusLineComponent.getBarString(event.getPercentageDone(), event.getEstimatedCompletion()));
217             if (event.getMessage() != null) {
218                 dynaLabel.setText(event.getMessage());
219             }
220             if (event.getSource().getState() == InternalHandle.STATE_REQUEST_STOP) {
221                 closeButton.setEnabled(false);
222             }
223             if (event.getDisplayName() != null) {
224                 mainLabel.setText(event.getDisplayName());
225             }
226         } else {
227             throw new IllegalStateException JavaDoc();
228         }
229     }
230
231     void markAsActive(boolean sel) {
232         if (sel == watched) {
233             return;
234         }
235         watched = sel;
236         if (sel) {
237             mainLabel.setFont(mainLabel.getFont().deriveFont(Font.BOLD));
238         } else {
239             mainLabel.setFont(mainLabel.getFont().deriveFont(Font.PLAIN));
240         }
241         if (mainLabel.isVisible()) {
242             mainLabel.repaint();
243         }
244     }
245     
246     private class MListener extends MouseAdapter JavaDoc {
247         public void mouseClicked(MouseEvent JavaDoc e) {
248             if (e.getSource() == bar) {
249                 handle.requestExplicitSelection();
250 // markAsSelected(true);
251
}
252             if (e.getClickCount() > 1 && (e.getSource() == mainLabel || e.getSource() == dynaLabel)) {
253                 handle.requestView();
254             }
255             if (e.getButton() != e.BUTTON1) {
256                 showMenu(e);
257             } else {
258                 ListComponent.this.requestFocus();
259             }
260             
261 // System.out.println("list component requesting focus..");
262
}
263     }
264     
265    private void showMenu(MouseEvent JavaDoc e) {
266         JPopupMenu JavaDoc popup = new JPopupMenu JavaDoc();
267         //mark teh popup for the status line awt listener
268
popup.setName("progresspopup");
269         popup.add(new ViewAction());
270         popup.add(new WatchAction());
271         popup.add(new CancelAction(true));
272         popup.show((Component JavaDoc)e.getSource(), e.getX(), e.getY());
273     }
274     
275    private class CancelAction extends AbstractAction JavaDoc {
276        CancelAction(boolean text) {
277            if (text) {
278                putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.Cancel"));
279                putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
280            } else {
281                Image JavaDoc icon = (Image JavaDoc)UIManager.get("nb.progress.cancel.icon");
282                if (icon == null) {
283                    // for custom L&F?
284
icon = Utilities.loadImage("org/netbeans/progress/module/resources/buton.png");
285                }
286                putValue(Action.SMALL_ICON, new ImageIcon JavaDoc(icon));
287            }
288             setEnabled(handle == null ? false : handle.isAllowCancel());
289        }
290
291         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
292             if (handle.getState() == InternalHandle.STATE_RUNNING) {
293                 String JavaDoc message = NbBundle.getMessage(ListComponent.class, "Cancel_Question", handle.getDisplayName());
294                 String JavaDoc title = NbBundle.getMessage(ListComponent.class, "Cancel_Question_Title");
295                 NotifyDescriptor dd = new NotifyDescriptor(message, title,
296                                            NotifyDescriptor.YES_NO_OPTION,
297                                            NotifyDescriptor.QUESTION_MESSAGE, null, null);
298                 Object JavaDoc retType = DialogDisplayer.getDefault().notify(dd);
299                 if (retType == NotifyDescriptor.YES_OPTION) {
300                     handle.requestCancel();
301                 }
302             }
303         }
304     }
305
306     private class ViewAction extends AbstractAction JavaDoc {
307         public ViewAction() {
308             putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.View"));
309             setEnabled(handle == null ? false : handle.isAllowView());
310             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
311         }
312         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
313             if (handle != null) {
314                 handle.requestView();
315             }
316         }
317     }
318
319     private class WatchAction extends AbstractAction JavaDoc {
320         public WatchAction() {
321             putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "ListComponent.Watch"));
322             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0));
323             setEnabled(true);
324         }
325         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
326             if (handle != null) {
327                 handle.requestExplicitSelection();
328             }
329         }
330     }
331     
332     
333     private static final int UPPERMARGIN = 3;
334     private static final int LEFTMARGIN = 2;
335     private static final int BOTTOMMARGIN = 2;
336     private static final int BETWEENTEXTMARGIN = 3;
337     
338     static final int ITEM_WIDTH = 400;
339     
340     private class CustomLayout implements LayoutManager JavaDoc {
341         
342         /**
343          * If the layout manager uses a per-component string,
344          * adds the component <code>comp</code> to the layout,
345          * associating it
346          * with the string specified by <code>name</code>.
347          *
348          * @param name the string to be associated with the component
349          * @param comp the component to be added
350          */

351         public void addLayoutComponent(String JavaDoc name, java.awt.Component JavaDoc comp) {
352         }
353
354         /**
355          * Calculates the preferred size dimensions for the specified
356          * container, given the components it contains.
357          * @param parent the container to be laid out
358          *
359          * @see #minimumLayoutSize
360          */

361         public Dimension JavaDoc preferredLayoutSize(java.awt.Container JavaDoc parent) {
362             int height = UPPERMARGIN + mainHeight + BETWEENTEXTMARGIN + dynaHeight + BOTTOMMARGIN;
363             return new Dimension JavaDoc(ITEM_WIDTH, height);
364         }
365
366         /**
367          *
368          * Lays out the specified container.
369          * @param parent the container to be laid out
370          */

371         public void layoutContainer(java.awt.Container JavaDoc parent) {
372             int parentWidth = parent.getWidth();
373             int parentHeight = parent.getHeight();
374             int offset = parentWidth - 18;
375             if (closeButton != null) {
376                 closeButton.setBounds(offset, UPPERMARGIN, 18, mainHeight);
377             }
378             // have the bar approx 30 percent of the width
379
int barOffset = offset - (ITEM_WIDTH / 3);
380             bar.setBounds(barOffset, UPPERMARGIN, offset - barOffset, mainHeight);
381             mainLabel.setBounds(LEFTMARGIN, UPPERMARGIN, barOffset - LEFTMARGIN, mainHeight);
382             dynaLabel.setBounds(LEFTMARGIN, mainHeight + UPPERMARGIN + BETWEENTEXTMARGIN,
383                                 parentWidth - LEFTMARGIN, dynaHeight);
384         }
385
386         /**
387          *
388          * Calculates the minimum size dimensions for the specified
389          * container, given the components it contains.
390          * @param parent the component to be laid out
391          * @see #preferredLayoutSize
392          */

393         public Dimension JavaDoc minimumLayoutSize(java.awt.Container JavaDoc parent) {
394             return preferredLayoutSize(parent);
395         }
396
397         /**
398          * Removes the specified component from the layout.
399          * @param comp the component to be removed
400          */

401         public void removeLayoutComponent(java.awt.Component JavaDoc comp) {
402         }
403
404         
405     }
406 }
Popular Tags