diff -r bd56db0c90b6 openide.text/apichanges.xml --- a/openide.text/apichanges.xml Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/apichanges.xml Fri Jul 11 19:18:14 2008 +0200 @@ -46,6 +46,24 @@ Text API + + + Improve Line.show + + + + + +

+ Improve Line.show to have better control over its open/visibility behavior. Previous version of Line.show + mixes open and visibility behavior into one parameter. New version separates this into 2 parameters. It is + possible to use any combination of these 2 parameters without need to add new constants for previous version. + 2 enum types ShowOpenType and ShowVisibilityType are added. +

+
+ + +
"beforeSaveRunnable" document property diff -r bd56db0c90b6 openide.text/arch.xml --- a/openide.text/arch.xml Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/arch.xml Fri Jul 11 19:18:14 2008 +0200 @@ -793,4 +793,282 @@ + + + + + +

+ XXX no answer for arch-overall +

+
+ + + + + +

+ XXX no answer for arch-quality +

+
+ + + + + +

+ XXX no answer for arch-time +

+
+ + + + + +

+ XXX no answer for arch-usecases +

+
+ + + + + + + + + + + + +

+ XXX no answer for compat-deprecation +

+
+ + + + + +

+ XXX no answer for deploy-dependencies +

+
+ + + + + +

+ XXX no answer for exec-ant-tasks +

+
+ + + + + +

+ XXX no answer for exec-threading +

+
+ + + + + +

+ XXX no answer for perf-spi +

+
+ + + + + +

+ XXX no answer for resources-preferences +

+
+ + + + + +

+ XXX no answer for security-grant +

+
+ + + + + +

+ XXX no answer for security-policy +

