1 19 20 package org.netbeans.modules.xml.schema.ui.nodes.categorized; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.Insets ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.FocusEvent ; 32 import java.beans.PropertyChangeEvent ; 33 import java.beans.PropertyVetoException ; 34 import java.io.IOException ; 35 import java.util.Date ; 36 import javax.swing.JButton ; 37 import javax.swing.JComponent ; 38 import javax.swing.JPanel ; 39 import javax.swing.JScrollPane ; 40 import javax.swing.JTextArea ; 41 import javax.swing.border.EmptyBorder ; 42 import org.netbeans.modules.xml.schema.ui.basic.SchemaColumn; 43 import org.netbeans.modules.xml.xam.ui.column.ColumnProvider; 44 import org.netbeans.modules.xml.xam.ui.customizer.AbstractCustomizer; 45 import org.netbeans.modules.xml.xam.ui.customizer.Customizer; 46 import org.openide.ErrorManager; 47 import org.openide.nodes.AbstractNode; 48 import org.openide.nodes.Children; 49 import org.openide.nodes.Node; 50 import org.openide.util.HelpCtx; 51 import org.openide.util.Lookup; 52 import org.openide.util.LookupEvent; 53 import org.openide.util.LookupListener; 54 import org.openide.util.NbBundle; 55 import org.openide.util.lookup.Lookups; 56 57 64 public class DetailsColumn extends SchemaColumn 65 implements ActionListener , LookupListener 66 { 67 static final long serialVersionUID = 1L; 68 69 72 private boolean readonly; 73 74 77 private Customizer customizer; 78 79 82 85 88 private JPanel bottomPanel; 89 90 93 private JButton applyButton; 94 95 98 private JButton resetButton; 99 100 103 private JScrollPane scrollPane; 104 105 106 private Lookup.Result lookupResult; 107 108 111 public DetailsColumn(Customizer cust) 112 { 113 super(null, null, false); 114 customizer = cust==null || cust.getComponent()==null 115 ? new DummyDetails() : cust; 116 initialize(); 117 lookupResult = getCustomizer().getLookup().lookup( 119 new Lookup.Template(ColumnProvider.class)); 120 if(lookupResult!=null) lookupResult.addLookupListener(this); 121 } 122 123 124 127 private void initialize() 128 { 129 setLayout(new BorderLayout ()); 130 setBackground(Color.white); 131 132 137 getCustomizer().getComponent().setBackground(Color.white); 139 add(getCustomizer().getComponent(),BorderLayout.CENTER); 140 scrollPane = new JScrollPane (getCustomizer().getComponent()); 141 scrollPane.setBorder(null); 142 scrollPane.setViewportBorder(null); 143 add(scrollPane,BorderLayout.CENTER); 144 145 bottomPanel = new JPanel (); 147 applyButton = new JButton (); 148 applyButton.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_ApplyButton")); 149 applyButton.setEnabled(false); 150 applyButton.addActionListener(this); 151 resetButton = new JButton (); 152 resetButton.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_ResetButton")); 153 resetButton.setEnabled(false); 154 resetButton.addActionListener(this); 155 bottomPanel.setLayout(new GridBagLayout ()); 156 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 157 gridBagConstraints.insets = new Insets (10, 10, 10, 2); 158 gridBagConstraints.anchor = GridBagConstraints.EAST; 159 gridBagConstraints.weightx = 1.0; 160 bottomPanel.add(applyButton,gridBagConstraints); 161 gridBagConstraints.insets = new Insets (10, 2, 10, 10); 162 gridBagConstraints.weightx = 0.0; 163 bottomPanel.add(resetButton,gridBagConstraints); 164 bottomPanel.setBackground(Color.white); 165 add(bottomPanel,BorderLayout.SOUTH); 166 167 getCustomizer().addPropertyChangeListener(this); 169 } 170 171 172 176 public String getTitle() 177 { 178 return NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn"); } 180 181 public boolean requestFocusInWindow() 183 { 184 return getCustomizer().getComponent().requestFocusInWindow(); 185 } 186 187 191 public JComponent getComponent() 192 { 193 return this; 194 } 195 196 public boolean isShowing() 197 { 198 boolean retValue = super.isShowing(); 199 if (retValue) 200 { 201 boolean enable = !isReadOnly(); 202 if(enable != getCustomizer().getComponent().isEnabled()) 203 { 204 DetailsColumn.setEnabledComponent(getCustomizer().getComponent(),enable); 205 } 206 } 207 return retValue; 208 } 209 public boolean isReadOnly() 211 { 212 return readonly || !getCustomizer().isEditable(); 213 } 214 215 public void setReadOnly(boolean readonly) 216 { 217 this.readonly = readonly; 218 } 219 220 private Customizer getCustomizer() 221 { 222 return customizer; 223 } 224 225 public void propertyChange(PropertyChangeEvent evt) 229 { 230 if(isReadOnly()) return; 231 if (evt.getPropertyName().equals(Customizer.PROP_ACTION_APPLY)) 232 { 233 applyButton.setEnabled(((Boolean ) evt.getNewValue()).booleanValue()); 234 } 235 else if (evt.getPropertyName().equals(Customizer.PROP_ACTION_RESET)) 236 { 237 resetButton.setEnabled(((Boolean ) evt.getNewValue()).booleanValue()); 238 } 239 else 240 super.propertyChange(evt); 241 } 242 243 public void actionPerformed(ActionEvent e) 245 { 246 if(isReadOnly()) return; 247 if (e.getSource() == applyButton) 248 { 249 try 250 { 251 getCustomizer().apply(); 252 } 253 catch (IOException ioe) 254 { 255 String msg = NbBundle.getMessage(DetailsColumn.class, "MSG_DetailsColumn_ApplyFailed"); 256 IllegalArgumentException iae = new IllegalArgumentException (msg); 257 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, 258 msg, msg, ioe, new Date ()); 259 getCustomizer().reset(); 260 } 261 262 } 263 else if (e.getSource() == resetButton) 264 { 265 getCustomizer().reset(); 266 } 267 } 268 269 public void focusGained(FocusEvent e) 271 { 272 } 273 274 public void resultChanged(LookupEvent ev) 276 { 277 if(isReadOnly()) return; 278 Lookup.Result source = (Lookup.Result) ev.getSource(); 279 if(source.allInstances().isEmpty()) 280 { 281 getColumnView().removeColumnsAfter(this); 282 } 283 else 284 { 285 for (Object obj: source.allInstances()) 286 { 287 if (obj instanceof ColumnProvider) 288 { 289 ColumnProvider columnProvider = (ColumnProvider)obj; 290 Node selNode = new AbstractNode(Children.LEAF, 291 Lookups.singleton(columnProvider)); 292 getExplorerManager().setRootContext(selNode); 293 try 294 { 295 getExplorerManager().setSelectedNodes(new Node[]{selNode}); 296 } catch (PropertyVetoException ex) 297 { 298 } 299 break; 300 } 301 } 302 } 303 } 304 305 308 private static void setEnabledComponent(Component component, boolean enabled) 309 { 310 component.setEnabled(enabled); 311 if(component instanceof Container ) 312 { 313 Component [] children = ((Container )component).getComponents(); 314 for (Component child:children) 315 { 316 setEnabledComponent(child, enabled); 317 } 318 } 319 } 320 321 325 static class DummyDetails extends AbstractCustomizer 326 { 327 328 static final long serialVersionUID = 1L; 329 330 public DummyDetails() 331 { 332 super(); 333 initialize(); 334 } 335 336 private void initialize() 337 { 338 JTextArea textArea = new JTextArea (); 339 textArea.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_NoCustomizer")); 340 textArea.setColumns(25); 341 textArea.setLineWrap(true); 342 textArea.setWrapStyleWord(true); 343 textArea.setEditable(false); 344 textArea.setBorder(new EmptyBorder (3,3,3,3)); 345 add(textArea); 346 } 347 348 public void apply() 349 { 350 setSaveEnabled(false); 352 } 353 354 public void reset() 355 { 356 setResetEnabled(false); 358 } 359 360 public boolean isEditable() 361 { 362 return false; 363 } 364 365 public HelpCtx getHelpCtx() 366 { 367 return HelpCtx.DEFAULT_HELP; 368 } 369 } 370 } | Popular Tags |