1 19 20 package org.netbeans.core.output2.ui; 21 22 import javax.accessibility.Accessible ; 23 import javax.accessibility.AccessibleContext ; 24 import javax.accessibility.AccessibleRole ; 25 import org.netbeans.core.output2.Controller; 26 27 import javax.swing.*; 28 import javax.swing.text.Document ; 29 import java.awt.*; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 33 43 public abstract class AbstractOutputTab extends JComponent implements ActionListener , Accessible { 44 private InputPanel input = null; 45 private AbstractOutputPane outputPane; 46 private Action[] actions = new Action[0]; 47 private JButton[] buttons = new JButton[0]; 48 49 private Component toFocus; 50 51 public AbstractOutputTab() { 52 outputPane = createOutputPane(); 53 add (outputPane); 54 setFocusable(false); 55 } 56 57 public void setDocument (Document doc) { 58 outputPane.setDocument(doc); 59 } 60 61 64 public AccessibleContext getAccessibleContext() { 65 if (accessibleContext == null) { 66 accessibleContext = new AccessibleJComponent() { 67 public AccessibleRole getAccessibleRole() { 68 return AccessibleRole.PANEL; 70 } 71 72 public String getAccessibleName() { 73 if (accessibleName != null) { 74 return accessibleName; 75 } 76 return getName(); 77 } 78 }; 79 } 80 81 return accessibleContext; 82 } 83 84 85 89 public void setToFocus(Component foc) { 90 toFocus = foc; 91 } 92 93 public void requestFocus() { 94 if (toFocus != null) { 97 toFocus.requestFocus(); 98 toFocus = null; 99 return; 100 } 101 if (isInputVisible()) { 102 input.requestFocus(); 103 } else { 104 outputPane.requestFocus(); 105 } 106 } 107 108 public boolean requestFocusInWindow() { 109 if (isInputVisible()) { 110 return input.requestFocusInWindow(); 111 } else { 112 return getOutputPane().requestFocusInWindow(); 113 } 114 } 115 116 protected abstract AbstractOutputPane createOutputPane(); 117 118 protected abstract void inputSent (String txt); 119 120 public final AbstractOutputPane getOutputPane() { 121 return outputPane; 122 } 123 124 public final void setToolbarActions (Action[] a) { 125 if (a == null || a.length == 0) { 126 actions = new Action[0]; 127 buttons = new JButton[0]; 128 return; 129 } 130 if (a.length > 5) { 131 throw new IllegalArgumentException ("No more than 5 actions allowed" + "in the output window toolbar"); } 134 actions = new Action[a.length]; 135 buttons = new JButton[a.length]; 136 for (int i=0; i < buttons.length; i++) { 137 actions[i] = a[i]; 138 installKeyboardAction (actions[i]); 142 buttons[i] = new JButton(actions[i]); 143 buttons[i].setBorderPainted(false); 144 buttons[i].setOpaque(false); 145 buttons[i].setText(null); 146 buttons[i].putClientProperty("hideActionText", Boolean.TRUE); if (a[i].getValue (Action.SMALL_ICON) == null) { 148 throw new IllegalStateException ("No icon provided for " + a[i]); } 150 } 151 } 152 153 154 160 public Action[] getToolbarActions() { 161 return actions; 162 } 163 164 172 public void installKeyboardAction (Action a) { 173 if (!(a instanceof WeakAction)) { 174 a = new WeakAction(a); 176 } 177 KeyStroke accel = null; 178 String name; 179 Object o = a.getValue (Action.ACCELERATOR_KEY); 180 if (o instanceof KeyStroke) { 181 accel = (KeyStroke) o; 182 } 183 name = (String ) a.getValue(Action.NAME); 184 if (accel != null) { 185 if (Controller.LOG) Controller.log ("Installed action " + name + " on " + accel); 186 JComponent c = getOutputPane().textView; 189 c.getInputMap().put(accel, name); 190 c.getActionMap().put(name, a); 191 getInputMap (WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (accel, name); 192 getActionMap().put(name, a); 193 } 194 } 195 196 public final boolean isInputVisible() { 197 return input != null && input.getParent() == this && input.isVisible(); 198 } 199 200 public final void setInputVisible (boolean val) { 201 if (val == isInputVisible()) { 202 return; 203 } 204 if (val) { 205 if (input == null) { 206 input = new InputPanel(); 207 input.addActionListener(this); 208 } 209 if (input.getParent() != this) { 210 add (input); 211 validate(); 212 } 213 } 214 input.setVisible (val); 215 validate(); 216 getOutputPane().ensureCaretPosition(); 217 } 218 219 public void actionPerformed(ActionEvent ae) { 220 InputPanel ip = (InputPanel) ae.getSource(); 221 if (InputPanel.ACTION_EOF.equals(ae.getActionCommand())) { 222 inputEof(); 223 } else { 224 inputSent (ip.getText()); 225 } 226 } 227 228 protected abstract void inputEof(); 229 230 public void doLayout() { 231 boolean hasInput = isInputVisible(); 232 Insets ins = getInsets(); 233 int left = ins.left; 234 int bottom = hasInput ? (getHeight() - ins.bottom - 235 (input.getPreferredSize().height)) - 3 : getHeight() - ins.bottom; 236 237 Component main = outputPane; 238 239 if (main != null) { 240 main.setBounds (left, ins.top, getWidth() - (left + ins.right), 241 bottom - ins.top); 242 } 243 if (hasInput) { 244 input.setBounds (left, bottom, getWidth() - (left + ins.right), 245 getHeight() - bottom); 246 } 247 } 248 249 public abstract void hasSelectionChanged(boolean val); 250 251 void notifyInputFocusGained(){ 252 getOutputPane().lockScroll(); 253 getOutputPane().ensureCaretPosition(); 254 } 255 256 JButton[] getToolbarButtons() { 257 return buttons; 258 } 259 260 } 261 | Popular Tags |