KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.util.RequestProcessor;
31 import org.openide.util.HelpCtx;
32
33 /**
34  * @author mkuchtiak
35  */

36 public class FiltersMultiViewElement extends ToolBarMultiViewElement implements java.beans.PropertyChangeListener JavaDoc {
37     private SectionView view;
38     private ToolBarDesignEditor comp;
39     private DDDataObject dObj;
40     private WebApp webApp;
41     private FilterPanelFactory factory;
42     private javax.swing.Action JavaDoc addAction, removeAction;
43     private boolean needInit=true;
44     private int index;
45     private RequestProcessor.Task repaintingTask;
46     private static final String JavaDoc FILTER_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_FILTERS;
47     private static final String JavaDoc HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_FILTERS;
48     
49     /** Creates a new instance of DDMultiViewElement */
50     public FiltersMultiViewElement(final DDDataObject dObj, int index) {
51         super(dObj);
52         this.dObj=dObj;
53         this.index=index;
54         comp = new ToolBarDesignEditor();
55         factory = new FilterPanelFactory(comp, dObj);
56         addAction = new AddAction(dObj, NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_addFilter"));
57         removeAction = new RemoveAction(NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_remove"));
58         setVisualEditor(comp);
59         repaintingTask = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
60             public void run() {
61                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
62                     public void run() {
63                         repaintView();
64                     }
65                 });
66             }
67         });
68     }
69     
70     public SectionView getSectionView() {
71         return view;
72     }
73     
74     public void componentShowing() {
75         super.componentShowing();
76         dObj.setLastOpenView(index);
77         if (needInit || !dObj.isDocumentParseable()) {
78             repaintView();
79             needInit=false;
80         }
81     }
82     
83     private void repaintView() {
84         webApp = dObj.getWebApp();
85         view = new FiltersView(webApp);
86         comp.setContentView(view);
87         Object JavaDoc lastActive = comp.getLastActive();
88         if (lastActive!=null) {
89             ((SectionView)view).openPanel(lastActive);
90         } else {
91             FiltersView filtersView = (FiltersView)view;
92             Node initialNode = filtersView.getFiltersNode();
93             Children ch = initialNode.getChildren();
94             if (ch.getNodesCount()>0)
95                 initialNode = ch.getNodes()[0];
96             filtersView.selectNode(initialNode);
97         }
98         view.checkValidity();
99         dObj.checkParseable();
100     }
101     
102     public void componentOpened() {
103         super.componentOpened();
104         dObj.getWebApp().addPropertyChangeListener(this);
105     }
106     
107     public void componentClosed() {
108         super.componentClosed();
109         dObj.getWebApp().removePropertyChangeListener(this);
110     }
111     
112     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
113         if (!dObj.isChangedFromUI()) {
114             String JavaDoc name = evt.getPropertyName();
115             if ( name.indexOf("Filter")>0 ) { //NOI18
116
// repaint view if the wiew is active and something is changed with filters
117
if (FILTER_MV_ID.equals(dObj.getSelectedPerspective().preferredID())) {
118                     repaintingTask.schedule(100);
119                 } else {
120                     needInit=true;
121                 }
122             }
123         }
124     }
125
126     class FiltersView extends SectionView {
127         private SectionContainer filtersCont;
128         private Node filtersNode, filterMappingsNode;
129         private SectionPanel filterMappingSectionPanel;
130         
131         FiltersView (WebApp webApp) {
132             super(factory);
133             Filter[] filters = webApp.getFilter();
134             Node[] nodes = new Node[filters.length];
135             Children ch = new Children.Array();
136             for (int i=0;i<nodes.length;i++) {
137                 nodes[i] = new FilterNode(this,webApp,filters[i]);
138             }
139             ch.add(nodes);
140             filtersNode = new SectionContainerNode(ch);
141             filtersNode.setDisplayName(NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_filters"));
142             filtersNode.setName(HELP_ID_PREFIX+"filtersNode"); //NOI18N
143
filtersCont = new SectionContainer(this,filtersNode,
144                 NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_filters"));
145             filtersCont.setHeaderActions(new javax.swing.Action JavaDoc[]{addAction});
146             Children filtersChildren = new Children.Array();
147
148             SectionPanel[] pan = new SectionPanel[filters.length];
149             for (int i=0;i<nodes.length;i++) {
150                 pan[i] = new SectionPanel(this, nodes[i], getFilterTitle(filters[i]),filters[i]);
151                 pan[i].setHeaderActions(new javax.swing.Action JavaDoc[]{removeAction});
152                 filtersCont.addSection(pan[i]);
153             }
154             addSection(filtersCont);
155             
156             filterMappingsNode = new FilterMappingsNode();
157             filterMappingSectionPanel = new SectionPanel(this,filterMappingsNode,"filter_mappings");
158             addSection(filterMappingSectionPanel); //NOI18N
159

160             filtersChildren.add(new Node[]{filtersNode, filterMappingsNode});
161             AbstractNode root = new AbstractNode(filtersChildren);
162             setRoot(root);
163         }
164         
165         Node getFiltersNode() {
166             return filtersNode;
167         }
168         
169         SectionContainer getFiltersContainer(){
170             return filtersCont;
171         }
172         
173         SectionPanel getFilterMappingSectionPanel() {
174             return filterMappingSectionPanel;
175         }
176         
177         String JavaDoc getFilterTitle(Filter filter) {
178             String JavaDoc filterName=filter.getFilterName();
179             if (filterName==null) filterName="";
180             String JavaDoc mappings = DDUtils.urlPatternList(DDUtils.getUrlPatterns(webApp,filter));
181             return NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_filterPanel",filterName,mappings);
182         }
183         
184         public Error JavaDoc validateView() {
185             return SectionValidator.validateFilters(webApp);
186         }
187     }
188     
189     private class FilterMappingsNode extends org.openide.nodes.AbstractNode {
190         FilterMappingsNode() {
191             super(org.openide.nodes.Children.LEAF);
192             setDisplayName(NbBundle.getMessage(PagesMultiViewElement.class,"TTL_FilterMappings"));
193             setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/mappingsNode.gif"); //NOI18N
194
}
195         public HelpCtx getHelpCtx() {
196             return new HelpCtx(HELP_ID_PREFIX+"filterMappingsNode"); //NOI18N
197
}
198     }
199     
200     private class FilterNode extends org.openide.nodes.AbstractNode {
201         private Filter filter;
202         private WebApp webApp;
203         private SectionView view;
204         FilterNode(SectionView view, WebApp webApp, Filter filter) {
205             super(org.openide.nodes.Children.LEAF);
206             this.filter=filter;
207             this.webApp=webApp;
208             this.view=view;
209             setDisplayName(filter.getFilterName());
210             setIconBaseWithExtension("org/netbeans/modules/j2ee/ddloaders/web/multiview/resources/class.gif"); //NOI18N
211
}
212         public HelpCtx getHelpCtx() {
213             return new HelpCtx(HELP_ID_PREFIX+"filterNode"); //NOI18N
214
}
215     }
216
217     private class AddAction extends javax.swing.AbstractAction JavaDoc {
218         
219         AddAction(final DDDataObject dObj, String JavaDoc actionName) {
220             super(actionName);
221             char mnem = NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_addFilter_mnem").charAt(0);
222             putValue(MNEMONIC_KEY,new Integer JavaDoc((int)mnem));
223         }
224         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
225
226             String JavaDoc[] labels = new String JavaDoc[]{
227                 NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_filterName"),
228                 NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_filterClass"),
229                 NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_description")
230             };
231             char[] mnem = new char[] {
232                 NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_filterName_mnem").charAt(0),
233                 NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_filterClass_mnem").charAt(0),
234                 NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_description_mnem").charAt(0)
235             };
236             boolean[] buttons = new boolean[]{false,true,false};
237             SimpleDialogPanel.DialogDescriptor descriptor = new SimpleDialogPanel.DialogDescriptor(labels);
238             descriptor.setMnemonics(mnem);
239             descriptor.setButtons(buttons);
240             descriptor.setTextField(new boolean[]{true,true,false});
241             
242             final SimpleDialogPanel dialogPanel = new SimpleDialogPanel(descriptor);
243             dialogPanel.getCustomizerButtons()[0].addActionListener(new java.awt.event.ActionListener JavaDoc() {
244                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
245                     try {
246                         org.netbeans.api.project.SourceGroup[] groups = DDUtils.getJavaSourceGroups(dObj);
247                         org.openide.filesystems.FileObject fo = BrowseFolders.showDialog(groups);
248                         if (fo!=null) {
249                             String JavaDoc className = DDUtils.getResourcePath(groups,fo);
250                             dialogPanel.getTextComponents()[1].setText(className);
251                         }
252                     } catch (java.io.IOException JavaDoc ex) {}
253                 }
254             });
255             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_Filter"),true) {
256                 protected String JavaDoc validate() {
257                     String JavaDoc[] values = dialogPanel.getValues();
258                     String JavaDoc filterName = values[0].trim();
259                     String JavaDoc filterClass = values[1].trim();
260                     return SectionValidator.validateNewFilter(dObj.getWebApp(),filterName, filterClass);
261                 }
262             };
263             dialog.setValid(false); // disable OK button
264

265             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
266             dialogPanel.getTextComponents()[0].getDocument().addDocumentListener(docListener);
267             dialogPanel.getTextComponents()[1].getDocument().addDocumentListener(docListener);
268             
269             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
270             d.setVisible(true);
271             dialogPanel.getTextComponents()[0].getDocument().removeDocumentListener(docListener);
272             dialogPanel.getTextComponents()[1].getDocument().removeDocumentListener(docListener);
273             
274             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
275                 dObj.modelUpdatedFromUI();
276                 dObj.setChangedFromUI(true);
277                 String JavaDoc[] values = dialogPanel.getValues();
278                 try {
279                     Filter filter = (Filter)webApp.createBean("Filter"); //NOI18N
280
filter.setFilterName((String JavaDoc)values[0]);
281                     filter.setFilterClass((String JavaDoc)values[1]);
282                     String JavaDoc desc = (String JavaDoc)values[2];
283                     if (desc.length()>0) filter.setDescription(desc);
284                     webApp.addFilter(filter);
285
286                     FiltersView view = (FiltersView)comp.getContentView();
287                     Node node = new FilterNode(view, webApp, filter);
288                     view.getFiltersNode().getChildren().add(new Node[]{node});
289
290                     SectionPanel pan = new SectionPanel(view, node, view.getFilterTitle(filter), filter);
291                     pan.setHeaderActions(new javax.swing.Action JavaDoc[]{removeAction});
292                     view.getFiltersContainer().addSection(pan, true);
293                 } catch (ClassNotFoundException JavaDoc ex){}
294                 finally {
295                     dObj.setChangedFromUI(false);
296                 }
297             }
298         }
299     }
300     
301     private class RemoveAction extends javax.swing.AbstractAction JavaDoc {
302         
303         RemoveAction(String JavaDoc actionName) {
304             super(actionName);
305             char mnem = NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_remove_mnem").charAt(0);
306             putValue(MNEMONIC_KEY,new Integer JavaDoc((int)mnem));
307         }
308         
309         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
310             org.openide.DialogDescriptor desc = new ConfirmDialog(
311                 NbBundle.getMessage(FiltersMultiViewElement.class,"TXT_removeFilterConfirm"));
312             java.awt.Dialog JavaDoc dialog = org.openide.DialogDisplayer.getDefault().createDialog(desc);
313             dialog.setVisible(true);
314             if (org.openide.DialogDescriptor.OK_OPTION.equals(desc.getValue())) {
315                 SectionPanel sectionPanel = ((SectionPanel.HeaderButton)evt.getSource()).getSectionPanel();
316                 Filter filter = (Filter)sectionPanel.getKey();
317                 // updating data model
318
dObj.modelUpdatedFromUI();
319                 dObj.setChangedFromUI(true);
320                 try {
321                     java.util.Stack JavaDoc deletedRows = DDUtils.removeFilterMappings(webApp,filter.getFilterName());
322                     webApp.removeFilter(filter);
323
324                     // removing section
325
sectionPanel.getSectionView().removeSection(sectionPanel.getNode());
326
327                     //updating Mappings table
328
SectionInnerPanel mappingsInnerPanel = ((FiltersView)sectionPanel.getSectionView()).getFilterMappingSectionPanel().getInnerPanel();
329                     if (mappingsInnerPanel!=null) {
330                         while (!deletedRows.empty())
331                             ((FilterMappingsPanel)mappingsInnerPanel).removeRow(((Integer JavaDoc)deletedRows.pop()).intValue());
332                     }
333                 } finally {
334                     dObj.setChangedFromUI(false);
335                 }
336             }
337         }
338     }
339 }
340
Popular Tags