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: /*
042: * Created on Mar 16, 2004
043: *
044: * To change the template for this generated file go to Window>Preferences>Java>Code
045: * Generation>Code and Comments
046: */
047: package org.netbeans.modules.visualweb.text.actions;
048:
049: import java.util.List;
050: import org.netbeans.modules.visualweb.css2.CssBox;
051: import org.netbeans.modules.visualweb.css2.ModelViewMapper;
052: import org.netbeans.modules.visualweb.designer.SelectionManager;
053: import org.netbeans.modules.visualweb.designer.WebForm;
054: import java.awt.Point;
055: import java.awt.Rectangle;
056: import java.awt.event.ActionEvent;
057: import java.util.ArrayList;
058:
059: import javax.swing.SwingConstants;
060: import javax.swing.UIManager;
061:
062: import org.netbeans.modules.visualweb.api.designer.DomProvider.DomPosition;
063: import org.netbeans.modules.visualweb.text.DesignerPaneBase;
064: import org.w3c.dom.Element;
065:
066: /*
067: * Action to move the selection by way of the getNextVisualPositionFrom method. Constructor
068: * indicates direction to use.
069: */
070: public class NextVisualPositionAction extends TextAction {
071: private boolean select;
072: private int direction;
073:
074: /**
075: * Create this action with the appropriate identifier.
076: *
077: * @param nm
078: * the name of the action, Action.NAME.
079: * @param select
080: * whether to extend the selection when changing the caret position.
081: */
082: public NextVisualPositionAction(String nm, boolean select,
083: int direction) {
084: super (nm);
085: this .select = select;
086: this .direction = direction;
087: }
088:
089: /** The operation to perform when this action is triggered. */
090: public void actionPerformed(ActionEvent e) {
091: DesignerPaneBase target = getTextComponent(e);
092:
093: if (target != null) {
094: // DesignerCaret caret = target.getCaret();
095: // if (caret == null) {
096: if (!target.hasCaret()) {
097: // No caret - no focus, no next visual position...
098: // Instead, we should move the selected components.
099: // When select is true, the shift key is pressed -
100: // in this case we use it to disable snapping
101: boolean snapDisabled = select;
102:
103: // (we do snapdisabled for selection forward/backward etc.
104: // target.getDocument().getWebForm().getActions().moveSelection(direction, snapDisabled);
105: // moveSelection(target.getDocument().getWebForm(), direction, snapDisabled);
106: moveSelection(target.getWebForm(), direction,
107: snapDisabled);
108:
109: return;
110: }
111:
112: // Position dot = caret.getDot();
113: // DomPosition dot = caret.getDot();
114: // Point magicPosition = caret.getMagicCaretPosition();
115: DomPosition dot = target.getCaretDot();
116: Point magicPosition = target.getCaretMagicPosition();
117:
118: if ((magicPosition == null)
119: && ((direction == SwingConstants.NORTH) || (direction == SwingConstants.SOUTH))) {
120: // Rectangle r = target.modelToView(dot);
121: // WebForm webForm = target.getDocument().getWebForm();
122: WebForm webForm = target.getWebForm();
123:
124: Rectangle r = webForm.modelToView(dot);
125: magicPosition = new Point(r.x, r.y);
126: }
127:
128: // Position originalDot = dot;
129: DomPosition originalDot = dot;
130: dot = target.getUI().getNextVisualPositionFrom(target, dot,
131: direction);
132:
133: // if ((dot == Position.NONE) || !caret.isWithinEditableRegion(dot)) {
134: // if ((dot == DomPosition.NONE) || !caret.isWithinEditableRegion(dot)) {
135: // if ((dot == DomPosition.NONE) || !target.isCaretWithinEditableRegion(dot)) {
136: if ((dot == DomPosition.NONE)
137: || !target.getWebForm().isInsideEditableRegion(dot)) {
138: // Can't move caret that way, but I should still clear the selection
139: // if any
140: // caret.setDot(originalDot);
141: target.setCaretDot(originalDot);
142:
143: UIManager.getLookAndFeel().provideErrorFeedback(target);
144:
145: // beep
146: return;
147: }
148:
149: if (select) {
150: // caret.moveDot(dot);
151: target.moveCaretDot(dot);
152: } else {
153: // caret.setDot(dot);
154: target.setCaretDot(dot);
155: }
156:
157: if ((magicPosition != null)
158: && ((direction == SwingConstants.NORTH) || (direction == SwingConstants.SOUTH))) {
159: // target.getCaret().setMagicCaretPosition(magicPosition);
160: target.setCaretMagicPosition(magicPosition);
161: }
162: }
163: }
164:
165: // XXX Moved from DesignerActions.
166: /**
167: * Component dragging via the keyboard.
168: * Direction is from SwingConstants.
169: * It will only move absolutely positioned children.
170: */
171: private void moveSelection(WebForm webform, int direction,
172: boolean snapDisabled) {
173: SelectionManager sm = webform.getSelection();
174:
175: if (sm.isSelectionEmpty()) {
176: return;
177: }
178:
179: int numSelected = sm.getNumSelected();
180: // ArrayList beans = new ArrayList(numSelected);
181: // List<Rectangle> rectangles = new ArrayList<Rectangle>(numSelected);
182: List<Point> points = new ArrayList<Point>(numSelected);
183: List<CssBox> boxes = new ArrayList<CssBox>(numSelected);
184: // Iterator it = sm.iterator();
185: //// ModelViewMapper mapper = webform.getMapper();
186: //
187: // while (it.hasNext()) {
188: // DesignBean bean = (DesignBean)it.next();
189: for (Element componentRootElement : sm
190: .getSelectedComponentRootElements()) {
191: // DesignBean bean = WebForm.getDomProviderService().getMarkupDesignBeanForElement(componentRootElement);
192: // CssBox box = mapper.findBox(bean);
193: CssBox box = ModelViewMapper
194: .findBoxForComponentRootElement(webform.getPane()
195: .getPageBox(), componentRootElement);
196:
197: if (box == null) {
198: continue;
199: }
200:
201: if (!box.getBoxType().isAbsolutelyPositioned()) {
202: continue;
203: }
204:
205: // beans.add(bean);
206:
207: //elements.add(box.getElement());
208: boxes.add(box);
209:
210: // Rectangle r =
211: // new Rectangle(box.getAbsoluteX(), box.getAbsoluteY(), box.getWidth(),
212: // box.getHeight());
213: // rectangles.add(r);
214: points
215: .add(new Point(box.getAbsoluteX(), box
216: .getAbsoluteY()));
217: }
218:
219: // GridHandler gm = GridHandler.getInstance();
220: // GridHandler gm = webform.getGridHandler();
221: // GridHandler gm = GridHandler.getDefault();
222:
223: int offsetX = 0;
224: int offsetY = 0;
225: int stepSize = 1;
226:
227: switch (direction) {
228: case SwingConstants.EAST:
229:
230: if (snapDisabled) {
231: offsetX = stepSize;
232: } else {
233: // offsetX = gm.getGridWidth();
234: offsetX = webform.getGridWidth();
235: }
236:
237: break;
238:
239: case SwingConstants.WEST:
240:
241: if (snapDisabled) {
242: offsetX = -stepSize;
243: } else {
244: // offsetX = -gm.getGridWidth();
245: offsetX = -webform.getGridWidth();
246: }
247:
248: break;
249:
250: case SwingConstants.NORTH:
251:
252: if (snapDisabled) {
253: offsetY = -stepSize;
254: } else {
255: // offsetY = -gm.getGridHeight();
256: offsetY = -webform.getGridHeight();
257: }
258:
259: break;
260:
261: case SwingConstants.SOUTH:
262:
263: if (snapDisabled) {
264: offsetY = stepSize;
265: } else {
266: // offsetY = gm.getGridHeight();
267: offsetY = webform.getGridHeight();
268: }
269:
270: break;
271: }
272:
273: // gm.move(webform.getPane(), /*beans,*/ rectangles, boxes, Position.NONE, offsetX, offsetY,
274: // snapDisabled);
275: // gm.move(webform.getPane(), /*beans,*/ rectangles, boxes, DomPosition.NONE, offsetX, offsetY, snapDisabled);
276: // gm.move(webform.getPane(), /*beans,*/ points.toArray(new Point[points.size()]), boxes.toArray(new CssBox[boxes.size()]),
277: // DomPosition.NONE, offsetX, offsetY, snapDisabled);
278: webform.getDomDocument().moveComponents(webform,
279: boxes.toArray(new CssBox[boxes.size()]),
280: points.toArray(new Point[points.size()]),
281: DomPosition.NONE, offsetX, offsetY, !snapDisabled);
282: }
283: }
|