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: package org.netbeans.modules.visualweb.designer.jsf.action;
043:
044: import org.netbeans.modules.visualweb.api.designer.Designer;
045: import com.sun.rave.designtime.DesignBean;
046: import org.netbeans.modules.visualweb.designer.jsf.JsfSupportUtilities;
047: import org.netbeans.modules.visualweb.insync.live.LiveUnit;
048: import org.netbeans.modules.visualweb.spi.designtime.idebridge.action.AbstractDesignBeanAction;
049: import javax.swing.Action;
050: import javax.swing.JMenuItem;
051: import javax.swing.event.ChangeListener;
052: import org.netbeans.modules.visualweb.designer.jsf.JsfForm;
053: import org.openide.awt.Actions;
054: import org.openide.util.HelpCtx;
055: import org.openide.util.Lookup;
056: import org.openide.util.NbBundle;
057:
058: /**
059: * Action providing aligning.
060: *
061: * @author Peter Zavadsky
062: * @author Tor Norbye (old functionality implementation -> performActionAt impl)
063: *
064: * @todo Implement it the way it works against context and not
065: * against the selection (the old impl is still in place).
066: */
067: public class AlignAction extends AbstractDesignBeanAction {
068:
069: /** Creates a new instance of AlignAction. */
070: public AlignAction() {
071: }
072:
073: protected String getDisplayName(DesignBean[] designBeans) {
074: String alignName = NbBundle.getMessage(AlignAction.class,
075: "LBL_AlignAction");
076: if (designBeans.length == 0) {
077: return alignName;
078: }
079:
080: // WebForm webform = WebForm.findWebFormForDesignContext(designBeans[0].getDesignContext());
081: Designer designer = JsfSupportUtilities
082: .findDesignerForDesignContext(designBeans[0]
083: .getDesignContext());
084: // if (webform == null) {
085: if (designer == null) {
086: return alignName;
087: // } else if (webform.getSelection().getNumSelected() == 1) {
088: } else if (designer.getSelectedCount() == 1) {
089: return AlignMenuModel.SNAP_TO_GRID.getDisplayName();
090: } else {
091: return alignName;
092: }
093: }
094:
095: protected String getIconBase(DesignBean[] designBeans) {
096: return null;
097: }
098:
099: protected boolean isEnabled(DesignBean[] designBeans) {
100: if (designBeans.length == 0) {
101: return false;
102: }
103:
104: // XXX FIXME Cannot be an insync dependency.
105: if (!LiveUnit.isTrayBean(designBeans[0])) {
106: // if (designBeans[0] instanceof MarkupDesignBean && !WebForm.getDomProviderService().isTrayComponent(
107: // WebForm.getDomProviderService().getComponentRootElementForMarkupDesignBean((MarkupDesignBean)designBeans[0]))) {
108: // WebForm webform = WebForm.findWebFormForDesignContext(designBeans[0].getDesignContext());
109: // if (webform != null && webform.getSelection().getNumSelected() > 0) {
110: Designer designer = JsfSupportUtilities
111: .findDesignerForDesignContext(designBeans[0]
112: .getDesignContext());
113: if (designer != null && designer.getSelectedCount() > 0) {
114: return true;
115: } else {
116: return false;
117: }
118: } else {
119: return false;
120: }
121: }
122:
123: protected void performAction(DesignBean[] designBeans) {
124: // XXX Strange impl of the Actions.SubMenu(action, model, isPopup). If the model provides one item,
125: // it doesn't call the performAt(0), but this method.
126: new AlignMenuModel(designBeans).performActionAt(0);
127: }
128:
129: protected JMenuItem getMenuPresenter(Action contextAwareAction,
130: Lookup.Result result) {
131: return new Actions.SubMenu(contextAwareAction,
132: new AlignMenuModel(getDesignBeans(result)), false);
133: }
134:
135: protected JMenuItem getPopupPresenter(Action contextAwareAction,
136: Lookup.Result result) {
137: return new Actions.SubMenu(contextAwareAction,
138: new AlignMenuModel(getDesignBeans(result)), true);
139: }
140:
141: /** Implementation of the actions submenu model. */
142: private static class AlignMenuModel implements Actions.SubMenuModel {
143: private static abstract class AlignmentAction {
144: private final JsfForm.Alignment alignment;
145: private final String displayName;
146:
147: public AlignmentAction(JsfForm.Alignment alignment,
148: String displayName) {
149: this .alignment = alignment;
150: this .displayName = displayName;
151: }
152:
153: public String getDisplayName() {
154: return displayName;
155: }
156:
157: public JsfForm.Alignment getAlignment() {
158: return alignment;
159: }
160:
161: // protected abstract void performAction(WebForm webform);
162: // protected abstract void performAction(Designer designer);
163: protected abstract void performAction(JsfForm jsfForm);
164: } // End of Alignment class.
165:
166: private static class SnapToGridAlignment extends
167: AlignmentAction {
168: public SnapToGridAlignment(String displayName) {
169: super (JsfForm.Alignment.SNAP_TO_GRID, displayName);
170: }
171:
172: // protected void performAction(WebForm webform) {
173: // snapToGrid(webform);
174: // }
175: // protected void performAction(Designer designer) {
176: // if (designer != null) {
177: // designer.snapToGrid();
178: // }
179: // }
180: protected void performAction(JsfForm jsfForm) {
181: Designer designer = JsfSupportUtilities
182: .findDesignerForJsfForm(jsfForm);
183: if (designer == null) {
184: return;
185: }
186: jsfForm.snapToGrid(designer);
187: }
188: } // End of SnapToGridAlignment class.
189:
190: private static class SimpleAlignment extends AlignmentAction {
191: public SimpleAlignment(JsfForm.Alignment designerAlignment,
192: String displayName) {
193: super (designerAlignment, displayName);
194: }
195:
196: // protected void performAction(WebForm webform) {
197: // align(webform, alignmentType);
198: // }
199: // protected void performAction(Designer designer) {
200: // if (designer != null) {
201: // designer.align(getDesignerAlignment());
202: // }
203: // }
204: protected void performAction(JsfForm jsfForm) {
205: Designer designer = JsfSupportUtilities
206: .findDesignerForJsfForm(jsfForm);
207: if (designer == null) {
208: return;
209: }
210: jsfForm.align(designer, getAlignment());
211: }
212: } // End of SimpleAlignment class.
213:
214: // XXX Make an enum once moved to jdk5.0 sources.
215: private static final AlignmentAction SNAP_TO_GRID = new SnapToGridAlignment(
216: NbBundle
217: .getMessage(AlignAction.class, "LBL_SnapToGrid"));
218: private static final AlignmentAction TOP = new SimpleAlignment(
219: JsfForm.Alignment.TOP, NbBundle.getMessage(
220: AlignAction.class, "LBL_Top"));
221: private static final AlignmentAction MIDDLE = new SimpleAlignment(
222: JsfForm.Alignment.MIDDLE, NbBundle.getMessage(
223: AlignAction.class, "LBL_Middle"));
224: private static final AlignmentAction BOTTOM = new SimpleAlignment(
225: JsfForm.Alignment.BOTTOM, NbBundle.getMessage(
226: AlignAction.class, "LBL_Bottom"));
227: private static final AlignmentAction LEFT = new SimpleAlignment(
228: JsfForm.Alignment.LEFT, NbBundle.getMessage(
229: AlignAction.class, "LBL_Left"));
230: private static final AlignmentAction CENTER = new SimpleAlignment(
231: JsfForm.Alignment.CENTER, NbBundle.getMessage(
232: AlignAction.class, "LBL_Center"));
233: private static final AlignmentAction RIGHT = new SimpleAlignment(
234: JsfForm.Alignment.RIGHT, NbBundle.getMessage(
235: AlignAction.class, "LBL_Right"));
236:
237: // private static final Alignment[] alignments = new Alignment[] {
238: // SNAP_TO_GRID, TOP, MIDDLE, BOTTOM, LEFT, CENTER, RIGHT};
239: private final AlignmentAction[] alignments;
240:
241: private final DesignBean[] designBeans;
242:
243: public AlignMenuModel(DesignBean[] designBeans) {
244: this .designBeans = designBeans;
245:
246: // WebForm webform = WebForm.findWebFormForDesignContext(designBeans[0].getDesignContext());
247: // if (webform == null) {
248: Designer designer = JsfSupportUtilities
249: .findDesignerForDesignContext(designBeans[0]
250: .getDesignContext());
251: if (designer == null) {
252: this .alignments = new AlignmentAction[0];
253: // } else if (webform.getSelection().getNumSelected() == 1) {
254: } else if (designer.getSelectedCount() == 1) {
255: this .alignments = new AlignmentAction[] { SNAP_TO_GRID };
256: } else {
257: this .alignments = new AlignmentAction[] { SNAP_TO_GRID,
258: TOP, MIDDLE, BOTTOM, LEFT, CENTER, RIGHT };
259: }
260: }
261:
262: public int getCount() {
263: return alignments.length;
264: }
265:
266: public String getLabel(int i) {
267: return alignments[i].getDisplayName();
268: }
269:
270: public HelpCtx getHelpCtx(int i) {
271: // XXX Implement?
272: return null;
273: }
274:
275: public void performActionAt(int i) {
276: if (designBeans.length == 0) {
277: return;
278: }
279:
280: // WebForm webform = WebForm.findWebFormForDesignContext(designBeans[0].getDesignContext());
281: // if (webform == null) {
282: // return;
283: // }
284: // Designer designer = JsfSupportUtilities.getDesignerForDesignContext(designBeans[0].getDesignContext());
285: // if (designer == null) {
286: // return;
287: // }
288: //
289: //// alignments[i].performAction(webform);
290: // alignments[i].performAction(designer);
291: JsfForm jsfForm = JsfSupportUtilities
292: .findJsfFormForDesignContext(designBeans[0]
293: .getDesignContext());
294: if (jsfForm == null) {
295: return;
296: }
297:
298: alignments[i].performAction(jsfForm);
299: }
300:
301: public void addChangeListener(ChangeListener changeListener) {
302: // dummy, this model is not mutable.
303: }
304:
305: public void removeChangeListener(ChangeListener changeListener) {
306: // dummy, this model is not mutable.
307: }
308:
309: } // End of AlignMenuModel.
310:
311: // // XXX Copied from before DesignerActions
312: // /** Snap selection to grid */
313: // private static void snapToGrid(WebForm webform) {
314: //// GridHandler handler = GridHandler.getInstance();
315: // GridHandler handler = webform.getGridHandler();
316: // DesignerPane editor = webform.getPane();
317: // SelectionManager sm = webform.getSelection();
318: //// Iterator it = sm.iterator();
319: // Element[] componentRootElements = sm.getSelectedComponentRootElements();
320: //// ModelViewMapper mapper = webform.getMapper();
321: // boolean haveMoved = false;
322: //// Document doc = webform.getDocument();
323: //
324: //// UndoEvent undoEvent = webform.getModel().writeLock(NbBundle.getMessage(AlignAction.class, "LBL_SnapToGrid")); // NOI18N
325: // DomProvider.WriteLock writeLock = webform.writeLock(NbBundle.getMessage(AlignAction.class, "LBL_SnapToGrid")); // NOI18N
326: // try {
327: //// doc.writeLock(NbBundle.getMessage(AlignAction.class, "LBL_SnapToGrid")); // NOI18N
328: //
329: //// while (it.hasNext()) {
330: //// MarkupDesignBean bean = (MarkupDesignBean)it.next();
331: // for (Element componentRootElement : componentRootElements) {
332: //// MarkupDesignBean bean = WebForm.getDomProviderService().getMarkupDesignBeanForElement(componentRootElement);
333: //// CssBox box = mapper.findBox(bean);
334: // CssBox box = ModelViewMapper.findBoxForComponentRootElement(webform.getPane().getPageBox(), componentRootElement);
335: //
336: // if (box == null) {
337: // continue;
338: // }
339: //
340: // boolean canAlign = box.getBoxType().isAbsolutelyPositioned();
341: //
342: // if (!canAlign) {
343: // continue;
344: // }
345: //
346: // int x = box.getAbsoluteX();
347: // int y = box.getAbsoluteY();
348: // handler.moveTo(editor, /*bean,*/ box, x, y, false);
349: // haveMoved = true;
350: // }
351: // } finally {
352: //// doc.writeUnlock();
353: //// webform.getModel().writeUnlock(undoEvent);
354: // webform.writeUnlock(writeLock);
355: // }
356: //
357: // if (!haveMoved) {
358: // StatusDisplayer.getDefault().setStatusText(
359: // NbBundle.getMessage(AlignAction.class, "MSG_AlignAbsolute"));
360: // UIManager.getLookAndFeel().provideErrorFeedback(webform.getPane());
361: // }
362: // }
363: //
364: // // XXX Copied from the former DesignerActions
365: // private static final int ALIGN_TOP = 0;
366: // private static final int ALIGN_MIDDLE = 1;
367: // private static final int ALIGN_BOTTOM = 2;
368: // private static final int ALIGN_LEFT = 3;
369: // private static final int ALIGN_CENTER = 4;
370: // private static final int ALIGN_RIGHT = 5;
371: //
372: // /** Align selection to the primary selection item */
373: // private static void align(WebForm webform, int direction) {
374: // // Primary
375: // SelectionManager sm = webform.getSelection();
376: //
377: // if (sm.isSelectionEmpty()) {
378: // return;
379: // }
380: //
381: // sm.pickPrimary();
382: //
383: //// ModelViewMapper mapper = webform.getMapper();
384: //// CssBox primaryBox = mapper.findBox(sm.getPrimary());
385: // CssBox primaryBox = ModelViewMapper.findBox(webform.getPane().getPageBox(), sm.getPrimary());
386: //
387: // if (primaryBox == null) {
388: // return;
389: // }
390: //
391: // boolean haveMoved = false;
392: //// Document doc = webform.getDocument();
393: //
394: //// UndoEvent undoEvent = webform.getModel().writeLock(NbBundle.getMessage(SelectionManager.class, "Align")); // NOI18N
395: // DomProvider.WriteLock writeLock = webform.writeLock(NbBundle.getMessage(SelectionManager.class, "Align")); // NOI18N
396: // try {
397: //// doc.writeLock(NbBundle.getMessage(SelectionManager.class, "Align")); // NOI18N
398: //
399: //// GridHandler handler = GridHandler.getInstance();
400: // GridHandler handler = webform.getGridHandler();
401: // DesignerPane editor = webform.getPane();
402: // boolean canAlign = primaryBox.getBoxType().isAbsolutelyPositioned();
403: // int x = primaryBox.getAbsoluteX();
404: // int y = primaryBox.getAbsoluteY();
405: // int w = primaryBox.getWidth();
406: // int h = primaryBox.getHeight();
407: //// Iterator it = sm.iterator();
408: ////
409: //// while (canAlign && it.hasNext()) {
410: //// MarkupDesignBean bean = (MarkupDesignBean)it.next();
411: // for (Element componentRootElement : sm.getSelectedComponentRootElements()) {
412: //// MarkupDesignBean bean = WebForm.getDomProviderService().getMarkupDesignBeanForElement(componentRootElement);
413: //// CssBox box = mapper.findBox(bean);
414: // CssBox box = ModelViewMapper.findBoxForComponentRootElement(webform.getPane().getPageBox(), componentRootElement);
415: //
416: // if (box == null) {
417: // continue;
418: // }
419: //
420: // // XXX Should I use isPositioned() instead? (e.g. are relative
421: // // positioned boxes alignable?
422: // if (!box.getBoxType().isAbsolutelyPositioned()) {
423: // continue;
424: // }
425: //
426: // haveMoved = true;
427: //
428: // /*
429: // Element element = FacesSupport.getElement(fob.component);
430: // if (element == null) {
431: // continue;
432: // }
433: // */
434: // switch (direction) {
435: // case ALIGN_TOP:
436: // handler.moveTo(editor, /*bean,*/ box, box.getAbsoluteX(), y, true);
437: //
438: // break;
439: //
440: // case ALIGN_MIDDLE:
441: // handler.moveTo(editor, /*bean,*/ box, box.getAbsoluteX(),
442: // (y + (h / 2)) - (box.getHeight() / 2), true);
443: //
444: // break;
445: //
446: // case ALIGN_BOTTOM:
447: // handler.moveTo(editor, /*bean,*/ box, box.getAbsoluteX(),
448: // (y + h) - box.getHeight(), true);
449: //
450: // break;
451: //
452: // case ALIGN_LEFT:
453: // handler.moveTo(editor, /*bean,*/ box, x, box.getAbsoluteY(), true);
454: //
455: // break;
456: //
457: // case ALIGN_CENTER:
458: // handler.moveTo(editor, /*bean,*/ box, (x + (w / 2)) - (box.getWidth() / 2),
459: // box.getAbsoluteY(), true);
460: //
461: // break;
462: //
463: // case ALIGN_RIGHT:
464: // handler.moveTo(editor, /*bean,*/ box, (x + w) - box.getWidth(), box.getAbsoluteY(),
465: // true);
466: //
467: // break;
468: // }
469: // }
470: // } finally {
471: //// doc.writeUnlock();
472: //// webform.getModel().writeUnlock(undoEvent);
473: // webform.writeUnlock(writeLock);
474: // }
475: //
476: // if (!haveMoved) {
477: // StatusDisplayer.getDefault().setStatusText(
478: // NbBundle.getMessage(AlignAction.class,"MSG_AlignAbsolute"));
479: // UIManager.getLookAndFeel().provideErrorFeedback(webform.getPane());
480: // }
481: // }
482: }
|