001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.complib.ui;
043:
044: import java.awt.Dialog;
045: import java.util.ArrayList;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.Set;
049:
050: import javax.swing.JButton;
051:
052: import org.netbeans.api.project.Project;
053: import org.netbeans.api.project.ProjectInformation;
054: import org.netbeans.api.project.ProjectUtils;
055: import org.netbeans.modules.visualweb.complib.ComplibServiceProvider;
056: import org.netbeans.modules.visualweb.complib.IdeUtil;
057: import org.netbeans.modules.visualweb.complib.SharedComplib;
058: import org.netbeans.modules.visualweb.complib.api.ComplibException;
059: import org.openide.DialogDescriptor;
060: import org.openide.DialogDisplayer;
061: import org.openide.awt.Mnemonics;
062: import org.openide.util.NbBundle;
063:
064: /**
065: * Temporary UI to implement shared component libraries.
066: *
067: * @author Edwin Goei
068: */
069: public class SharedComplibPanel extends javax.swing.JPanel {
070:
071: private static final ComplibServiceProvider csp = ComplibServiceProvider
072: .getInstance();
073:
074: private List<String> postedMessages;
075:
076: /** Creates new form SharedComplibPanel */
077: public SharedComplibPanel() {
078: initComponents();
079:
080: clearStatusMessage();
081:
082: initOptionsJList();
083:
084: updateSharedComplibList();
085: }
086:
087: private void clearStatusMessage() {
088: postedMessages = new ArrayList<String>();
089: messageJLabel.setText(" ");
090: }
091:
092: private void addErrorMessage(String message) {
093: if (postedMessages.size() == 0) {
094: messageJLabel.setText("Error: " + message);
095: postedMessages.add(message);
096: } else {
097: messageJLabel.setText("First Error: "
098: + postedMessages.get(0));
099: }
100: }
101:
102: private static class ProjectWrapper {
103: private Project project;
104:
105: public ProjectWrapper(Project project) {
106: this .project = project;
107: }
108:
109: public String toString() {
110: ProjectInformation info = ProjectUtils
111: .getInformation(project);
112: return info.getDisplayName();
113: }
114:
115: public Project getProject() {
116: return project;
117: }
118: }
119:
120: private void initOptionsJList() {
121: List<Project> projects = csp.getEligibleSharedComplibProjects();
122: final ProjectWrapper[] options = new ProjectWrapper[projects
123: .size()];
124: int i = 0;
125: for (Project project : projects) {
126: options[i++] = new ProjectWrapper(project);
127: }
128:
129: optionsJList.setModel(new javax.swing.AbstractListModel() {
130: public int getSize() {
131: return options.length;
132: }
133:
134: public Object getElementAt(int i) {
135: return options[i];
136: }
137: });
138:
139: // Set a default selection
140: if (options.length > 0) {
141: optionsJList.setSelectedIndex(0);
142: }
143: }
144:
145: /**
146: * This method is called from within the constructor to initialize the form.
147: * WARNING: Do NOT modify this code. The content of this method is always
148: * regenerated by the Form Editor.
149: */
150: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
151: private void initComponents() {
152: messageJLabel = new javax.swing.JLabel();
153: jPanel1 = new javax.swing.JPanel();
154: jLabel3 = new javax.swing.JLabel();
155: jScrollPane2 = new javax.swing.JScrollPane();
156: optionsJList = new javax.swing.JList();
157: addJButton = new javax.swing.JButton();
158: jLabel2 = new javax.swing.JLabel();
159: jPanel2 = new javax.swing.JPanel();
160: jLabel1 = new javax.swing.JLabel();
161: jScrollPane1 = new javax.swing.JScrollPane();
162: sharedComplibJList = new javax.swing.JList();
163: removeJButton = new javax.swing.JButton();
164: refreshJButton = new javax.swing.JButton();
165:
166: messageJLabel.setForeground(java.awt.Color.red);
167: messageJLabel.setText("Status message goes here");
168:
169: jPanel1.setBorder(javax.swing.BorderFactory
170: .createEtchedBorder());
171: jLabel3
172: .setText("Select an Open Shared Component Library Project*:");
173:
174: optionsJList.setModel(new javax.swing.AbstractListModel() {
175: String[] strings = { "Item 1", "Item 2", "Item 3",
176: "Item 4", "Item 5" };
177:
178: public int getSize() {
179: return strings.length;
180: }
181:
182: public Object getElementAt(int i) {
183: return strings[i];
184: }
185: });
186: jScrollPane2.setViewportView(optionsJList);
187:
188: addJButton.setText("Add");
189: addJButton
190: .addActionListener(new java.awt.event.ActionListener() {
191: public void actionPerformed(
192: java.awt.event.ActionEvent evt) {
193: addActionPerformed(evt);
194: }
195: });
196:
197: jLabel2
198: .setText("* Shared Component Library Project must contain a functional \"build/complib\" directory.");
199:
200: org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
201: jPanel1);
202: jPanel1.setLayout(jPanel1Layout);
203: jPanel1Layout
204: .setHorizontalGroup(jPanel1Layout
205: .createParallelGroup(
206: org.jdesktop.layout.GroupLayout.LEADING)
207: .add(
208: jPanel1Layout
209: .createSequentialGroup()
210: .addContainerGap()
211: .add(
212: jPanel1Layout
213: .createParallelGroup(
214: org.jdesktop.layout.GroupLayout.LEADING)
215: .add(
216: jScrollPane2,
217: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
218: 419,
219: Short.MAX_VALUE)
220: .add(jLabel3)
221: .add(addJButton)
222: .add(jLabel2))
223: .addContainerGap()));
224: jPanel1Layout
225: .setVerticalGroup(jPanel1Layout
226: .createParallelGroup(
227: org.jdesktop.layout.GroupLayout.LEADING)
228: .add(
229: jPanel1Layout
230: .createSequentialGroup()
231: .add(jLabel3)
232: .addPreferredGap(
233: org.jdesktop.layout.LayoutStyle.RELATED)
234: .add(
235: jScrollPane2,
236: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
237: 84,
238: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
239: .addPreferredGap(
240: org.jdesktop.layout.LayoutStyle.RELATED)
241: .add(addJButton)
242: .addPreferredGap(
243: org.jdesktop.layout.LayoutStyle.RELATED)
244: .add(jLabel2)
245: .addContainerGap(
246: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
247: Short.MAX_VALUE)));
248:
249: jPanel2.setBorder(javax.swing.BorderFactory
250: .createEtchedBorder());
251: jLabel1
252: .setText("Shared Component Libraries for Active Project:");
253:
254: sharedComplibJList
255: .setModel(new javax.swing.AbstractListModel() {
256: String[] strings = { "Item 1", "Item 2", "Item 3",
257: "Item 4", "Item 5" };
258:
259: public int getSize() {
260: return strings.length;
261: }
262:
263: public Object getElementAt(int i) {
264: return strings[i];
265: }
266: });
267: jScrollPane1.setViewportView(sharedComplibJList);
268:
269: removeJButton.setText("Remove");
270: removeJButton
271: .addActionListener(new java.awt.event.ActionListener() {
272: public void actionPerformed(
273: java.awt.event.ActionEvent evt) {
274: removeActionPerformed(evt);
275: }
276: });
277:
278: refreshJButton.setText("Refresh All");
279: refreshJButton
280: .addActionListener(new java.awt.event.ActionListener() {
281: public void actionPerformed(
282: java.awt.event.ActionEvent evt) {
283: refreshActionPerformed(evt);
284: }
285: });
286:
287: org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(
288: jPanel2);
289: jPanel2.setLayout(jPanel2Layout);
290: jPanel2Layout
291: .setHorizontalGroup(jPanel2Layout
292: .createParallelGroup(
293: org.jdesktop.layout.GroupLayout.LEADING)
294: .add(
295: jPanel2Layout
296: .createSequentialGroup()
297: .addContainerGap()
298: .add(
299: jPanel2Layout
300: .createParallelGroup(
301: org.jdesktop.layout.GroupLayout.LEADING)
302: .add(
303: jScrollPane1,
304: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
305: 419,
306: Short.MAX_VALUE)
307: .add(jLabel1)
308: .add(
309: jPanel2Layout
310: .createSequentialGroup()
311: .add(
312: removeJButton)
313: .addPreferredGap(
314: org.jdesktop.layout.LayoutStyle.RELATED)
315: .add(
316: refreshJButton)))
317: .addContainerGap()));
318: jPanel2Layout
319: .setVerticalGroup(jPanel2Layout
320: .createParallelGroup(
321: org.jdesktop.layout.GroupLayout.LEADING)
322: .add(
323: jPanel2Layout
324: .createSequentialGroup()
325: .addContainerGap()
326: .add(jLabel1)
327: .addPreferredGap(
328: org.jdesktop.layout.LayoutStyle.RELATED)
329: .add(
330: jScrollPane1,
331: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
332: 67,
333: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
334: .addPreferredGap(
335: org.jdesktop.layout.LayoutStyle.RELATED)
336: .add(
337: jPanel2Layout
338: .createParallelGroup(
339: org.jdesktop.layout.GroupLayout.BASELINE)
340: .add(
341: removeJButton)
342: .add(
343: refreshJButton))
344: .addContainerGap(
345: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
346: Short.MAX_VALUE)));
347:
348: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
349: this );
350: this .setLayout(layout);
351: layout
352: .setHorizontalGroup(layout
353: .createParallelGroup(
354: org.jdesktop.layout.GroupLayout.LEADING)
355: .add(
356: layout
357: .createSequentialGroup()
358: .addContainerGap()
359: .add(
360: layout
361: .createParallelGroup(
362: org.jdesktop.layout.GroupLayout.LEADING)
363: .add(
364: org.jdesktop.layout.GroupLayout.TRAILING,
365: jPanel1,
366: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
367: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
368: Short.MAX_VALUE)
369: .add(
370: jPanel2,
371: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
372: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
373: Short.MAX_VALUE)
374: .add(
375: messageJLabel))
376: .addContainerGap()));
377: layout
378: .setVerticalGroup(layout
379: .createParallelGroup(
380: org.jdesktop.layout.GroupLayout.LEADING)
381: .add(
382: layout
383: .createSequentialGroup()
384: .addContainerGap()
385: .add(
386: jPanel1,
387: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
388: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
389: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
390: .addPreferredGap(
391: org.jdesktop.layout.LayoutStyle.RELATED)
392: .add(
393: jPanel2,
394: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
395: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
396: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
397: .addPreferredGap(
398: org.jdesktop.layout.LayoutStyle.RELATED)
399: .add(messageJLabel)
400: .addContainerGap(
401: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
402: Short.MAX_VALUE)));
403: }// </editor-fold>//GEN-END:initComponents
404:
405: private void refreshActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_refreshActionPerformed
406: try {
407: csp.refreshSharedComplibsForActiveProject();
408: } catch (Exception e) {
409: addErrorMessage(e.getMessage());
410: IdeUtil.logError(e);
411: }
412: updateSharedComplibList();
413: }// GEN-LAST:event_refreshActionPerformed
414:
415: private void removeActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_removeActionPerformed
416: Object[] selected = sharedComplibJList.getSelectedValues();
417: Set<SharedComplib> selectedSet = new HashSet<SharedComplib>();
418: for (Object object : selected) {
419: selectedSet.add((SharedComplib) object);
420: }
421:
422: try {
423: csp.removeSharedComplibsFromActiveProject(selectedSet);
424: } catch (Exception e) {
425: addErrorMessage(e.getMessage());
426: IdeUtil.logError(e);
427: }
428: updateSharedComplibList();
429: }
430:
431: private void addActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addActionPerformed
432: ProjectWrapper pw = (ProjectWrapper) optionsJList
433: .getSelectedValue();
434: if (pw == null) {
435: addErrorMessage("Select a Shared Component Library");
436: return;
437: } else {
438: clearStatusMessage();
439: }
440:
441: Project project = pw.getProject();
442: try {
443: csp.addSharedComplibProject(project);
444: } catch (Exception e) {
445: addErrorMessage(e.getMessage());
446: IdeUtil.logError(e);
447: }
448: updateSharedComplibList();
449: }// GEN-LAST:event_addActionPerformed
450:
451: private void updateSharedComplibList() {
452: /*
453: * Get the shared complibs for the active project and convert to an
454: * array for the JList model
455: */
456: SharedComplib[] complibsArray0;
457: try {
458: Set<SharedComplib> complibs = csp
459: .getSharedComplibsForActiveProject();
460: complibsArray0 = (SharedComplib[]) complibs
461: .toArray(new SharedComplib[complibs.size()]);
462: } catch (ComplibException e) {
463: addErrorMessage(e.getMessage());
464: IdeUtil.logError(e);
465: complibsArray0 = new SharedComplib[0];
466: }
467:
468: final SharedComplib[] complibsArray = complibsArray0;
469: sharedComplibJList
470: .setModel(new javax.swing.AbstractListModel() {
471: public int getSize() {
472: return complibsArray.length;
473: }
474:
475: public Object getElementAt(int i) {
476: return complibsArray[i];
477: }
478: });
479: }
480:
481: // Variables declaration - do not modify//GEN-BEGIN:variables
482: private javax.swing.JButton addJButton;
483: private javax.swing.JLabel jLabel1;
484: private javax.swing.JLabel jLabel2;
485: private javax.swing.JLabel jLabel3;
486: private javax.swing.JPanel jPanel1;
487: private javax.swing.JPanel jPanel2;
488: private javax.swing.JScrollPane jScrollPane1;
489: private javax.swing.JScrollPane jScrollPane2;
490: private javax.swing.JLabel messageJLabel;
491: private javax.swing.JList optionsJList;
492: private javax.swing.JButton refreshJButton;
493: private javax.swing.JButton removeJButton;
494: private javax.swing.JList sharedComplibJList;
495:
496: // End of variables declaration//GEN-END:variables
497:
498: /**
499: * This method is the main entry point for this dialog panel
500: *
501: */
502: public void showDialog() {
503: String title = NbBundle.getMessage(SharedComplibPanel.class,
504: "sharedComplib.dialogTitle"); // NOI18N
505: JButton closeButton = new JButton();
506: Mnemonics.setLocalizedText(closeButton, NbBundle.getMessage(
507: SharedComplibPanel.class, "sharedComplib.closeButton"));
508:
509: Object[] options = new Object[] { closeButton };
510: DialogDescriptor descriptor = new DialogDescriptor(this , title,
511: true, options, closeButton,
512: DialogDescriptor.DEFAULT_ALIGN, null, null);
513: Dialog dialog = DialogDisplayer.getDefault().createDialog(
514: descriptor);
515: descriptor.setValid(true);
516:
517: dialog.setVisible(true);
518: }
519: }
|