Removed
Link Here
|
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.apache.tools.ant.module.run; |
43 |
|
44 |
import java.awt.Component; |
45 |
import java.text.Collator; |
46 |
import java.util.Comparator; |
47 |
import java.util.Map; |
48 |
import java.util.SortedSet; |
49 |
import java.util.TreeSet; |
50 |
import javax.swing.DefaultListCellRenderer; |
51 |
import javax.swing.DefaultListModel; |
52 |
import javax.swing.JButton; |
53 |
import javax.swing.JList; |
54 |
import javax.swing.JPanel; |
55 |
import javax.swing.event.ListSelectionEvent; |
56 |
import javax.swing.event.ListSelectionListener; |
57 |
import org.openide.DialogDescriptor; |
58 |
import org.openide.DialogDisplayer; |
59 |
import org.openide.NotifyDescriptor; |
60 |
import org.openide.util.NbBundle; |
61 |
|
62 |
/** |
63 |
* Displays an alert asking user to pick a running build to stop. |
64 |
* @author Jesse Glick |
65 |
*/ |
66 |
final class StopBuildingAlert extends JPanel { |
67 |
|
68 |
/** |
69 |
* Select one or more processes to kill among several choices. |
70 |
* @param processesWithDisplayNames a list of possible threads to kill, mapped to display names |
71 |
* @return the selection(s) (or empty if cancelled) |
72 |
*/ |
73 |
public static Thread[] selectProcessToKill(final Map<Thread,String> processesWithDisplayNames) { |
74 |
StopBuildingAlert alert = new StopBuildingAlert(processesWithDisplayNames); |
75 |
final JList list = alert.buildsList; |
76 |
// Add all threads, sorted by display name. |
77 |
DefaultListModel model = new DefaultListModel(); |
78 |
Comparator<Thread> comp = new Comparator<Thread>() { |
79 |
private final Collator coll = Collator.getInstance(); |
80 |
public int compare(Thread t1, Thread t2) { |
81 |
String n1 = processesWithDisplayNames.get(t1); |
82 |
String n2 = processesWithDisplayNames.get(t2); |
83 |
int r = coll.compare(n1, n2); |
84 |
if (r != 0) { |
85 |
return r; |
86 |
} else { |
87 |
// Arbitrary. XXX Note that there is no way to predict which is |
88 |
// which if you have more than one build running. Ideally it |
89 |
// would be subsorted by creation time, probably. |
90 |
return System.identityHashCode(t1) - System.identityHashCode(t2); |
91 |
} |
92 |
} |
93 |
}; |
94 |
SortedSet<Thread> threads = new TreeSet<Thread>(comp); |
95 |
threads.addAll(processesWithDisplayNames.keySet()); |
96 |
for (Thread t : threads) { |
97 |
model.addElement(t); |
98 |
} |
99 |
list.setModel(model); |
100 |
list.setSelectedIndex(0); |
101 |
// Make a dialog with buttons "Stop Building" and "Cancel". |
102 |
DialogDescriptor dd = new DialogDescriptor(alert, NbBundle.getMessage(StopBuildingAlert.class, "TITLE_SBA")); |
103 |
dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); |
104 |
final JButton stopButton = new JButton(NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_stop")); |
105 |
list.addListSelectionListener(new ListSelectionListener() { |
106 |
public void valueChanged(ListSelectionEvent e) { |
107 |
stopButton.setEnabled(list.getSelectedValue() != null); |
108 |
} |
109 |
}); |
110 |
dd.setOptions(new Object[] {stopButton, DialogDescriptor.CANCEL_OPTION}); |
111 |
DialogDisplayer.getDefault().createDialog(dd).setVisible(true); |
112 |
if (dd.getValue() == stopButton) { |
113 |
Object[] _selectedThreads = list.getSelectedValues(); |
114 |
Thread[] selectedThreads = new Thread[_selectedThreads.length]; |
115 |
for (int i = 0; i < _selectedThreads.length; i++) { |
116 |
selectedThreads[i] = (Thread) _selectedThreads[i]; |
117 |
} |
118 |
return selectedThreads; |
119 |
} else { |
120 |
return new Thread[0]; |
121 |
} |
122 |
} |
123 |
|
124 |
private final Map<Thread,String> processesWithDisplayNames; |
125 |
|
126 |
private StopBuildingAlert(Map<Thread,String> processesWithDisplayNames) { |
127 |
this.processesWithDisplayNames = processesWithDisplayNames; |
128 |
initComponents(); |
129 |
buildsList.setCellRenderer(new ProcessCellRenderer()); |
130 |
} |
131 |
|
132 |
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents |
133 |
private void initComponents() { |
134 |
java.awt.GridBagConstraints gridBagConstraints; |
135 |
|
136 |
introLabel = new javax.swing.JLabel(); |
137 |
buildsLabel = new javax.swing.JLabel(); |
138 |
buildsScrollPane = new javax.swing.JScrollPane(); |
139 |
buildsList = new javax.swing.JList(); |
140 |
|
141 |
org.openide.awt.Mnemonics.setLocalizedText(introLabel, org.openide.util.NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_intro")); |
142 |
|
143 |
buildsLabel.setLabelFor(buildsList); |
144 |
org.openide.awt.Mnemonics.setLocalizedText(buildsLabel, org.openide.util.NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_select")); |
145 |
|
146 |
buildsScrollPane.setViewportView(buildsList); |
147 |
|
148 |
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); |
149 |
this.setLayout(layout); |
150 |
layout.setHorizontalGroup( |
151 |
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
152 |
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() |
153 |
.addContainerGap() |
154 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) |
155 |
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() |
156 |
.add(buildsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE) |
157 |
.addContainerGap()) |
158 |
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() |
159 |
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) |
160 |
.add(buildsLabel) |
161 |
.add(introLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
162 |
.add(28, 28, 28)))) |
163 |
); |
164 |
layout.setVerticalGroup( |
165 |
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
166 |
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() |
167 |
.addContainerGap() |
168 |
.add(introLabel) |
169 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
170 |
.add(buildsLabel) |
171 |
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
172 |
.add(buildsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) |
173 |
.addContainerGap()) |
174 |
); |
175 |
} |
176 |
// </editor-fold>//GEN-END:initComponents |
177 |
|
178 |
|
179 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
180 |
public javax.swing.JLabel buildsLabel; |
181 |
public javax.swing.JList buildsList; |
182 |
public javax.swing.JScrollPane buildsScrollPane; |
183 |
public javax.swing.JLabel introLabel; |
184 |
// End of variables declaration//GEN-END:variables |
185 |
|
186 |
private final class ProcessCellRenderer extends DefaultListCellRenderer/*<Thread>*/ { |
187 |
|
188 |
public ProcessCellRenderer() {} |
189 |
|
190 |
@Override |
191 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
192 |
Thread t = (Thread) value; |
193 |
String displayName = processesWithDisplayNames.get(t); |
194 |
return super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); |
195 |
} |
196 |
|
197 |
} |
198 |
|
199 |
} |