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.editor.ext;
043:
044: import java.awt.event.ActionEvent;
045: import javax.swing.JPopupMenu;
046: import javax.swing.Action;
047: import javax.swing.text.JTextComponent;
048: import org.netbeans.editor.EditorUI;
049: import org.netbeans.editor.BaseKit;
050: import org.netbeans.editor.Utilities;
051: import org.netbeans.editor.PopupManager;
052:
053: /**
054: * Editor UI for the component. All the additional UI features
055: * like advanced scrolling, info about fonts, abbreviations,
056: * keyword matching are based on this class.
057: *
058: * @author Miloslav Metelka
059: * @version 1.00
060: */
061: public class ExtEditorUI extends EditorUI {
062:
063: private ToolTipSupport toolTipSupport;
064:
065: private JPopupMenu popupMenu;
066:
067: private Completion completion;
068:
069: private PopupManager popupManager;
070:
071: private CompletionJavaDoc completionJavaDoc;
072:
073: private boolean noCompletion; // no completion available
074:
075: private boolean noCompletionJavaDoc; // no completion available
076:
077: public ExtEditorUI() {
078:
079: getToolTipSupport();
080: getCompletion();
081: getCompletionJavaDoc();
082: }
083:
084: public ToolTipSupport getToolTipSupport() {
085: if (toolTipSupport == null) {
086: toolTipSupport = new ToolTipSupport(this );
087: }
088: return toolTipSupport;
089: }
090:
091: public Completion getCompletion() {
092:
093: if (completion == null) {
094: if (noCompletion) {
095: return null;
096: }
097:
098: synchronized (getComponentLock()) {
099: JTextComponent component = getComponent();
100: if (component != null) {
101: BaseKit kit = Utilities.getKit(component);
102: if (kit != null && kit instanceof ExtKit) {
103: completion = ((ExtKit) kit)
104: .createCompletion(this );
105: if (completion == null) {
106: noCompletion = true;
107: }
108: }
109: }
110: }
111: }
112:
113: return completion;
114: }
115:
116: public CompletionJavaDoc getCompletionJavaDoc() {
117: if (completionJavaDoc == null) {
118: if (noCompletionJavaDoc) {
119: return null;
120: }
121:
122: synchronized (getComponentLock()) {
123: JTextComponent component = getComponent();
124: if (component != null) {
125: BaseKit kit = Utilities.getKit(component);
126: if (kit != null && kit instanceof ExtKit) {
127: completionJavaDoc = ((ExtKit) kit)
128: .createCompletionJavaDoc(this );
129: if (completionJavaDoc == null) {
130: noCompletionJavaDoc = true;
131: }
132: }
133: }
134: }
135: }
136:
137: return completionJavaDoc;
138: }
139:
140: public PopupManager getPopupManager() {
141: if (popupManager == null) {
142:
143: synchronized (getComponentLock()) {
144: JTextComponent component = getComponent();
145: if (component != null) {
146: popupManager = new PopupManager(component);
147: }
148: }
149: }
150:
151: return popupManager;
152: }
153:
154: public void showPopupMenu(int x, int y) {
155: // First call the build-popup-menu action to possibly rebuild the popup menu
156: JTextComponent component = getComponent();
157: JPopupMenu pm = getPopupMenu();
158: if (component != null && (pm == null || !pm.isVisible())) {
159: BaseKit kit = Utilities.getKit(component);
160: if (kit != null) {
161: Action a = kit
162: .getActionByName(ExtKit.buildPopupMenuAction);
163: if (a != null) {
164: a
165: .actionPerformed(new ActionEvent(component,
166: 0, "")); // NOI18N
167: }
168: }
169:
170: pm = getPopupMenu(); // refresh after building of the popup menu
171: if (pm != null) {
172: if (component.isShowing()) { // fix of #18808
173: pm.show(component, x, y);
174: }
175: }
176: }
177: }
178:
179: public void hidePopupMenu() {
180: JPopupMenu pm = getPopupMenu();
181: if (pm != null) {
182: pm.setVisible(false);
183: setPopupMenu(null);
184: }
185: }
186:
187: public JPopupMenu getPopupMenu() {
188: return popupMenu;
189: }
190:
191: public void setPopupMenu(JPopupMenu popupMenu) {
192: this.popupMenu = popupMenu;
193: }
194:
195: }
|