001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ruby.platform.execution;
043:
044: import java.io.IOException;
045: import java.util.Iterator;
046: import java.util.Map;
047: import java.util.Map.Entry;
048: import java.util.WeakHashMap;
049: import org.openide.util.Exceptions;
050: import org.openide.windows.InputOutput;
051:
052: final class FreeIOHandler {
053:
054: private FreeIOHandler() {
055: }
056:
057: /**
058: * All tabs which were used for some process which has now ended.
059: * These are closed when you start a fresh process.
060: * Map from tab to tab display name.
061: * @see "#43001"
062: */
063: private static final Map<InputOutput, String> FREE_IOS = new WeakHashMap<InputOutput, String>();
064:
065: static void addFreeIO(InputOutput io, String displayName) {
066: synchronized (FREE_IOS) {
067: FREE_IOS.put(io, displayName);
068: }
069: }
070:
071: /**
072: * Tries to find free Output Window tab for the given name.
073: *
074: * @param name the name of the free tab. Other free tabs are ignored.
075: * @param toFront to front or not
076: * @return free tab and its current display name or <tt>null</tt>
077: */
078: static Entry<InputOutput, String> findFreeIO(final String name,
079: final boolean toFront) {
080: Entry<InputOutput, String> result = null;
081: synchronized (FREE_IOS) {
082: for (Iterator<Entry<InputOutput, String>> it = FREE_IOS
083: .entrySet().iterator(); it.hasNext();) {
084: Entry<InputOutput, String> entry = it.next();
085: final InputOutput freeIO = entry.getKey();
086: final String freeName = entry.getValue();
087: if (freeIO.isClosed()) {
088: it.remove();
089: continue;
090: }
091:
092: if (result == null
093: && ExecutionService.isAppropriateName(name,
094: freeName)) {
095: // Reuse it.
096: result = new Entry<InputOutput, String>() {
097: public InputOutput getKey() {
098: return freeIO;
099: }
100:
101: public String getValue() {
102: return freeName;
103: }
104:
105: public String setValue(String value) {
106: throw new UnsupportedOperationException(
107: "Not supported yet.");
108: }
109: };
110: try {
111: freeIO.getOut().reset();
112: } catch (IOException ioe) {
113: Exceptions.printStackTrace(ioe);
114: }
115: if (toFront) {
116: freeIO.select();
117: }
118: it.remove();
119: // continue to remove all closed tabs
120: }// else {
121: // if ('auto close tabs' options implemented and checked) { // see #47753
122: // free.io.closeInputOutput();
123: // }
124: //}
125: }
126: }
127: return result;
128: }
129:
130: }
|