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.project.jsf.api;
043:
044: import org.netbeans.modules.visualweb.api.portlet.dd.PortletModeType;
045: import org.netbeans.modules.visualweb.api.portlet.dd.PortletDDHelper;
046: import org.netbeans.modules.visualweb.project.jsf.*;
047: import java.io.File;
048: import java.io.IOException;
049: import java.text.MessageFormat;
050: import org.netbeans.api.project.Project;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileUtil;
053: import org.openide.util.NbBundle;
054:
055: /**
056: * This class implements the portlet support for a project.
057: * @author David Botterill
058: */
059: public class JsfPortletSupportImpl implements JsfPortletSupport {
060: private Project project;
061:
062: /**
063: * This constructor needs to be as FAST as possible since it will be called
064: * by a static method each time someone simply CHECKS for portlet support on
065: * a project.
066: * @param project The Project that this class will provide portlet support for.
067: */
068: public JsfPortletSupportImpl(Project project) {
069: this .project = project;
070: }
071:
072: /**
073: * This method will set the portlet name
074: * @param oldName the current name of the portlet to change.
075: * @param newName the new name to give to the portlet.
076: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if oldName or newName == null
077: */
078: public void setPortletName(String oldName, String newName)
079: throws JsfPortletSupportException {
080: if (null == oldName || null == newName) {
081: throw new JsfPortletSupportException(NbBundle.getMessage(
082: JsfPortletSupportImpl.class,
083: "MSG_JsfPortletSupportImpl_NAMES_NULL"));
084: }
085: /**
086: * Now find the portlet.xml file.
087: */
088:
089: File portletDDFile = getPortletDD();
090:
091: /**
092: * Create a helper and set the inital page.
093: */
094: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
095:
096: ddHelper.setPortletName(oldName, newName);
097: }
098:
099: /**
100: * This method will set the initial page for the portlet.
101: * @param inMode The PortletModeType to set the initial page for
102: * @param inFilePath The path of the file using the context relative path. The path should include the leading "/".
103: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if inMode or inFilePath == null
104: */
105: public void setInitialPage(PortletModeType inMode, String inFilePath)
106: throws JsfPortletSupportException {
107:
108: File initialFile = new File(inFilePath);
109: if (null == initialFile || null == inMode) {
110: throw new JsfPortletSupportException(NbBundle.getMessage(
111: JsfPortletSupportImpl.class,
112: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
113: }
114:
115: /**
116: * Now find the portlet.xml file.
117: */
118:
119: File portletDDFile = getPortletDD();
120:
121: /**
122: * Create a helper and set the inital page.
123: */
124: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
125: ddHelper.setInitialPage(inMode, inFilePath);
126:
127: }
128:
129: /**
130: * This method will set the initial VIEW page for the portlet.
131: * @param inMode The PortletModeType to set the initial page for
132: * @param inFileObject The FileObject of the file to set as the inital VIEW Page.
133: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if inFileObject == null
134: */
135: public void setInitialPage(PortletModeType inMode,
136: FileObject inFileObject) throws JsfPortletSupportException {
137: if (null == inFileObject) {
138: throw new JsfPortletSupportException(NbBundle.getMessage(
139: JsfPortletSupportImpl.class,
140: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
141: }
142: if (null == inMode) {
143: throw new JsfPortletSupportException(NbBundle.getMessage(
144: JsfPortletSupportImpl.class,
145: "MSG_JsfPortletSupportImpl_MODE_NULL"));
146: }
147:
148: /**
149: * First get the file to be made the initial page.
150: */
151: FileObject initialFO = inFileObject;
152: File initialFile = FileUtil.toFile(initialFO);
153: if (null == initialFile || null == initialFile.getName()
154: || initialFile.getName().equals("")) {
155: throw new JsfPortletSupportException(NbBundle.getMessage(
156: JsfPortletSupportImpl.class,
157: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
158: }
159:
160: /**
161: * Now find the portlet.xml file.
162: */
163:
164: File portletDDFile = getPortletDD();
165:
166: /**
167: * Create a helper and set the inital page.
168: */
169: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
170:
171: String initialPageName = initialFile.getName();
172: String initialPath = getPortletPageFolderPath(initialFO);
173: this
174: .setInitialPage(inMode, initialPath + "/"
175: + initialPageName);
176:
177: }
178:
179: /**
180: * This method will unset the given initial page to no initial page.
181: * @param inFileObject The FileObject of the file to set as the inital VIEW Page.
182: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if inFileObject == null
183: */
184: public void unsetInitialPage(FileObject inFileObject)
185: throws JsfPortletSupportException {
186: if (null == inFileObject) {
187: throw new JsfPortletSupportException(NbBundle.getMessage(
188: JsfPortletSupportImpl.class,
189: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
190: }
191: /**
192: * First get the file to be made the initial page.
193: */
194: FileObject initialFO = inFileObject;
195: File initialFile = FileUtil.toFile(initialFO);
196: if (null == initialFile || null == initialFile.getName()
197: || initialFile.getName().equals("")) {
198: throw new JsfPortletSupportException(NbBundle.getMessage(
199: JsfPortletSupportImpl.class,
200: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
201: }
202:
203: /**
204: * Now find the portlet.xml file.
205: */
206:
207: File portletDDFile = getPortletDD();
208:
209: /**
210: * Create a helper and set the inital page.
211: */
212: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
213: String initialPageName = initialFile.getName();
214: String initialPath = getPortletPageFolderPath(initialFO);
215: ddHelper.unsetInitialPage(initialPath + "/" + initialPageName);
216:
217: }
218:
219: /**
220: * This method will check whether a given page is the initial page for a given mode.
221: * @param inMode The PortletModeType to check for the initial page.
222: * @param inFileObject The FileObject of the file to set check as the initial page.
223: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if inFileObject == null
224: */
225: public boolean isInitialPage(PortletModeType inMode,
226: FileObject inFileObject) throws JsfPortletSupportException {
227: if (null == inFileObject) {
228: throw new JsfPortletSupportException(NbBundle.getMessage(
229: JsfPortletSupportImpl.class,
230: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
231: }
232: if (null == inMode) {
233: throw new JsfPortletSupportException(NbBundle.getMessage(
234: JsfPortletSupportImpl.class,
235: "MSG_JsfPortletSupportImpl_MODE_NULL"));
236: }
237:
238: /**
239: * First get the file to be made checked as initial page.
240: */
241: FileObject initialFO = inFileObject;
242:
243: File initialFile = FileUtil.toFile(initialFO);
244: if (null == initialFile || null == initialFile.getName()
245: || initialFile.getName().equals("")) {
246: throw new JsfPortletSupportException(NbBundle.getMessage(
247: JsfPortletSupportImpl.class,
248: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
249: }
250:
251: /**
252: * Now find the portlet.xml file.
253: */
254:
255: File portletDDFile = getPortletDD();
256:
257: /**
258: * Create a helper and set check the inital page.
259: */
260: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
261: String initialPageName = initialFile.getName();
262: String initialPath = getPortletPageFolderPath(initialFO);
263: return ddHelper.isInitialPage(inMode, initialPath + "/"
264: + initialPageName);
265:
266: }
267:
268: /**
269: * This method will check whether a given page is an initial page for any mode.
270: * @param inFileObject The FileObject of the file to set check as the initial page.
271: * @return the PortletModeType the page is the initial page for. If the page is not an initial page,
272: * null is returned.
273: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if inFileObject == null
274: */
275: public PortletModeType getPortletMode(FileObject inFileObject)
276: throws JsfPortletSupportException {
277: if (null == inFileObject) {
278: throw new JsfPortletSupportException(NbBundle.getMessage(
279: JsfPortletSupportImpl.class,
280: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
281: }
282:
283: /**
284: * First get the file to be made checked as initial page.
285: */
286: FileObject initialFO = inFileObject;
287:
288: File initialFile = FileUtil.toFile(initialFO);
289: if (null == initialFile || null == initialFile.getName()
290: || initialFile.getName().equals("")) {
291: throw new JsfPortletSupportException(NbBundle.getMessage(
292: JsfPortletSupportImpl.class,
293: "MSG_JsfPortletSupportImpl_DATAOBJECT_NULL"));
294: }
295:
296: /**
297: * Now find the portlet.xml file.
298: */
299:
300: File portletDDFile = getPortletDD();
301:
302: /**
303: * Create a helper and set check the inital page.
304: */
305: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
306: String initialPageName = initialFile.getName();
307: String initialPath = getPortletPageFolderPath(initialFO);
308: return ddHelper.isInitialPage(initialPath + "/"
309: + initialPageName);
310:
311: }
312:
313: /**
314: * This method return the page for the given initial mode.
315: * @param inMode The PortletModeType to check for the initial page.
316: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if inMode == null
317: */
318: public String getInitialPage(PortletModeType inMode)
319: throws JsfPortletSupportException {
320:
321: if (null == inMode) {
322: throw new JsfPortletSupportException(NbBundle.getMessage(
323: JsfPortletSupportImpl.class,
324: "MSG_JsfPortletSupportImpl_MODE_NULL"));
325: }
326:
327: /**
328: * Now find the portlet.xml file.
329: */
330:
331: File portletDDFile = getPortletDD();
332:
333: /**
334: * Create a helper and set the inital page.
335: */
336: PortletDDHelper ddHelper = new PortletDDHelper(portletDDFile);
337: return ddHelper.getInitialPage(inMode);
338:
339: }
340:
341: /**
342: * This method returns the portlet.xml file for the current project.
343: * @return File representing the "portlet.xml" file for the project.
344: * @throws org.netbeans.modules.visualweb.project.jsf.JsfPortletSupportException thrown if the portlet.xml file can not be found.
345: */
346:
347: public File getPortletDD() throws JsfPortletSupportException {
348: FileObject portletDDFO = project.getProjectDirectory()
349: .getFileObject(
350: JsfProjectConstants.PATH_WEB_INF
351: + "/portlet.xml"); // NOI18N
352: if (null == portletDDFO) {
353: String message = MessageFormat.format(NbBundle.getMessage(
354: JsfPortletSupportImpl.class,
355: "MSG_JsfPortletSupportImpl_PORTLETDDNOTFOUND"),
356: new Object[] { project.getProjectDirectory()
357: .getName() });
358: throw new JsfPortletSupportException(message);
359: }
360:
361: File portletDDFile = FileUtil.toFile(portletDDFO);
362:
363: return portletDDFile;
364: }
365:
366: /**
367: * This method returns the relative path of the folder for the given portlet page.
368: * @param inFO is the FileObject to get the relative Folder path for.
369: * @returns String representing the relative path of the folder for the given portlet relative to the
370: * portlet project. For example, if the inFO has a path of "/home/david/Creator/Projects/Portlet1/web/edit/EditPage.jsp"
371: * the folder "/edit" will be returned.
372: */
373: public String getPortletPageFolderPath(FileObject inFO) {
374: String returnPath = null;
375: FileObject webRoot = project.getProjectDirectory()
376: .getFileObject(JsfProjectConstants.PATH_DOC_ROOT);
377: if (webRoot == null) {
378: return null;
379: }
380: String webRootPath = webRoot.getPath();
381: String jspPath = inFO.getParent().getPath();
382: if (jspPath.startsWith(webRootPath)) {
383: returnPath = jspPath.substring(webRootPath.length());
384: }
385:
386: return returnPath;
387: }
388:
389: }
|