~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to core/progressui/src/org/netbeans/modules/progress/ui/ListComponent.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.progress.ui;
 
43
 
 
44
import java.awt.Color;
 
45
import java.awt.Component;
 
46
import java.awt.Cursor;
 
47
import java.awt.Dimension;
 
48
import java.awt.Font;
 
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;
 
72
 
 
73
/**
 
74
 *
 
75
 * @author mkleint
 
76
 */
 
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;
 
92
    
 
93
    /** Creates a new instance of ListComponent */
 
94
    public ListComponent(InternalHandle hndl) {
 
95
        setFocusable(true);
 
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
 
101
        // #59419
 
102
        setOpaque(true);
 
103
        dynaLabel.setFont(dynaLabel.getFont().deriveFont((float) (dynaLabel.getFont().getSize() - 2)));
 
104
        bar = new NbProgressBar();        
 
105
        handle = hndl;
 
106
        Color bg = UIManager.getColor("nbProgressBar.popupText.background");
 
107
        if (bg != null) {
 
108
            setBackground(bg);
 
109
            mainLabel.setBackground(bg);
 
110
            dynaLabel.setBackground(bg);
 
111
        }
 
112
        bgColor = getBackground();
 
113
        Color dynaFg = UIManager.getColor("nbProgressBar.popupDynaText.foreground");
 
114
        if (dynaFg != null) {
 
115
            dynaLabel.setForeground(dynaFg);
 
116
        }
 
117
//        dynaFgColor = dynaLabel.getForeground();
 
118
        fgColor = UIManager.getColor("nbProgressBar.popupText.foreground");
 
119
        if (fgColor != null) {
 
120
            mainLabel.setForeground(fgColor);
 
121
        }
 
122
        fgColor = mainLabel.getForeground();
 
123
        selectBgColor = UIManager.getColor("nbProgressBar.popupText.selectBackground");
 
124
        if (selectBgColor == null) {
 
125
            selectBgColor = UIManager.getColor("List.selectionBackground");
 
126
        }
 
127
        selectFgColor = UIManager.getColor("nbProgressBar.popupText.selectForeground");
 
128
        if (selectFgColor == null) {
 
129
            selectFgColor = UIManager.getColor("List.selectionForeground");
 
130
        }
 
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
 
141
        
 
142
        setLayout(new CustomLayout());
 
143
        add(mainLabel);
 
144
        add(bar);
 
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);
 
154
            
 
155
            Image img = (Image)UIManager.get("nb.progress.cancel.icon");
 
156
            if( null != img ) {
 
157
                closeButton.setIcon( new ImageIcon( img ) );
 
158
            }
 
159
            img = (Image)UIManager.get("nb.progress.cancel.icon.mouseover");
 
160
            if( null != img ) {
 
161
                closeButton.setRolloverEnabled(true);
 
162
                closeButton.setRolloverIcon( new ImageIcon( img ) );
 
163
            }
 
164
            img = (Image)UIManager.get("nb.progress.cancel.icon.pressed");
 
165
            if( null != img ) {
 
166
                closeButton.setPressedIcon( new ImageIcon( img ) );
 
167
            }
 
168
            
 
169
            closeButton.setToolTipText(NbBundle.getMessage(ListComponent.class, "ListComponent.btnClose.tooltip"));
 
170
            add(closeButton);
 
171
            if (handle.getState() != InternalHandle.STATE_RUNNING) {
 
172
                closeButton.setEnabled(false);
 
173
            }
 
174
        }
 
175
        add(dynaLabel);
 
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);
 
183
        }
 
184
        
 
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());
 
197
                }
 
198
            }
 
199
 
 
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
 
208
                    
 
209
                }
 
210
            }
 
211
            
 
212
        });
 
213
        
 
214
    }
 
215
    
 
216
    
 
217
    Action getCancelAction() {
 
218
        return cancelAction;
 
219
    }
 
220
    
 
221
    InternalHandle getHandle() {
 
222
        return handle;
 
223
    }
 
224
    
 
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();
 
231
            }
 
232
            if (event.isSwitched()) {
 
233
                NbProgressBar.setupBar(event.getSource(), bar);
 
234
            }
 
235
            if (event.getWorkunitsDone() > 0) {
 
236
                bar.setValue(event.getWorkunitsDone());
 
237
            }
 
238
            bar.setString(StatusLineComponent.getBarString(event.getPercentageDone(), event.getEstimatedCompletion()));
 
239
            if (event.getMessage() != null) {
 
240
                dynaLabel.setText(event.getMessage());
 
241
            }
 
242
            if (event.getSource().getState() == InternalHandle.STATE_REQUEST_STOP) {
 
243
                closeButton.setEnabled(false);
 
244
            }
 
245
            if (event.getDisplayName() != null) {
 
246
                mainLabel.setText(event.getDisplayName());
 
247
            }
 
