1 19 20 package org.netbeans.core.output2; 21 22 import java.awt.AWTEvent ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Graphics ; 26 import java.awt.Insets ; 27 import java.awt.Point ; 28 import java.awt.event.FocusEvent ; 29 import java.awt.event.MouseAdapter ; 30 import java.awt.event.MouseEvent ; 31 import java.awt.event.MouseListener ; 32 import java.util.HashSet ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 import javax.swing.UIManager ; 36 import org.netbeans.core.output2.ui.AbstractOutputTab; 37 import org.netbeans.core.output2.ui.AbstractOutputWindow; 38 import org.openide.util.NbBundle; 39 import org.openide.util.Utilities; 40 import org.openide.windows.TopComponent; 41 import org.openide.windows.WindowManager; 42 43 54 public class OutputWindow extends AbstractOutputWindow { 55 private Controller controller; 56 static OutputWindow DEFAULT = null; 57 public static final String ICON_RESOURCE = 58 "org/netbeans/core/resources/frames/output.gif"; 60 private MouseListener activateListener = new MouseAdapter () { 61 public void mouseClicked(MouseEvent e) { 62 requestActive(); 64 } 65 }; 66 67 public OutputWindow() { 68 this (new Controller()); 69 enableEvents(AWTEvent.FOCUS_EVENT_MASK); 70 putClientProperty ("dontActivate", Boolean.TRUE); 71 getActionMap().put("PreviousViewAction", controller.prevTabAction); 72 getActionMap().put("NextViewAction", controller.nextTabAction); 73 } 74 75 public void addNotify() { 76 super.addNotify(); 77 pane.addMouseListener(activateListener); 78 } 79 80 public void removeNotify() { 81 super.removeNotify(); 82 pane.removeMouseListener(activateListener); 83 } 84 85 86 protected void closeRequest(AbstractOutputTab tab) { 87 controller.close (this, (OutputTab) tab, false); 88 } 89 90 OutputWindow (Controller controller) { 91 if (Controller.LOG) Controller.log("Created an output window"); 92 this.controller = controller; 93 setDisplayName (NbBundle.getMessage(OutputWindow.class, "LBL_OUTPUT")); setName (NbBundle.getMessage(OutputWindow.class, "LBL_OUTPUT")); 97 setIcon(Utilities.loadImage(ICON_RESOURCE)); putClientProperty("SlidingName", getDisplayName()); } 102 103 public static synchronized OutputWindow findDefault() { 104 if (DEFAULT == null) { 105 TopComponent tc = WindowManager.getDefault().findTopComponent("output"); if (tc != null) { 109 if (!(tc instanceof OutputWindow)) { 110 IllegalStateException exc = new IllegalStateException 114 ("Incorrect settings file. Unexpected class returned." + " Expected:" + OutputWindow.class.getName() + " Returned:" + tc.getClass().getName()); Logger.getLogger(OutputWindow.class.getName()).log(Level.WARNING, null, exc); 118 OutputWindow.getDefault(); 120 } 121 } else { 122 OutputWindow.getDefault(); 123 } 124 } 125 return DEFAULT; 126 } 127 130 public static synchronized OutputWindow getDefault() { 131 if (DEFAULT == null) { 132 DEFAULT = new OutputWindow(); 133 } 134 return DEFAULT; 135 } 136 137 public int getPersistenceType() { 138 return PERSISTENCE_ALWAYS; 139 } 140 141 public String preferredID() { 142 return "output"; } 144 145 public Object readResolve() throws java.io.ObjectStreamException { 146 return getDefault(); 147 } 148 149 public String getToolTipText() { 150 return getDisplayName(); 151 } 152 153 Controller getController() { 154 return controller; 155 } 156 157 public void requestVisible () { 158 if (Controller.LOG) { 159 Controller.log("Request visible"); 160 Controller.logStack(); 161 } 162 super.requestVisible(); 163 } 164 165 void requestVisibleForNewTab() { 166 if (Controller.LOG) Controller.log("Request visible for new tab"); 167 if (isOpened() && isShowing()) { 168 if (!isActivated()) { 169 super.requestVisible(); 170 } 171 } else { 172 if (Controller.LOG) Controller.log ("CALLING OPEN() ON OUTPUT WINDOW!"); 173 open(); 174 super.requestVisible(); 175 if (Boolean.TRUE.equals(getClientProperty("isSliding"))) { requestActiveForNewTab(); 177 } 178 } 179 } 180 181 public void processFocusEvent (FocusEvent fe) { 182 super.processFocusEvent (fe); 183 if (Boolean.TRUE.equals(getClientProperty("isSliding"))) { repaint(200); 185 } 186 } 187 188 public void paintComponent (Graphics g) { 189 super.paintComponent (g); 190 if (hasFocus()) { 191 Insets ins = getInsets(); 192 Color col = UIManager.getColor ("controlShadow"); if (col == null) col = Color.GRAY; 195 g.setColor(col); 196 g.drawRect ( 197 ins.left + 2, 198 ins.top + 2, 199 getWidth() - (ins.left + ins.right + 4), 200 getHeight() - (ins.top + ins.bottom + 4)); 201 } 202 } 203 204 void requestActiveForNewTab() { 205 requestActive(); 206 } 207 208 public void requestActive() { 209 boolean activated = isActivated(); 210 if (Controller.LOG) Controller.log("Request active"); 211 super.requestActive(); 212 if (!activated) { 213 requestFocus(); 214 } 215 } 216 217 private boolean activated = false; 218 protected void componentActivated () { 219 if (Controller.LOG) Controller.log("ComponentActivated"); 220 super.componentActivated(); 221 activated = true; 222 controller.notifyActivated (this); 223 requestFocus(); 224 } 225 226 protected void componentDeactivated() { 227 if (Controller.LOG) Controller.log("ComponentDeactivated"); 228 super.componentDeactivated(); 229 activated = false; 230 } 231 232 protected void removed(AbstractOutputTab view) { 233 if (Controller.LOG) Controller.log("Removed tab " + view); 234 if (Controller.LOG) Controller.log ("Tab has been removed. Notifying controller."); 235 controller.notifyRemoved((OutputTab) view); 236 } 237 238 protected void selectionChanged(AbstractOutputTab former, AbstractOutputTab current) { 239 controller.selectionChanged (this, (OutputTab) former, (OutputTab) current); 240 } 241 242 public void lineClicked(OutputTab outputComponent, int line) { 243 controller.lineClicked (this, outputComponent, line); 244 } 245 246 public void postPopupMenu(OutputTab outputComponent, Point p, Component src) { 247 controller.postPopupMenu (this, outputComponent, p, src); 248 } 249 250 public void caretEnteredLine(OutputTab outputComponent, int line) { 251 controller.caretEnteredLine(outputComponent, line); 252 } 253 254 public void documentChanged(OutputTab comp) { 255 controller.documentChanged (this, comp); 256 } 257 258 private HashSet hiddenTabs = null; 259 public void putHiddenView (OutputTab comp) { 260 if (hiddenTabs == null) { 261 hiddenTabs = new HashSet (); 262 } 263 comp.putClientProperty("outputWindow", this); hiddenTabs.add(comp); 265 if (comp.getParent() != null) { 266 comp.getParent().remove(comp); 267 } 268 } 269 270 public void removeHiddenView (OutputTab comp) { 271 hiddenTabs.remove(comp); 272 comp.putClientProperty("outputWindow", null); } 274 275 public void setSelectedTab (AbstractOutputTab op) { 276 if (op.getParent() == null && hiddenTabs.contains(op)) { 277 removeHiddenView ((OutputTab) op); 278 add(op); 279 } 280 super.setSelectedTab (op); 281 } 282 283 protected void updateSingletonName(String name) { 284 String winName = NbBundle.getMessage(OutputWindow.class, "LBL_OUTPUT"); if (name != null) { 286 String newName = NbBundle.getMessage(OutputWindow.class, 287 "FMT_OUTPUT", new Object [] {winName, name}); if (newName.indexOf ("<html>") != -1) { 289 newName = Utilities.replaceString(newName, "<html>", ""); setHtmlDisplayName("<html>" + newName); } else { 292 setDisplayName(newName); 293 setHtmlDisplayName(null); 294 } 295 } else { 296 setDisplayName(winName); 297 setHtmlDisplayName(null); 298 } 299 } 300 301 302 public OutputTab[] getHiddenTabs() { 303 if (hiddenTabs != null && !hiddenTabs.isEmpty()) { 304 OutputTab[] result = new OutputTab[hiddenTabs.size()]; 305 return (OutputTab[]) hiddenTabs.toArray(result); 306 } 307 return new OutputTab[0]; 308 } 309 310 public OutputTab getTabForIO (NbIO io) { 311 AbstractOutputTab[] views = getTabs(); 312 for (int i=0; i < views.length; i++) { 313 if (((OutputTab) views[i]).getIO() == io) { 314 return ((OutputTab) views[i]); 315 } 316 } 317 OutputTab[] hidden = getHiddenTabs(); 318 for (int i=0; i < hidden.length; i++) { 319 if (hidden[i].getIO() == io) { 320 return hidden[i]; 321 } 322 } 323 return null; 324 } 325 326 public void eventDispatched(IOEvent ioe) { 327 if (Controller.LOG) Controller.log ("Event received: " + ioe); 328 NbIO io = ioe.getIO(); 329 int command = ioe.getCommand(); 330 boolean value = ioe.getValue(); 331 Object data = ioe.getData(); 332 OutputTab comp = getTabForIO (io); 333 if (command == IOEvent.CMD_DETACH) { 334 if (!ioe.isConsumed()) { 335 ioe.consume(); 337 DEFAULT = null; 338 return; 339 } 340 } 341 if (Controller.LOG) Controller.log ("Passing command to controller " + ioe); 342 controller.performCommand (this, comp, io, command, value, data); 343 ioe.consume(); 344 } 345 346 public void hasSelectionChanged(OutputTab tab, boolean val) { 347 controller.hasSelectionChanged(this, tab, val); 348 } 349 350 public boolean isActivated() { 351 return activated; 352 } 353 354 public void hasOutputListenersChanged(OutputTab tab, boolean hasOutputListeners) { 355 controller.hasOutputListenersChanged(this, tab, hasOutputListeners); 356 } 357 358 public void inputEof(OutputTab tab) { 359 if (Controller.LOG) Controller.log ("Input EOF on " + this); 360 controller.inputEof(tab); 361 } 362 363 public void inputSent(OutputTab c, String txt) { 364 if (Controller.LOG) Controller.log ("Notifying controller input sent " + txt); 365 controller.notifyInput(this, c, txt); 366 } 367 } 368 | Popular Tags |