+
+ diff -r bd56db0c90b6 openide.text/src/org/openide/text/CloneableEditorSupport.java --- a/openide.text/src/org/openide/text/CloneableEditorSupport.java Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/src/org/openide/text/CloneableEditorSupport.java Fri Jul 11 19:18:14 2008 +0200 @@ -2183,7 +2183,7 @@ /** If one or more editors are opened finds one. * @return an editor or null if none is opened */ - private Pane getAnyEditor() { + Pane getAnyEditor() { CloneableTopComponent ctc; ctc = allEditors.getArbitraryComponent(); @@ -2220,9 +2220,14 @@ return null; } } - + final Pane openReuse(final PositionRef pos, final int column, int mode) { if (mode == Line.SHOW_REUSE_NEW) lastReusable.clear(); + return openAtImpl(pos, column, true); + } + + final Pane openReuse(final PositionRef pos, final int column, Line.ShowOpenType mode) { + if (mode == Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW) lastReusable.clear(); return openAtImpl(pos, column, true); } diff -r bd56db0c90b6 openide.text/src/org/openide/text/DocumentLine.java --- a/openide.text/src/org/openide/text/DocumentLine.java Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/src/org/openide/text/DocumentLine.java Fri Jul 11 19:18:15 2008 +0200 @@ -131,8 +131,21 @@ /* Shows the line. * @param kind one of SHOW_XXX constants. * @column the column of this line which should be selected + * @deprecated Deprecated since 6.21. Use {@link #show(ShowOpenType, ShowVisibilityType, int)} instead. */ + @Deprecated public abstract void show(int kind, int column); + + /* Shows the line. + * @param openType one of {@link ShowOpenType#SHOW_OPEN_TYPE_NONE}, {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN}, + * {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE} or {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE_NEW} + * @param visibilityType one of {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_NONE}, + * {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FRONT} + * or {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} + * @column the column of this line which should be selected + * @since org.openide.text 6.21 + */ + public abstract void show(ShowOpenType openType, ShowVisibilityType visibilityType, int column); /* Sets the breakpoint. */ @SuppressWarnings("deprecation") diff -r bd56db0c90b6 openide.text/src/org/openide/text/EditorSupportLineSet.java --- a/openide.text/src/org/openide/text/EditorSupportLineSet.java Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/src/org/openide/text/EditorSupportLineSet.java Fri Jul 11 19:18:15 2008 +0200 @@ -95,6 +95,7 @@ /** Shows the line. * @param kind one of SHOW_XXX constants. * @column the column of this line which should be selected + * @deprecated Deprecated since 6.21. Use {@link #show(ShowOpenType, ShowVisibilityType, int)} instead. */ public void show(int kind, int column) { CloneableEditorSupport support = pos.getCloneableEditorSupport(); @@ -113,6 +114,41 @@ } if (kind != SHOW_TRY_SHOW && kind != SHOW_SHOW) { editor.getComponent().requestActive(); + } + } + + /** Shows the line. + * @param openType one of {@link ShowOpenType#SHOW_OPEN_TYPE_NONE}, {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN}, + * {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE} or {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE_NEW} + * @param visibilityType one of {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_NONE}, + * {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FRONT} + * or {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} + * @column the column of this line which should be selected + * @since org.openide.text 6.21 + */ + public void show(ShowOpenType openType, ShowVisibilityType visibilityType, int column) { + CloneableEditorSupport support = pos.getCloneableEditorSupport(); + + if ((openType == ShowOpenType.SHOW_OPEN_TYPE_NONE) && !support.isDocumentLoaded()) { + return; + } + + CloneableEditorSupport.Pane editor = null; + + if ((openType == ShowOpenType.SHOW_OPEN_TYPE_REUSE) || (openType == ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW)) { + editor = support.openReuse(pos, column, openType); + } else if (openType == ShowOpenType.SHOW_OPEN_TYPE_OPEN) { + editor = support.openAt(pos, column); + } else if (openType == ShowOpenType.SHOW_OPEN_TYPE_NONE) { + editor = support.getAnyEditor(); + } + + if (editor != null) { + if (visibilityType == ShowVisibilityType.SHOW_VISIBILITY_TYPE_FRONT) { + editor.getComponent().requestVisible(); + } else if (visibilityType == ShowVisibilityType.SHOW_VISIBILITY_TYPE_FOCUS) { + editor.getComponent().requestActive(); + } } } diff -r bd56db0c90b6 openide.text/src/org/openide/text/Line.java --- a/openide.text/src/org/openide/text/Line.java Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/src/org/openide/text/Line.java Fri Jul 11 19:18:15 2008 +0200 @@ -70,16 +70,22 @@ /** Shows the line only if the editor is open. * @see #show(int) show + * @deprecated Deprecated since 6.21. Use {@link ShowOpenType#SHOW_OPEN_TYPE_NONE} + * and {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_NONE} instead. */ public final static int SHOW_TRY_SHOW = 0; /** Opens the editor if necessary and shows the line. * @see #show(int) show + * @deprecated Deprecated since 6.21. Use {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN} + * and {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_NONE} instead. */ public final static int SHOW_SHOW = 1; /** Opens the editor if necessary, shows the line, and takes the focus. * @see #show(int) show + * @deprecated Deprecated since 6.21. Use {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN} + * and {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} instead. */ public final static int SHOW_GOTO = 2; @@ -88,6 +94,8 @@ * @see #show(int) show * @see org.openide.windows.TopComponent#toFront() * @since 5.8 + * @deprecated Deprecated since 6.21. Use {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN} + * and {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FRONT} instead. */ public final static int SHOW_TOFRONT = 3; @@ -98,16 +106,65 @@ * and marks it for editor reusal. * @see #show(int) show * @since org.openide.text 6.14 + * @deprecated Deprecated since 6.21. Use {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE} + * and {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} instead. */ public final static int SHOW_REUSE = 4; /** Focuses or opens given editor, marking it as reusable editor if it - * was not opened before. Similar to {@link #SHOW_REUSE) but ignores + * was not opened before. Similar to {@link #SHOW_REUSE} but ignores * currently reusable editor. * @see #show(int) show * @since org.openide.text 6.14 + * @deprecated Deprecated since 6.21. Use {@link ShowOpenType#SHOW_OPEN_REUSE_NEW} + * and {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} instead. */ public final static int SHOW_REUSE_NEW = 5; + + /** ShowOpenType and ShowVisibilityType is replacement for constants SHOW_TRY_SHOW, SHOW_SHOW, + * SHOW_GOTO, SHOW_TOFRONT, SHOW_REUSE, SHOW_REUSE_NEW. It is to provide full control + * over show method behavior without need to add new constant for missing flag combination. + * + *

