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:
042: package org.netbeans.modules.vmd.api.model.presenters.actions;
043:
044: import java.awt.event.ActionEvent;
045: import java.lang.ref.WeakReference;
046: import java.util.ArrayList;
047: import java.util.List;
048:
049: import javax.swing.AbstractAction;
050: import javax.swing.Action;
051:
052: import org.netbeans.modules.vmd.api.model.DesignComponent;
053: import org.netbeans.modules.vmd.api.model.PropertyValue;
054: import org.netbeans.modules.vmd.api.model.TypeID;
055: import org.openide.util.NbBundle;
056:
057: /**
058: *
059: * @author Karol Harezlak
060: */
061: public abstract class MoveAction extends AbstractAction implements
062: ActionContext {
063:
064: public static final String DISPLAY_NAME_MOVE_UP = NbBundle
065: .getMessage(MoveAction.class, "NAME_MoveUpAction"); //NOI18N
066: public static final String DISPLAY_NAME_MOVE_DOWN = NbBundle
067: .getMessage(MoveAction.class, "NAME_MoveDownAction"); //NOI18N
068: public static final String PROPERTY_NAME_REFERENCE = "propertyName"; //NOI18N
069:
070: public static final Action createMoveUpAction(
071: String arrayPropertyName) {
072: return new MoveAction(arrayPropertyName, DISPLAY_NAME_MOVE_UP) {
073: protected void invokeMoveAction(List<PropertyValue> array,
074: List<PropertyValue> newArray,
075: PropertyValue currentValue) {
076: int index = array.indexOf(currentValue);
077: if (index != 0) {
078: newArray.remove(currentValue);
079: newArray.add(index - 1, currentValue);
080: saveToModel(newArray);
081: }
082: }
083: };
084: }
085:
086: public static final Action createMoveDownAction(
087: String arrayPropertyName) {
088: return new MoveAction(arrayPropertyName, DISPLAY_NAME_MOVE_DOWN) {
089: protected void invokeMoveAction(List<PropertyValue> array,
090: List<PropertyValue> newArray,
091: PropertyValue currentValue) {
092: int index = array.indexOf(currentValue);
093: int minIndex = array.size() - index;
094: if (minIndex > 1) {
095: newArray.remove(currentValue);
096: newArray.add(index + 1, currentValue);
097: saveToModel(newArray);
098: }
099: }
100: };
101: }
102:
103: private WeakReference<DesignComponent> component;
104: private boolean isEnabled;
105: private String arrayPropertyName;
106: private TypeID newArrayTypeID;
107:
108: private MoveAction(String arrayPropertyName, String displayName) {
109: this .putValue(Action.NAME, displayName);
110: this .arrayPropertyName = arrayPropertyName;
111: }
112:
113: protected DesignComponent getComponent() {
114: return component.get();
115: }
116:
117: @Override
118: public boolean isEnabled() {
119: component.get().getDocument().getTransactionManager()
120: .readAccess(new Runnable() {
121: public void run() {
122: if (component.get().getDocument()
123: .getSelectedComponents().size() > 1)
124: isEnabled = false;
125: else
126: isEnabled = true;
127: }
128: });
129:
130: return isEnabled;
131: }
132:
133: public void actionPerformed(ActionEvent actionEvent) {
134: getComponent().getDocument().getTransactionManager()
135: .writeAccess(new Runnable() {
136: public void run() {
137: PropertyValue arrayPropertyValue = getComponent()
138: .getParentComponent().readProperty(
139: getArrayPropertyName());
140: List<PropertyValue> array = arrayPropertyValue
141: .getArray();
142: newArrayTypeID = arrayPropertyValue.getType()
143: .getComponentType();
144: if (array.size() < 1)
145: return;
146: List<PropertyValue> newArray = new ArrayList(
147: array);
148: for (PropertyValue value : array) {
149: if (value.getComponent() == getComponent()) {
150: invokeMoveAction(array, newArray, value);
151: }
152: }
153: }
154: });
155: }
156:
157: protected abstract void invokeMoveAction(
158: List<PropertyValue> currentArray,
159: List<PropertyValue> newArray, PropertyValue currentValue);
160:
161: protected TypeID getNewArrayType() {
162: return newArrayTypeID;
163: }
164:
165: public void setComponent(DesignComponent component) {
166: this .component = new WeakReference<DesignComponent>(component);
167: }
168:
169: private String getArrayPropertyName() {
170: return arrayPropertyName;
171: }
172:
173: protected void saveToModel(List<PropertyValue> newArray) {
174: DesignComponent parentComponent = getComponent()
175: .getParentComponent();
176: parentComponent.writeProperty(getArrayPropertyName(),
177: PropertyValue.createArray(getNewArrayType(), newArray));
178: }
179: }
|