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 org.netbeans.modules.bpel.project.IcanproConstants;
023: import org.netbeans.api.project.Project;
024: import org.netbeans.api.project.ant.AntArtifact;
025:
026: import org.openide.util.NbBundle;
027: import org.openide.util.RequestProcessor;
028: import org.openide.filesystems.FileUtil;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Node;
032: import org.w3c.dom.NodeList;
033:
034: import java.awt.Component;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037:
038: import java.io.*;
039:
040: import java.util.*;
041:
042: import javax.swing.*;
043: import javax.swing.event.ListSelectionEvent;
044: import javax.swing.event.ListSelectionListener;
045: import javax.swing.event.TableModelEvent;
046: import javax.swing.event.TableModelListener;
047: import javax.swing.table.AbstractTableModel;
048: import javax.swing.table.DefaultTableCellRenderer;
049:
050: import javax.xml.parsers.DocumentBuilderFactory;
051: import org.openide.filesystems.FileObject;
052: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
053:
054: /**
055: * Handles adding and removing of additional war content.
056: *
057: * @author
058: * @version
059: */
060:
061: final class VisualArchiveIncludesSupport {
062: /**
063: * DOCUMENT ME!
064: */
065: // static final String JBIPROJ_JAR_LOC = "/org-netbeans-modules-compapp-projects-jbi.jar";
066: private IcanproProjectProperties webProperties;
067:
068: /**
069: * DOCUMENT ME!
070: */
071: final Project master;
072:
073: /**
074: * DOCUMENT ME!
075: */
076: final JTable classpathTable;
077:
078: /**
079: * DOCUMENT ME!
080: */
081: final JButton addArtifactButton;
082:
083: /**
084: * DOCUMENT ME!
085: */
086: final JButton removeButton;
087:
088: /**
089: * DOCUMENT ME!
090: */
091: //final JTable jTableComp;
092: /**
093: * DOCUMENT ME!
094: */
095: //final JButton jButtonUpdate;
096: /**
097: * DOCUMENT ME!
098: */
099: final JButton jButtonConfig;
100: private final VisualArchiveIncludesSupport.ClasspathTableModel classpathModel;
101: private Object[][] data;
102: private final ArrayList actionListeners = new ArrayList();
103: // private ComponentTableModel mTableModel;
104: // private ComponentTableRenderer mTableRenderer;
105: private Vector mColumnNames;
106: private String nbuser;
107: private String compFilename;
108: private String compFileSrc;
109: private String compFileDst;
110: private String jbiFilename;
111: private String jbiFileLoc;
112: private JComboBox comboTarget = null;
113: private DefaultComboBoxModel comboModel = null;
114: private Vector comboValues = new Vector();
115: private Vector bindingList = null; // new Vector();
116: private AntArtifact bcjar = null;
117: private String mModuleDir = null;
118:
119: /**
120: * Creates a new VisualArchiveIncludesSupport object.
121: *
122: * @param webProperties DOCUMENT ME!
123: * @param jTableComp DOCUMENT ME!
124: * @param classpathTable DOCUMENT ME!
125: * @param jButtonUpdate DOCUMENT ME!
126: * @param jButtonConfig DOCUMENT ME!
127: * @param addArtifactButton DOCUMENT ME!
128: * @param removeButton DOCUMENT ME!
129: */
130: public VisualArchiveIncludesSupport(
131: IcanproProjectProperties webProperties, /*JTable jTableComp, */
132: JTable classpathTable,
133: /*JButton jButtonUpdate,*/JButton jButtonConfig,
134: JButton addArtifactButton, JButton removeButton) {
135: // Remember all buttons
136: this .webProperties = webProperties;
137: this .jButtonConfig = jButtonConfig;
138: this .jButtonConfig.setEnabled(false);
139:
140: this .classpathTable = classpathTable;
141: this .classpathModel = new VisualArchiveIncludesSupport.ClasspathTableModel();
142: this .classpathTable.setModel(classpathModel);
143: this .classpathTable.getColumnModel().getColumn(0)
144: .setHeaderValue(
145: NbBundle.getMessage(
146: VisualArchiveIncludesSupport.class,
147: "TXT_Archive_Item"));
148: this .classpathTable.getColumnModel().getColumn(1)
149: .setHeaderValue(
150: NbBundle.getMessage(
151: VisualArchiveIncludesSupport.class,
152: "TXT_Archive_PathInArchive"));
153: this .classpathTable
154: .getColumnModel()
155: .getColumn(0)
156: .setCellRenderer(
157: new VisualArchiveIncludesSupport.ClassPathCellRenderer());
158: this .classpathTable
159: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
160:
161: this .addArtifactButton = addArtifactButton;
162: this .removeButton = removeButton;
163: this .master = webProperties.getProject();
164:
165: // Register the listeners
166: VisualArchiveIncludesSupport.ClasspathSupportListener csl = new VisualArchiveIncludesSupport.ClasspathSupportListener();
167:
168: // On all buttons
169: this .jButtonConfig.addActionListener(csl);
170: this .addArtifactButton.addActionListener(csl);
171: this .removeButton.addActionListener(csl);
172:
173: // On list selection
174: classpathTable.getSelectionModel()
175: .addListSelectionListener(csl);
176: classpathModel.addTableModelListener(csl);
177:
178: // Set the initial state of the buttons
179: csl.valueChanged(null);
180: }
181:
182: /**
183: * DOCUMENT ME!
184: *
185: * @param items DOCUMENT ME!
186: */
187: public void setVisualWarItems(List items) {
188: Object[][] data = new Object[items.size()][2];
189: this .data = data;
190:
191: for (int i = 0; i < items.size(); i++) {
192: VisualClassPathItem vi = (VisualClassPathItem) items.get(i);
193: classpathModel.setValueAt(vi, i, 0);
194: classpathModel.setValueAt("", i, 1);
195: }
196:
197: classpathModel.fireTableDataChanged();
198: }
199:
200: /**
201: * DOCUMENT ME!
202: *
203: * @return DOCUMENT ME!
204: */
205: public List getVisualWarItems() {
206: ArrayList items = new ArrayList();
207:
208: for (int i = 0; i < data.length; i++)
209: items.add((VisualClassPathItem) classpathModel.getValueAt(
210: i, 0));
211:
212: return items;
213: }
214:
215: /**
216: * DOCUMENT ME!
217: *
218: * @param tml DOCUMENT ME!
219: */
220: public void addTableModelListener(TableModelListener tml) {
221: classpathModel.addTableModelListener(tml);
222: }
223:
224: /**
225: * DOCUMENT ME!
226: *
227: * @param tml DOCUMENT ME!
228: */
229: public void removeTableModelListener(TableModelListener tml) {
230: classpathModel.removeTableModelListener(tml);
231: }
232:
233: /**
234: * Action listeners will be informed when the value of the list changes.
235: *
236: * @param listener DOCUMENT ME!
237: */
238: public void addActionListener(ActionListener listener) {
239: actionListeners.add(listener);
240: }
241:
242: /**
243: * DOCUMENT ME!
244: *
245: * @param listener DOCUMENT ME!
246: */
247: public void removeActionListener(ActionListener listener) {
248: actionListeners.remove(listener);
249: }
250:
251: private void fireActionPerformed() {
252: ArrayList listeners;
253:
254: synchronized (this ) {
255: listeners = new ArrayList(actionListeners);
256: }
257:
258: ActionEvent ae = new ActionEvent(this , 0, null);
259:
260: for (Iterator it = listeners.iterator(); it.hasNext();) {
261: ActionListener al = (ActionListener) it.next();
262: al.actionPerformed(ae);
263: }
264: }
265:
266: // Private methods ---------------------------------------------------------
267: private void addArtifacts(AntArtifact[] artifacts) {
268: Object[][] newData = new Object[data.length + artifacts.length][2];
269:
270: for (int i = 0; i < data.length; i++)
271: newData[i] = data[i];
272:
273: for (int i = 0; i < artifacts.length; i++) {
274: VisualClassPathItem vi = new VisualClassPathItem(
275: artifacts[i], VisualClassPathItem.TYPE_ARTIFACT,
276: null, artifacts[i].getArtifactLocations()[0]
277: .toString(), true);
278:
279: newData[data.length + i][0] = vi;
280: newData[data.length + i][1] = artifacts[i].getType(); //tbd my test
281: // newData[data.length + i][1] = getDefaultTarget(vi.getAsaType());
282: }
283:
284: data = newData;
285: classpathModel.fireTableRowsInserted(data.length,
286: (data.length + artifacts.length) - 1);
287:
288: fireActionPerformed();
289: }
290:
291: private void removeElements() {
292: ListSelectionModel sm = classpathTable.getSelectionModel();
293: int index = sm.getMinSelectionIndex();
294:
295: if (sm.isSelectionEmpty()) {
296: assert false : "Remove button should be disabled"; // NOI18N
297: }
298:
299: Collection elements = new ArrayList();
300: final int n0 = data.length;
301:
302: for (int i = 0; i < n0; i++) {
303: if (!sm.isSelectedIndex(i)) {
304: elements.add(data[i]);
305: }
306: }
307:
308: final int n = elements.size();
309: data = (Object[][]) elements.toArray(new Object[n][2]);
310: classpathModel.fireTableRowsDeleted(elements.size(), n0 - 1);
311:
312: if (index >= n) {
313: index = n - 1;
314: }
315:
316: sm.setSelectionInterval(index, index);
317:
318: fireActionPerformed();
319: }
320:
321: private String parseTargetID(String str) {
322: if (str != null) {
323: int i = str.indexOf(" [");
324: int j = str.lastIndexOf(']');
325:
326: if ((i > 0) && (j > 0)) {
327: return str.substring(i + 2, j);
328: }
329: }
330:
331: return null;
332: }
333:
334: private String getDefaultTarget(String type) {
335: int tsize = comboValues.size();
336:
337: for (int i = 0; i < tsize; i++) {
338: String val = (String) comboValues.get(i);
339:
340: if (val.startsWith(type)) {
341: return val;
342: }
343: }
344:
345: return "";
346: }
347:
348: private void updateComboTarget() {
349: comboModel.removeAllElements();
350: comboModel.addElement(" ");
351:
352: for (int i = 0; i < comboValues.size(); i++) {
353: comboModel.addElement(comboValues.get(i));
354: }
355: }
356:
357: private void updateComboTargetWithType(String type) {
358: comboModel.removeAllElements();
359: comboModel.addElement(" ");
360:
361: if ((type == null) || (type.length() < 1)) {
362: return;
363: }
364:
365: for (int i = 0; i < comboValues.size(); i++) {
366: String val = (String) comboValues.get(i);
367:
368: if (val.startsWith(type)) {
369: comboModel.addElement(val);
370: }
371: }
372: }
373:
374: private void updateModels(String jar, String uuid, String desc,
375: String cid) {
376: for (int i = 0, size = classpathModel.getRowCount(); i < size; i++) {
377: VisualClassPathItem vi = (VisualClassPathItem) classpathModel
378: .getValueAt(i, 0);
379: // System.out.println(vi);
380: } //tbd temp add
381: }
382:
383: /**
384: * DOCUMENT ME!
385: */
386: public void initTableValues() {
387: // tbd
388: }
389:
390: private void updateAsaTarget() {
391: for (int i = 0, size = classpathModel.getRowCount(); i < size; i++) {
392: VisualClassPathItem vi = (VisualClassPathItem) classpathModel
393: .getValueAt(i, 0);
394: String tid = (String) classpathModel.getValueAt(i, 1);
395:
396: if ((tid == null) || (tid.trim().length() < 1)) {
397: // not set yet.. default to the first non-blank traget on the list
398: //tbd temp add classpathModel.setValueAt(getDefaultTarget(vi.getAsaType()), i, 1);
399: classpathModel.setValueAt("test", i, 1);
400: }
401: }
402: }
403:
404: private String getCompList(
405: VisualArchiveIncludesSupport.ClasspathTableModel classpathModel) {
406: String val = "";
407:
408: for (int i = 0; i < classpathModel.getRowCount(); i++) {
409: if (i > 0) {
410: val += ";";
411: }
412:
413: //String targetID = parseTargetID((String) classpathModel.getValueAt(i, 1));
414: String targetID = (String) classpathModel.getValueAt(i, 1);
415: val += (((targetID == null) || (targetID.length() < 1)) ? "null"
416: : targetID);
417: }
418:
419: return val;
420: }
421:
422: private class ClasspathSupportListener implements ActionListener,
423: ListSelectionListener, TableModelListener {
424: // Implementation of ActionListener ------------------------------------
425:
426: /**
427: * Handles button events
428: *
429: * @param e DOCUMENT ME!
430: */
431: public void actionPerformed(ActionEvent e) {
432: Object source = e.getSource();
433:
434: if (source == addArtifactButton) {
435: AntArtifact[] artifacts = AntArtifactChooser
436: .showDialog(
437: IcanproConstants.ARTIFACT_TYPE_EJB_WS,
438: master);
439:
440: if (artifacts != null) {
441: addArtifacts(artifacts);
442: }
443: } else if (source == removeButton) {
444: removeElements();
445: } else if (source == jButtonConfig) {
446: // removeElements();
447: }
448: }
449:
450: // ListSelectionModel --------------------------------------------------
451:
452: /**
453: * Handles changes in the selection
454: *
455: * @param e DOCUMENT ME!
456: */
457: public void valueChanged(ListSelectionEvent e) {
458: DefaultListSelectionModel sm = (DefaultListSelectionModel) classpathTable
459: .getSelectionModel();
460: int index = sm.getMinSelectionIndex();
461:
462: // remove enabled only if selection is not empty
463: boolean remove = index != -1;
464:
465: // and when the selection does not contain unremovable item
466: if (remove) {
467: VisualClassPathItem vcpi = (VisualClassPathItem) classpathModel
468: .getValueAt(index, 0);
469:
470: if (!vcpi.canDelete()) {
471: remove = false;
472: }
473: }
474:
475: removeButton.setEnabled(remove);
476: }
477:
478: // TableModelListener --------------------------------------
479: public void tableChanged(TableModelEvent e) {
480: // webProperties.put(
481: // IcanproProjectProperties.JBI_CONTENT_COMPONENT, getCompList(classpathModel)
482: // );
483:
484: if (e.getColumn() == 1) {
485: //VisualClassPathItem cpItem = (VisualClassPathItem) classpathModel.getValueAt(e.getFirstRow(), 0);
486: // cpItem.setPathInWAR((String) classpathModel.getValueAt(e.getFirstRow(), 1));
487: fireActionPerformed();
488: }
489: }
490: }
491:
492: private static class ClassPathCellRenderer extends
493: DefaultTableCellRenderer {
494: /**
495: * DOCUMENT ME!
496: *
497: * @param table DOCUMENT ME!
498: * @param value DOCUMENT ME!
499: * @param isSelected DOCUMENT ME!
500: * @param hasFocus DOCUMENT ME!
501: * @param row DOCUMENT ME!
502: * @param column DOCUMENT ME!
503: *
504: * @return DOCUMENT ME!
505: */
506: public Component getTableCellRendererComponent(JTable table,
507: Object value, boolean isSelected, boolean hasFocus,
508: int row, int column) {
509: if (value instanceof VisualClassPathItem) {
510: final VisualClassPathItem item = (VisualClassPathItem) value;
511: setIcon(item.getIcon());
512: }
513:
514: final String s = (value == null) ? null : value.toString();
515:
516: return super .getTableCellRendererComponent(table, s,
517: isSelected, false, row, column);
518: }
519: }
520:
521: /**
522: * DOCUMENT ME!
523: *
524: * @author
525: * @version
526: */
527: class ClasspathTableModel extends AbstractTableModel {
528: /**
529: * DOCUMENT ME!
530: *
531: * @return DOCUMENT ME!
532: */
533: public int getColumnCount() {
534: return 2; //classpath item name, item location within WAR
535: }
536:
537: /**
538: * DOCUMENT ME!
539: *
540: * @return DOCUMENT ME!
541: */
542: public int getRowCount() {
543: if (data == null) {
544: return 0;
545: }
546:
547: return data.length;
548: }
549:
550: /**
551: * DOCUMENT ME!
552: *
553: * @param row DOCUMENT ME!
554: * @param col DOCUMENT ME!
555: *
556: * @return DOCUMENT ME!
557: */
558: public Object getValueAt(int row, int col) {
559: return data[row][col];
560: }
561:
562: /**
563: * DOCUMENT ME!
564: *
565: * @param row DOCUMENT ME!
566: * @param col DOCUMENT ME!
567: *
568: * @return DOCUMENT ME!
569: */
570: public boolean isCellEditable(int row, int col) {
571: if (col == 1) {
572: return true;
573: } else {
574: return false;
575: }
576: }
577:
578: /**
579: * DOCUMENT ME!
580: *
581: * @param value DOCUMENT ME!
582: * @param row DOCUMENT ME!
583: * @param col DOCUMENT ME!
584: */
585: public void setValueAt(Object value, int row, int col) {
586: data[row][col] = value;
587: fireTableCellUpdated(row, col);
588: }
589: }
590:
591: private class TargetComboBoxEditor extends DefaultCellEditor {
592: /**
593: * DOCUMENT ME!
594: */
595: JComboBox combo = null;
596:
597: /**
598: * Creates a new TargetComboBoxEditor object.
599: *
600: * @param combo DOCUMENT ME!
601: */
602: public TargetComboBoxEditor(JComboBox combo) {
603: super (combo);
604: this .combo = combo;
605: }
606:
607: /**
608: * DOCUMENT ME!
609: *
610: * @param table DOCUMENT ME!
611: * @param value DOCUMENT ME!
612: * @param isSelected DOCUMENT ME!
613: * @param row DOCUMENT ME!
614: * @param column DOCUMENT ME!
615: *
616: * @return DOCUMENT ME!
617: */
618: public Component getTableCellEditorComponent(JTable table,
619: Object value, boolean isSelected, int row, int column) {
620: String type = null;
621: VisualClassPathItem vi = (VisualClassPathItem) classpathModel
622: .getValueAt(row, 0);
623:
624: if (vi != null) {
625: //type = vi.getAsaType();
626: type = "test";
627: }
628:
629: updateComboTargetWithType(type);
630:
631: return combo;
632: }
633: }
634: }
|