1 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 36 public class FiltersMultiViewElement extends ToolBarMultiViewElement implements java.beans.PropertyChangeListener { 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 addAction, removeAction; 43 private boolean needInit=true; 44 private int index; 45 private RequestProcessor.Task repaintingTask; 46 private static final String FILTER_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_FILTERS; 47 private static final String HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_FILTERS; 48 49 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 () { 60 public void run() { 61 javax.swing.SwingUtilities.invokeLater(new Runnable () { 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 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 evt) { 113 if (!dObj.isChangedFromUI()) { 114 String name = evt.getPropertyName(); 115 if ( name.indexOf("Filter")>0 ) { 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"); filtersCont = new SectionContainer(this,filtersNode, 144 NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_filters")); 145 filtersCont.setHeaderActions(new javax.swing.Action []{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 []{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); 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 getFilterTitle(Filter filter) { 178 String filterName=filter.getFilterName(); 179 if (filterName==null) filterName=""; 180 String mappings = DDUtils.urlPatternList(DDUtils.getUrlPatterns(webApp,filter)); 181 return NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_filterPanel",filterName,mappings); 182 } 183 184 public Error 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"); } 195 public HelpCtx getHelpCtx() { 196 return new HelpCtx(HELP_ID_PREFIX+"filterMappingsNode"); } 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"); } 212 public HelpCtx getHelpCtx() { 213 return new HelpCtx(HELP_ID_PREFIX+"filterNode"); } 215 } 216 217 private class AddAction extends javax.swing.AbstractAction { 218 219 AddAction(final DDDataObject dObj, String actionName) { 220 super(actionName); 221 char mnem = NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_addFilter_mnem").charAt(0); 222 putValue(MNEMONIC_KEY,new Integer ((int)mnem)); 223 } 224 public void actionPerformed(java.awt.event.ActionEvent evt) { 225 226 String [] labels = new String []{ 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 () { 244 public void actionPerformed(java.awt.event.ActionEvent 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 className = DDUtils.getResourcePath(groups,fo); 250 dialogPanel.getTextComponents()[1].setText(className); 251 } 252 } catch (java.io.IOException ex) {} 253 } 254 }); 255 EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(FiltersMultiViewElement.class,"TTL_Filter"),true) { 256 protected String validate() { 257 String [] values = dialogPanel.getValues(); 258 String filterName = values[0].trim(); 259 String filterClass = values[1].trim(); 260 return SectionValidator.validateNewFilter(dObj.getWebApp(),filterName, filterClass); 261 } 262 }; 263 dialog.setValid(false); 265 javax.swing.event.DocumentListener docListener = new EditDialog.DocListener(dialog); 266 dialogPanel.getTextComponents()[0].getDocument().addDocumentListener(docListener); 267 dialogPanel.getTextComponents()[1].getDocument().addDocumentListener(docListener); 268 269 java.awt.Dialog 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 [] values = dialogPanel.getValues(); 278 try { 279 Filter filter = (Filter)webApp.createBean("Filter"); filter.setFilterName((String )values[0]); 281 filter.setFilterClass((String )values[1]); 282 String desc = (String )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 []{removeAction}); 292 view.getFiltersContainer().addSection(pan, true); 293 } catch (ClassNotFoundException ex){} 294 finally { 295 dObj.setChangedFromUI(false); 296 } 297 } 298 } 299 } 300 301 private class RemoveAction extends javax.swing.AbstractAction { 302 303 RemoveAction(String actionName) { 304 super(actionName); 305 char mnem = NbBundle.getMessage(FiltersMultiViewElement.class,"LBL_remove_mnem").charAt(0); 306 putValue(MNEMONIC_KEY,new Integer ((int)mnem)); 307 } 308 309 public void actionPerformed(java.awt.event.ActionEvent evt) { 310 org.openide.DialogDescriptor desc = new ConfirmDialog( 311 NbBundle.getMessage(FiltersMultiViewElement.class,"TXT_removeFilterConfirm")); 312 java.awt.Dialog 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 dObj.modelUpdatedFromUI(); 319 dObj.setChangedFromUI(true); 320 try { 321 java.util.Stack deletedRows = DDUtils.removeFilterMappings(webApp,filter.getFilterName()); 322 webApp.removeFilter(filter); 323 324 sectionPanel.getSectionView().removeSection(sectionPanel.getNode()); 326 327 SectionInnerPanel mappingsInnerPanel = ((FiltersView)sectionPanel.getSectionView()).getFilterMappingSectionPanel().getInnerPanel(); 329 if (mappingsInnerPanel!=null) { 330 while (!deletedRows.empty()) 331 ((FilterMappingsPanel)mappingsInnerPanel).removeRow(((Integer )deletedRows.pop()).intValue()); 332 } 333 } finally { 334 dObj.setChangedFromUI(false); 335 } 336 } 337 } 338 } 339 } 340 | Popular Tags |