001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.project.ui.customizer;
021:
022: import java.awt.Component;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.util.*;
026: import java.net.URI;
027:
028: import javax.swing.*;
029: import javax.swing.event.ListSelectionEvent;
030: import javax.swing.event.ListSelectionListener;
031:
032: import org.openide.util.Utilities;
033: import org.netbeans.api.project.ant.AntArtifact;
034: import org.netbeans.api.project.Project;
035: import org.netbeans.modules.bpel.project.IcanproProject;
036: import org.netbeans.modules.bpel.project.spi.JbiArtifactProvider;
037:
038: /** Handles adding, removing, editing and reordering of classpath.
039: */
040: final class VisualClasspathSupport {
041:
042: final Project master;
043: final JList classpathList;
044: final JButton addArtifactButton;
045: final JButton removeButton;
046: final JButton upButton;
047: final JButton downButton;
048:
049: private final DefaultListModel classpathModel;
050:
051: private final ArrayList actionListeners = new ArrayList();
052:
053: public VisualClasspathSupport(Project master, JList classpathList,
054: JButton addArtifactButton, JButton removeButton,
055: JButton upButton, JButton downButton) {
056: this .master = master;
057:
058: this .classpathList = classpathList;
059: this .classpathModel = new DefaultListModel();
060: this .classpathList.setModel(classpathModel);
061: this .classpathList.setCellRenderer(new ClassPathCellRenderer());
062:
063: this .addArtifactButton = addArtifactButton;
064: this .removeButton = removeButton;
065: this .upButton = upButton;
066: this .downButton = downButton;
067:
068: // Register the listeners
069: ClasspathSupportListener csl = new ClasspathSupportListener();
070:
071: // On all buttons
072: addArtifactButton.addActionListener(csl);
073: removeButton.addActionListener(csl);
074: upButton.addActionListener(csl);
075: downButton.addActionListener(csl);
076:
077: // On list selection
078: classpathList.getSelectionModel().addListSelectionListener(csl);
079:
080: // Set the initial state of the buttons
081: csl.valueChanged(null);
082: }
083:
084: public void setVisualClassPathItems(List items) {
085:
086: classpathModel.clear();
087: for (Iterator it = items.iterator(); it.hasNext();) {
088: VisualClassPathItem cpItem = (VisualClassPathItem) it
089: .next();
090: classpathModel.addElement(cpItem);
091: }
092: }
093:
094: public List getVisualClassPathItems() {
095:
096: ArrayList items = new ArrayList();
097: for (Enumeration e = classpathModel.elements(); e
098: .hasMoreElements();) {
099: VisualClassPathItem cpItem = (VisualClassPathItem) e
100: .nextElement();
101: items.add(cpItem);
102: }
103:
104: return items;
105:
106: }
107:
108: /** Action listeners will be informed when the value of the
109: * list changes.
110: */
111: public void addActionListener(ActionListener listener) {
112: actionListeners.add(listener);
113: }
114:
115: public void removeActionListener(ActionListener listener) {
116: actionListeners.remove(listener);
117: }
118:
119: private void fireActionPerformed() {
120: ArrayList listeners;
121:
122: synchronized (this ) {
123: listeners = new ArrayList(actionListeners);
124: }
125:
126: ActionEvent ae = new ActionEvent(this , 0, null);
127:
128: for (Iterator it = listeners.iterator(); it.hasNext();) {
129: ActionListener al = (ActionListener) it.next();
130: al.actionPerformed(ae);
131: }
132: }
133:
134: // Private methods ---------------------------------------------------------
135:
136: private URI getArtifactLocation(AntArtifact artifact) {
137: URI[] us = artifact.getArtifactLocations();
138: if ((us != null) && (us.length > 0)) {
139: return us[0];
140: }
141: return null;
142: }
143:
144: private void addArtifacts(AntArtifact artifacts[]) {
145:
146: int[] si = classpathList.getSelectedIndices();
147:
148: int lastIndex = si == null || si.length == 0 ? -1
149: : si[si.length - 1];
150: int[] indexes = new int[artifacts.length];
151: for (int i = 0; i < artifacts.length; i++) {
152: int current = lastIndex + 1 + i;
153: classpathModel.add(current, new VisualClassPathItem(
154: artifacts[i], VisualClassPathItem.TYPE_ARTIFACT,
155: null, getArtifactLocation(artifacts[i]).toString(),
156: true));
157: // VisualClassPathItem.create(artifacts[i]));
158: indexes[i] = current;
159: }
160: this .classpathList.setSelectedIndices(indexes);
161:
162: fireActionPerformed();
163: }
164:
165: private void removeElements() {
166:
167: int[] si = classpathList.getSelectedIndices();
168:
169: if (si == null || si.length == 0) {
170: assert false : "Remove button should be disabled"; // NOI18N
171: }
172:
173: // Remove the items
174: for (int i = si.length - 1; i >= 0; i--) {
175: classpathModel.remove(si[i]);
176: }
177:
178: if (!classpathModel.isEmpty()) {
179: // Select reasonable item
180: int selectedIndex = si[si.length - 1] - si.length + 1;
181: if (selectedIndex > classpathModel.size() - 1) {
182: selectedIndex = classpathModel.size() - 1;
183: }
184: classpathList.setSelectedIndex(selectedIndex);
185: }
186:
187: fireActionPerformed();
188: }
189:
190: private void moveUp() {
191:
192: int[] si = classpathList.getSelectedIndices();
193:
194: if (si == null || si.length == 0) {
195: assert false : "MoveUp button should be disabled"; // NOI18N
196: }
197:
198: // Move the items up
199: for (int i = 0; i < si.length; i++) {
200: Object item = classpathModel.get(si[i]);
201: classpathModel.remove(si[i]);
202: classpathModel.add(si[i] - 1, item);
203: }
204:
205: // Keep the selection a before
206: for (int i = 0; i < si.length; i++) {
207: si[i] -= 1;
208: }
209: classpathList.setSelectedIndices(si);
210:
211: fireActionPerformed();
212: }
213:
214: private void moveDown() {
215:
216: int[] si = classpathList.getSelectedIndices();
217:
218: if (si == null || si.length == 0) {
219: assert false : "MoveDown button should be disabled"; // NOI18N
220: }
221:
222: // Move the items up
223: for (int i = si.length - 1; i >= 0; i--) {
224: Object item = classpathModel.get(si[i]);
225: classpathModel.remove(si[i]);
226: classpathModel.add(si[i] + 1, item);
227: }
228:
229: // Keep the selection a before
230: for (int i = 0; i < si.length; i++) {
231: si[i] += 1;
232: }
233: classpathList.setSelectedIndices(si);
234:
235: fireActionPerformed();
236: }
237:
238: // Private innerclasses ----------------------------------------------------
239:
240: private class ClasspathSupportListener implements ActionListener,
241: ListSelectionListener {
242:
243: // Implementation of ActionListener ------------------------------------
244: /** Handles button events
245: */
246: public void actionPerformed(ActionEvent e) {
247: Object source = e.getSource();
248: if (source == addArtifactButton) {
249: AntArtifact artifacts[] = AntArtifactChooser
250: .showDialog(
251: JbiArtifactProvider.ARTIFACT_TYPE_JBI_ASA,
252: classpathModel, master);
253: if (artifacts != null) {
254: addArtifacts(artifacts);
255: }
256: } else if (source == removeButton) {
257: removeElements();
258: } else if (source == upButton) {
259: moveUp();
260: } else if (source == downButton) {
261: moveDown();
262: }
263: }
264:
265: // ListSelectionModel --------------------------------------------------
266: /** Handles changes in the selection
267: */
268: public void valueChanged(ListSelectionEvent e) {
269:
270: int[] si = classpathList.getSelectedIndices();
271:
272: // addJar allways enabled
273:
274: // addLibrary allways enabled
275:
276: // addArtifact allways enabled
277:
278: // edit enabled only if selection is not empty
279: boolean edit = si != null && si.length > 0;
280:
281: // remove enabled only if selection is not empty
282: boolean remove = si != null && si.length > 0;
283: // and when the selection does not contain unremovable item
284: if (remove) {
285: for (int i = 0; i < si.length; i++) {
286: assert si[i] < classpathModel.getSize() : "The selected indices "
287: + Arrays
288: .asList(Utilities.toObjectArray(si))
289: + // NOI18N
290: " at "
291: + i
292: + // NOI18N
293: " must fit into size of classpathModel"
294: + classpathModel.getSize(); // NOI18N
295: VisualClassPathItem vcpi = (VisualClassPathItem) classpathModel
296: .get(si[i]);
297: if (!vcpi.canDelete()) {
298: remove = false;
299: break;
300: }
301: }
302: }
303:
304: // up button enabled if selection is not empty
305: // and the first selected index is not the first row
306: boolean up = si != null && si.length > 0 && si[0] != 0;
307:
308: // up button enabled if selection is not empty
309: // and the laset selected index is not the last row
310: boolean down = si != null && si.length > 0
311: && si[si.length - 1] != classpathModel.size() - 1;
312:
313: removeButton.setEnabled(remove);
314: upButton.setEnabled(up);
315: downButton.setEnabled(down);
316:
317: }
318: }
319:
320: private static class ClassPathCellRenderer extends
321: DefaultListCellRenderer {
322:
323: public Component getListCellRendererComponent(JList list,
324: Object value, int index, boolean isSelected,
325: boolean cellHasFocus) {
326:
327: super .getListCellRendererComponent(list, value, index,
328: isSelected, cellHasFocus);
329: setIcon(((VisualClassPathItem) value).getIcon());
330: setToolTipText(value.toString());
331:
332: return this;
333: }
334:
335: }
336: }
|