changeset: 263453:8abec78a074f tag: IZ234795 tag: qbase tag: qtip tag: tip user: Andrew Krasny date: Fri Sep 06 18:56:04 2013 +0400 summary: Bug #234795 - Multiple tabs with the same code diff --git a/openide.text/src/org/openide/text/EditorSupportLineSet.java b/openide.text/src/org/openide/text/EditorSupportLineSet.java --- a/openide.text/src/org/openide/text/EditorSupportLineSet.java +++ b/openide.text/src/org/openide/text/EditorSupportLineSet.java @@ -44,11 +44,19 @@ package org.openide.text; import java.lang.ref.Reference; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; import javax.swing.event.*; import javax.swing.text.Position; import javax.swing.text.StyledDocument; +import org.openide.util.Lookup; +import org.openide.util.LookupEvent; +import org.openide.util.LookupListener; +import org.openide.windows.CloneableOpenSupport; +import org.openide.windows.CloneableOpenSupportRedirector; /** Line set for an EditorSupport. @@ -98,7 +106,7 @@ @Deprecated public void show(int kind, int column) { - CloneableEditorSupport support = pos.getCloneableEditorSupport(); + CloneableEditorSupport support = getCloneableEditorSupport(pos); if ((kind == SHOW_TRY_SHOW) && !support.isDocumentLoaded()) { return; @@ -119,8 +127,8 @@ @Override public void show(ShowOpenType openType, ShowVisibilityType visibilityType, int column) { - CloneableEditorSupport support = pos.getCloneableEditorSupport(); - + CloneableEditorSupport support = getCloneableEditorSupport(pos); + if ((openType == ShowOpenType.NONE) && !support.isDocumentLoaded()) { return; } @@ -149,7 +157,7 @@ DocumentLine.Part part = new DocumentLine.Part( this, new PositionRef( - pos.getCloneableEditorSupport().getPositionManager(), pos.getOffset() + column, + getCloneableEditorSupport(pos).getPositionManager(), pos.getOffset() + column, Position.Bias.Forward ), length ); @@ -160,7 +168,7 @@ @Override public String getDisplayName() { - CloneableEditorSupport support = pos.getCloneableEditorSupport(); + CloneableEditorSupport support = getCloneableEditorSupport(pos); return support.messageLine(this); } @@ -170,6 +178,10 @@ return "SupportLine@" + Integer.toHexString(System.identityHashCode(this)) + " at line: " + getLineNumber(); // NOI18N } + + private static CloneableEditorSupport getCloneableEditorSupport(final PositionRef pos) { + return CESHack.getCloneableEditorSupport(pos); + } } /** Line set for closed EditorSupport. @@ -263,4 +275,69 @@ } } } + +// + private static final class CESHack { + + private static final Method redirectMethod; + private static final Lookup.Result lkp; + private static final AtomicReference> redirectors; + private static final LookupListener lkpListener; + + static { + Method m = null; + try { + m = CloneableOpenSupportRedirector.class.getDeclaredMethod( + "redirect", // NOI18N + new Class[]{CloneableOpenSupport.Env.class}); + m.setAccessible(true); + } catch (NoSuchMethodException ex) { + } catch (SecurityException ex) { + } finally { + redirectMethod = m; + } + + if (m != null) { + lkp = Lookup.getDefault().lookupResult(CloneableOpenSupportRedirector.class); + redirectors = new AtomicReference>(); + lkpListener = new LookupListener() { + @Override + public void resultChanged(LookupEvent ev) { + redirectors.set(lkp.allInstances()); + } + }; + lkp.addLookupListener(lkpListener); + lkpListener.resultChanged(null); + } else { + redirectors = null; + lkpListener = null; + lkp = null; + } + } + + private static CloneableEditorSupport getCloneableEditorSupport(final PositionRef pos) { + final CloneableEditorSupport orig = pos.getCloneableEditorSupport(); + if (orig == null || redirectMethod == null) { + return orig; + } + Collection rlist = redirectors.get(); + if (rlist != null) { + for (CloneableOpenSupportRedirector r : rlist) { + try { + Object result = redirectMethod.invoke(r, orig.cesEnv()); + if (result instanceof CloneableEditorSupport) { + return (CloneableEditorSupport) result; + } + } catch (IllegalAccessException ex) { + } catch (IllegalArgumentException ex) { + } catch (SecurityException ex) { + } catch (InvocationTargetException ex) { + } + } + } + + return orig; + } + } +// }