KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > PagesMultiViewElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.web.multiview;
21
22 import org.netbeans.core.spi.multiview.*;
23 import org.openide.nodes.*;
24 import org.netbeans.modules.j2ee.dd.api.web.*;
25 import org.netbeans.modules.j2ee.ddloaders.web.*;
26 import org.netbeans.modules.xml.multiview.ui.*;
27 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement;
28 import org.netbeans.modules.xml.multiview.Error;
29 import org.openide.util.NbBundle;
30 import org.netbeans.api.project.SourceGroup;
31 import org.openide.util.NbBundle;
32 import org.openide.util.RequestProcessor;
33 import org.openide.util.HelpCtx;
34
35 /**
36  * @author mkuchtiak
37  */

38 public class PagesMultiViewElement extends ToolBarMultiViewElement implements java.beans.PropertyChangeListener JavaDoc {
39     private SectionView view;
40     private ToolBarDesignEditor comp;
41     private DDDataObject dObj;
42     private WebApp webApp;
43     private PagesPanelFactory factory;
44     private javax.swing.Action JavaDoc addAction, removeAction;
45     private boolean needInit=true;
46     private int index;
47     private RequestProcessor.Task repaintingTask;
48     private static final String JavaDoc PAGES_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_PAGES;
49     private static final String JavaDoc HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_PAGES;
50     
51     /** Creates a new instance of DDMultiViewElement */
52     public PagesMultiViewElement(final DDDataObject dObj, int index) {
53         super(dObj);
54         this.dObj=dObj;
55         this.index=index;
56         comp = new ToolBarDesignEditor();
57         factory = new PagesPanelFactory(comp, dObj);
58         addAction = new AddAction(dObj, NbBundle.getMessage(PagesMultiViewElement.class,"LBL_addJspPG"));
59         removeAction = new RemoveAction(NbBundle.getMessage(PagesMultiViewElement.class,"LBL_remove"));
60         setVisualEditor(comp);
61         repaintingTask = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
62             public void run() {
63                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
64                     public void run() {
65                         repaintView();
66                     }
67                 });
68             }
69         });
70     }
71     
72     
73     public SectionView getSectionView() {
74         return view;
75     }
76     
77     public void componentShowing() {
78         super.componentShowing();
79         dObj.setLastOpenView(index);
80         if (needInit || !dObj.isDocumentParseable()) {
81             repaintView();
82             needInit = false;
83         }
84     }
85     
86     private void repaintView() {
87         webApp = dObj.getWebApp();
88         view =new PagesView(webApp);
89         comp.setContentView(view);
90         Object JavaDoc lastActive = comp.getLastActive();
91         if (lastActive!=null) {
92             ((SectionView)view).openPanel(lastActive);
93         } else {
94             ((SectionView)view).openPanel("welcome_files"); //NOI18N
95
}
96         view.checkValidity();
97         dObj.checkParseable();
98     }
99     
100     public void componentOpened() {
101         super.componentOpened();
102         dObj.getWebApp().addPropertyChangeListener(this);
103     }
104     
105     public void componentClosed() {
106         super.componentClosed();
107         dObj.getWebApp().removePropertyChangeListener(this);
108     }
109     
110     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
111         if (!dObj.isChangedFromUI()) {
112             String JavaDoc name = evt.getPropertyName();
113             if ( name.indexOf("WelcomeFileList")>0 || //NOI18N
114
name.indexOf("JspConfig")>0 || //NOI18N
115
name.indexOf("ErrorPage")>0 || //NOI18N
116
name.indexOf("version")>0 ) { //NOI18N
117
// repaint view if the wiew is active and something is changed with elements listed above
118
if (PAGES_MV_ID.equals(dObj.getSelectedPerspective().preferredID())) {
119                     repaintingTask.schedule(100);
120                 } else {
121                     needInit=true;
122                 }
123             }
124         }
125     }
126     
127     class PagesView extends SectionView {
128         private SectionContainer jspPGCont;
129         private Node groupsNode, welcomeFilesNode, errorPagesNode;
130         
131         PagesView(WebApp webApp) {
132             super(factory);
133             JspConfig jspConfig=null;
134             JspPropertyGroup[] groups=null;
135             boolean jspConfigSupported=true;
136             try {
137                 jspConfig = webApp.getSingleJspConfig();
138                 
139             } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {
140                 jspConfigSupported=false;
141             }
142             
143             welcomeFilesNode = new WelcomeFilesNode();
144             addSection(new SectionPanel(this,welcomeFilesNode,"welcome_files")); //NOI18N
145

146             errorPagesNode = new ErrorPagesNode();
147             addSection(new SectionPanel(this,errorPagesNode,"error_pages")); //NOI18N
148

149             Children rootChildren = new Children.Array();
150             
151             if (jspConfigSupported) {
152                 if (jspConfig==null) {
153                     groups = new JspPropertyGroup[0];
154                 } else groups = jspConfig.getJspPropertyGroup();
155                 Node[] nodes = new Node[groups.length];
156                 Children ch = new Children.Array();
157                 for (int i=0;i<nodes.length;i++) {
158                     nodes[i] = new JspPGNode(this,groups[i]);
159                 }
160                 ch.add(nodes);
161                 // creatings JSP Groups node, section container for JSP groups
162
groupsNode = new SectionContainerNode(ch);
163                 groupsNode.setDisplayName(NbBundle.getMessage(PagesMultiViewElement.class,"TTL_jspPropGroups"));
164                 groupsNode.setName(HELP_ID_PREFIX+"jspPropertyGroupsNode"); //NOI18N
165
jspPGCont = new SectionContainer(this,groupsNode,
166                     NbBundle.getMessage(PagesMultiViewElement.class,"TTL_jspPropGroups"));
167                 jspPGCont.setHeaderActions(new javax.swing.Action JavaDoc[]{addAction});
168
169                 // creatings section panels for JSP groups
170
SectionPanel[] pan = new SectionPanel[groups.length];
171                 for (int i=0;i<nodes.length;i++) {
172                     pan[i] = new SectionPanel(this, nodes[i], getJspGroupTitle(groups[i]),groups[i]);
173                     pan[i].setHeaderActions(new javax.swing.Action JavaDoc[]{removeAction});
174                     jspPGCont.addSection(pan[i]);
175                 }
176                 addSection(jspPGCont);
177                 rootChildren.add(new Node[]{welcomeFilesNode,errorPagesNode,groupsNode});
178             } else {
179                 addAction.setEnabled(false);
180                 rootChildren.add(new Node[]{welcomeFilesNode,errorPagesNode});
181             }
182             AbstractNode root = new AbstractNode(rootChildren);
183             setRoot(root);
184         }
185         
186         Node getJspPGsNode() {
187             return groupsNode;
188         }
189         
190         Node getWelcomeFilesNode() {
191             return welcomeFilesNode;
192         }
193         
194         SectionContainer getJspGroupsContainer(){
195             return jspPGCont;
196         }
197         
198         String JavaDoc getJspGroupTitle(JspPropertyGroup jspGroup) {
199             String JavaDoc name=jspGroup.getDefaultDisplayName();
200             if (name==null) name = NbBundle.getMessage(PagesMultiViewElement.class,"NODE_JSP_GROUP");
201                 
202             String JavaDoc[] patterns = jspGroup.getUrlPattern();
203             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
204             for (int i=0;i<patterns.length;i++) {
205                 if (i>0) buf.append(", ");
206                 buf.append(patterns[i]);
207             }
208             return NbBundle.getMessage(PagesMultiViewElement.class,"TTL_JSP_GROUP", name, buf.toString());
209         }
210     
211         String JavaDoc getJspGroupNodeName(JspPropertyGroup jspGroup) {
212             String JavaDoc displayName=jspGroup.getDefaultDisplayName();
213             if (displayName!=null) return displayName;
214             else return NbBundle.getMessage(PagesMultiViewElement.class,"NODE_JSP_GROUP");
215         }
216         
217         public Error JavaDoc validateView() {
218             return SectionValidator.validatePages(webApp);
219         }
220         
221     }
222     
223     private class JspPGNode extends org.openide.nodes.AbstractNode {
224         private JspPropertyGroup group;
225         JspPGNode(PagesView view, JspPropertyGroup group) {
226             super(org.openide.nodes.Children.LEAF);
227             this.group=group;
228             setDisplayName(view.getJspGroupNodeName(group));
229             setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/jspObject.gif"); //NOI18N
230
}
231         public HelpCtx getHelpCtx() {
232             return new HelpCtx(HELP_ID_PREFIX+"jspPropertyGroupNode"); //NOI18N
233
}
234     }
235     
236     private class WelcomeFilesNode extends org.openide.nodes.AbstractNode {
237         WelcomeFilesNode() {
238             super(org.openide.nodes.Children.LEAF);
239             setDisplayName(NbBundle.getMessage(PagesMultiViewElement.class,"TTL_welcomeFiles"));
240             setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/htmlObject.gif"); //NOI18N
241
}
242         public HelpCtx getHelpCtx() {
243             return new HelpCtx(HELP_ID_PREFIX+"welcomeFilesNode"); //NOI18N
244
}
245     }
246     
247     private class ErrorPagesNode extends org.openide.nodes.AbstractNode {
248         ErrorPagesNode() {
249             super(org.openide.nodes.Children.LEAF);
250             setDisplayName(NbBundle.getMessage(PagesMultiViewElement.class,"TTL_errorPages"));
251             setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/htmlObject.gif"); //NOI18N
252
}
253         public HelpCtx getHelpCtx() {
254             return new HelpCtx(HELP_ID_PREFIX+"errorPagesNode"); //NOI18N
255
}
256     }
257
258     private class AddAction extends javax.swing.AbstractAction JavaDoc {
259         
260         AddAction(final DDDataObject dObj, String JavaDoc actionName) {
261             super(actionName);
262             char mnem = NbBundle.getMessage(PagesMultiViewElement.class,"LBL_addJspPG_mnem").charAt(0);
263             putValue(MNEMONIC_KEY,new Integer JavaDoc((int)mnem));
264         }
265         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
266
267             String JavaDoc[] labels = new String JavaDoc[]{
268                 NbBundle.getMessage(PagesMultiViewElement.class,"LBL_displayName"),
269                 NbBundle.getMessage(PagesMultiViewElement.class,"LBL_description"),
270                 NbBundle.getMessage(PagesMultiViewElement.class,"LBL_urlPatterns")
271             };
272             char[] mnem = new char[] {
273                 NbBundle.getMessage(PagesMultiViewElement.class,"LBL_displayName_mnem").charAt(0),
274                 NbBundle.getMessage(PagesMultiViewElement.class,"LBL_description_mnem").charAt(0),
275                 NbBundle.getMessage(PagesMultiViewElement.class,"LBL_urlPatterns_mnem").charAt(0)
276             };
277             boolean[] buttons = new boolean[]{false,false,true};
278             SimpleDialogPanel.DialogDescriptor descriptor = new SimpleDialogPanel.DialogDescriptor(labels);
279             descriptor.setMnemonics(mnem);
280             descriptor.setButtons(buttons);
281             descriptor.setTextField(new boolean[]{true,false,true});
282             
283             final SimpleDialogPanel dialogPanel = new SimpleDialogPanel(descriptor);
284             dialogPanel.getCustomizerButtons()[0].addActionListener(new java.awt.event.ActionListener JavaDoc() {
285                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
286                     try {
287                         SourceGroup[] groups = DDUtils.getDocBaseGroups(dObj);
288                         org.openide.filesystems.FileObject fo = BrowseFolders.showDialog(groups);
289                         if (fo!=null) {
290                             String JavaDoc fileName = "/"+DDUtils.getResourcePath(groups,fo,'/',true); //NOI18N
291
String JavaDoc oldValue = dialogPanel.getValues()[2];
292                             if (fileName.length()>0) {
293                                 String JavaDoc newValue = DDUtils.addItem(oldValue,fileName,false);
294                                 if (!oldValue.equals(newValue)) {
295                                     dialogPanel.getTextComponents()[2].setText(newValue);
296                                 }
297                             }
298                         }
299                     } catch (java.io.IOException JavaDoc ex) {}
300                 }
301             });
302             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(PagesMultiViewElement.class,"TTL_JspPropertyGroup"),true) {
303                 protected String JavaDoc validate() {
304                     String JavaDoc[] values = dialogPanel.getValues();
305                     String JavaDoc urlPatterns = values[2].trim();
306                     return SectionValidator.validateNewJspPropertyGroup(urlPatterns);
307                 }
308             };
309             dialog.setValid(false); // disable OK button
310

