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.actions;
043:
044: import org.netbeans.modules.visualweb.project.jsf.api.JsfPortletSupport;
045: import org.netbeans.modules.visualweb.project.jsf.api.JsfPortletSupportException;
046: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
047:
048: import java.awt.event.ActionEvent;
049: import javax.swing.Action;
050: import javax.swing.AbstractAction;
051:
052: import org.openide.ErrorManager;
053: import org.openide.awt.StatusDisplayer;
054: import org.openide.filesystems.FileObject;
055: import org.openide.loaders.DataObject;
056: import org.openide.util.ContextAwareAction;
057: import org.openide.util.Lookup;
058: import org.openide.util.NbBundle;
059:
060: import org.netbeans.api.project.FileOwnerQuery;
061: import org.netbeans.api.project.Project;
062: import org.netbeans.modules.visualweb.api.portlet.dd.PortletModeType;
063: import java.awt.event.ActionListener;
064: import java.awt.event.KeyEvent;
065: import java.io.File;
066: import java.io.IOException;
067: import javax.swing.ButtonGroup;
068: import javax.swing.JMenu;
069: import javax.swing.JMenuItem;
070: import javax.swing.JRadioButtonMenuItem;
071: import javax.swing.event.MenuEvent;
072: import javax.swing.event.MenuListener;
073: import org.openide.awt.JMenuPlus;
074: import org.openide.awt.Mnemonics;
075: import org.openide.filesystems.FileUtil;
076: import org.openide.loaders.DataNode;
077: import org.openide.loaders.DataObjectNotFoundException;
078: import org.openide.util.actions.Presenter;
079:
080: /**
081: * Action for setting the start (initial) page.
082: * Formerly there were two actions put into project/jsfloader module, which was not a proper place.
083: *
084: * @author Peter Zavadsky (refactored previous actions)
085: * @author Mark Dey (originally action for jsp)
086: * @author David Botterill (originally action for portlet)
087: */
088: public class SetStartPageAction extends AbstractAction implements
089: Presenter.Menu, Presenter.Popup, ContextAwareAction {
090:
091: private static final int TYPE_NONE = 0;
092: private static final int TYPE_JSP = 1;
093: private static final int TYPE_PORTLET = 2;
094:
095: private final int type;
096:
097: private final FileObject fo;
098: private final DataObject dataObject;
099:
100: /** Creates a new instance of SetStartPageAction */
101: public SetStartPageAction() {
102: this (TYPE_NONE, null); // Fake action -> The context aware is real one, drawback of the NB design?
103: }
104:
105: private SetStartPageAction(int type, DataObject inDataObject) {
106: this .type = type;
107: this .dataObject = inDataObject;
108:
109: if (null != inDataObject) {
110: this .fo = dataObject.getPrimaryFile();
111: } else
112: this .fo = null;
113:
114: String name;
115: if (type == TYPE_JSP) {
116: name = NbBundle.getMessage(SetStartPageAction.class,
117: "LBL_SetAsStartPage");
118: } else if (type == TYPE_PORTLET) {
119: name = NbBundle.getMessage(SetStartPageAction.class,
120: "LBL_SetInitalPageAction_SETINITIALVIEWPAGE");
121: } else {
122: name = null;
123: }
124: putValue(Action.NAME, name);
125: }
126:
127: public void actionPerformed(ActionEvent evt) {
128: if (type == TYPE_JSP) {
129: // Copy from previous SetAsStartPageAction (Mark)
130: String newStartPage = JsfProjectUtils.setStartPage(fo);
131: String msg = newStartPage != null ? NbBundle.getMessage(
132: SetStartPageAction.class, "MSG_StartPageChanged")
133: + " " + newStartPage : NbBundle.getMessage(
134: SetStartPageAction.class, "MSG_NoStartPage");
135:
136: StatusDisplayer.getDefault().setStatusText(msg);
137: } else if (type == TYPE_PORTLET) {
138: // Copy from previous SetInitialPageAction (David)
139: Project project = FileOwnerQuery.getOwner(fo);
140: if (project == null)
141: return;
142: JsfPortletSupport portletSupport = JsfProjectUtils
143: .getPortletSupport(project);
144: if (portletSupport == null)
145: return;
146: String actionCommand = evt.getActionCommand();
147:
148: try {
149: /**
150: * Fix for CR 6337056. Need to get the root path to the JSP files to be able
151: * to set the currently set initial page icon back to the default one.
152: * -David Botterill 10/14/2005
153: */
154: /**
155: * We need the path to the JSP root directory so we can use it to set the icons.
156: */
157:
158: FileObject jspRootFO = JsfProjectUtils
159: .getDocumentRoot(project);
160: File jspRootFile = FileUtil.toFile(jspRootFO);
161: String dataNodePath = null;
162: try {
163: dataNodePath = jspRootFile.getCanonicalPath();
164: } catch (IOException ioe) {
165: ErrorManager.getDefault().notify(
166: ErrorManager.EXCEPTION, ioe);
167: }
168: if (actionCommand.equals(NbBundle.getMessage(
169: SetStartPageAction.class, "MNU_VIEWMODE"))) {
170: /**
171: * Set the icon for the current one for this mode to the default icon.
172: */
173: String currentViewPage = portletSupport
174: .getInitialPage(PortletModeType.VIEW);
175: if (null != currentViewPage) {
176: FileObject currentFO = FileUtil
177: .toFileObject(new File(dataNodePath
178: + File.separator
179: + currentViewPage));
180: /**
181: * Fix for CR 6329425
182: * Make sure the page is found since the user may have deleted the page after they set the initial
183: * mode.
184: * -David Botterill 9/28/2005
185: */
186: if (null != currentFO) {
187: try {
188: DataObject currentDO = DataObject
189: .find(currentFO);
190: ((DataNode) currentDO.getNodeDelegate())
191: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/jsfJspObject.gif"); //NOI18N
192: } catch (DataObjectNotFoundException donfe) {
193: NbBundle.getMessage(
194: SetStartPageAction.class,
195: "MSG_UnableToSetDefaultIcon",
196: currentViewPage);
197: }
198: }
199:
200: }
201: portletSupport.setInitialPage(PortletModeType.VIEW,
202: fo);
203: /**
204: * Now set the right data node with the mode icon.
205: */
206: ((DataNode) dataObject.getNodeDelegate())
207: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/initialviewpage.png"); //NOI18N
208: ((DataNode) dataObject.getNodeDelegate())
209: .setShortDescription(NbBundle.getMessage(
210: SetStartPageAction.class,
211: "LBL_InitialViewShortDesc"));
212: } else if (actionCommand.equals(NbBundle.getMessage(
213: SetStartPageAction.class, "MNU_EDITMODE"))) {
214: /**
215: * Set the icon for the current one for this mode to the default icon.
216: */
217: String currentEditPage = portletSupport
218: .getInitialPage(PortletModeType.EDIT);
219: if (null != currentEditPage) {
220: FileObject currentFO = FileUtil
221: .toFileObject(new File(dataNodePath
222: + File.separator
223: + currentEditPage));
224: /**
225: * Fix for CR 6329425
226: * Make sure the page is found since the user may have deleted the page after they set the initial
227: * mode.
228: * -David Botterill 9/28/2005
229: */
230: if (null != currentFO) {
231: try {
232: DataObject currentDO = DataObject
233: .find(currentFO);
234: ((DataNode) currentDO.getNodeDelegate())
235: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/jsfJspObject.gif"); //NOI18N
236: } catch (DataObjectNotFoundException donfe) {
237: NbBundle.getMessage(
238: SetStartPageAction.class,
239: "MSG_UnableToSetDefaultIcon",
240: currentEditPage);
241: }
242: }
243:
244: }
245: portletSupport.setInitialPage(PortletModeType.EDIT,
246: fo);
247: /**
248: * Now set the right data node with the mode icon.
249: */
250: ((DataNode) dataObject.getNodeDelegate())
251: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/initialeditpage.png");//NOI18N
252: ((DataNode) dataObject.getNodeDelegate())
253: .setShortDescription(NbBundle.getMessage(
254: SetStartPageAction.class,
255: "LBL_InitialEditShortDesc"));
256: } else if (actionCommand.equals(NbBundle.getMessage(
257: SetStartPageAction.class, "MNU_HELPMODE"))) {
258: /**
259: * Set the icon for the current one for this mode to the default icon.
260: */
261: String currentHelpPage = portletSupport
262: .getInitialPage(PortletModeType.HELP);
263: FileObject currentFO = FileUtil
264: .toFileObject(new File(dataNodePath
265: + File.separator + currentHelpPage));
266: /**
267: * Fix for CR 6329425
268: * Make sure the page is found since the user may have deleted the page after they set the initial
269: * mode.
270: * -David Botterill 9/28/2005
271: */
272: if (null != currentFO) {
273: try {
274: DataObject currentDO = DataObject
275: .find(currentFO);
276: ((DataNode) currentDO.getNodeDelegate())
277: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/jsfJspObject.gif"); //NOI18N
278: } catch (DataObjectNotFoundException donfe) {
279: NbBundle.getMessage(
280: SetStartPageAction.class,
281: "MSG_UnableToSetDefaultIcon",
282: currentHelpPage);
283: }
284: }
285: portletSupport.setInitialPage(PortletModeType.HELP,
286: fo);
287:
288: /**
289: * Now set the right data node with the mode icon.
290: */
291: ((DataNode) dataObject.getNodeDelegate())
292: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/initialhelppage.png");//NOI18N
293: ((DataNode) dataObject.getNodeDelegate())
294: .setShortDescription(NbBundle.getMessage(
295: SetStartPageAction.class,
296: "LBL_InitialHelpShortDesc"));
297: } else if (actionCommand.equals(NbBundle.getMessage(
298: SetStartPageAction.class, "MNU_NONEMODE"))) {
299: /**
300: * Unset the page as an initial page.
301: */
302: portletSupport.unsetInitialPage(fo);
303:
304: /**
305: * Now set the icon to the default icon.
306: */
307: try {
308: DataObject currentDO = DataObject.find(fo);
309: ((DataNode) currentDO.getNodeDelegate())
310: .setIconBase("org/netbeans/modules/visualweb/project/jsfloader/resources/jsfJspObject.gif"); //NOI18N
311: } catch (DataObjectNotFoundException donfe) {
312: NbBundle.getMessage(SetStartPageAction.class,
313: "MSG_UnableToSetDefaultIcon", fo
314: .getNameExt());
315: }
316:
317: }
318: } catch (JsfPortletSupportException jpse) {
319: ErrorManager.getDefault().notify(
320: ErrorManager.EXCEPTION, jpse);
321: }
322: }
323: }
324:
325: public Action createContextAwareInstance(Lookup context) {
326: DataObject dob = (DataObject) context.lookup(DataObject.class);
327:
328: if (dob == null) {
329: ErrorManager.getDefault().notify(
330: ErrorManager.INFORMATIONAL,
331: new IllegalStateException(
332: "SetStartPageAction: missing DataObject instance in the context, context="
333: + context)); // NOI18N
334: return null;
335: }
336:
337: FileObject fo = dob.getPrimaryFile();
338: if (isJspInPortletProject(fo)) {
339: return new SetStartPageAction(TYPE_PORTLET, dob);
340: } else {
341: return new SetStartPageAction(TYPE_JSP, dob);
342: }
343: }
344:
345: // Implementation of Presenter.Menu -----------------------------------
346:
347: public JMenuItem getMenuPresenter() {
348: if (isJspInPortletProject(fo)) {
349: JMenu mainItem = new JMenuPlus();
350: String name = NbBundle.getMessage(SetStartPageAction.class,
351: "LBL_SetInitalPageAction_SETINITIALVIEWPAGE");
352: Mnemonics.setLocalizedText(mainItem, name);
353: mainItem.addMenuListener(new InitialItemListener(this ));
354:
355: return mainItem;
356: } else {
357: JMenuItem mainItem = new JMenuItem();
358: String name = NbBundle.getMessage(SetStartPageAction.class,
359: "LBL_SetAsStartPage");
360: mainItem.setEnabled(!(JsfProjectUtils.isStartPage(fo) || fo
361: .getExt().equalsIgnoreCase("jspf")));
362: Mnemonics.setLocalizedText(mainItem, name);
363: mainItem.addActionListener(this );
364: return mainItem;
365: }
366: }
367:
368: // Implementation of Presenter.Popup ----------------------------------
369:
370: public JMenuItem getPopupPresenter() {
371: if (isJspInPortletProject(fo)) {
372: JMenu mainItem = new JMenuPlus();
373: String name = NbBundle.getMessage(SetStartPageAction.class,
374: "LBL_SetInitalPageAction_SETINITIALVIEWPAGE");
375: Mnemonics.setLocalizedText(mainItem, name);
376: mainItem.addMenuListener(new InitialItemListener(this ));
377:
378: return mainItem;
379: } else {
380: JMenuItem mainItem = new JMenuItem();
381: String name = NbBundle.getMessage(SetStartPageAction.class,
382: "LBL_SetAsStartPage");
383: mainItem.setEnabled(!(JsfProjectUtils.isStartPage(fo) || fo
384: .getExt().equalsIgnoreCase("jspf")));
385: Mnemonics.setLocalizedText(mainItem, name);
386: mainItem.addActionListener(this );
387: return mainItem;
388: }
389: }
390:
391: private static boolean isJspInPortletProject(FileObject fo) {
392: //check for jsp extension
393: if (fo.getExt().compareToIgnoreCase("jsp") == 0) { // NOI18N
394: //check if this is a portlet project
395: Project this Proj = FileOwnerQuery.getOwner(fo);
396: if (JsfProjectUtils.getPortletSupport(this Proj) != null) {
397: return true;
398: }
399: }
400: return false;
401: }
402:
403: /** Listens to selection of the INITIAL menu item and expands it
404: * into a submenu listing INITIAL modes.
405: */
406: private class InitialItemListener implements MenuListener {
407: ActionListener actionListener;
408:
409: public InitialItemListener(ActionListener inListener) {
410: this .actionListener = inListener;
411: }
412:
413: public void menuCanceled(MenuEvent e) {
414: }
415:
416: public void menuDeselected(MenuEvent e) {
417: JMenu menu = (JMenu) e.getSource();
418: menu.removeAll();
419: }
420:
421: public void menuSelected(MenuEvent e) {
422: JMenu menu = (JMenu) e.getSource();
423: ButtonGroup group = new ButtonGroup();
424:
425: JRadioButtonMenuItem rbViewItem = new JRadioButtonMenuItem(
426: NbBundle.getMessage(SetStartPageAction.class,
427: "MNU_VIEWMODE"));
428: rbViewItem.addActionListener(actionListener);
429: group.add(rbViewItem);
430: menu.add(rbViewItem);
431: rbViewItem
432: .setMnemonic(NbBundle.getMessage(
433: SetStartPageAction.class, "MNE_VIEWMODE")
434: .charAt(0));
435:
436: JRadioButtonMenuItem rbEditItem = new JRadioButtonMenuItem(
437: NbBundle.getMessage(SetStartPageAction.class,
438: "MNU_EDITMODE"));
439: rbEditItem.addActionListener(actionListener);
440: group.add(rbEditItem);
441: menu.add(rbEditItem);
442: rbEditItem
443: .setMnemonic(NbBundle.getMessage(
444: SetStartPageAction.class, "MNE_EDITMODE")
445: .charAt(0));
446:
447: JRadioButtonMenuItem rbHelpItem = new JRadioButtonMenuItem(
448: NbBundle.getMessage(SetStartPageAction.class,
449: "MNU_HELPMODE"));
450: rbHelpItem.addActionListener(actionListener);
451: group.add(rbHelpItem);
452: menu.add(rbHelpItem);
453: rbHelpItem
454: .setMnemonic(NbBundle.getMessage(
455: SetStartPageAction.class, "MNE_HELPMODE")
456: .charAt(0));
457:
458: JRadioButtonMenuItem rbNoneItem = new JRadioButtonMenuItem(
459: NbBundle.getMessage(SetStartPageAction.class,
460: "MNU_NONEMODE"));
461: rbNoneItem.addActionListener(actionListener);
462: group.add(rbNoneItem);
463: menu.add(rbNoneItem);
464: rbNoneItem
465: .setMnemonic(NbBundle.getMessage(
466: SetStartPageAction.class, "MNE_NONEMODE")
467: .charAt(0));
468:
469: /**
470: * Now set the one that is currently selected.
471: */
472: Project project = FileOwnerQuery.getOwner(fo);
473: if (project == null)
474: return;
475: JsfPortletSupport portletSupport = JsfProjectUtils
476: .getPortletSupport(project);
477: if (null == portletSupport)
478: return;
479: try {
480: if (portletSupport.isInitialPage(PortletModeType.VIEW,
481: fo)) {
482: rbViewItem.setSelected(true);
483: } else if (portletSupport.isInitialPage(
484: PortletModeType.EDIT, fo)) {
485: rbEditItem.setSelected(true);
486: } else if (portletSupport.isInitialPage(
487: PortletModeType.HELP, fo)) {
488: rbHelpItem.setSelected(true);
489: } else {
490: rbNoneItem.setSelected(true);
491: }
492: } catch (JsfPortletSupportException jpse) {
493: ErrorManager.getDefault().notify(
494: ErrorManager.INFORMATIONAL, jpse);
495: }
496:
497: }
498: }
499:
500: }
|