248
        } else {
 
249
            throw new IllegalStateException();
 
250
        }
 
251
    }
 
252
 
 
253
    void markAsActive(boolean sel) {
 
254
        if (sel == watched) {
 
255
            return;
 
256
        }
 
257
        watched = sel;
 
258
        if (sel) {
 
259
            mainLabel.setFont(mainLabel.getFont().deriveFont(Font.BOLD));
 
260
        } else {
 
261
            mainLabel.setFont(mainLabel.getFont().deriveFont(Font.PLAIN));
 
262
        }
 
263
        if (mainLabel.isVisible()) {
 
264
            mainLabel.repaint();
 
265
        }
 
266
    }
 
267
    
 
268
    private class MListener extends MouseAdapter {
 
269
        public void mouseClicked(MouseEvent e) {
 
270
            if (e.getSource() == bar) {
 
271
                handle.requestExplicitSelection();
 
272
//                markAsSelected(true);
 
273
            }
 
274
            if (e.getClickCount() > 1 && (e.getSource() == mainLabel || e.getSource() == dynaLabel)) {
 
275
                handle.requestView();
 
276
            }
 
277
            if (e.getButton() != e.BUTTON1) {
 
278
                showMenu(e);
 
279
            } else {
 
280
                ListComponent.this.requestFocus();
 
281
            }
 
282
            
 
283
//            System.out.println("list component requesting focus..");
 
284
        }
 
285
    }
 
286
    
 
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());
 
295
    }    
 
296
    
 
297
   private class CancelAction extends AbstractAction {
 
298
       CancelAction(boolean text) {
 
299
           if (text) {
 
300
               putValue(Action.NAME, NbBundle.getMessage(ListComponent.class, "StatusLineComponent.Cancel"));
 
301
               putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
 
302
           } else {
 
303
               Image icon = (Image)UIManager.get("nb.progress.cancel.icon");
 
304
               if (icon == null) {
 
305
                   // for custom L&F?
 
306
                   icon = Utilities.loadImage("org/netbeans/progress/module/resources/buton.png");
 
307
               }
 
308
               putValue(Action.SMALL_ICON, new ImageIcon(icon));
 
309
           }
 
310
            setEnabled(handle == null ? false : handle.isAllowCancel());
 
311
       }
 
312
 
 
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();
 
323
                }
 
324
            }
 
325
        }
 
326
    }
 
327
 
 
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));
 
333
        }
 
334
        public void actionPerformed(ActionEvent actionEvent) {
 
335
            if (handle != null) {
 
336
                handle.requestView();
 
337
            }
 
338
        }
 
339
    }    
 
340
 
 
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));
 
345
            setEnabled(true);
 
346
        }
 
347
        public void actionPerformed(ActionEvent actionEvent) {
 
348
            if (handle != null) {
 
349
                handle.requestExplicitSelection();
 
350
            }
 
351
        }
 
352
    }    
 
353
    
 
354
    
 
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;
 
359
    
 
360
    static final int ITEM_WIDTH = 400;
 
361
    
 
362
    private class CustomLayout implements LayoutManager {
 
363
        
 
364
        /**
 
365
         * If the layout manager uses a per-component string,
 
366
         * adds the component <code>comp</code> to the layout,
 
367
         * associating it 
 
368
         * with the string specified by <code>name</code>.
 
369
         * 
 
370
         * @param name the string to be associated with the component
 
371
         * @param comp the component to be added
 
372
         */
 
373
        public void addLayoutComponent(String name, java.awt.Component comp) {
 
374
        }
 
375
 
 
376
        /**
 
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
 
380
         *  
 
381
         * @see #minimumLayoutSize
 
382
         */
 
383
        public Dimension preferredLayoutSize(java.awt.Container parent) {
 
384
            int height = UPPERMARGIN + mainHeight + BETWEENTEXTMARGIN + dynaHeight + BOTTOMMARGIN;
 
385
            return new Dimension(ITEM_WIDTH, height);
 
386
        }
 
387
 
 
388
        /**
 
389
         * 
 
390
         * Lays out the specified container.
 
391
         * @param parent the container to be laid out 
 
392
         */
 
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);            
 
399
            }
 
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);
 
406
        }
 
407
 
 
408
        /**
 
409
         * 
 
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
 
414
         */
 
415
        public Dimension minimumLayoutSize(java.awt.Container parent) {
 
416
            return preferredLayoutSize(parent);
 
417
        }
 
418
 
 
419
        /**
 
420
         * Removes the specified component from the layout.
 
421
         * @param comp the component to be removed
 
422
         */
 
423
        public void removeLayoutComponent(java.awt.Component comp) {
 
424
        }
 
425
 
 
426
        
 
427
    }
 
428
}
 
 
b'\\ No newline at end of file'