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-2007 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: package org.netbeans.modules.visualweb.text.actions;
042:
043: import java.awt.event.ActionEvent;
044:
045: import javax.swing.UIManager;
046: import org.netbeans.modules.visualweb.api.designer.Designer.DesignerEvent;
047: import org.netbeans.modules.visualweb.designer.InlineEditor;
048:
049: import org.netbeans.modules.visualweb.designer.WebForm;
050: import org.netbeans.modules.visualweb.designer.WebForm.DefaultDesignerEvent;
051: import org.netbeans.modules.visualweb.text.DesignerPaneBase;
052:
053: /**
054: * Action run by default on most keyboard keys - performs a "self insert".
055: * There are a couple of exceptions - an Escape gets special handling; it may
056: * select parent, or get you out of flow mode editing of a gridpositioned container,
057: * or if you're in a read-only region, beep.
058: * Originally based on DefaultEditorKit.DefaultKeyTypedAction.
059: */
060: public class DefaultKeyTypedAction extends TextAction {
061: /**
062: * Creates this object with the appropriate identifier.
063: */
064: public DefaultKeyTypedAction() {
065: super (DesignerPaneBase.defaultKeyTypedAction);
066: }
067:
068: /**
069: * The operation to perform when this action is triggered.
070: *
071: * @param e
072: * the action event
073: */
074: public void actionPerformed(ActionEvent e) {
075: DesignerPaneBase target = getTextComponent(e);
076:
077: if ((target != null) && (e != null)) {
078: // Document doc = target.getDocument();
079: // WebForm webform = doc.getWebForm();
080: WebForm webform = target.getWebForm();
081:
082: //// if (webform.getModel().getLiveUnit() == null) {
083: // // XXX This seems to be wrong. Model should handle it.
084: // if (!webform.isAlive()) {
085: // // Can't mutate document
086: // UIManager.getLookAndFeel().provideErrorFeedback(target);
087: //
088: // return;
089: // }
090:
091: String content = e.getActionCommand();
092:
093: if (content == null) {
094: return;
095: }
096:
097: // XXX TODO This is very suspicious, revise.
098: // Escape?
099: // TODO - make separate action for this and have separate key binding?
100: // This has advantage in that it picks up the key regardless of modifiers
101: if ((content.length() == 1)
102: && (((short) content.charAt(0)) == 27)) {
103: /* This no longer seems true -- debug!
104: // Already processed by separate Escape key listener in the DesignerPane;
105: // special handled there since the Escape key needs to be discovered
106: // during DND etc. in which case this action isn't active
107: */
108: // if (!webform.getTopComponent().seenEscape(e.getWhen())) {
109: // if (!webform.tcSeenEscape(e)) {
110: // webform.getManager().getMouseHandler().escape();
111: // }
112: target.escape(e.getWhen());
113:
114: return;
115: }
116:
117: int mod = e.getModifiers();
118:
119: if ((content.length() > 0)
120: && ((mod & ActionEvent.ALT_MASK) == (mod & ActionEvent.CTRL_MASK))) {
121: char c = content.charAt(0);
122:
123: if (((c >= 0x20) && (c != 0x7F)) || (c == '\r')
124: || (c == '\n')) {
125: // if (target.getCaret() == null) {
126: if (!target.hasCaret()) {
127: if ((c != '\r') && (c != '\n')) {
128: // If you're typing without a caret, start editing
129: // the default property of the selection
130: boolean success = webform.getSelection()
131: .focusDefaultProperty(e);
132:
133: if (!success) {
134: UIManager.getLookAndFeel()
135: .provideErrorFeedback(target);
136: }
137:
138: return;
139: } else if ((c == '\r') || (c == '\n')) {
140: // Enter key has same effect as double click
141: // webform.getActions().handleDoubleClick(true);
142: // webform.getManager().handleDoubleClick(true);
143: // webform.getManager().handleDoubleClick();
144: DesignerEvent evt = new DefaultDesignerEvent(
145: webform, null);
146: webform.fireUserActionPerformed(evt);
147:
148: return;
149: } else {
150: UIManager.getLookAndFeel()
151: .provideErrorFeedback(target);
152:
153: return;
154: }
155: }
156:
157: // XXX Moved from DesingerPaneBase
158: // XXX Hack, check whether to finish inline editing.
159: // TODO Fix inline editing.
160: InlineEditor editor = webform.getManager()
161: .getInlineEditor();
162: if ((content.equals("\n") || content.equals("\r\n")) // NOI18N
163: && (editor != null)
164: && !editor.isMultiLine()) {
165: // Commit
166: // Should I look to see if the Shift key is pressed, and if so let
167: // you insert a newline?
168: webform.getManager().finishInlineEditing(false);
169: return;
170: }
171:
172: //// UndoEvent undoEvent = webform.getModel().writeLock(NbBundle.getMessage(DefaultKeyTypedAction.class, "InsertChar")); // NOI18N
173: // DomProvider.WriteLock writeLock = webform.writeLock(NbBundle.getMessage(DefaultKeyTypedAction.class, "InsertChar")); // NOI18N
174: // try {
175: // doc.writeLock(NbBundle.getMessage(DefaultKeyTypedAction.class, "InsertChar")); // NOI18N
176: // target.getCaret().replaceSelection(content);
177: target.replaceSelection(content);
178: // } finally {
179: //// doc.writeUnlock();
180: //// webform.getModel().writeUnlock(undoEvent);
181: // webform.writeUnlock(writeLock);
182: // }
183: } // 0x7f (del) will get processed by a different action
184: }
185: }
186: }
187: }
|