Note: Any modification of editor marked for reuse resets reuse flag. There is one global static reference + * so only one or none editor can be marked for reuse. + * + * @see #show(ShowOpenType, ShowVisibilityType) show + * @see ShowVisibilityType ShowVisibilityType + * @since org.openide.text 6.21 + * + */ + public enum ShowOpenType { + /** shows the line only if the editor is open */ + SHOW_OPEN_TYPE_NONE, + /** opens editor if necessary (editor was not opened) and shows the line */ + SHOW_OPEN_TYPE_OPEN, + /** replaces editor marked for reuse (last editor opened using SHOW_OPEN_TYPE_REUSE + * or SHOW_OPEN_TYPE_REUSE_NEW) and opens editor if necessary, if editor is being opened (editor was not opened) + * marks it for reuse, shows the line */ + SHOW_OPEN_TYPE_REUSE, + /** ignores editor marked for reuse (resets reference to editor marked for reuse), + * opens editor if necessary, if editor is being opened (editor was not opened) + * marks it for reuse, shows the line */ + SHOW_OPEN_TYPE_REUSE_NEW + }; + + /** ShowOpenType and ShowVisibilityType is replacement for constants SHOW_TRY_SHOW, SHOW_SHOW, + * SHOW_GOTO, SHOW_TOFRONT, SHOW_REUSE, SHOW_REUSE_NEW. It is to provide full control + * over show method behavior without need to add new constant for missing flag combination. + * + * @since org.openide.text 6.21 + * @see #show(ShowOpenType, ShowVisibilityType) show + * @see ShowOpenType ShowOpenType + */ + public enum ShowVisibilityType { + /** no action */ + SHOW_VISIBILITY_TYPE_NONE, + /** fronts editor component to become visible */ + SHOW_VISIBILITY_TYPE_FRONT, + /** front editor component to become visible and activates/focuses it. It does + * the same as SHOW_VISIBILITY_TYPE_FRONT plus activates/focuses editor */ + SHOW_VISIBILITY_TYPE_FOCUS + }; /** Instance of null implementation of Line.Part */ static final private Line.Part nullPart = new Line.NullPart(); @@ -187,16 +244,46 @@ /** Show the line. * @param kind one of {@link #SHOW_TRY_SHOW}, {@link #SHOW_SHOW}, or {@link #SHOW_GOTO} * @param column the column of this line which should be selected (starting at 0) + * @deprecated Deprecated since 6.21. Use {@link #show(ShowOpenType, ShowVisibilityType, int)} instead. */ + @Deprecated public abstract void show(int kind, int column); /** Shows the line (at the first column). * @param kind one of {@link #SHOW_TRY_SHOW}, {@link #SHOW_SHOW}, {@link #SHOW_GOTO}, * {@link #SHOW_REUSE} or {@link #SHOW_REUSE_NEW} * @see #show(int, int) + * @deprecated Deprecated since 6.21. Use {@link #show(ShowOpenType, ShowVisibilityType)} instead. */ + @Deprecated public void show(int kind) { show(kind, 0); + } + + /** Show the line. + * @param openType one of {@link ShowOpenType#SHOW_OPEN_TYPE_NONE}, + * {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN}, + * {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE} + * or {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE_NEW} + * @param visibilityType one of {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_NONE}, + * {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FRONT} + * or {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} + * @param column the column of this line which should be selected (starting at 0) + * @since org.openide.text 6.21 + */ + public abstract void show(ShowOpenType openType, ShowVisibilityType visibilityType, int column); + + /** Shows the line (at the first column). + * @param openType one of {@link ShowOpenType#SHOW_OPEN_TYPE_NONE}, {@link ShowOpenType#SHOW_OPEN_TYPE_OPEN}, + * {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE} or {@link ShowOpenType#SHOW_OPEN_TYPE_REUSE_NEW} + * @param visibilityType one of {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_NONE}, + * {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FRONT} + * or {@link ShowVisibilityType#SHOW_VISIBILITY_TYPE_FOCUS} + * @see #show(ShowOpenType, ShowVisibilityType, int) + * @since org.openide.text 6.21 + */ + public void show(ShowOpenType openType, ShowVisibilityType visibilityType) { + show(openType, visibilityType, 0); } /** Set or clear a (debugger) breakpoint at this line. diff -r bd56db0c90b6 openide.text/test/unit/src/org/openide/text/CloneableEditorSupportPaneTest.java --- a/openide.text/test/unit/src/org/openide/text/CloneableEditorSupportPaneTest.java Fri Jul 04 10:43:39 2008 +0200 +++ b/openide.text/test/unit/src/org/openide/text/CloneableEditorSupportPaneTest.java Fri Jul 11 19:18:15 2008 +0200 @@ -116,8 +116,23 @@ assertTrue(instance.activated); } + + /** Test with new Line.show API */ + public void testGetOpenedPanes2 () throws Exception { + content = "Ahoj\nMyDoc"; + javax.swing.text.Document doc = support.openDocument (); + support.open(); + Line line = support.getLineSet().getCurrent(0); + line.show(Line.ShowOpenType.SHOW_OPEN_TYPE_OPEN, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); + JEditorPane[] panes = support.getOpenedPanes(); + assertNotNull(panes); + assertEquals(1, panes.length); + assertNotNull(instance); + assertTrue(instance.activated); + + } - public void testGetOpenedPanes2ForSeparatePane() throws Exception { + public void testGetOpenedPanes2ForSeparatePane() throws Exception { content = "Ahoj\nMyDoc"; javax.swing.text.Document doc = support2.openDocument (); support2.open(); @@ -127,10 +142,21 @@ assertNotNull(panes); assertEquals(1, panes.length); assertNotNull(instance2); - } - + /** Test with new Line.show API */ + public void testGetOpenedPanes2ForSeparatePane2() throws Exception { + content = "Ahoj\nMyDoc"; + javax.swing.text.Document doc = support2.openDocument (); + support2.open(); + Line line = support2.getLineSet().getCurrent(0); + line.show(Line.ShowOpenType.SHOW_OPEN_TYPE_OPEN, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); + JEditorPane[] panes = support2.getOpenedPanes(); + assertNotNull(panes); + assertEquals(1, panes.length); + assertNotNull(instance2); + } + public void testDocumentSaveCancelledByUser() throws Exception { //register DialogDisplayer which "pushes" Yes option in the document save dialog MockServices.setServices(DD.class); diff -r bd56db0c90b6 openide.text/test/unit/src/org/openide/text/ReusableEditor2Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/openide.text/test/unit/src/org/openide/text/ReusableEditor2Test.java Fri Jul 11 19:18:15 2008 +0200 @@ -0,0 +1,343 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.openide.text; + + +import java.beans.PropertyChangeListener; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import javax.swing.JEditorPane; +import junit.framework.*; +import org.netbeans.junit.*; +import org.openide.util.Lookup; +import org.openide.util.Mutex; +import org.openide.util.lookup.*; + + +/** Testing the behavior of editor reusal framework. It uses new Line.show API. + * The behavior was discussed thoroughly at issue 94607. + * + * @author Petr Nejedly, Marek Slama + */ +public class ReusableEditor2Test extends NbTestCase { + static { + System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); + } + CES c1, c2, c3; + + /** + * Test ctor + * @param testName + */ + public ReusableEditor2Test(java.lang.String testName) { + super(testName); + } + + + /** + * Prepares few editors at the test dispoition. + */ + protected void setUp () { + c1 = createSupport("c1"); + c2 = createSupport("c2"); + c3 = createSupport("c3"); + } + + /** + * Closes any precreated editors left open. + */ + @Override + protected void tearDown() { + forceClose(c1); + forceClose(c2); + forceClose(c3); + } + + /** + * Test that verifies ShowOpenType.SHOW_OPEN_TYPE_REUSE closes original tab (keeps only one) + * Scenario: + * 1. Open first file with ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 2. Open second file with ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 3. Verify first is closed + * 4. Open first file with ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 5. Verify second is closed + */ + public void testReuse() { + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 1 + openAndCheck(c2, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 2 + assertClosed(c1); // 3 + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 4 + assertClosed(c2); // 5 + } + + /** Test that verifies ShowOpenType.SHOW_OPEN_TYPE_REUSE doesn't reuse modified, even saved tab + * 1. Open first file with ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 2. Modify it + * 3. Open second file with ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 4. Verify first still open + * 5. Modify second file + * 6. Unmodify second file + * 7. Open third file with ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 8. Verify second still open + */ + public void testKeepTouched() { + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 1 + c1.notifyModified(); // 2 + openAndCheck(c2, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 3 + assertOpened(c1); // 4 + c2.notifyModified(); // 5 + c2.notifyUnmodified(); // 6 + openAndCheck(c3, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 7 + assertOpened(c2); // 8 + assertOpened(c1); + } + + /** Test that verifies ShowOpenType.SHOW_OPEN_TYPE_REUSE don't consider non-reusable tabs. + * There are three things tested: + * A) Don't replace ordinary tabs + * B) Don't mark ordinary tabs as reusable if switched to + * C) Keep reusable tab mark even through (B) + * + * Scenario: + * 1. Open first file using ShowOpenType.SHOW_OPEN_TYPE_OPEN and ShowVisibilityType.SHOW_VISIBILITY_TYPE_FOCUS + * 2. Open second file using ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 3. Verify first still opened (A) + * 4. open first using ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 5. verify second still opened + * 6. open third file using ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 7. verify first still opened (B) + * 8. verify second closed (C) + */ + public void testLeaveNonreusable() { + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_OPEN, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_FOCUS); // 1 + openAndCheck(c2, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 2 + assertOpened(c1); // 3 + + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 4 + assertOpened(c2); // 5 + openAndCheck(c3, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 6 + assertOpened(c1); // 7 + + assertClosed(c2); // 8 + } + + /** Test that verifies ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW don't close existing reusable tab, + * but can be reused itself + * + * Scenario: + * 1. Open first file using ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 2. Open second file using ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW + * 3. Verify first still opened + * 4. Open third using ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 5. verify second closed + */ + public void testReuseNewKeepsOld() { + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 1 + openAndCheck(c2, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 2 + assertOpened(c1); // 3 + openAndCheck(c3, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 4 + assertClosed(c2); // 5 + } + + /** + * Test that specifies behaviour of ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW in case currently + * reusable tab is not the selected one. + * + * Scenario: + * 1. Open first file using ShowOpenType.SHOW_OPEN_TYPE_REUSE + * 2. Open second file using ShowOpenType.SHOW_OPEN_TYPE_OPEN + ShowVisibilityType.SHOW_VISIBILITY_TYPE_FOCUS + * 3. Open third file using ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW + * 4. Verify first still open. + */ + public void testReuseNewKeepsOldEvenWhenNotFocused() { + openAndCheck(c1, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 1 + openAndCheck(c2, Line.ShowOpenType.SHOW_OPEN_TYPE_OPEN, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_FOCUS); // 2 + openAndCheck(c3, Line.ShowOpenType.SHOW_OPEN_TYPE_REUSE_NEW, Line.ShowVisibilityType.SHOW_VISIBILITY_TYPE_NONE); // 3 + assertOpened(c1); // 4 + } + + private CES createSupport(String txt) { + Env env = new Env(); + env.content = txt; + CES c = new CES(env, Lookups.singleton(txt)); + env.support = c; + return c; + } + + private void openAndCheck(final CES ces, final Line.ShowOpenType openType, final Line.ShowVisibilityType visibilityType) { + Mutex.EVENT.readAccess(new Mutex.Action() { + public Void run() { + ces.getLineSet().getCurrent(0).show(openType, visibilityType); + return null; + } + + }); + assertOpened(ces); + } + + private void forceClose(CES ces) { + if (ces.isModified()) ces.notifyUnmodified(); + ces.close(); + } + + private void assertClosed(CES ces) { + assertEquals(0, getOpenedCount(ces)); + } + + private void assertOpened(CES ces) { + assertEquals(1, getOpenedCount(ces)); + } + + private int getOpenedCount(final CES ces) { + return Mutex.EVENT.readAccess(new Mutex.Action() { + public Integer run() { + JEditorPane[] panes = ces.getOpenedPanes(); + return panes == null ? 0 : panes.length; + } + }); + } + + + + // + // Implementation of the CloneableEditorSupport.Env + // + private class Env implements CloneableEditorSupport.Env { + // Env variables + private String content = ""; + private boolean valid = true; + private boolean modified = false; + private java.util.Date date = new java.util.Date (); + private List propL = new ArrayList(); + private java.beans.VetoableChangeListener vetoL; + /** the support to work with */ + CloneableEditorSupport support; + + public synchronized void addPropertyChangeListener(PropertyChangeListener l) { + propL.add (l); + } + public synchronized void removePropertyChangeListener(PropertyChangeListener l) { + propL.remove (l); + } + + public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { + assertNull ("This is the first veto listener", vetoL); + vetoL = l; + } + public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { + assertEquals ("Removing the right veto one", vetoL, l); + vetoL = null; + } + + public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { + return support; + } + + public String getMimeType() { + return "text/plain"; + } + + public java.util.Date getTime() { + return date; + } + + public java.io.InputStream inputStream() throws java.io.IOException { + return new java.io.ByteArrayInputStream (content.getBytes ()); + } + public java.io.OutputStream outputStream() throws java.io.IOException { + class ContentStream extends java.io.ByteArrayOutputStream { + public void close () throws java.io.IOException { + super.close (); + content = new String (toByteArray ()); + } + } + + return new ContentStream (); + } + + public boolean isValid() { + return valid; + } + + public boolean isModified() { + return modified; + } + + public void markModified() throws java.io.IOException { + modified = true; + } + + public void unmarkModified() { + modified = false; + } + } + + /** Implementation of the CES */ + private static final class CES extends CloneableEditorSupport { + public CES (Env env, Lookup l) { + super (env, l); + } + + protected String messageName() { + return "Name"; + } + + protected String messageOpened() { + return "Opened"; + } + + protected String messageOpening() { + return "Opening"; + } + + protected String messageSave() { + return "Save"; + } + + protected String messageToolTip() { + return "ToolTip"; + } + + } + +}