1 19 20 package org.apache.tools.ant.module.bridge.impl; 21 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import javax.swing.JComboBox ; 26 import javax.swing.JComponent ; 27 import javax.swing.JLabel ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTextField ; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.input.InputHandler; 32 import org.apache.tools.ant.input.InputRequest; 33 import org.apache.tools.ant.input.MultipleChoiceInputRequest; 34 import org.openide.DialogDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 import org.openide.util.Exceptions; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 41 44 final class NbInputHandler implements InputHandler { 45 46 private JComboBox combo = null; 47 private JTextField input = null; 48 private final Runnable interestingOutputCallback; 49 50 public NbInputHandler(Runnable interestingOutputCallback) { 51 this.interestingOutputCallback = interestingOutputCallback; 52 } 53 54 public void handleInput(InputRequest request) throws BuildException { 55 interestingOutputCallback.run(); 56 57 combo = null; 61 input = null; 62 63 JPanel panel = createPanel(request); 64 DialogDescriptor dlg = new DialogDescriptor(panel, 65 NbBundle.getMessage(NbInputHandler.class, "TITLE_input_handler")); do { 67 DialogDisplayer.getDefault().createDialog(dlg).setVisible(true); 68 if (dlg.getValue() != NotifyDescriptor.OK_OPTION) { 69 throw new BuildException(NbBundle.getMessage(NbInputHandler.class, "MSG_input_aborted")); } 71 String value; 72 if (combo != null) { 73 value = (String ) combo.getSelectedItem(); 74 } else { 75 value = input.getText(); 76 } 77 request.setInput(value); 78 } while (!request.isInputValid()); 79 } 80 81 private JPanel createPanel(InputRequest request) { 82 83 JPanel pane = new JPanel (); 84 pane.setLayout(new GridBagLayout ()); 85 86 JLabel jLabel1 = new JLabel (request.getPrompt()); 87 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 88 gridBagConstraints.anchor = GridBagConstraints.WEST; 89 gridBagConstraints.insets = new Insets (12, 12, 11, 6); 90 pane.add(jLabel1, gridBagConstraints); 91 92 JComponent comp = null; 93 if (request instanceof MultipleChoiceInputRequest) { 94 combo = new JComboBox (((MultipleChoiceInputRequest)request).getChoices()); 95 if (defaultValue != null && defaultValue.length() > 0) { 96 combo.setSelectedItem(getDefaultValue(request)); 97 } 98 comp = combo; 99 } else { 100 input = new JTextField (getDefaultValue(request), 25); 101 comp = input; 102 } 103 104 comp.getAccessibleContext().setAccessibleDescription( 105 NbBundle.getMessage(NbInputHandler.class, "ACSD_input_handler")); 107 gridBagConstraints = new GridBagConstraints (); 108 gridBagConstraints.gridx = 1; 109 gridBagConstraints.gridy = 0; 110 gridBagConstraints.fill = GridBagConstraints.BOTH; 111 gridBagConstraints.weightx = 1.0; 112 gridBagConstraints.insets = new Insets (12, 6, 11, 6); 113 pane.add(comp, gridBagConstraints); 114 115 jLabel1.setLabelFor(comp); 116 if (request.getPrompt().length() > 0) 117 jLabel1.setDisplayedMnemonic(request.getPrompt().charAt(0)); 118 119 pane.getAccessibleContext().setAccessibleName( 120 NbBundle.getMessage(NbInputHandler.class, "TITLE_input_handler")); pane.getAccessibleContext().setAccessibleDescription( 122 NbBundle.getMessage(NbInputHandler.class, "ACSD_input_handler")); 124 HelpCtx.setHelpIDString(pane, "org.apache.tools.ant.module.run.NBInputHandler"); 126 return pane; 127 } 128 129 private static String defaultValue; 130 static void setDefaultValue(String d) { 131 defaultValue = d; 132 } 133 private static String getDefaultValue(InputRequest req) { 134 try { 135 return (String ) InputRequest.class.getMethod("getDefaultValue").invoke(req); } catch (NoSuchMethodException e) { 138 } catch (Exception e) { 140 Exceptions.printStackTrace(e); 141 } 142 return defaultValue; 143 } 144 145 } 146 | Popular Tags |