1 19 20 package org.netbeans.modules.options.advanced; 21 22 import java.awt.Color ; 23 import java.text.Collator ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import javax.swing.JComponent ; 31 import javax.swing.border.Border ; 32 import javax.swing.border.CompoundBorder ; 33 import javax.swing.border.EmptyBorder ; 34 import org.netbeans.modules.options.ui.TabbedPanelModel; 35 import org.netbeans.spi.options.AdvancedOption; 36 import org.netbeans.spi.options.OptionsPanelController; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.Repository; 39 import org.openide.loaders.DataFolder; 40 import org.openide.loaders.FolderLookup; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.Lookup; 43 import org.openide.util.lookup.ProxyLookup; 44 45 49 public final class Model extends TabbedPanelModel { 50 51 private Map <String ,AdvancedOption> categoryToOption = new HashMap <String ,AdvancedOption>(); 52 private Map <String , JComponent > categoryToPanel = new HashMap <String , JComponent > (); 53 private Map <String , OptionsPanelController> categoryToController = new HashMap <String , OptionsPanelController>(); 54 private Lookup masterLookup; 55 56 57 public List getCategories () { 58 init (); 59 List <String > l = new ArrayList <String >(categoryToOption.keySet ()); 60 Collections.sort(l, Collator.getInstance()); 61 return l; 62 } 63 64 public String getToolTip (String category) { 65 AdvancedOption option = (AdvancedOption) categoryToOption.get (category); 66 return option.getTooltip (); 67 } 68 69 public JComponent getPanel (String category) { 70 init (); 71 JComponent panel = (JComponent ) categoryToPanel.get (category); 72 if (panel != null) return panel; 73 AdvancedOption option = (AdvancedOption) categoryToOption.get (category); 74 OptionsPanelController controller = option.create (); 75 categoryToController.put (category, controller); 76 panel = controller.getComponent (masterLookup); 77 categoryToPanel.put (category, panel); 78 Border b = panel.getBorder (); 79 if (b != null) 80 b = new CompoundBorder ( 81 new EmptyBorder (6, 16, 6, 6), 82 b 83 ); 84 else 85 b = new EmptyBorder (6, 16, 6, 6); 86 panel.setBorder (b); 87 panel.setBackground (Color.white); 88 panel.setMaximumSize (panel.getPreferredSize ()); 89 return panel; 90 } 91 92 93 95 void update () { 96 Iterator it = categoryToController.values ().iterator (); 97 while (it.hasNext ()) 98 ((OptionsPanelController) it.next ()).update (); 99 } 100 101 void applyChanges () { 102 Iterator it = categoryToController.values ().iterator (); 103 while (it.hasNext ()) 104 ((OptionsPanelController) it.next ()).applyChanges (); 105 } 106 107 void cancel () { 108 Iterator it = categoryToController.values ().iterator (); 109 while (it.hasNext ()) 110 ((OptionsPanelController) it.next ()).cancel (); 111 } 112 113 boolean isValid () { 114 Iterator it = categoryToController.values ().iterator (); 115 while (it.hasNext ()) 116 if (!((OptionsPanelController) it.next ()).isValid ()) 117 return false; 118 return true; 119 } 120 121 boolean isChanged () { 122 Iterator it = categoryToController.values ().iterator (); 123 while (it.hasNext ()) 124 if (((OptionsPanelController) it.next ()).isChanged ()) 125 return true; 126 return false; 127 } 128 129 Lookup getLookup () { 130 List <Lookup> lookups = new ArrayList <Lookup> (); 131 Iterator <OptionsPanelController> it = categoryToController.values ().iterator (); 132 while (it.hasNext ()) 133 lookups.add (it.next ().getLookup ()); 134 return new ProxyLookup 135 ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()])); 136 } 137 138 HelpCtx getHelpCtx (JComponent panel) { 139 Iterator it = categoryToPanel.keySet ().iterator (); 140 while (it.hasNext ()) { 141 String category = (String ) it.next (); 142 if (panel == categoryToPanel.get (category)) { 143 OptionsPanelController controller = (OptionsPanelController) 144 categoryToController.get (category); 145 return controller.getHelpCtx (); 146 } 147 } 148 return new HelpCtx ("netbeans.optionsDialog.advanced"); 149 } 150 151 private boolean initialized = false; 152 153 private void init () { 154 if (initialized) return; 155 initialized = true; 156 FileObject fo = Repository.getDefault ().getDefaultFileSystem (). 157 findResource ("OptionsDialog/Advanced"); 158 if (fo == null) return; 159 Lookup lookup = new FolderLookup (DataFolder.findFolder (fo)).getLookup (); 160 Iterator <? extends AdvancedOption> it = lookup.lookup (new Lookup.Template<AdvancedOption> (AdvancedOption.class)). 161 allInstances ().iterator (); 162 while (it.hasNext ()) { 163 AdvancedOption option = it.next (); 164 categoryToOption.put (option.getDisplayName (), option); 165 } 166 } 167 168 void setLoookup (Lookup masterLookup) { 169 this.masterLookup = masterLookup; 170 } 171 } 172 173 174 | Popular Tags |