KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > run > StopBuildingAlert


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.apache.tools.ant.module.run;
21
22 import java.awt.Component JavaDoc;
23 import java.text.Collator JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.SortedSet JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import javax.swing.DefaultListCellRenderer JavaDoc;
29 import javax.swing.DefaultListModel JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JList JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35 import org.openide.DialogDescriptor;
36 import org.openide.DialogDisplayer;
37 import org.openide.NotifyDescriptor;
38 import org.openide.util.NbBundle;
39
40 /**
41  * Displays an alert asking user to pick a running build to stop.
42  * @author Jesse Glick
43  */

44 final class StopBuildingAlert extends JPanel JavaDoc {
45     
46     /**
47      * Select one or more processes to kill among several choices.
48      * @param processesWithDisplayNames a list of possible threads to kill, mapped to display names
49      * @return the selection(s) (or empty if cancelled)
50      */

51     public static Thread JavaDoc[] selectProcessToKill(final Map JavaDoc<Thread JavaDoc,String JavaDoc> processesWithDisplayNames) {
52         StopBuildingAlert alert = new StopBuildingAlert(processesWithDisplayNames);
53         final JList JavaDoc list = alert.buildsList;
54         // Add all threads, sorted by display name.
55
DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
56         Comparator JavaDoc<Thread JavaDoc> comp = new Comparator JavaDoc<Thread JavaDoc>() {
57             private final Collator JavaDoc coll = Collator.getInstance();
58             public int compare(Thread JavaDoc t1, Thread JavaDoc t2) {
59                 String JavaDoc n1 = processesWithDisplayNames.get(t1);
60                 String JavaDoc n2 = processesWithDisplayNames.get(t2);
61                 int r = coll.compare(n1, n2);
62                 if (r != 0) {
63                     return r;
64                 } else {
65                     // Arbitrary. XXX Note that there is no way to predict which is
66
// which if you have more than one build running. Ideally it
67
// would be subsorted by creation time, probably.
68
return System.identityHashCode(t1) - System.identityHashCode(t2);
69                 }
70             }
71         };
72         SortedSet JavaDoc<Thread JavaDoc> threads = new TreeSet JavaDoc<Thread JavaDoc>(comp);
73         threads.addAll(processesWithDisplayNames.keySet());
74         for (Thread JavaDoc t : threads) {
75             model.addElement(t);
76         }
77         list.setModel(model);
78         list.setSelectedIndex(0);
79         // Make a dialog with buttons "Stop Building" and "Cancel".
80
DialogDescriptor dd = new DialogDescriptor(alert, NbBundle.getMessage(StopBuildingAlert.class, "TITLE_SBA"));
81         dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
82         final JButton JavaDoc stopButton = new JButton JavaDoc(NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_stop"));
83         list.addListSelectionListener(new ListSelectionListener JavaDoc() {
84             public void valueChanged(ListSelectionEvent JavaDoc e) {
85                 stopButton.setEnabled(list.getSelectedValue() != null);
86             }
87         });
88         dd.setOptions(new Object JavaDoc[] {stopButton, DialogDescriptor.CANCEL_OPTION});
89         DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
90         if (dd.getValue() == stopButton) {
91             Object JavaDoc[] _selectedThreads = list.getSelectedValues();
92             Thread JavaDoc[] selectedThreads = new Thread JavaDoc[_selectedThreads.length];
93             for (int i = 0; i < _selectedThreads.length; i++) {
94                 selectedThreads[i] = (Thread JavaDoc) _selectedThreads[i];
95             }
96             return selectedThreads;
97         } else {
98             return new Thread JavaDoc[0];
99         }
100     }
101     
102     private final Map JavaDoc<Thread JavaDoc,String JavaDoc> processesWithDisplayNames;
103     
104     private StopBuildingAlert(Map JavaDoc<Thread JavaDoc,String JavaDoc> processesWithDisplayNames) {
105         this.processesWithDisplayNames = processesWithDisplayNames;
106         initComponents();
107         buildsList.setCellRenderer(new ProcessCellRenderer());
108     }
109     
110     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
111
private void initComponents() {
112         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
113
114         introLabel = new javax.swing.JLabel JavaDoc();
115         buildsLabel = new javax.swing.JLabel JavaDoc();
116         buildsScrollPane = new javax.swing.JScrollPane JavaDoc();
117         buildsList = new javax.swing.JList JavaDoc();
118
119         org.openide.awt.Mnemonics.setLocalizedText(introLabel, org.openide.util.NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_intro"));
120
121         buildsLabel.setLabelFor(buildsList);
122         org.openide.awt.Mnemonics.setLocalizedText(buildsLabel, org.openide.util.NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_select"));
123
124         buildsScrollPane.setViewportView(buildsList);
125
126         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
127         this.setLayout(layout);
128         layout.setHorizontalGroup(
129             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
130             .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
131                 .addContainerGap()
132                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
133                     .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
134                         .add(buildsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
135                         .addContainerGap())
136                     .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
137                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
138                             .add(buildsLabel)
139                             .add(introLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
140                         .add(28, 28, 28))))
141         );
142         layout.setVerticalGroup(
143             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
144             .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
145                 .addContainerGap()
146                 .add(introLabel)
147                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
148                 .add(buildsLabel)
149                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
150                 .add(buildsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
151                 .addContainerGap())
152         );
153     }
154     // </editor-fold>//GEN-END:initComponents
155

156     
157     // Variables declaration - do not modify//GEN-BEGIN:variables
158
public javax.swing.JLabel JavaDoc buildsLabel;
159     public javax.swing.JList JavaDoc buildsList;
160     public javax.swing.JScrollPane JavaDoc buildsScrollPane;
161     public javax.swing.JLabel JavaDoc introLabel;
162     // End of variables declaration//GEN-END:variables
163

164     private final class ProcessCellRenderer extends DefaultListCellRenderer JavaDoc/*<Thread>*/ {
165         
166         public ProcessCellRenderer() {}
167
168         @Override JavaDoc
169         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
170             Thread JavaDoc t = (Thread JavaDoc) value;
171             String JavaDoc displayName = processesWithDisplayNames.get(t);
172             return super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
173         }
174         
175     }
176     
177 }
178
Popular Tags