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