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.form.actions;
043:
044: import java.awt.event.KeyEvent;
045: import java.util.List;
046: import javax.swing.undo.UndoableEdit;
047: import org.netbeans.modules.form.FormEditor;
048: import org.netbeans.modules.form.FormModel;
049: import org.netbeans.modules.form.FormUtils;
050: import org.netbeans.modules.form.MetaComponentCreator;
051: import org.netbeans.modules.form.RADComponent;
052: import org.netbeans.modules.form.RADVisualComponent;
053: import org.netbeans.modules.form.RADVisualContainer;
054: import org.netbeans.modules.form.layoutdesign.LayoutConstants;
055: import org.netbeans.modules.form.layoutdesign.LayoutModel;
056: import org.openide.nodes.Node;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.NbBundle;
059: import org.openide.util.actions.NodeAction;
060:
061: /**
062: */
063: public class DuplicateAction extends NodeAction {
064:
065: @Override
066: protected boolean asynchronous() {
067: return false;
068: }
069:
070: protected boolean enable(Node[] nodes) {
071: List comps = FormUtils.getSelectedLayoutComponents(nodes);
072: return ((comps != null) && getParent(comps) != null);
073: }
074:
075: public String getName() {
076: return NbBundle.getMessage(AlignAction.class, "ACT_Duplicate"); // NOI18N
077: }
078:
079: public HelpCtx getHelpCtx() {
080: return HelpCtx.DEFAULT_HELP;
081: }
082:
083: protected void performAction(Node[] nodes) {
084: duplicate(nodes, -1, -1);
085: }
086:
087: public static void performAction(Node[] nodes, int keyCode) {
088: int dimension = (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT) ? LayoutConstants.HORIZONTAL
089: : LayoutConstants.VERTICAL;
090: int direction = (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_UP) ? LayoutConstants.LEADING
091: : LayoutConstants.TRAILING;
092: duplicate(nodes, dimension, direction);
093: }
094:
095: private static void duplicate(Node[] nodes, int dimension,
096: int direction) {
097: List<RADComponent> comps = FormUtils
098: .getSelectedLayoutComponents(nodes);
099: RADVisualContainer parent = (comps != null) ? getParent(comps)
100: : null;
101: if (parent != null) {
102: FormModel formModel = parent.getFormModel();
103: LayoutModel layoutModel = formModel.getLayoutModel();
104: Object layoutUndoMark = layoutModel.getChangeMark();
105: UndoableEdit layoutEdit = layoutModel.getUndoableEdit();
106: boolean autoUndo = true; // in case of unexpected error, for robustness
107:
108: String[] sourceIds = new String[comps.size()];
109: String[] targetIds = new String[comps.size()];
110: int i = 0;
111: MetaComponentCreator creator = formModel
112: .getComponentCreator();
113: try {
114: for (RADComponent comp : comps) {
115: RADComponent copiedComp = creator.copyComponent(
116: comp, parent);
117: if (copiedComp == null) {
118: return; // copy failed...
119: }
120: sourceIds[i] = comp.getId();
121: targetIds[i] = copiedComp.getId();
122: i++;
123: }
124: FormEditor.getFormDesigner(formModel)
125: .getLayoutDesigner().duplicateLayout(sourceIds,
126: targetIds, dimension, direction);
127: autoUndo = false;
128: } finally {
129: if (layoutUndoMark != null
130: && !layoutUndoMark.equals(layoutModel
131: .getChangeMark())) {
132: formModel.addUndoableEdit(layoutEdit);
133: }
134: if (autoUndo) {
135: formModel.forceUndoOfCompoundEdit();
136: }
137: }
138: }
139: }
140:
141: private static RADVisualContainer getParent(List components) {
142: RADVisualContainer commonParent = null;
143: for (Object comp : components) {
144: if (comp instanceof RADVisualComponent) {
145: RADVisualContainer parent = ((RADVisualComponent) comp)
146: .getParentContainer();
147: if (parent == null
148: || (commonParent != null && parent != commonParent)) {
149: return null;
150: }
151: if (commonParent == null) {
152: commonParent = parent;
153: }
154: } else {
155: return null;
156: }
157: }
158: return commonParent != null
159: && commonParent.getLayoutSupport() == null ? commonParent
160: : null;
161: }
162: }
|