1 19 20 package org.netbeans.modules.project.uiapi; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.WindowAdapter ; 26 import java.awt.event.WindowEvent ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import java.util.Arrays ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import javax.swing.AbstractAction ; 33 import javax.swing.Action ; 34 import javax.swing.JButton ; 35 import javax.swing.JPanel ; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.spi.project.ui.support.ProjectCustomizer; 38 import org.openide.DialogDescriptor; 39 import org.openide.DialogDisplayer; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.Mutex; 42 import org.openide.util.NbBundle; 43 44 48 public class CustomizerDialog { 49 50 52 private CustomizerDialog() {} 53 54 private static final int OPTION_OK = 0; 56 private static final int OPTION_CANCEL = OPTION_OK + 1; 57 58 private static final String COMMAND_OK = "OK"; private static final String COMMAND_CANCEL = "CANCEL"; 62 public static Dialog createDialog( ActionListener okOptionListener, final CustomizerPane innerPane, 63 HelpCtx helpCtx, final ProjectCustomizer.Category[] categories ) { 64 65 ListeningButton okButton = new ListeningButton( 66 NbBundle.getMessage(CustomizerDialog.class, "LBL_Customizer_Ok_Option"), categories); 68 okButton.setEnabled(CustomizerDialog.checkValidity(categories)); 69 70 JButton options[] = { 72 okButton, 73 new JButton ( NbBundle.getMessage( CustomizerDialog.class, "LBL_Customizer_Cancel_Option" ) ) , }; 75 76 options[ OPTION_OK ].setActionCommand( COMMAND_OK ); 78 options[ OPTION_CANCEL ].setActionCommand( COMMAND_CANCEL ); 79 80 options[ OPTION_OK ].getAccessibleContext().setAccessibleDescription ( NbBundle.getMessage( CustomizerDialog.class, "AD_Customizer_Ok_Option") ); options[ OPTION_CANCEL ].getAccessibleContext().setAccessibleDescription ( NbBundle.getMessage( CustomizerDialog.class, "AD_Customizer_Cancel_Option") ); 84 85 ActionListener optionsListener = new OptionListener( okOptionListener, categories ); 87 options[ OPTION_OK ].addActionListener( optionsListener ); 88 options[ OPTION_CANCEL ].addActionListener( optionsListener ); 89 90 innerPane.getAccessibleContext().setAccessibleName( NbBundle.getMessage( CustomizerDialog.class, "AN_ProjectCustomizer") ); innerPane.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage( CustomizerDialog.class, "AD_ProjectCustomizer") ); 93 if ( helpCtx == null ) { 94 helpCtx = HelpCtx.DEFAULT_HELP; 95 } 96 97 DialogDescriptor dialogDescriptor = new DialogDescriptor( 98 innerPane, NbBundle.getMessage( CustomizerDialog.class, "LBL_Customizer_Title" ), false, options, options[OPTION_OK], DialogDescriptor.BOTTOM_ALIGN, helpCtx, null ); 107 innerPane.addPropertyChangeListener( new HelpCtxChangeListener( dialogDescriptor, helpCtx ) ); 108 if ( innerPane instanceof HelpCtx.Provider ) { 109 dialogDescriptor.setHelpCtx( ((HelpCtx.Provider)innerPane).getHelpCtx() ); 110 } 111 dialogDescriptor.setClosingOptions( new Object [] { options[ OPTION_OK ], options[ OPTION_CANCEL ] } ); 112 113 Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor ); 114 115 dialog.addWindowListener(new WindowAdapter () { 116 public void windowClosed(WindowEvent e) { 117 innerPane.clearPanelComponentCache(); 118 List <ProjectCustomizer.Category> queue = new LinkedList <ProjectCustomizer.Category>(Arrays.asList(categories)); 119 120 while (!queue.isEmpty()) { 121 ProjectCustomizer.Category category = queue.remove(0); 122 123 Utilities.removeCategoryChangeSupport(category); 124 125 if (category.getSubcategories() != null) { 126 queue.addAll(Arrays.asList(category.getSubcategories())); 127 } 128 } 129 } 130 }); 131 132 return dialog; 133 134 } 135 136 137 private static boolean checkValidity(ProjectCustomizer.Category[] categories) { 138 for (ProjectCustomizer.Category c : categories) { 139 if (!c.isValid()) { 140 return false; 141 } 142 ProjectCustomizer.Category[] subCategories = c.getSubcategories(); 143 if (subCategories != null) { 144 if (!checkValidity(subCategories)) { 145 return false; 146 } 147 } 148 } 149 return true; 150 } 151 152 153 private static class OptionListener implements ActionListener { 154 155 private ActionListener okOptionListener; 156 private ProjectCustomizer.Category[] categories; 157 158 OptionListener( ActionListener okOptionListener, ProjectCustomizer.Category[] categs) { 159 this.okOptionListener = okOptionListener; 160 categories = categs; 161 } 162 163 public void actionPerformed( final ActionEvent e ) { 164 String command = e.getActionCommand(); 165 166 if ( COMMAND_OK.equals( command ) ) { 167 ProjectManager.mutex().writeAccess(new Mutex.Action<Object >() { 169 public Object run() { 170 okOptionListener.actionPerformed( e ); actionPerformed(e, categories); 172 return null; 173 } 174 }); 175 } 176 } 177 178 private void actionPerformed(ActionEvent e, ProjectCustomizer.Category[] categs) { 179 for (int i = 0; i < categs.length; i++) { 180 ActionListener list = categs[i].getOkButtonListener(); 181 if (list != null) { 182 list.actionPerformed(e); } 184 if (categs[i].getSubcategories() != null) { 185 actionPerformed(e, categs[i].getSubcategories()); 186 } 187 } 188 } 189 190 } 191 192 private static class HelpCtxChangeListener implements PropertyChangeListener { 193 194 DialogDescriptor dialogDescriptor; 195 HelpCtx defaultHelpCtx; 196 197 HelpCtxChangeListener( DialogDescriptor dialogDescriptor, HelpCtx defaultHelpCtx ) { 198 this.dialogDescriptor = dialogDescriptor; 199 this.defaultHelpCtx = defaultHelpCtx; 200 } 201 202 public void propertyChange( PropertyChangeEvent evt ) { 203 204 if ( CustomizerPane.HELP_CTX_PROPERTY.equals( evt.getPropertyName() ) ) { 205 HelpCtx newHelp = (HelpCtx)evt.getNewValue(); 206 dialogDescriptor.setHelpCtx( newHelp == null || newHelp == HelpCtx.DEFAULT_HELP ? defaultHelpCtx : newHelp ); 207 } 208 209 } 210 211 } 212 213 private static class ListeningButton extends JButton implements PropertyChangeListener { 214 215 private ProjectCustomizer.Category[] categories; 216 217 public ListeningButton(String label, ProjectCustomizer.Category[] categories) { 218 super(label); 219 this.categories = categories; 220 for (ProjectCustomizer.Category c : categories) { 221 Utilities.getCategoryChangeSupport(c).addPropertyChangeListener(this); 222 } 223 224 } 225 226 public void propertyChange(PropertyChangeEvent evt) { 227 if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) { 228 boolean valid = (Boolean ) evt.getNewValue(); 229 setEnabled(valid && CustomizerDialog.checkValidity(categories)); 231 } 232 } 233 234 } 235 236 } 237 | Popular Tags |