1 19 20 package org.netbeans.modules.editor.lib2; 21 22 import com.sun.org.apache.bcel.internal.generic.LOOKUPSWITCH; 23 import java.awt.Insets ; 24 import java.awt.Dialog ; 25 import java.awt.event.*; 26 import java.awt.LayoutManager ; 27 import java.awt.BorderLayout ; 28 import java.awt.GridLayout ; 29 import java.util.Collection ; 30 import javax.swing.*; 31 import javax.swing.border.EmptyBorder ; 32 import org.netbeans.spi.editor.DialogFactory; 33 import org.openide.util.Lookup; 34 35 44 public final class DialogSupport { 45 46 private static DialogSupport instance; 47 48 private DialogFactory externalFactory; 49 private Lookup.Result<DialogFactory> result; 50 51 public static synchronized DialogSupport getInstance() { 52 if (instance == null) { 53 instance = new DialogSupport(); 54 } 55 return instance; 56 } 57 58 59 private DialogSupport() { 60 result = Lookup.getDefault().lookup(new Lookup.Template<DialogFactory>(DialogFactory.class)); 61 } 62 63 79 public Dialog createDialog( 80 String title, JPanel panel, boolean modal, 81 JButton[] buttons, boolean sidebuttons, int defaultIndex, int cancelIndex, 82 ActionListener listener 83 ) { 84 DialogFactory factory = null; 85 86 if( externalFactory != null ) { 87 factory = externalFactory; 88 } else { 89 Collection <? extends DialogFactory> factories = result.allInstances(); 90 if (factories.isEmpty()) { 91 factory = new DefaultDialogFactory(); 92 } else { 93 factory = factories.iterator().next(); 94 } 95 } 96 97 return factory.createDialog(title, panel, modal, buttons, sidebuttons, 98 defaultIndex, cancelIndex, listener ); 99 } 100 101 112 public void setExternalDialogFactory( DialogFactory factory ) { 113 externalFactory = factory; 114 } 115 116 119 private static class DefaultDialogFactory extends WindowAdapter implements DialogFactory, ActionListener { 120 121 private JButton cancelButton; 122 123 125 private JPanel createButtonPanel( JButton[] buttons, boolean sidebuttons ) { 126 int count = buttons.length; 127 128 JPanel outerPanel = new JPanel( new BorderLayout () ); 129 outerPanel.setBorder( new EmptyBorder ( new Insets ( 130 sidebuttons ? 5 : 0, sidebuttons ? 0 : 5, 5, 5 ) ) ); 131 132 LayoutManager lm = new GridLayout ( sidebuttons ? count : 1, sidebuttons ? 1 : count, 5, 5 ); 134 135 JPanel innerPanel = new JPanel( lm ); 136 137 for( int i = 0; i < count; i++ ) innerPanel.add( buttons[i] ); 138 139 outerPanel.add( innerPanel, 140 sidebuttons ? BorderLayout.NORTH : BorderLayout.EAST ) ; 141 return outerPanel; 142 } 143 144 public Dialog createDialog( String title, JPanel panel, boolean modal, 145 JButton[] buttons, boolean sidebuttons, int defaultIndex, 146 int cancelIndex, ActionListener listener ) { 147 148 JDialog d = new JDialog( (javax.swing.JFrame )null, title, modal ); 150 d.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); 151 d.getContentPane().add( panel, BorderLayout.CENTER); 152 153 JPanel buttonPanel = createButtonPanel( buttons, sidebuttons ); 155 String buttonAlign = sidebuttons ? BorderLayout.EAST : BorderLayout.SOUTH; 156 d.getContentPane().add( buttonPanel, buttonAlign ); 157 158 if( listener != null ) { 160 for( int i = 0; i < buttons.length; i++ ) { 161 buttons[i].addActionListener( listener ); 162 } 163 } 164 165 if( defaultIndex >= 0 ) { 167 d.getRootPane().setDefaultButton( buttons[defaultIndex] ); 168 } 169 170 if( cancelIndex >= 0 ) { 172 cancelButton = buttons[cancelIndex]; 173 d.getRootPane().registerKeyboardAction( 175 this, 176 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), 177 JComponent.WHEN_IN_FOCUSED_WINDOW 178 ); 179 180 d.addWindowListener( this ); 182 } 183 184 d.pack(); 185 return d; 186 } 187 188 public void actionPerformed(ActionEvent evt) { 189 cancelButton.doClick( 10 ); 190 } 191 192 public void windowClosing( WindowEvent evt ) { 193 cancelButton.doClick( 10 ); 194 } 195 } } 197 | Popular Tags |