311             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
312             dialogPanel.getTextComponents()[2].getDocument().addDocumentListener(docListener);
313             
314             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
315             d.setVisible(true);
316             dialogPanel.getTextComponents()[2].getDocument().removeDocumentListener(docListener);
317             
318             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
319                 dObj.modelUpdatedFromUI();
320                 dObj.setChangedFromUI(true);
321                 String JavaDoc[] values = dialogPanel.getValues();
322                 String JavaDoc name = values[0];
323                 String JavaDoc description = values[1];
324                 String JavaDoc urls = values[2].trim();
325                 try {
326                     JspConfig jspConfig=null;
327                     try {
328                         jspConfig = webApp.getSingleJspConfig();
329                         if (jspConfig==null) {
330                             jspConfig = (JspConfig)webApp.createBean("JspConfig"); //NOI18N
331
webApp.setJspConfig(jspConfig);
332                         }
333                     } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {}
334                     JspPropertyGroup group = (JspPropertyGroup)jspConfig.createBean("JspPropertyGroup"); //NOI18N
335
if (name.length()>0) group.setDisplayName((String JavaDoc)name);
336                     if (description.length()>0) group.setDescription((String JavaDoc)description);
337                     
338                     String JavaDoc[] patterns = DDUtils.getStringArray(urls);
339                     group.setUrlPattern(patterns);
340                     jspConfig.addJspPropertyGroup(group);
341
342                     PagesView view = (PagesView)comp.getContentView();
343                     Node node = new JspPGNode(view, group);
344                     view.getJspPGsNode().getChildren().add(new Node[]{node});
345
346                     SectionPanel pan = new SectionPanel(view, node, view.getJspGroupTitle(group),group);
347                     pan.setHeaderActions(new javax.swing.Action JavaDoc[]{removeAction});
348                     view.getJspGroupsContainer().addSection(pan, true);
349                 } catch (ClassNotFoundException JavaDoc ex){}
350                 finally {
351                     dObj.setChangedFromUI(false);
352                 }
353             }
354         }
355     }
356     
357     private class RemoveAction extends javax.swing.AbstractAction JavaDoc {
358         
359         RemoveAction(String JavaDoc actionName) {
360             super(actionName);
361             char mnem = NbBundle.getMessage(PagesMultiViewElement.class,"LBL_remove_mnem").charAt(0);
362             putValue(MNEMONIC_KEY,new Integer JavaDoc((int)mnem));
363         }
364         
365         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
366             org.openide.DialogDescriptor desc = new ConfirmDialog(
367                 NbBundle.getMessage(PagesMultiViewElement.class,"TXT_removeJspGroupConfirm"));
368             java.awt.Dialog JavaDoc dialog = org.openide.DialogDisplayer.getDefault().createDialog(desc);
369             dialog.setVisible(true);
370             if (org.openide.DialogDescriptor.OK_OPTION.equals(desc.getValue())) {
371                 SectionPanel sectionPanel = ((SectionPanel.HeaderButton)evt.getSource()).getSectionPanel();
372                 JspPropertyGroup group = (JspPropertyGroup)sectionPanel.getKey();
373                 try {
374                     // removing jsp-property-group from data model
375
dObj.modelUpdatedFromUI();
376                     dObj.setChangedFromUI(true);
377                     webApp.getSingleJspConfig().removeJspPropertyGroup(group);
378                     
379                     // removing section
380
sectionPanel.getSectionView().removeSection(sectionPanel.getNode());
381                 } catch (org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException ex) {}
382                 finally {
383                     dObj.setChangedFromUI(false);
384                 }
385             }
386         }
387     }
388 }
389
Popular Tags