001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 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-2006 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.mobility.svgcore.view.source;
043:
044: import org.netbeans.modules.mobility.svgcore.palette.SVGPaletteFactory;
045: import java.io.IOException;
046: import javax.swing.JEditorPane;
047: import javax.swing.SwingUtilities;
048: import org.netbeans.modules.mobility.svgcore.SVGDataObject;
049: import org.netbeans.modules.mobility.svgcore.composer.SceneManager;
050: import org.netbeans.modules.mobility.svgcore.view.svg.SelectionCookie;
051: import org.netbeans.modules.xml.multiview.XmlMultiViewElement;
052: import org.netbeans.spi.palette.PaletteController;
053: import org.openide.cookies.EditCookie;
054: import org.openide.cookies.EditorCookie;
055: import org.openide.cookies.OpenCookie;
056: import org.openide.util.Lookup;
057: import org.openide.util.lookup.Lookups;
058: import org.openide.util.lookup.ProxyLookup;
059: import org.openide.windows.TopComponent;
060:
061: /**
062: *
063: * @author Pavel Benes
064: */
065: public class SVGSourceMultiViewElement extends XmlMultiViewElement {
066: private static final long serialVersionUID = 7525761714575627761L;
067:
068: /** Creates a new instance of SVGXmlMultiViewElement */
069: public SVGSourceMultiViewElement(SVGDataObject dObj) {
070: super (dObj);
071: }
072:
073: public Lookup getLookup() {
074: try {
075: PaletteController pc = SVGPaletteFactory.getPalette();
076:
077: return new ProxyLookup(new org.openide.util.Lookup[] {
078: dObj.getNodeDelegate().getLookup(),
079: Lookups.singleton(pc),
080: Lookups.singleton(new SelectionCookie() {
081: public void updateSelection(SVGDataObject doj,
082: String id, int startOff,
083: boolean doubleClick) {
084: selectElement(doj, startOff, doubleClick);
085: }
086: }) });
087: } catch (IOException e) {
088: SceneManager.error("Lookup creation failed", e); //NOI18N
089: return super .getLookup();
090: }
091: }
092:
093: public void componentHidden() {
094: ((SVGDataObject) dObj).setMultiViewElement(null);
095: super .componentHidden();
096: }
097:
098: public void componentOpened() {
099: super .componentOpened();
100: ((SVGDataObject) dObj).getModel().attachToOpenedDocument();
101: }
102:
103: public void componentShowing() {
104: super .componentShowing();
105: dObj.setLastOpenView(SVGDataObject.XML_VIEW_INDEX);
106: ((SVGDataObject) dObj).setMultiViewElement(this );
107: }
108:
109: public static void selectElement(final SVGDataObject svgDoj,
110: int startOffset, final boolean requestFocus) {
111: if (startOffset != -1) {
112: selectPosition(svgDoj, startOffset, requestFocus);
113: }
114: }
115:
116: public static void selectPosition(final SVGDataObject svgDoj,
117: final int position, final boolean requestFocus) {
118: openFileInEditor(svgDoj);
119:
120: SwingUtilities.invokeLater(new Runnable() {
121: @SuppressWarnings({"deprecation"})
122: public void run() {
123: EditorCookie ed = svgDoj.getCookie(EditorCookie.class);
124: try {
125: if (ed != null) {
126: ed.openDocument();
127: JEditorPane[] opened = ed.getOpenedPanes();
128: if (opened != null && opened.length > 0) {
129: final JEditorPane pane = opened[0];
130: pane.setSelectionStart(position);
131: pane.setSelectionEnd(position);
132:
133: if (requestFocus) {
134: TopComponent tc = (TopComponent) SwingUtilities
135: .getAncestorOfClass(
136: TopComponent.class,
137: pane);
138: if (tc != null) {
139: tc.requestActive();
140: // the requestActive itself does not work
141: tc.requestFocus();
142: }
143: }
144: }
145: }
146: } catch (Exception e) {
147: SceneManager.error("Select in editor failed.", e); //NOI18N
148: }
149: }
150: });
151: }
152:
153: private static boolean openFileInEditor(SVGDataObject svgDoj) {
154: EditCookie ck = svgDoj.getCookie(EditCookie.class);
155:
156: if (ck != null) {
157: ck.edit();
158: return true;
159: }
160:
161: OpenCookie oc = svgDoj.getCookie(OpenCookie.class);
162: if (oc != null) {
163: oc.open();
164: return true;
165: }
166: return false;
167: }
168: }
|