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: package org.netbeans.modules.vmd.midp.screen.display;
042:
043: import org.netbeans.modules.vmd.midp.screen.*;
044: import java.awt.datatransfer.Transferable;
045: import java.util.ArrayList;
046: import java.util.List;
047: import org.netbeans.modules.vmd.api.model.ComponentProducer.Result;
048: import org.netbeans.modules.vmd.api.model.DesignComponent;
049: import org.netbeans.modules.vmd.api.model.PropertyValue;
050: import org.netbeans.modules.vmd.api.model.TypeID;
051: import org.netbeans.modules.vmd.api.model.common.AcceptPresenter;
052: import org.netbeans.modules.vmd.api.model.common.DesignComponentDataFlavorSupport;
053: import org.netbeans.modules.vmd.api.model.common.AcceptSuggestion;
054: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
055:
056: /**
057: *
058: * @author Karol Harezlak
059: */
060: public class ScreenMoveArrayAcceptPresenter extends AcceptPresenter {
061:
062: private String arrayPropertyName;
063: private TypeID arrayType;
064:
065: public ScreenMoveArrayAcceptPresenter(String arrayPropertyName,
066: TypeID arrayType) {
067: super (AcceptPresenter.Kind.TRANSFERABLE);
068: this .arrayPropertyName = arrayPropertyName;
069: this .arrayType = arrayType;
070: }
071:
072: @Override
073: public boolean isAcceptable(Transferable transferable,
074: AcceptSuggestion suggestion) {
075: if (!(transferable
076: .isDataFlavorSupported(DesignComponentDataFlavorSupport.DESIGN_COMPONENT_DATA_FLAVOR)))
077: return false;
078:
079: if (getComponent().getDocument().getSelectedComponents().size() > 1)
080: return false;
081:
082: DesignComponent componentTrans = DesignComponentDataFlavorSupport
083: .getTransferableDesignComponent(transferable);
084: if (!(suggestion instanceof ScreenMoveArrayAcceptSuggestion))
085: return false;
086:
087: if (componentTrans.getParentComponent() == getComponent()
088: .getParentComponent())
089: return true;
090: return false;
091: }
092:
093: @Override
094: public Result accept(Transferable transferable,
095: AcceptSuggestion suggestion) {
096: DesignComponent componentTrans = DesignComponentDataFlavorSupport
097: .getTransferableDesignComponent(transferable);
098: DesignComponent parentComponent = getComponent()
099: .getParentComponent();
100:
101: List<PropertyValue> array = parentComponent.readProperty(
102: arrayPropertyName).getArray();
103: if (array == null || array.size() < 1)
104: return super .accept(transferable, suggestion);
105:
106: List<PropertyValue> newArray = new ArrayList<PropertyValue>(
107: array);
108: PropertyValue movedValue = null;
109: int componentTransIndex = -1;
110: int componentIndex = -1;
111: for (PropertyValue value : array) {
112: if (value.getComponent().equals(componentTrans)) {
113: movedValue = value;
114: componentTransIndex = array.indexOf(value);
115: }
116: if (value.getComponent().equals(getComponent()))
117: componentIndex = array.indexOf(value);
118: }
119:
120: ScreenDeviceInfo.Edge verticalPosition = ((ScreenMoveArrayAcceptSuggestion) suggestion)
121: .getVerticalPosition();
122: newArray.remove(componentTransIndex);
123: if (verticalPosition == ScreenDeviceInfo.Edge.TOP
124: && (componentTransIndex + 1) != componentIndex)
125: newArray.add(componentIndex, movedValue);
126: else if (verticalPosition == ScreenDeviceInfo.Edge.BOTTOM) {
127: if (componentTransIndex > componentIndex)
128: newArray.add(componentIndex + 1, movedValue);
129: else
130: newArray.add(componentIndex, movedValue);
131: } else
132: return super .accept(transferable, suggestion);
133:
134: DesignComponent dragComponent = DesignComponentDataFlavorSupport
135: .getTransferableDesignComponent(transferable);
136: if (dragComponent == null)
137: return super .accept(transferable, suggestion);
138: parentComponent.writeProperty(arrayPropertyName, PropertyValue
139: .createArray(arrayType, newArray));
140: return new Result(dragComponent);
141: }
142:
143: }
|