001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.ffj.wizards;
007:
008: import java.util.Set;
009: import java.util.Collections;
010: import java.util.Enumeration;
011:
012: import java.io.IOException;
013: import java.io.ObjectStreamException;
014: import java.io.File;
015:
016: import java.awt.Component;
017:
018: import javax.swing.event.ChangeListener;
019:
020: import org.openide.WizardDescriptor;
021: import org.openide.TopManager;
022: import org.openide.ErrorManager;
023: import org.openide.NotifyDescriptor;
024:
025: import org.openide.util.NbBundle;
026:
027: import org.openide.execution.NbClassPath;
028:
029: import org.openide.loaders.TemplateWizard;
030: import org.openide.loaders.DataObject;
031: import org.openide.loaders.DataFolder;
032:
033: import org.openide.filesystems.FileSystem;
034: import org.openide.filesystems.Repository;
035: import org.openide.filesystems.LocalFileSystem;
036: import org.openide.filesystems.FileObject;
037:
038: import com.sun.portal.ffj.filesystems.PSFileSystem;
039:
040: import com.sun.portal.ffj.util.PSConstants;
041: import com.sun.portal.ffj.util.PortletWebInf;
042:
043: /** Iterator implementation which can iterate through three
044: * panels which forms jar template wizard
045: */
046: public final class PortletAppWizardIterator implements
047: TemplateWizard.Iterator, PSConstants {
048:
049: /** No need to be public, access is provided through singleton()
050: * method call */
051: public PortletAppWizardIterator() {
052: super ();
053: }
054:
055: public void initialize(TemplateWizard tw) {
056: m_TemplateWizard = tw;
057:
058: if (m_ArrayIterator == null) {
059: initializePanelNames();
060: Component panel = m_TemplateWizard.targetChooser()
061: .getComponent();
062: if (panel instanceof javax.swing.JComponent) {
063: ((javax.swing.JComponent) panel).putClientProperty(
064: "WizardPanel_contentData", m_PanelNames);//NOI18N
065: }
066:
067: // build the array of panels
068: WizardDescriptor.Panel[] panelArray = initializePanels(tw);
069: m_ArrayIterator = new WizardDescriptor.ArrayIterator(
070: panelArray);
071: }
072:
073: }
074:
075: /** Initializes all panels of jar wizard. */
076: protected WizardDescriptor.Panel[] initializePanels(
077: TemplateWizard tw) {
078: m_TemplateWizard = tw;
079: m_WizDat = new PortletAppWizardData(tw);
080: WizardDescriptor.Panel[] panelArray = new WizardDescriptor.Panel[1];
081: panelArray[0] = new PortletAppPanel(m_WizDat);
082: return panelArray;
083: }
084:
085: public void initializePanelNames() {
086: if (m_PanelNames == null) {
087: m_PanelNames = new String[1];
088: m_PanelNames[0] = NbBundle.getMessage(
089: PortletAppWizardIterator.class,
090: "CTL_PortletAppWizard.PanelName");
091: }
092: }
093:
094: public void uninitialize(TemplateWizard tw) {
095: m_TemplateWizard = null;
096: m_ArrayIterator = null;
097: }
098:
099: //
100: // WizardDescriptorInterface methods delegated to the ArrayIterator
101: //
102:
103: /* The current panel.
104: */
105: public WizardDescriptor.Panel current() {
106: return m_ArrayIterator.current();
107: }
108:
109: /* Is there a next panel?
110: * @return true if so
111: */
112: public boolean hasNext() {
113: return m_ArrayIterator.hasNext();
114: }
115:
116: /* Is there a previous panel?
117: * @return true if so
118: */
119: public boolean hasPrevious() {
120: return m_ArrayIterator.hasNext();
121: }
122:
123: /* Moves to the next panel.
124: * @exception NoSuchElementException if the panel does not exist
125: */
126: public synchronized void nextPanel() {
127: m_ArrayIterator.nextPanel();
128: }
129:
130: /* Moves to previous panel.
131: * @exception NoSuchElementException if the panel does not exist
132: */
133: public synchronized void previousPanel() {
134: m_ArrayIterator.previousPanel();
135: }
136:
137: /* Ignores the listener, there are no changes in order of panels.
138: */
139: public void addChangeListener(ChangeListener l) {
140: m_ArrayIterator.addChangeListener(l);
141: }
142:
143: /* Ignored.
144: */
145: public void removeChangeListener(ChangeListener l) {
146: m_ArrayIterator.removeChangeListener(l);
147: }
148:
149: //
150: // WizardDescriptorInterface methods implemented here.
151: //
152:
153: public String name() {
154: return NbBundle.getMessage(PortletAppWizardIterator.class,
155: "PortletAppWizard.Title");
156: }
157:
158: public boolean validateDocumentPath() {
159: String documentBasePath = m_WizDat.getDirectory();
160: File file = new File(documentBasePath);
161: if (file.exists()) {
162: if (file.isDirectory()) {
163: return true;
164: } else {
165: Object[] args = { documentBasePath };
166: showErrorMessageI18N(
167: "MSG_PortletAppWizard.InvalidMountDirectory",
168: args); // NOI18N
169: return false;
170: }
171: } else {
172: //
173: // try to make the directory.
174: //
175: boolean success = file.mkdirs();
176:
177: if (success) {
178: return true;
179: } else {
180: Object[] args = { documentBasePath };
181: showErrorMessageI18N(
182: "MSG_PortletAppWizard.InvalidMountDirectory",
183: args); // NOI18N
184: return false;
185: }
186: }
187: }
188:
189: public Set instantiate(TemplateWizard wiz) throws IOException {
190: /* ***
191: String n = wiz.getTargetName();
192: if(n == null) return Collections.singleton(null);
193: FileSystem fs = TopManager.getDefault().getRepository().findFileSystem(n);
194: WebContextLoader.convert(fs);
195: return Collections.singleton(WebContextLoader.findContext(fs));
196: *** */
197:
198: // force creation of filesystem, if it has to happen, before we mount this one.
199: PSFileSystem.getPSFilesystem(true);
200:
201: String documentBasePath = m_WizDat.getDirectory();
202: if (documentBasePath == null || documentBasePath.length() <= 0
203: || !validateDocumentPath()) {
204: return Collections.singleton(null);
205: }
206:
207: // mount the documentBase
208: LocalFileSystem localFS = null;
209: FileSystem webModuleFS = null;
210: File pdfile = new File(documentBasePath + File.separator
211: + "WEB-INF" + File.separator + PORTLET_DESCRIPTOR_FILE);
212:
213: File f = new File(documentBasePath);
214: if ((f != null) && (f.isDirectory())) {
215: localFS = new org.openide.filesystems.LocalFileSystem(); // PENDING
216: try {
217: try {
218: f = f.getCanonicalFile();
219: } catch (java.io.IOException ex) {
220: // ignore - just use the non-canonical version
221: }
222: localFS.setRootDirectory(f);
223: Repository r = TopManager.getDefault().getRepository();
224: FileSystem existingFS = findAlreadyMountedFileSystem(r,
225: localFS);
226: if (existingFS == null) {
227: r.addFileSystem(localFS);
228: webModuleFS = (FileSystem) localFS;
229: } else {
230: if (pdfile.exists()) {
231: showErrorMessageI18N(
232: "MSG_PortletAppWizard.AlreadyMounted",
233: null);
234: return Collections.singleton(null);
235: }
236:
237: webModuleFS = (FileSystem) existingFS;
238: }
239: } catch (Exception ex) {
240: TopManager.getDefault().getErrorManager().notify(
241: ErrorManager.WARNING, ex);
242: }
243: }
244:
245: if (webModuleFS == null) {
246: return Collections.singleton(null);
247: }
248:
249: DataFolder projectFolder = (DataFolder) TopManager.getDefault()
250: .getPlaces().nodes().projectDesktop().getCookie(
251: DataFolder.class);
252: if (projectFolder != null) {
253: wiz.setTargetFolder(projectFolder);
254: }
255: wiz.setTargetName(webModuleFS.getSystemName());
256:
257: PortletWebInf pinf = new PortletWebInf(pdfile.getParentFile());
258: pinf.create();
259:
260: webModuleFS.getRoot().refresh();
261: FileObject fow = webModuleFS.findResource("WEB-INF");
262: if (fow != null) {
263: fow.refresh();
264: }
265:
266: // re-add to make contents show up.
267:
268: Repository r = TopManager.getDefault().getRepository();
269: r.removeFileSystem(webModuleFS);
270: r.addFileSystem(webModuleFS);
271:
272: return Collections.singleton(DataObject.find(webModuleFS
273: .getRoot()));
274: }
275:
276: public void debugPrintMountedFilesystems(Repository r) {
277: for (Enumeration fsen = r.fileSystems(); fsen.hasMoreElements();) {
278: FileSystem fs = (FileSystem) fsen.nextElement();
279: System.out.println(" " + fs.getSystemName());//NOI18N
280: }
281: }
282:
283: public FileSystem findAlreadyMountedFileSystem(Repository r,
284: FileSystem targetFs) throws IOException {
285: FileObject targetRoot = targetFs.getRoot();
286: String targetDisplayName = targetFs.getDisplayName();
287: String targetSystemName = targetFs.getSystemName();
288: File targetFile = NbClassPath.toFile(targetRoot);
289: String targetFileCan = targetFile.getCanonicalPath();
290:
291: for (Enumeration fsen = r.fileSystems(); fsen.hasMoreElements();) {
292: FileSystem fs = (FileSystem) fsen.nextElement();
293: boolean hidden = fs.isHidden();
294: if (!hidden) {
295: File fsFile = NbClassPath.toFile(fs.getRoot());
296: if (fsFile != null) {
297: String fsFileCan = fsFile.getCanonicalPath();
298: if (targetFileCan.equals(fsFileCan)) {
299: return fs;
300: }
301: String sn = fs.getSystemName();
302: if (targetSystemName.equals(sn)) {
303: return fs;
304: }
305: String dn = fs.getDisplayName();
306: if (targetDisplayName.equals(dn)) {
307: return fs;
308: }
309: if (targetSystemName.equals(dn)) {
310: return fs;
311: }
312: }
313: }
314: }
315: return null;
316: }
317:
318: /** @return Returns singleton instance of this iterator */
319: public static PortletAppWizardIterator singleton() {
320: if (m_Singleton == null) {
321: m_Singleton = new PortletAppWizardIterator();
322: }
323: return m_Singleton;
324: }
325:
326: /** Resolve to the singleton instance durign deserialization */
327: public Object readResolve() throws ObjectStreamException {
328: return singleton();
329: }
330:
331: private static void showErrorMessageI18N(String i18nErrorMsg,
332: Object[] args) {
333: String msgtext = NbBundle.getMessage(
334: PortletAppWizardIterator.class, i18nErrorMsg);
335: if (args != null) {
336: msgtext = java.text.MessageFormat.format(msgtext, args);
337: }
338: NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
339: msgtext, NotifyDescriptor.ERROR_MESSAGE);
340: msg.setTitle(NbBundle.getMessage(
341: PortletAppWizardIterator.class,
342: "PortletAppWizard.ErrDialogTitle")); // NOI18N
343: TopManager.getDefault().notify(msg);
344: }
345:
346: private transient PortletAppWizardData m_WizDat;
347: private transient TemplateWizard m_TemplateWizard = null;
348: private transient String[] m_PanelNames = null;
349:
350: //
351: // This gets created in the call to initialize(TemplateWizard)
352: //
353: private WizardDescriptor.ArrayIterator m_ArrayIterator = null;
354:
355: private static PortletAppWizardIterator m_Singleton = null;
356: }
|