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-2006 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.j2ee.ddloaders.web.multiview;
043:
044: import org.netbeans.core.spi.multiview.*;
045: import org.openide.nodes.*;
046: import org.netbeans.modules.j2ee.dd.api.web.*;
047: import org.netbeans.modules.j2ee.ddloaders.web.*;
048: import org.netbeans.modules.xml.multiview.ui.*;
049: import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement;
050: import org.netbeans.modules.xml.multiview.Error;
051: import org.openide.util.NbBundle;
052: import org.netbeans.api.project.SourceGroup;
053: import org.openide.util.NbBundle;
054: import org.openide.util.RequestProcessor;
055: import org.openide.util.HelpCtx;
056:
057: /**
058: * @author mkuchtiak
059: */
060: public class PagesMultiViewElement extends ToolBarMultiViewElement
061: implements java.beans.PropertyChangeListener {
062: private SectionView view;
063: private ToolBarDesignEditor comp;
064: private DDDataObject dObj;
065: private WebApp webApp;
066: private PagesPanelFactory factory;
067: private javax.swing.Action addAction, removeAction;
068: private boolean needInit = true;
069: private int index;
070: private RequestProcessor.Task repaintingTask;
071: private static final String PAGES_MV_ID = DDDataObject.DD_MULTIVIEW_PREFIX
072: + DDDataObject.MULTIVIEW_PAGES;
073: private static final String HELP_ID_PREFIX = DDDataObject.HELP_ID_PREFIX_PAGES;
074:
075: /** Creates a new instance of DDMultiViewElement */
076: public PagesMultiViewElement(final DDDataObject dObj, int index) {
077: super (dObj);
078: this .dObj = dObj;
079: this .index = index;
080: comp = new ToolBarDesignEditor();
081: factory = new PagesPanelFactory(comp, dObj);
082: addAction = new AddAction(dObj, NbBundle.getMessage(
083: PagesMultiViewElement.class, "LBL_addJspPG"));
084: removeAction = new RemoveAction(NbBundle.getMessage(
085: PagesMultiViewElement.class, "LBL_remove"));
086: setVisualEditor(comp);
087: repaintingTask = RequestProcessor.getDefault().create(
088: new Runnable() {
089: public void run() {
090: javax.swing.SwingUtilities
091: .invokeLater(new Runnable() {
092: public void run() {
093: repaintView();
094: }
095: });
096: }
097: });
098: }
099:
100: public SectionView getSectionView() {
101: return view;
102: }
103:
104: public void componentShowing() {
105: super .componentShowing();
106: dObj.setLastOpenView(index);
107: if (needInit || !dObj.isDocumentParseable()) {
108: repaintView();
109: needInit = false;
110: }
111: }
112:
113: private void repaintView() {
114: webApp = dObj.getWebApp();
115: view = new PagesView(webApp);
116: comp.setContentView(view);
117: Object lastActive = comp.getLastActive();
118: if (lastActive != null) {
119: ((SectionView) view).openPanel(lastActive);
120: } else {
121: ((SectionView) view).openPanel("welcome_files"); //NOI18N
122: }
123: view.checkValidity();
124: dObj.checkParseable();
125: }
126:
127: public void componentOpened() {
128: super .componentOpened();
129: dObj.getWebApp().addPropertyChangeListener(this );
130: }
131:
132: public void componentClosed() {
133: super .componentClosed();
134: dObj.getWebApp().removePropertyChangeListener(this );
135: }
136:
137: public void propertyChange(java.beans.PropertyChangeEvent evt) {
138: if (!dObj.isChangedFromUI()) {
139: String name = evt.getPropertyName();
140: if (name.indexOf("WelcomeFileList") > 0 || //NOI18N
141: name.indexOf("JspConfig") > 0 || //NOI18N
142: name.indexOf("ErrorPage") > 0 || //NOI18N
143: name.indexOf("version") > 0) { //NOI18N
144: // repaint view if the wiew is active and something is changed with elements listed above
145: if (PAGES_MV_ID.equals(dObj.getSelectedPerspective()
146: .preferredID())) {
147: repaintingTask.schedule(100);
148: } else {
149: needInit = true;
150: }
151: }
152: }
153: }
154:
155: class PagesView extends SectionView {
156: private SectionContainer jspPGCont;
157: private Node groupsNode, welcomeFilesNode, errorPagesNode;
158:
159: PagesView(WebApp webApp) {
160: super (factory);
161: JspConfig jspConfig = null;
162: JspPropertyGroup[] groups = null;
163: boolean jspConfigSupported = true;
164: try {
165: jspConfig = webApp.getSingleJspConfig();
166:
167: } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {
168: jspConfigSupported = false;
169: }
170:
171: welcomeFilesNode = new WelcomeFilesNode();
172: addSection(new SectionPanel(this , welcomeFilesNode,
173: "welcome_files")); //NOI18N
174:
175: errorPagesNode = new ErrorPagesNode();
176: addSection(new SectionPanel(this , errorPagesNode,
177: "error_pages")); //NOI18N
178:
179: Children rootChildren = new Children.Array();
180:
181: if (jspConfigSupported) {
182: if (jspConfig == null) {
183: groups = new JspPropertyGroup[0];
184: } else
185: groups = jspConfig.getJspPropertyGroup();
186: Node[] nodes = new Node[groups.length];
187: Children ch = new Children.Array();
188: for (int i = 0; i < nodes.length; i++) {
189: nodes[i] = new JspPGNode(this , groups[i]);
190: }
191: ch.add(nodes);
192: // creatings JSP Groups node, section container for JSP groups
193: groupsNode = new SectionContainerNode(ch);
194: groupsNode.setDisplayName(NbBundle.getMessage(
195: PagesMultiViewElement.class,
196: "TTL_jspPropGroups"));
197: groupsNode.setName(HELP_ID_PREFIX
198: + "jspPropertyGroupsNode"); //NOI18N
199: jspPGCont = new SectionContainer(this , groupsNode,
200: NbBundle.getMessage(
201: PagesMultiViewElement.class,
202: "TTL_jspPropGroups"));
203: jspPGCont
204: .setHeaderActions(new javax.swing.Action[] { addAction });
205:
206: // creatings section panels for JSP groups
207: SectionPanel[] pan = new SectionPanel[groups.length];
208: for (int i = 0; i < nodes.length; i++) {
209: pan[i] = new SectionPanel(this , nodes[i],
210: getJspGroupTitle(groups[i]), groups[i]);
211: pan[i]
212: .setHeaderActions(new javax.swing.Action[] { removeAction });
213: jspPGCont.addSection(pan[i]);
214: }
215: addSection(jspPGCont);
216: rootChildren.add(new Node[] { welcomeFilesNode,
217: errorPagesNode, groupsNode });
218: } else {
219: addAction.setEnabled(false);
220: rootChildren.add(new Node[] { welcomeFilesNode,
221: errorPagesNode });
222: }
223: AbstractNode root = new AbstractNode(rootChildren);
224: setRoot(root);
225: }
226:
227: Node getJspPGsNode() {
228: return groupsNode;
229: }
230:
231: Node getWelcomeFilesNode() {
232: return welcomeFilesNode;
233: }
234:
235: SectionContainer getJspGroupsContainer() {
236: return jspPGCont;
237: }
238:
239: String getJspGroupTitle(JspPropertyGroup jspGroup) {
240: String name = jspGroup.getDefaultDisplayName();
241: if (name == null)
242: name = NbBundle.getMessage(PagesMultiViewElement.class,
243: "NODE_JSP_GROUP");
244:
245: String[] patterns = jspGroup.getUrlPattern();
246: StringBuffer buf = new StringBuffer();
247: for (int i = 0; i < patterns.length; i++) {
248: if (i > 0)
249: buf.append(", ");
250: buf.append(patterns[i]);
251: }
252: return NbBundle.getMessage(PagesMultiViewElement.class,
253: "TTL_JSP_GROUP", name, buf.toString());
254: }
255:
256: String getJspGroupNodeName(JspPropertyGroup jspGroup) {
257: String displayName = jspGroup.getDefaultDisplayName();
258: if (displayName != null)
259: return displayName;
260: else
261: return NbBundle.getMessage(PagesMultiViewElement.class,
262: "NODE_JSP_GROUP");
263: }
264:
265: public Error validateView() {
266: return SectionValidator.validatePages(webApp);
267: }
268:
269: }
270:
271: private class JspPGNode extends org.openide.nodes.AbstractNode {
272: private JspPropertyGroup group;
273:
274: JspPGNode(PagesView view, JspPropertyGroup group) {
275: super (org.openide.nodes.Children.LEAF);
276: this .group = group;
277: setDisplayName(view.getJspGroupNodeName(group));
278: setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/jspObject.gif"); //NOI18N
279: }
280:
281: public HelpCtx getHelpCtx() {
282: return new HelpCtx(HELP_ID_PREFIX + "jspPropertyGroupNode"); //NOI18N
283: }
284: }
285:
286: private class WelcomeFilesNode extends
287: org.openide.nodes.AbstractNode {
288: WelcomeFilesNode() {
289: super (org.openide.nodes.Children.LEAF);
290: setDisplayName(NbBundle.getMessage(
291: PagesMultiViewElement.class, "TTL_welcomeFiles"));
292: setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/htmlObject.gif"); //NOI18N
293: }
294:
295: public HelpCtx getHelpCtx() {
296: return new HelpCtx(HELP_ID_PREFIX + "welcomeFilesNode"); //NOI18N
297: }
298: }
299:
300: private class ErrorPagesNode extends org.openide.nodes.AbstractNode {
301: ErrorPagesNode() {
302: super (org.openide.nodes.Children.LEAF);
303: setDisplayName(NbBundle.getMessage(
304: PagesMultiViewElement.class, "TTL_errorPages"));
305: setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/htmlObject.gif"); //NOI18N
306: }
307:
308: public HelpCtx getHelpCtx() {
309: return new HelpCtx(HELP_ID_PREFIX + "errorPagesNode"); //NOI18N
310: }
311: }
312:
313: private class AddAction extends javax.swing.AbstractAction {
314:
315: AddAction(final DDDataObject dObj, String actionName) {
316: super (actionName);
317: char mnem = NbBundle.getMessage(
318: PagesMultiViewElement.class, "LBL_addJspPG_mnem")
319: .charAt(0);
320: putValue(MNEMONIC_KEY, new Integer((int) mnem));
321: }
322:
323: public void actionPerformed(java.awt.event.ActionEvent evt) {
324:
325: String[] labels = new String[] {
326: NbBundle.getMessage(PagesMultiViewElement.class,
327: "LBL_displayName"),
328: NbBundle.getMessage(PagesMultiViewElement.class,
329: "LBL_description"),
330: NbBundle.getMessage(PagesMultiViewElement.class,
331: "LBL_urlPatterns") };
332: char[] mnem = new char[] {
333: NbBundle.getMessage(PagesMultiViewElement.class,
334: "LBL_displayName_mnem").charAt(0),
335: NbBundle.getMessage(PagesMultiViewElement.class,
336: "LBL_description_mnem").charAt(0),
337: NbBundle.getMessage(PagesMultiViewElement.class,
338: "LBL_urlPatterns_mnem").charAt(0) };
339: String[] a11y_desc = new String[] {
340: NbBundle.getMessage(PagesMultiViewElement.class,
341: "ACSD_jsp_property_display_name"),
342: NbBundle.getMessage(PagesMultiViewElement.class,
343: "ACSD_jsp_property_desc"),
344: NbBundle.getMessage(PagesMultiViewElement.class,
345: "ACSD_jsp_property_url_pattern") };
346: boolean[] buttons = new boolean[] { false, false, true };
347: SimpleDialogPanel.DialogDescriptor descriptor = new SimpleDialogPanel.DialogDescriptor(
348: labels);
349: descriptor.setMnemonics(mnem);
350: descriptor.setButtons(buttons);
351: descriptor.setA11yDesc(a11y_desc);
352: descriptor
353: .setTextField(new boolean[] { true, false, true });
354:
355: final SimpleDialogPanel dialogPanel = new SimpleDialogPanel(
356: descriptor);
357:
358: dialogPanel.getAccessibleContext().setAccessibleName(
359: NbBundle.getMessage(PagesMultiViewElement.class,
360: "ACSD_add_jsp_property_group"));
361: dialogPanel.getAccessibleContext()
362: .setAccessibleDescription(
363: NbBundle.getMessage(
364: PagesMultiViewElement.class,
365: "ACSD_add_jsp_property_group"));
366:
367: dialogPanel.getCustomizerButtons()[0]
368: .addActionListener(new java.awt.event.ActionListener() {
369: public void actionPerformed(
370: java.awt.event.ActionEvent evt) {
371: try {
372: SourceGroup[] groups = DDUtils
373: .getDocBaseGroups(dObj);
374: org.openide.filesystems.FileObject fo = BrowseFolders
375: .showDialog(groups);
376: if (fo != null) {
377: String fileName = "/"
378: + DDUtils.getResourcePath(
379: groups, fo, '/',
380: true); //NOI18N
381: String oldValue = dialogPanel
382: .getValues()[2];
383: if (fileName.length() > 0) {
384: String newValue = DDUtils
385: .addItem(oldValue,
386: fileName, false);
387: if (!oldValue.equals(newValue)) {
388: dialogPanel
389: .getTextComponents()[2]
390: .setText(newValue);
391: }
392: }
393: }
394: } catch (java.io.IOException ex) {
395: }
396: }
397: });
398: EditDialog dialog = new EditDialog(dialogPanel, NbBundle
399: .getMessage(PagesMultiViewElement.class,
400: "TTL_JspPropertyGroup"), true) {
401: protected String validate() {
402: String[] values = dialogPanel.getValues();
403: String urlPatterns = values[2].trim();
404: return SectionValidator
405: .validateNewJspPropertyGroup(urlPatterns);
406: }
407: };
408: dialog.setValid(false); // disable OK button
409:
410: javax.swing.event.DocumentListener docListener = new EditDialog.DocListener(
411: dialog);
412: dialogPanel.getTextComponents()[2].getDocument()
413: .addDocumentListener(docListener);
414:
415: java.awt.Dialog d = org.openide.DialogDisplayer
416: .getDefault().createDialog(dialog);
417: d.setVisible(true);
418: dialogPanel.getTextComponents()[2].getDocument()
419: .removeDocumentListener(docListener);
420:
421: if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
422: dObj.modelUpdatedFromUI();
423: dObj.setChangedFromUI(true);
424: String[] values = dialogPanel.getValues();
425: String name = values[0];
426: String description = values[1];
427: String urls = values[2].trim();
428: try {
429: JspConfig jspConfig = null;
430: try {
431: jspConfig = webApp.getSingleJspConfig();
432: if (jspConfig == null) {
433: jspConfig = (JspConfig) webApp
434: .createBean("JspConfig"); //NOI18N
435: webApp.setJspConfig(jspConfig);
436: }
437: } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {
438: }
439: JspPropertyGroup group = (JspPropertyGroup) jspConfig
440: .createBean("JspPropertyGroup"); //NOI18N
441: if (name.length() > 0)
442: group.setDisplayName((String) name);
443: if (description.length() > 0)
444: group.setDescription((String) description);
445:
446: String[] patterns = DDUtils.getStringArray(urls);
447: group.setUrlPattern(patterns);
448: jspConfig.addJspPropertyGroup(group);
449:
450: PagesView view = (PagesView) comp.getContentView();
451: Node node = new JspPGNode(view, group);
452: view.getJspPGsNode().getChildren().add(
453: new Node[] { node });
454:
455: SectionPanel pan = new SectionPanel(view, node,
456: view.getJspGroupTitle(group), group);
457: pan
458: .setHeaderActions(new javax.swing.Action[] { removeAction });
459: view.getJspGroupsContainer().addSection(pan, true);
460: } catch (ClassNotFoundException ex) {
461: } finally {
462: dObj.setChangedFromUI(false);
463: }
464: }
465: }
466: }
467:
468: private class RemoveAction extends javax.swing.AbstractAction {
469:
470: RemoveAction(String actionName) {
471: super (actionName);
472: char mnem = NbBundle.getMessage(
473: PagesMultiViewElement.class, "LBL_remove_mnem")
474: .charAt(0);
475: putValue(MNEMONIC_KEY, new Integer((int) mnem));
476: }
477:
478: public void actionPerformed(java.awt.event.ActionEvent evt) {
479: org.openide.DialogDescriptor desc = new ConfirmDialog(
480: NbBundle.getMessage(PagesMultiViewElement.class,
481: "TXT_removeJspGroupConfirm"));
482: java.awt.Dialog dialog = org.openide.DialogDisplayer
483: .getDefault().createDialog(desc);
484: dialog.setVisible(true);
485: if (org.openide.DialogDescriptor.OK_OPTION.equals(desc
486: .getValue())) {
487: SectionPanel sectionPanel = ((SectionPanel.HeaderButton) evt
488: .getSource()).getSectionPanel();
489: JspPropertyGroup group = (JspPropertyGroup) sectionPanel
490: .getKey();
491: try {
492: // removing jsp-property-group from data model
493: dObj.modelUpdatedFromUI();
494: dObj.setChangedFromUI(true);
495: webApp.getSingleJspConfig().removeJspPropertyGroup(
496: group);
497:
498: // removing section
499: sectionPanel.getSectionView().removeSection(
500: sectionPanel.getNode());
501: } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {
502: } finally {
503: dObj.setChangedFromUI(false);
504: }
505: }
506: }
507: }
508: }
|